kosei-n пре 2 година
родитељ
комит
76a5485032
1 измењених фајлова са 26 додато и 33 уклоњено
  1. 26 33
      apps/app/src/components/PageEditor/markdown-table-util-for-editor.ts

+ 26 - 33
apps/app/src/components/PageEditor/markdown-table-util-for-editor.ts

@@ -1,22 +1,20 @@
-import type { EditorState } from '@codemirror/state';
 import type { EditorView } from '@codemirror/view';
 import type { EditorView } from '@codemirror/view';
 
 
 import MarkdownTable from '~/client/models/MarkdownTable';
 import MarkdownTable from '~/client/models/MarkdownTable';
-
 // https://regex101.com/r/7BN2fR/10
 // https://regex101.com/r/7BN2fR/10
 const linePartOfTableRE = /^([^\r\n|]*)\|(([^\r\n|]*\|)+)$/;
 const linePartOfTableRE = /^([^\r\n|]*)\|(([^\r\n|]*\|)+)$/;
 // https://regex101.com/r/1UuWBJ/3
 // https://regex101.com/r/1UuWBJ/3
 export const emptyLineOfTableRE = /^([^\r\n|]*)\|((\s*\|)+)$/;
 export const emptyLineOfTableRE = /^([^\r\n|]*)\|((\s*\|)+)$/;
 
 
-const curPos = (editorState: EditorState): number => {
-  return editorState.selection.main.head;
+const curPos = (editor: EditorView): number => {
+  return editor.state.selection.main.head;
 };
 };
 
 
 /**
 /**
    * return boolean value whether the cursor position is in a table
    * return boolean value whether the cursor position is in a table
    */
    */
-export const isInTable = (editorState: EditorState): boolean => {
-  const lineText = editorState.doc.lineAt(curPos(editorState)).text;
+export const isInTable = (editor: EditorView): boolean => {
+  const lineText = editor.state.doc.lineAt(curPos(editor)).text;
   return linePartOfTableRE.test(lineText);
   return linePartOfTableRE.test(lineText);
 };
 };
 
 
