ryohek 5 лет назад
Родитель
Сommit
4e3316198e

+ 4 - 0
src/client/js/components/PageEditor/CodeMirrorEditor.jsx

@@ -674,6 +674,10 @@ export default class CodeMirrorEditor extends AbstractEditor {
     this.handsontableModal.current.show(mtu.getMarkdownTable(this.getCodeMirror()));
   }
 
+  showHandsonLinkHandler() {
+    this.handsontableModal.current.show(mlu.getMarkdownLink(this.getCodeMirror()));
+  }
+
   showDrawioHandler() {
     this.drawioModal.current.show(mdu.getMarkdownDrawioMxfile(this.getCodeMirror()));
   }

+ 55 - 3
src/client/js/components/PageEditor/MarkdownLinkUtil.js

@@ -1,14 +1,66 @@
+import MarkdownTable from '../../models/MarkdownTable';
+
 /**
  * Utility for markdown link
  */
 class MarkdownLinkUtil {
 
   constructor() {
-    // TODO Regular expression for link
-    this.linePartOfLink = /^([^\r\n|]*)\|(([^\r\n|]*\|)+)$/;
+    // TODO Regular expression for link /^([^\r\n|]*)\|(([^\r\n|]*\|)+)$/
+    this.linePartOfLink = /^\[/;
     this.isInTable = this.isInTable.bind(this);
   }
 
+  getBot(editor) {
+    const curPos = editor.getCursor();
+    if (!this.isInTable(editor)) {
+      return { line: curPos.line, ch: curPos.ch };
+    }
+
+    const firstLine = editor.getDoc().firstLine();
+    let line = curPos.line - 1;
+    for (; line >= firstLine; line--) {
+      const strLine = editor.getDoc().getLine(line);
+      if (!this.linePartOfTableRE.test(strLine)) {
+        break;
+      }
+    }
+    const botLine = Math.max(firstLine, line + 1);
+    return { line: botLine, ch: 0 };
+  }
+
+  /**
+   * return the postion of the EOT(end of table)
+   * (If the cursor is not in a table, return its position)
+   */
+  getEot(editor) {
+    const curPos = editor.getCursor();
+    if (!this.isInTable(editor)) {
+      return { line: curPos.line, ch: curPos.ch };
+    }
+
+    const lastLine = editor.getDoc().lastLine();
+    let line = curPos.line + 1;
+    for (; line <= lastLine; line++) {
+      const strLine = editor.getDoc().getLine(line);
+      if (!this.linePartOfTableRE.test(strLine)) {
+        break;
+      }
+    }
+    const eotLine = Math.min(line - 1, lastLine);
+    const lineLength = editor.getDoc().getLine(eotLine).length;
+    return { line: eotLine, ch: lineLength };
+  }
+
+  getMarkdownLink(editor) {
+    if (!this.isInTable(editor)) {
+      return null;
+    }
+
+    const strFromBotToEot = editor.getDoc().getRange(this.getBot(editor), this.getEot(editor));
+    return MarkdownTable.fromMarkdownString(strFromBotToEot);
+  }
+
   getSelectedTextInEditor(editor) {
     return editor.getDoc().getSelection();
   }
@@ -19,7 +71,7 @@ class MarkdownLinkUtil {
 
   isInTable(editor) {
     const curPos = editor.getCursor();
-    return this.linePartOfTableRE.test(editor.getDoc().getLine(curPos.line));
+    return this.linePartOfLink.test(editor.getDoc().getLine(curPos.line));
   }
 
 }

+ 16 - 0
src/client/styles/scss/_on-edit.scss

@@ -183,6 +183,22 @@ body.on-edit {
         }
       }
 
+      .markdown-link-activated .CodeMirror-cursor {
+        &:after {
+          position: relative;
+          top: -1.1em;
+          left: 0.3em;
+          display: block;
+          width: 1em;
+          height: 1em;
+          content: ' ';
+
+          background-image: url(/images/icons/editor/link.svg);
+          background-repeat: no-repeat;
+          background-size: 1em;
+        }
+      }
+
       .textarea-editor {
         font-family: monospace;
         border: none;