Browse Source

create renumberListIndex function

kosei-n 2 years ago
parent
commit
6c384ba224

+ 1 - 0
packages/editor/src/services/codemirror-editor/use-codemirror-editor/use-codemirror-editor.ts

@@ -39,6 +39,7 @@ const markdownHighlighting = HighlightStyle.define([
 const onPressEnter: StateCommand = ({ state, dispatch }) => {
 const onPressEnter: StateCommand = ({ state, dispatch }) => {
   const insertBool = insertNewlineContinueMarkup({ state, dispatch });
   const insertBool = insertNewlineContinueMarkup({ state, dispatch });
   // ここにrenumber的な処理を書く
   // ここにrenumber的な処理を書く
+  console.log('renumber');
 
 
   return insertBool;
   return insertBool;
 };
 };

+ 19 - 29
packages/editor/src/services/list-util/markdown-list-util.ts

@@ -1,39 +1,29 @@
-import { EditorView } from '@codemirror/view';
+import { text } from 'stream/consumers';
 
 
-const getBol = (editor: EditorView) => {
-  const curPos = editor.state.selection.main.head;
-  const aboveLine = editor.state.doc.lineAt(curPos).number - 1;
-  return editor.state.doc.line(aboveLine).from;
-};
+import type { EditorState } from '@codemirror/state';
+
+// https://regex101.com/r/7BN2fR/5
+// const indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
+
+const numberListMarkdownRE = /^(\s|\d+[.)])\s*/;
 
 
-const getStrFromBol = (editor: EditorView) => {
-  const curPos = editor.state.selection.main.head;
-  return editor.state.sliceDoc(getBol(editor), curPos);
+const getBol = (editorState: EditorState) => {
+  const curPos = editorState.selection.main.head;
+  const aboveLine = editorState.doc.lineAt(curPos).number;
+  return editorState.doc.line(aboveLine).from;
 };
 };
 
 
-// https://regex101.com/r/UKMNlO/1
-const indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
-
-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 getStrFromBol = (editorState: EditorState): string => {
+  const curPos = editorState.selection.main.head;
+  return editorState.sliceDoc(getBol(editorState), curPos);
 };
 };
 
 
-export const newlineAndIndentContinueMarkdownList = (editor: EditorView): void => {
-  const strFromBol = getStrFromBol(editor);
+export const renumberListIndex = ({ state, dispatch }) => {
 
 
-  const matchResult = strFromBol.match(indentAndMarkRE);
+  const strFromBol = getStrFromBol(state);
+
+  if (strFromBol.match(numberListMarkdownRE)) {
 
 
-  if (matchResult != null) {
-    // continue list
-    const indentAndMark = matchResult[0];
-    insertText(editor, indentAndMark);
   }
   }
+
 };
 };