@@ -24,14 +22,14 @@ export const isInTable = (editorState: EditorState): boolean => {
    * return the postion of the BOT(beginning of table)
    * return the postion of the BOT(beginning of table)
    * (If the cursor is not in a table, return its position)
    * (If the cursor is not in a table, return its position)
    */
    */
-const getBot = (editorState: EditorState): number => {
-  if (!isInTable(editorState)) {
-    return curPos(editorState);
+const getBot = (editor: EditorView): number => {
+  if (!isInTable(editor)) {
+    return curPos(editor);
   }
   }
 
 
-  const doc = editorState.doc;
+  const doc = editor.state.doc;
   const firstLine = 1;
   const firstLine = 1;
-  let line = doc.lineAt(curPos(editorState)).number - 1;
+  let line = doc.lineAt(curPos(editor)).number - 1;
   for (; line >= firstLine; line--) {
   for (; line >= firstLine; line--) {
     const strLine = doc.line(line).text;
     const strLine = doc.line(line).text;
     if (!linePartOfTableRE.test(strLine)) {
     if (!linePartOfTableRE.test(strLine)) {
@@ -41,19 +39,18 @@ const getBot = (editorState: EditorState): number => {
   const botLine = Math.max(firstLine, line + 1);
   const botLine = Math.max(firstLine, line + 1);
   return doc.line(botLine).from;
   return doc.line(botLine).from;
 };
 };
-
 /**
 /**
    * return the postion of the EOT(end of table)
    * return the postion of the EOT(end of table)
    * (If the cursor is not in a table, return its position)
    * (If the cursor is not in a table, return its position)
    */
    */
-const getEot = (editorState: EditorState): number => {
-  if (!isInTable(editorState)) {
-    return curPos(editorState);
+const getEot = (editor: EditorView): number => {
+  if (!isInTable(editor)) {
+    return curPos(editor);
   }
   }
 
 
-  const doc = editorState.doc;
+  const doc = editor.state.doc;
   const lastLine = doc.lines;
   const lastLine = doc.lines;
-  let line = doc.lineAt(curPos(editorState)).number + 1;
+  let line = doc.lineAt(curPos(editor)).number + 1;
   for (; line <= lastLine; line++) {
   for (; line <= lastLine; line++) {
     const strLine = doc.line(line).text;
     const strLine = doc.line(line).text;
     if (!linePartOfTableRE.test(strLine)) {
     if (!linePartOfTableRE.test(strLine)) {
@@ -63,39 +60,38 @@ const getEot = (editorState: EditorState): number => {
   const eotLine = Math.min(line - 1, lastLine);
   const eotLine = Math.min(line - 1, lastLine);
   return doc.line(eotLine).to;
   return doc.line(eotLine).to;
 };
 };
-
 /**
 /**
    * return strings from BOT(beginning of table) to the cursor position
    * return strings from BOT(beginning of table) to the cursor position
    */
    */
-export const getStrFromBot = (editorState: EditorState): string => {
-  return editorState.sliceDoc(getBot(editorState), curPos(editorState));
+export const getStrFromBot = (editor: EditorView): string => {
+  return editor.state.sliceDoc(getBot(editor), curPos(editor));
 };
 };
 
 
 /**
 /**
    * return strings from the cursor position to EOT(end of table)
    * return strings from the cursor position to EOT(end of table)
    */
    */
-export const getStrToEot = (editorState: EditorState): string => {
-  return editorState.sliceDoc(curPos(editorState), getEot(editorState));
+export const getStrToEot = (editor: EditorView): string => {
+  return editor.state.sliceDoc(curPos(editor), getEot(editor));
 };
 };
 
 
 /**
 /**
    * return MarkdownTable instance of the table where the cursor is
    * return MarkdownTable instance of the table where the cursor is
    * (If the cursor is not in a table, return null)
    * (If the cursor is not in a table, return null)
    */
    */
-export const getMarkdownTable = (editorState: EditorState): MarkdownTable | undefined => {
-  if (!isInTable(editorState)) {
+export const getMarkdownTable = (editor: EditorView): MarkdownTable | undefined => {
+  if (!isInTable(editor)) {
     return;
     return;
   }
   }
 
 
-  const strFromBotToEot = editorState.sliceDoc(getBot(editorState), getEot(editorState));
+  const strFromBotToEot = editor.state.sliceDoc(getBot(editor), getEot(editor));
   return MarkdownTable.fromMarkdownString(strFromBotToEot);
   return MarkdownTable.fromMarkdownString(strFromBotToEot);
 };
 };
 
 
 /**
 /**
    * return boolean value whether the cursor position is end of line
    * return boolean value whether the cursor position is end of line
    */
    */
-export const isEndOfLine = (editorState: EditorState): boolean => {
-  return curPos(editorState) === editorState.doc.lineAt(curPos(editorState)).to;
+export const isEndOfLine = (editor: EditorView): boolean => {
+  return curPos(editor) === editor.state.doc.lineAt(curPos(editor)).to;
 };
 };
 
 
 /**
 /**
@@ -108,7 +104,6 @@ export const addRowToMarkdownTable = (mdtable: MarkdownTable): any => {
   (new Array(numCol)).forEach(() => { return newRow.push('') }); // create cols
   (new Array(numCol)).forEach(() => { return newRow.push('') }); // create cols
   mdtable.table.push(newRow);
   mdtable.table.push(newRow);
 };
 };
-
 /**
 /**
    * return markdown table that is merged all of markdown table in array
    * return markdown table that is merged all of markdown table in array
    * (The merged markdown table options are used for the first markdown table.)
    * (The merged markdown table options are used for the first markdown table.)
@@ -117,7 +112,6 @@ export const mergeMarkdownTable = (mdtableList: MarkdownTable): MarkdownTable |
   if (mdtableList == null || !(mdtableList instanceof Array)) {
   if (mdtableList == null || !(mdtableList instanceof Array)) {
     return undefined;
     return undefined;
   }
   }
-
   let newTable = [];
   let newTable = [];
   const options = mdtableList[0].options; // use option of first markdown-table
   const options = mdtableList[0].options; // use option of first markdown-table
   mdtableList.forEach((mdtable) => {
   mdtableList.forEach((mdtable) => {
@@ -125,14 +119,13 @@ export const mergeMarkdownTable = (mdtableList: MarkdownTable): MarkdownTable |
   });
   });
   return (new MarkdownTable(newTable, options));
   return (new MarkdownTable(newTable, options));
 };
 };
-
 /**
 /**
    * replace focused markdown table with editor
    * replace focused markdown table with editor
    * (A replaced table is reformed by markdown-table.)
    * (A replaced table is reformed by markdown-table.)
    */
    */
 export const replaceFocusedMarkdownTableWithEditor = (editor: EditorView, table: MarkdownTable): void => {
 export const replaceFocusedMarkdownTableWithEditor = (editor: EditorView, table: MarkdownTable): void => {
-  const botPos = getBot(editor.state);
-  const eotPos = getEot(editor.state);
+  const botPos = getBot(editor);
+  const eotPos = getEot(editor);
 
 
   editor.dispatch({
   editor.dispatch({
     changes: {
     changes: {
@@ -142,7 +135,7 @@ export const replaceFocusedMarkdownTableWithEditor = (editor: EditorView, table:
     },
     },
   });
   });
   editor.dispatch({
   editor.dispatch({
-    selection: { anchor: editor.state.doc.lineAt(curPos(editor.state)).to },
+    selection: { anchor: editor.state.doc.lineAt(curPos(editor)).to },
   });
   });
   editor.focus();
   editor.focus();
 };
 };