yusuketk 5 лет назад
Родитель
Сommit
4fd6b2a59b

+ 6 - 41
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -41,7 +41,6 @@ class LinkEditModal extends React.PureComponent {
     this.cancel = this.cancel.bind(this);
     this.handleChangeLabelInput = this.handleChangeLabelInput.bind(this);
     this.handleChangeLinkInput = this.handleChangeLinkInput.bind(this);
-    this.parseMakdownLink = this.parseMakdownLink.bind(this);
     this.handleSelecteLinkerType = this.handleSelecteLinkerType.bind(this);
     this.toggleIsUseRelativePath = this.toggleIsUseRelativePath.bind(this);
     this.setMarkdown = this.setMarkdown.bind(this);
@@ -58,52 +57,18 @@ class LinkEditModal extends React.PureComponent {
     }
   }
 
-  show(defaultMarkdownLink = '') {
-    const { labelInputValue, linkInputValue, linkerType } = this.parseMakdownLink(defaultMarkdownLink);
+  show(defaultMarkdownLink = null) {
+    // if defaultMarkdownLink is null, set default value in inputs.
+    const {type='mdLink', label='', link=''} = defaultMarkdownLink ;
 
     this.setState({
       show: true,
-      labelInputValue,
-      linkInputValue,
-      linkerType,
+      labelInputValue: label,
+      linkInputValue: link,
+      linkerType: type,
     });
   }
 
-  parseMakdownLink(MarkdownLink) {
-    let labelInputValue = MarkdownLink;
-    let linkInputValue = '';
-    let linkerType = 'mdLink';
-
-    // https://regex101.com/r/2fNmUN/1
-    if (MarkdownLink.match(/^\[\[.*\]\]$/) && this.isApplyPukiwikiLikeLinkerPlugin) {
-      linkerType = 'pukiwikiLink';
-      const value = MarkdownLink.slice(2, -2);
-      const indexOfSplit = value.lastIndexOf('>');
-      if (indexOfSplit < 0) {
-        labelInputValue = value;
-        linkInputValue = value;
-      }
-      labelInputValue = value.slice(0, indexOfSplit);
-      linkInputValue = value.slice(indexOfSplit + 1);
-    }
-    // https://regex101.com/r/DJfkYf/1
-    else if (MarkdownLink.match(/^\[\/.*\]$/)) {
-      linkerType = 'growiLink';
-      const value = MarkdownLink.slice(1, -1);
-      labelInputValue = value;
-      linkInputValue = value;
-    }
-    // https://regex101.com/r/DZCKP3/1
-    else if (MarkdownLink.match(/^\[.*\]\(.*\)$/)) {
-      const value = MarkdownLink.slice(1, -1);
-      const indexOfSplit = value.lastIndexOf('](');
-      labelInputValue = value.slice(0, indexOfSplit);
-      linkInputValue = value.slice(indexOfSplit + 2);
-    }
-
-    return { labelInputValue, linkInputValue, linkerType };
-  }
-
   cancel() {
     this.hide();
   }

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

@@ -1,3 +1,5 @@
+import Linker from '../models/Linker';
+
 /**
  * Utility for markdown link
  */
@@ -15,9 +17,8 @@ class MarkdownLinkUtil {
     if (!this.isInLink(editor)) {
       return editor.getDoc().getSelection();
     }
-    const { beginningOfLink, endOfLink } = this.getBeginningAndEndOfTheClosestLinkToCursor(editor);
     const curPos = editor.getCursor();
-    return editor.getDoc().getLine(curPos.line).substring(beginningOfLink, endOfLink);
+    return Linker.fromLineAndPos(editor.getDoc().getLine(curPos.line), curPos.ch)
   }
 
   isInLink(editor) {
@@ -26,28 +27,6 @@ class MarkdownLinkUtil {
     return beginningOfLink >= 0 && endOfLink >= 0 && beginningOfLink <= curPos.ch && curPos.ch <= endOfLink;
   }
 
-  // return beginning index and end index of the closest link to cursor
-  // if there is no link, return { beginningOfLink: -1, endOfLink: -1}
-  getBeginningAndEndOfTheClosestLinkToCursor(editor) {
-    const curPos = editor.getCursor();
-    const line = editor.getDoc().getLine(curPos.line);
-
-    // get beginning and end of growi link ('[link]')
-    let beginningOfLink = line.lastIndexOf('[', curPos.ch);
-    let endOfLink = line.indexOf(']', beginningOfLink) + 1;
-
-    // if it is markdown link ('[label](link)'), get beginning and end of it
-    if (line.charAt(endOfLink) === '(') {
-      endOfLink = line.indexOf(')', endOfLink) + 1;
-    }
-    // if it is pukiwiki link ('[[link]]'), get beginning and end of it
-    else if (line.charAt(beginningOfLink - 1) === '[' && line.charAt(endOfLink) === ']') {
-      beginningOfLink -= 1;
-      endOfLink += 1;
-    }
-    return { beginningOfLink, endOfLink };
-  }
-
   replaceFocusedMarkdownLinkWithEditor(editor) {
     // GW-3023
   }