Procházet zdrojové kódy

move method to convert path absolute and relative

yusuketk před 5 roky
rodič
revize
61ff3441a6

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

@@ -8,6 +8,7 @@ import {
   ModalFooter,
 } from 'reactstrap';
 
+import path from 'path';
 import Preview from './Preview';
 
 import AppContainer from '../../services/AppContainer';
@@ -62,14 +63,20 @@ class LinkEditModal extends React.PureComponent {
   // defaultMarkdownLink is an instance of Linker
   show(defaultMarkdownLink = null) {
     // if defaultMarkdownLink is null, set default value in inputs.
-    const { label = '', link = '' } = defaultMarkdownLink;
-    let { type = Linker.types.markdownLink } = defaultMarkdownLink;
+    const { pageContainer } = this.props;
+    const { label = '' } = defaultMarkdownLink;
+    let { link = '', 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 === Linker.types.pukiwikiLink && !this.isApplyPukiwikiLikeLinkerPlugin) {
       type = Linker.types.markdownLink;
     }
 
+    const isUseRelativePath = link.startsWith('.');
+    if (isUseRelativePath) {
+      link = path.resolve(pageContainer.state.path, link);
+    }
+
     this.setState({
       show: true,
       labelInputValue: label,
@@ -77,7 +84,7 @@ class LinkEditModal extends React.PureComponent {
       isUsePermanentLink: false,
       permalink: '',
       linkerType: type,
-      isUseRelativePath: false,
+      isUseRelativePath,
     });
   }
 
@@ -178,12 +185,15 @@ class LinkEditModal extends React.PureComponent {
       permalink,
     } = this.state;
 
+    let reshapedLink = linkInputValue;
+    if (isUseRelativePath && linkInputValue.match(/^\//)) {
+      reshapedLink = path.relative(pageContainer.state.path, linkInputValue);
+    }
+
     return new Linker(
       linkerType,
       labelInputValue,
-      linkInputValue,
-      isUseRelativePath,
-      pageContainer.state.path,
+      reshapedLink,
       isUsePermanentLink,
       permalink,
     );

+ 0 - 17
src/client/js/models/Linker.js

@@ -1,21 +1,15 @@
-import path from 'path';
-
 export default class Linker {
 
   constructor(
       type,
       label,
       link,
-      isUseRelativePath = false,
-      rootPath = '',
       isUsePermanentLink = false,
       permalink = '',
   ) {
     this.type = type;
     this.label = label;
     this.link = link;
-    this.isUseRelativePath = isUseRelativePath;
-    this.rootPath = rootPath;
     this.isUsePermanentLink = isUsePermanentLink;
     this.permalink = permalink;
 
@@ -38,9 +32,6 @@ export default class Linker {
   generateMarkdownText() {
     let reshapedLink = this.link;
 
-    if (this.isUseRelativePath && this.link.match(/^\//)) {
-      reshapedLink = path.relative(this.rootPath, this.link);
-    }
     if (this.isUsePermanentLink && this.permalink != null) {
       reshapedLink = this.permalink;
     }
@@ -87,12 +78,6 @@ export default class Linker {
       ({ label, link } = str.match(this.patterns.markdownLink).groups);
     }
 
-    // TODO GW-3074 相対パスを利用しているかテキストから判定し以下の値に反映する
-    const isUseRelativePath = link.startsWith('.');
-    const rootPath = window.location.pathname;
-    if (isUseRelativePath) {
-      link = path.resolve(rootPath, link);
-    }
     const isUsePermanentLink = false;
     const permalink = '';
 
@@ -100,8 +85,6 @@ export default class Linker {
       type,
       label,
       link,
-      isUseRelativePath,
-      rootPath,
       isUsePermanentLink,
       permalink,
     );