Răsfoiți Sursa

move method of generating link

yusuketk 5 ani în urmă
părinte
comite
7915c9deda

+ 10 - 20
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -1,8 +1,6 @@
 import React from 'react';
 import React from 'react';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 
 
-import path from 'path';
-
 import {
 import {
   Modal,
   Modal,
   ModalHeader,
   ModalHeader,
@@ -188,26 +186,18 @@ class LinkEditModal extends React.PureComponent {
       linkerType,
       linkerType,
       isUseRelativePath,
       isUseRelativePath,
       isUsePermanentLink,
       isUsePermanentLink,
+      permalink,
     } = this.state;
     } = this.state;
 
 
-    let reshapedLink = linkInputValue;
-
-    if (isUseRelativePath && linkInputValue.match(/^\//)) {
-      reshapedLink = path.relative(pageContainer.state.path, linkInputValue);
-    }
-    if (isUsePermanentLink) {
-      reshapedLink = this.state.permalink;
-    }
-
-    if (linkerType === Linker.types.pukiwikiLink) {
-      return `[[${labelInputValue}>${reshapedLink}]]`;
-    }
-    if (linkerType === Linker.types.growiLink) {
-      return `[${reshapedLink}]`;
-    }
-    if (linkerType === Linker.types.markdownLink) {
-      return `[${labelInputValue}](${reshapedLink})`;
-    }
+    return new Linker(
+      linkerType,
+      labelInputValue,
+      linkInputValue,
+      isUseRelativePath,
+      pageContainer.state.path,
+      isUsePermanentLink,
+      permalink,
+    );
   }
   }
 
 
   render() {
   render() {

+ 52 - 3
src/client/js/models/Linker.js

@@ -1,10 +1,25 @@
+import path from 'path';
+
 export default class Linker {
 export default class Linker {
 
 
-  constructor(type, label, link) {
+  constructor(
+      type,
+      label,
+      link,
+      isUseRelativePath = false,
+      rootPath = '',
+      isUsePermanentLink = false,
+      permalink = '',
+  ) {
     this.type = type;
     this.type = type;
     this.label = label;
     this.label = label;
     this.link = link;
     this.link = link;
-    // TODO GW-3074 相対パスを利用しているかの情報も持つようにする
+    this.isUseRelativePath = isUseRelativePath;
+    this.rootPath = rootPath;
+    this.isUsePermanentLink = isUsePermanentLink;
+    this.permalink = permalink;
+
+    this.generateMarkdownText = this.generateMarkdownText.bind(this);
   }
   }
 
 
   static types = {
   static types = {
@@ -20,6 +35,28 @@ export default class Linker {
     markdownLink: /^\[(?<label>.*)\]\((?<link>.*)\)$/, // https://regex101.com/r/DZCKP3/2
     markdownLink: /^\[(?<label>.*)\]\((?<link>.*)\)$/, // https://regex101.com/r/DZCKP3/2
   }
   }
 
 
+  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;
+    }
+
+    if (this.type === Linker.types.pukiwikiLink) {
+      if (this.label === reshapedLink) return `[[${reshapedLink}]]`;
+      return `[[${this.label}>${reshapedLink}]]`;
+    }
+    if (this.type === Linker.types.growiLink) {
+      return `[${reshapedLink}]`;
+    }
+    if (this.type === Linker.types.markdownLink) {
+      return `[${this.label}](${reshapedLink})`;
+    }
+  }
+
   // create an instance of Linker from string
   // create an instance of Linker from string
   static fromMarkdownString(str) {
   static fromMarkdownString(str) {
     // if str doesn't mean a linker, create a link whose label is str
     // if str doesn't mean a linker, create a link whose label is str
@@ -50,7 +87,19 @@ export default class Linker {
       ({ label, link } = str.match(this.patterns.markdownLink).groups);
       ({ label, link } = str.match(this.patterns.markdownLink).groups);
     }
     }
 
 
-    return new Linker(type, label, link);
+    // TODO GW-3074 相対パスを利用しているかテキストから判定し以下の値に反映する
+    const isUseRelativePath = false;
+    const rootPath = '';
+
+    return new Linker(
+      type,
+      label,
+      link,
+      isUseRelativePath,
+      rootPath,
+      false,
+      '',
+    );
   }
   }
 
 
   // create an instance of Linker from text with index
   // create an instance of Linker from text with index