|
|
@@ -5,7 +5,7 @@ import { EditorView } from '@codemirror/view';
|
|
|
import { useInsertText } from '../codemirror-editor/use-codemirror-editor/utils/insert-text';
|
|
|
|
|
|
|
|
|
-export type NewlineAndIndentContinueMarkdownList = () => void;
|
|
|
+export type NewlineAndIndentContinueMarkdownList = ((editor: EditorView) => void) | undefined;
|
|
|
|
|
|
// https://regex101.com/r/7BN2fR/5
|
|
|
const indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
|
|
|
@@ -21,37 +21,30 @@ export const getLineToCursor = (editor: EditorView, lineNumBeforeCursor = 0): st
|
|
|
return editor.state.sliceDoc(firstLineToGet, curPos);
|
|
|
};
|
|
|
|
|
|
-
|
|
|
-// const insertText = (editor: EditorView, text: string) => {
|
|
|
-// const curPos = editor.state.selection.main.head;
|
|
|
-// const line = editor.state.doc.lineAt(curPos).from;
|
|
|
-// editor.dispatch({
|
|
|
-// changes: {
|
|
|
-// from: line,
|
|
|
-// to: curPos,
|
|
|
-// insert: text,
|
|
|
-// },
|
|
|
-// });
|
|
|
-// };
|
|
|
-
|
|
|
-export const useNewlineAndIndentContinueMarkdownList = (editor: EditorView): NewlineAndIndentContinueMarkdownList => {
|
|
|
+export const useNewlineAndIndentContinueMarkdownList = (editor?: EditorView): NewlineAndIndentContinueMarkdownList => {
|
|
|
const insertText = useInsertText(editor);
|
|
|
|
|
|
- const curPos = editor.state.selection.main.head;
|
|
|
- const line = editor.state.doc.lineAt(curPos).from;
|
|
|
+ const NewlineAndIndentContinueMarkdownList = useCallback((view: EditorView) => {
|
|
|
|
|
|
- return useCallback(() => {
|
|
|
+ const curPos = view?.state.selection.main.head;
|
|
|
+ const line = view?.state.doc.lineAt(curPos).from;
|
|
|
|
|
|
- const getStrFromAboveLine = getLineToCursor(editor, 1);
|
|
|
+ const getStrFromAboveLine = getLineToCursor(view, 1);
|
|
|
|
|
|
const matchResult = getStrFromAboveLine.match(indentAndMarkRE);
|
|
|
|
|
|
if (matchResult != null) {
|
|
|
const indentAndMark = matchResult[0];
|
|
|
insertText(indentAndMark, line, curPos);
|
|
|
+ // insertText(editor, indentAndMark);
|
|
|
}
|
|
|
- }, [curPos, editor, insertText, line]);
|
|
|
+ }, [insertText]);
|
|
|
+
|
|
|
+ if (editor == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
+ return () => { NewlineAndIndentContinueMarkdownList(editor) };
|
|
|
};
|
|
|
|
|
|
export const adjustPasteData = (indentAndMark: string, text: string): string => {
|