|
@@ -1,27 +1,26 @@
|
|
|
-import type {
|
|
|
|
|
- ChangeSpec, StateCommand, EditorState, Transaction,
|
|
|
|
|
-} from '@codemirror/state';
|
|
|
|
|
|
|
+import type { ChangeSpec } from '@codemirror/state';
|
|
|
|
|
+import { EditorView } from '@codemirror/view';
|
|
|
|
|
|
|
|
// 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*)/;
|
|
|
const indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
|
|
const indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
|
|
|
|
|
|
|
|
-export const insertNewlineContinueMarkup = (state: EditorState, dispatch: (transaction: Transaction) => boolean): boolean => {
|
|
|
|
|
|
|
+export const insertNewlineContinueMarkup = (editor: EditorView): void => {
|
|
|
|
|
|
|
|
const changes: ChangeSpec[] = [];
|
|
const changes: ChangeSpec[] = [];
|
|
|
|
|
|
|
|
let selection;
|
|
let selection;
|
|
|
|
|
|
|
|
- const curPos = state.selection.main.head;
|
|
|
|
|
|
|
+ const curPos = editor.state.selection.main.head;
|
|
|
|
|
|
|
|
- const aboveLine = state.doc.lineAt(curPos).number;
|
|
|
|
|
- const bolPos = state.doc.line(aboveLine).from;
|
|
|
|
|
|
|
+ const aboveLine = editor.state.doc.lineAt(curPos).number;
|
|
|
|
|
+ const bolPos = editor.state.doc.line(aboveLine).from;
|
|
|
|
|
|
|
|
- const strFromBol = state.sliceDoc(bolPos, curPos);
|
|
|
|
|
|
|
+ const strFromBol = editor.state.sliceDoc(bolPos, curPos);
|
|
|
|
|
|
|
|
// If the text before the cursor is only markdown symbols
|
|
// If the text before the cursor is only markdown symbols
|
|
|
if (indentAndMarkOnlyRE.test(strFromBol)) {
|
|
if (indentAndMarkOnlyRE.test(strFromBol)) {
|
|
|
- const insert = state.lineBreak;
|
|
|
|
|
|
|
+ const insert = editor.state.lineBreak;
|
|
|
|
|
|
|
|
changes.push({
|
|
changes.push({
|
|
|
from: bolPos,
|
|
from: bolPos,
|
|
@@ -35,10 +34,10 @@ export const insertNewlineContinueMarkup = (state: EditorState, dispatch: (trans
|
|
|
const indentAndMark = strFromBol.match(indentAndMarkRE)?.[0];
|
|
const indentAndMark = strFromBol.match(indentAndMarkRE)?.[0];
|
|
|
|
|
|
|
|
if (indentAndMark == null) {
|
|
if (indentAndMark == null) {
|
|
|
- return false;
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const insert = state.lineBreak + indentAndMark;
|
|
|
|
|
|
|
+ const insert = editor.state.lineBreak + indentAndMark;
|
|
|
const nextCurPos = curPos + insert.length;
|
|
const nextCurPos = curPos + insert.length;
|
|
|
|
|
|
|
|
selection = { anchor: nextCurPos };
|
|
selection = { anchor: nextCurPos };
|
|
@@ -51,7 +50,7 @@ export const insertNewlineContinueMarkup = (state: EditorState, dispatch: (trans
|
|
|
|
|
|
|
|
// If the text before the cursor is regular text
|
|
// If the text before the cursor is regular text
|
|
|
else {
|
|
else {
|
|
|
- const insert = state.lineBreak;
|
|
|
|
|
|
|
+ const insert = editor.state.lineBreak;
|
|
|
const nextCurPos = curPos + insert.length;
|
|
const nextCurPos = curPos + insert.length;
|
|
|
|
|
|
|
|
selection = { anchor: nextCurPos };
|
|
selection = { anchor: nextCurPos };
|
|
@@ -62,11 +61,9 @@ export const insertNewlineContinueMarkup = (state: EditorState, dispatch: (trans
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- dispatch(state.update({
|
|
|
|
|
|
|
+ editor.dispatch({
|
|
|
changes,
|
|
changes,
|
|
|
selection,
|
|
selection,
|
|
|
userEvent: 'input',
|
|
userEvent: 'input',
|
|
|
- }));
|
|
|
|
|
-
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|