yusuketk 5 лет назад
Родитель
Сommit
1fe499704d
1 измененных файлов с 35 добавлено и 27 удалено
  1. 35 27
      src/client/js/components/PageEditor/LinkEditModal.jsx

+ 35 - 27
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -40,6 +40,7 @@ class LinkEditModal extends React.PureComponent {
     this.show = this.show.bind(this);
     this.hide = this.hide.bind(this);
     this.cancel = this.cancel.bind(this);
+    this.parseMakdownLink = this.parseMakdownLink.bind(this);
     this.handleChangeLinkInput = this.handleChangeLinkInput.bind(this);
     this.handleChangeLabelInput = this.handleChangeLabelInput.bind(this);
     this.handleChangeTypeahead = this.handleChangeTypeahead.bind(this);
@@ -61,31 +62,47 @@ class LinkEditModal extends React.PureComponent {
   }
 
   show(defaultMarkdownLink = '') {
-    let labelInputValue = defaultMarkdownLink;
+    const { labelInputValue, linkInputValue, linkerType } = this.parseMakdownLink(defaultMarkdownLink);
+
+    this.setState({
+      show: true,
+      labelInputValue,
+      linkInputValue,
+      linkerType,
+    });
+  }
+
+  parseMakdownLink(MarkdownLink) {
+    let labelInputValue = MarkdownLink;
     let linkInputValue = '';
     let linkerType = 'mdLink';
 
-    if (defaultMarkdownLink.match(/^\[\[.*\]\]$/) && this.isApplyPukiwikiLikeLinkerPlugin) {
+    if (MarkdownLink.match(/^\[\[.*\]\]$/)) {
+    // if (MarkdownLink.match(/^\[\[.*\]\]$/) && this.isApplyPukiwikiLikeLinkerPlugin) {
       linkerType = 'pukiwikiLink';
-      labelInputValue = 'pukilabel';
-      linkInputValue = 'pukilink';
+      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);
     }
-    else if (defaultMarkdownLink.match(/^\[\/.*\]$/)) {
+    else if (MarkdownLink.match(/^\[\/.*\]$/)) {
       linkerType = 'growiLink';
-      labelInputValue = 'growilabel';
-      linkInputValue = 'growilink';
+      const value = MarkdownLink.slice(1, -1);
+      labelInputValue = value;
+      linkInputValue = value;
     }
-    else if (defaultMarkdownLink.match(/^\[.*\]\(.*\)$/)) {
-      labelInputValue = 'mdlabel';
-      linkInputValue = 'mdlink';
+    else if (MarkdownLink.match(/^\[.*\]\(.*\)$/)) {
+      const value = MarkdownLink.slice(1, -1);
+      const indexOfSplit = value.lastIndexOf('](');
+      labelInputValue = value.slice(0, indexOfSplit);
+      linkInputValue = value.slice(indexOfSplit + 2);
     }
 
-    this.setState({
-      show: true,
-      labelInputValue,
-      linkInputValue,
-      linkerType,
-    });
+    return { labelInputValue, linkInputValue, linkerType };
   }
 
   cancel() {
@@ -138,7 +155,6 @@ class LinkEditModal extends React.PureComponent {
     if (page != null) {
       this.handleChangeLinkInput(page.path);
     }
-
   }
 
   handleSelecteLinkerType(linkerType) {
@@ -167,8 +183,7 @@ class LinkEditModal extends React.PureComponent {
       markdown = res.page.revision.body;
       permalink = `${window.location.origin}/${res.page.id}`;
       isEnablePermanentLink = true;
-    }
-    catch (err) {
+    } catch (err) {
       markdown = `<div class="alert alert-warning" role="alert"><strong>${err.message}</strong></div>`;
     }
     this.setState({ markdown, permalink, isEnablePermanentLink });
@@ -176,13 +191,7 @@ class LinkEditModal extends React.PureComponent {
 
   generateLink() {
     const { pageContainer } = this.props;
-    const {
-      linkInputValue,
-      labelInputValue,
-      linkerType,
-      isUseRelativePath,
-      isUsePermanentLink,
-    } = this.state;
+    const { linkInputValue, labelInputValue, linkerType, isUseRelativePath, isUsePermanentLink } = this.state;
 
     let reshapedLink = linkInputValue;
 
@@ -320,7 +329,6 @@ class LinkEditModal extends React.PureComponent {
       </Modal>
     );
   }
-
 }
 
 LinkEditModal.propTypes = {