2024-11-01 15:31:54 +05:30
|
|
|
import { textblockTypeInputRule } from 'prosemirror-inputrules';
|
|
|
|
|
import { Schema } from 'prosemirror-model';
|
|
|
|
|
import { schema } from 'prosemirror-schema-basic';
|
|
|
|
|
import { addListNodes } from 'prosemirror-schema-list';
|
2024-11-15 12:18:17 -05:00
|
|
|
import type { Transaction } from 'prosemirror-state';
|
|
|
|
|
import type { EditorView } from 'prosemirror-view';
|
|
|
|
|
import type { MutableRefObject } from 'react';
|
2024-11-01 15:31:54 +05:30
|
|
|
|
|
|
|
|
import { buildContentFromDocument } from './functions';
|
|
|
|
|
|
|
|
|
|
export const documentSchema = new Schema({
|
|
|
|
|
nodes: addListNodes(schema.spec.nodes, 'paragraph block*', 'block'),
|
|
|
|
|
marks: schema.spec.marks,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function headingRule(level: number) {
|
|
|
|
|
return textblockTypeInputRule(
|
|
|
|
|
new RegExp(`^(#{1,${level}})\\s$`),
|
|
|
|
|
documentSchema.nodes.heading,
|
2024-11-15 13:00:15 -05:00
|
|
|
() => ({ level }),
|
2024-11-01 15:31:54 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const handleTransaction = ({
|
|
|
|
|
transaction,
|
|
|
|
|
editorRef,
|
|
|
|
|
saveContent,
|
|
|
|
|
}: {
|
|
|
|
|
transaction: Transaction;
|
|
|
|
|
editorRef: MutableRefObject<EditorView | null>;
|
|
|
|
|
saveContent: (updatedContent: string, debounce: boolean) => void;
|
|
|
|
|
}) => {
|
|
|
|
|
if (!editorRef || !editorRef.current) return;
|
|
|
|
|
|
|
|
|
|
const newState = editorRef.current.state.apply(transaction);
|
|
|
|
|
editorRef.current.updateState(newState);
|
|
|
|
|
|
|
|
|
|
if (transaction.docChanged && !transaction.getMeta('no-save')) {
|
|
|
|
|
const updatedContent = buildContentFromDocument(newState.doc);
|
|
|
|
|
|
|
|
|
|
if (transaction.getMeta('no-debounce')) {
|
|
|
|
|
saveContent(updatedContent, false);
|
|
|
|
|
} else {
|
|
|
|
|
saveContent(updatedContent, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|