Просмотр исходного кода

add a conditional branch to onPressEnter

kosei-n 2 лет назад
Родитель
Сommit
1bd495d065

+ 8 - 3
packages/editor/src/services/codemirror-editor/use-codemirror-editor/use-codemirror-editor.ts

@@ -27,7 +27,7 @@ import { FoldDrawio, useFoldDrawio } from './utils/fold-drawio';
 import { useGetDoc, type GetDoc } from './utils/get-doc';
 import { useGetDoc, type GetDoc } from './utils/get-doc';
 import { useInitDoc, type InitDoc } from './utils/init-doc';
 import { useInitDoc, type InitDoc } from './utils/init-doc';
 import { useInsertMarkdownElements, type InsertMarkdowElements } from './utils/insert-markdown-elements';
 import { useInsertMarkdownElements, type InsertMarkdowElements } from './utils/insert-markdown-elements';
-import { insertNewRowToMarkdownTable } from './utils/insert-new-row-to-table-markdown';
+import { insertNewRowToMarkdownTable, isInTable } from './utils/insert-new-row-to-table-markdown';
 import { insertNewlineContinueMarkup } from './utils/insert-newline-continue-markup';
 import { insertNewlineContinueMarkup } from './utils/insert-newline-continue-markup';
 import { useInsertPrefix, type InsertPrefix } from './utils/insert-prefix';
 import { useInsertPrefix, type InsertPrefix } from './utils/insert-prefix';
 import { useInsertText, type InsertText } from './utils/insert-text';
 import { useInsertText, type InsertText } from './utils/insert-text';
@@ -36,8 +36,13 @@ import { useSetCaretLine, type SetCaretLine } from './utils/set-caret-line';
 
 
 
 
 const onPressEnter: Command = (editor) => {
 const onPressEnter: Command = (editor) => {
-  // insertNewlineContinueMarkup(state, dispatch);
-  insertNewRowToMarkdownTable(editor);
+
+  if (isInTable(editor)) {
+    insertNewRowToMarkdownTable(editor);
+    return true;
+  }
+
+  insertNewlineContinueMarkup(editor);
 
 
   return true;
   return true;
 };
 };

+ 9 - 14
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-new-row-to-table-markdown.ts

@@ -13,7 +13,7 @@ const getCurPos = (editor: EditorView): number => {
   return editor.state.selection.main.head;
   return editor.state.selection.main.head;
 };
 };
 
 
-const isInTable = (editor: EditorView): boolean => {
+export const isInTable = (editor: EditorView): boolean => {
   const curPos = getCurPos(editor);
   const curPos = getCurPos(editor);
   const lineText = editor.state.doc.lineAt(curPos).text;
   const lineText = editor.state.doc.lineAt(curPos).text;
   return linePartOfTableRE.test(lineText);
   return linePartOfTableRE.test(lineText);
@@ -170,18 +170,13 @@ export const insertNewRowToMarkdownTable = (editor: EditorView): void => {
   const isLastRow = getStrToEot(editor) === strToEol;
   const isLastRow = getStrToEot(editor) === strToEol;
   const isEndOfLine = curPos === eolPos;
   const isEndOfLine = curPos === eolPos;
 
 
-  if (isInTable(editor)) {
-
-    if (isEndOfLine) {
-      addRow(editor);
-    }
-
-    else if (isLastRow && emptyLineOfTableRE.test(strFromBol + strToEol)) {
-      removeRow(editor);
-    }
-
-    else {
-      reformTable(editor);
-    }
+  if (isEndOfLine) {
+    addRow(editor);
+  }
+  else if (isLastRow && emptyLineOfTableRE.test(strFromBol + strToEol)) {
+    removeRow(editor);
+  }
+  else {
+    reformTable(editor);
   }
   }
 };
 };

+ 13 - 16
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-newline-continue-markup.ts

@@ -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;
+  });
 };
 };