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