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

+ 26 - 14
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -67,37 +67,49 @@ class LinkEditModal extends React.PureComponent {
   // defaultMarkdownLink is an instance of Linker
   // defaultMarkdownLink is an instance of Linker
   show(defaultMarkdownLink = null) {
   show(defaultMarkdownLink = null) {
     // if defaultMarkdownLink is null, set default value in inputs.
     // if defaultMarkdownLink is null, set default value in inputs.
-    const { label = '' } = defaultMarkdownLink;
-    let { link = '', type = Linker.types.markdownLink } = defaultMarkdownLink;
+    const { label = '', link = '' } = defaultMarkdownLink;
+    let { type = Linker.types.markdownLink } = defaultMarkdownLink;
 
 
     // if type of defaultMarkdownLink is pukiwikiLink when pukiwikiLikeLinker plugin is disable, change type(not change label and link)
     // if type of defaultMarkdownLink is pukiwikiLink when pukiwikiLikeLinker plugin is disable, change type(not change label and link)
     if (type === Linker.types.pukiwikiLink && !this.isApplyPukiwikiLikeLinkerPlugin) {
     if (type === Linker.types.pukiwikiLink && !this.isApplyPukiwikiLikeLinkerPlugin) {
       type = Linker.types.markdownLink;
       type = Linker.types.markdownLink;
     }
     }
 
 
-    let isUseRelativePath = false;
-
-    const url = new URL(link, 'http://example.com');
-    if (url.origin === window.location.origin) {
-      link = decodeURI(url.pathname);
-    }
-    else if (url.origin === 'http://example.com' && !link.startsWith('/') && link !== '') {
-      isUseRelativePath = true;
-      const rootPath = this.getRootPath(type);
-      link = path.resolve(rootPath, link);
-    }
+    this.parseLinkAndSetState(link, type);
 
 
     this.setState({
     this.setState({
       show: true,
       show: true,
       labelInputValue: label,
       labelInputValue: label,
-      linkInputValue: link,
       isUsePermanentLink: false,
       isUsePermanentLink: false,
       permalink: '',
       permalink: '',
       linkerType: type,
       linkerType: type,
+    });
+  }
+
+  parseLinkAndSetState(link, type) {
+    const url = new URL(link, 'http://example.com');
+    let isUseRelativePath = false;
+    let reshapedLink = link;
+
+    reshapedLink = this.checkIsPageUrlAndConvertToPath(reshapedLink, url);
+
+    if (url.origin === 'http://example.com' && !reshapedLink.startsWith('/') && reshapedLink !== '') {
+      isUseRelativePath = true;
+      const rootPath = this.getRootPath(type);
+      reshapedLink = path.resolve(rootPath, reshapedLink);
+    }
+
+    this.setState({
+      linkInputValue: reshapedLink,
       isUseRelativePath,
       isUseRelativePath,
     });
     });
   }
   }
 
 
+  // return path name of link if link is this growi page url, else return original link.
+  checkIsPageUrlAndConvertToPath(link, url) {
+    return url.origin === window.location.origin ? decodeURI(url.pathname) : link;
+  }
+
   cancel() {
   cancel() {
     this.hide();
     this.hide();
   }
   }