2
0
Эх сурвалжийг харах

change mathod of output link

yusuketk 5 жил өмнө
parent
commit
169325085c

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

@@ -1,7 +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,
@@ -123,7 +122,9 @@ class LinkEditModal extends React.PureComponent {
 
 
   handleChangeTypeahead(selected) {
   handleChangeTypeahead(selected) {
     const page = selected[0];
     const page = selected[0];
-    this.setState({ linkInputValue: page.path });
+    if (page != null) {
+      this.setState({ linkInputValue: page.path });
+    }
   }
   }
 
 
   handleChangeLabelInput(label) {
   handleChangeLabelInput(label) {
@@ -160,21 +161,13 @@ class LinkEditModal extends React.PureComponent {
       isUseRelativePath,
       isUseRelativePath,
     } = this.state;
     } = this.state;
 
 
-    let reshapedLink = linkInputValue;
-
-    if (isUseRelativePath && linkInputValue.match(/^\//)) {
-      reshapedLink = path.relative(pageContainer.state.path, linkInputValue);
-    }
-
-    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,
+    );
   }
   }
 
 
   render() {
   render() {

+ 13 - 6
src/client/js/components/PageEditor/MarkdownLinkUtil.js

@@ -1,3 +1,4 @@
+import { cursorCoords } from 'codemirror/src/measurement/position_measurement';
 import Linker from '../../models/Linker';
 import Linker from '../../models/Linker';
 
 
 /**
 /**
@@ -26,13 +27,19 @@ class MarkdownLinkUtil {
     return beginningOfLink >= 0 && endOfLink >= 0;
     return beginningOfLink >= 0 && endOfLink >= 0;
   }
   }
 
 
-  replaceFocusedMarkdownLinkWithEditor(editor, linkStr) {
+  // replace link(link is an instance of Linker)
+  replaceFocusedMarkdownLinkWithEditor(editor, link) {
     const curPos = editor.getCursor();
     const curPos = editor.getCursor();
-    const line = editor.getDoc().getLine(curPos.line);
-    const { beginningOfLink, endOfLink } = getBeginningAndEndIndexOfLink(line, curPos.ch)
-    editor.getDoc().replaceRange(linkStr, { line, ch: beginningOfLink }, { line, ch: endOfLink });
-    editor.getDoc().setCursor(curPos.line + 1, 2);
-    // 洗濯中テキスト内の改行対策
+    const linkStr = link.generateMarkdownText();
+    if (!this.isInLink(editor)) {
+      editor.getDoc().replaceSelection(linkStr);
+    }
+    else {
+      const line = editor.getDoc().getLine(curPos.line);
+      const { beginningOfLink, endOfLink } = Linker.getBeginningAndEndIndexOfLink(line, curPos.ch);
+      editor.getDoc().replaceRange(linkStr, { line: curPos.line, ch: beginningOfLink }, { line: curPos.line, ch: endOfLink });
+    }
+    editor.getDoc().setCursor({ line: curPos.line, ch: linkStr.length });
   }
   }
 
 
 }
 }

+ 27 - 2
src/client/js/models/Linker.js

@@ -1,10 +1,15 @@
+import path from 'path';
+
 export default class Linker {
 export default class Linker {
 
 
-  constructor(type, label, link) {
+  constructor(type = this.type.markdownLink, label = '', link = '', isUseRelativePath = false, rootPath = '') {
     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.generateMarkdownText = this.generateMarkdownText.bind(this);
   }
   }
 
 
   static types = {
   static types = {
@@ -13,6 +18,26 @@ export default class Linker {
     pukiwikiLink: 'pukiwikiLink',
     pukiwikiLink: 'pukiwikiLink',
   }
   }
 
 
+  generateMarkdownText() {
+    let reshapedLink = this.link;
+
+    if (this.isUseRelativePath && this.link.match(/^\//)) {
+      reshapedLink = path.relative(this.rootPath, this.link);
+    }
+
+    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