yusuketk 5 лет назад
Родитель
Сommit
831a4afe36
2 измененных файлов с 34 добавлено и 33 удалено
  1. 21 15
      src/client/js/components/PageEditor/LinkEditModal.jsx
  2. 13 18
      src/client/js/models/Linker.js

+ 21 - 15
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -15,6 +15,7 @@ import AppContainer from '../../services/AppContainer';
 import PageContainer from '../../services/PageContainer';
 
 import SearchTypeahead from '../SearchTypeahead';
+import Linker from '../../models/Linker';
 
 import { withUnstatedContainers } from '../UnstatedUtils';
 
@@ -29,7 +30,7 @@ class LinkEditModal extends React.PureComponent {
       isUsePermanentLink: false,
       linkInputValue: '',
       labelInputValue: '',
-      linkerType: 'mdLink',
+      linkerType: Linker.types.markdownLink,
       markdown: '',
     };
 
@@ -60,7 +61,12 @@ class LinkEditModal extends React.PureComponent {
   // defaultMarkdownLink is an instance of Linker
   show(defaultMarkdownLink = null) {
     // if defaultMarkdownLink is null, set default value in inputs.
-    const {type='mdLink', label='', link=''} = defaultMarkdownLink ;
+    const {type=Linker.types.markdownLink, label='', link=''} = 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;
+    }
 
     this.setState({
       show: true,
@@ -81,7 +87,7 @@ class LinkEditModal extends React.PureComponent {
   }
 
   toggleIsUseRelativePath() {
-    if (this.state.linkerType === 'growiLink') {
+    if (this.state.linkerType === Linker.types.growiLink) {
       return;
     }
     this.setState({ isUseRelativePath: !this.state.isUseRelativePath });
@@ -123,7 +129,7 @@ class LinkEditModal extends React.PureComponent {
   }
 
   handleSelecteLinkerType(linkerType) {
-    if (this.state.isUseRelativePath && linkerType === 'growiLink') {
+    if (this.state.isUseRelativePath && linkerType === Linker.types.growiLink) {
       this.toggleIsUseRelativePath();
     }
     this.setState({ linkerType });
@@ -154,13 +160,13 @@ class LinkEditModal extends React.PureComponent {
       reshapedLink = path.relative(pageContainer.state.path, linkInputValue);
     }
 
-    if (linkerType === 'pukiwikiLink') {
+    if (linkerType === Linker.types.pukiwikiLink) {
       return `[[${labelInputValue}>${reshapedLink}]]`;
     }
-    if (linkerType === 'growiLink') {
+    if (linkerType === Linker.types.growiLink) {
       return `[${reshapedLink}]`;
     }
-    if (linkerType === 'mdLink') {
+    if (linkerType === Linker.types.markdownLink) {
       return `[${labelInputValue}](${reshapedLink})`;
     }
   }
@@ -208,16 +214,16 @@ class LinkEditModal extends React.PureComponent {
                     <div className="form-group btn-group d-flex" role="group" aria-label="type">
                       <button
                         type="button"
-                        name="mdLink"
-                        className={`btn btn-outline-secondary w-100 ${this.state.linkerType === 'mdLink' && 'active'}`}
+                        name={Linker.types.markdownLink}
+                        className={`btn btn-outline-secondary w-100 ${this.state.linkerType === Linker.types.markdownLink && 'active'}`}
                         onClick={e => this.handleSelecteLinkerType(e.target.name)}
                       >
                         Markdown
                       </button>
                       <button
                         type="button"
-                        name="growiLink"
-                        className={`btn btn-outline-secondary w-100 ${this.state.linkerType === 'growiLink' && 'active'}`}
+                        name={Linker.types.growiLink}
+                        className={`btn btn-outline-secondary w-100 ${this.state.linkerType === Linker.types.growiLink && 'active'}`}
                         onClick={e => this.handleSelecteLinkerType(e.target.name)}
                       >
                         Growi Original
@@ -225,8 +231,8 @@ class LinkEditModal extends React.PureComponent {
                       {this.isApplyPukiwikiLikeLinkerPlugin && (
                         <button
                           type="button"
-                          name="pukiwikiLink"
-                          className={`btn btn-outline-secondary w-100 ${this.state.linkerType === 'pukiwikiLink' && 'active'}`}
+                          name={Linker.types.pukiwikiLink}
+                          className={`btn btn-outline-secondary w-100 ${this.state.linkerType === Linker.types.pukiwikiLink && 'active'}`}
                           onClick={e => this.handleSelecteLinkerType(e.target.name)}
                         >
                           Pukiwiki
@@ -242,7 +248,7 @@ class LinkEditModal extends React.PureComponent {
                         id="label"
                         value={this.state.labelInputValue}
                         onChange={e => this.handleChangeLabelInput(e.target.value)}
-                        disabled={this.state.linkerType === 'growiLink'}
+                        disabled={this.state.linkerType === Linker.types.growiLink}
                       />
                     </div>
                     <div className="form-inline">
@@ -252,7 +258,7 @@ class LinkEditModal extends React.PureComponent {
                           id="relativePath"
                           type="checkbox"
                           checked={this.state.isUseRelativePath}
-                          disabled={this.state.linkerType === 'growiLink'}
+                          disabled={this.state.linkerType === Linker.types.growiLink}
                         />
                         <label className="custom-control-label" htmlFor="relativePath" onClick={this.toggleIsUseRelativePath}>
                           Use relative path

+ 13 - 18
src/client/js/models/Linker.js

@@ -1,12 +1,3 @@
-const types = {
-  markdownLink: 'mdLink',
-  growiLink: 'growiLink',
-  pukiwikiLink: 'pukiwikiLink',
-}
-
-// const isApplyPukiwikiLikeLinkerPlugin = window.growiRenderer.preProcessors.some(process => process.constructor.name === 'PukiwikiLikeLinker');
-const isApplyPukiwikiLikeLinkerPlugin = true;
-
 export default class Linker {
 
   constructor(type, label, link) {
@@ -15,17 +6,23 @@ export default class Linker {
     this.link = link;
   }
 
+  static types = {
+    markdownLink: 'mdLink',
+    growiLink: 'growiLink',
+    pukiwikiLink: 'pukiwikiLink',
+  }
+
   // create an instance of Linker from string
   static fromMarkdownString(str) {
     // if str doesn't mean a linker, create a link whose label is str
     let label=str;
     let link='';
-    let type=types.markdownLink;
+    let type=this.types.markdownLink;
 
     // pukiwiki
     // https://regex101.com/r/2fNmUN/1
-    if (str.match(/^\[\[.*\]\]$/) && isApplyPukiwikiLikeLinkerPlugin) {
-      type = types.pukiwikiLink;
+    if (str.match(/^\[\[.*\]\]$/)) {
+      type = this.types.pukiwikiLink;
       const value = str.slice(2, -2);
       const indexOfSplit = value.lastIndexOf('>');
       if (indexOfSplit < 0) {
@@ -38,7 +35,7 @@ export default class Linker {
     // growi
     // https://regex101.com/r/DJfkYf/1
     else if (str.match(/^\[\/.*\]$/)) {
-      type = types.growiLink;
+      type = this.types.growiLink;
       const value = str.slice(1, -1);
       label = value;
       link = value;
@@ -46,7 +43,7 @@ export default class Linker {
     // markdown
     // https://regex101.com/r/DZCKP3/1
     else if (str.match(/^\[.*\]\(.*\)$/)) {
-      type = types.markdownLink;
+      type = this.types.markdownLink;
       const value = str.slice(1, -1);
       const indexOfSplit = value.lastIndexOf('](');
       label = value.slice(0, indexOfSplit);
@@ -62,7 +59,7 @@ export default class Linker {
     // if index is in a link, extract it from line
     let linkStr = '';
     if (beginningOfLink >= 0 && endOfLink >= 0) {
-     linkStr = line.substring(beginningOfLink, endOfLink);
+      linkStr = line.substring(beginningOfLink, endOfLink);
     }
     return this.fromMarkdownString(linkStr);
   }
@@ -73,9 +70,7 @@ export default class Linker {
     let beginningOfLink, endOfLink;
 
     // pukiwiki link ('[[link]]')
-    if (isApplyPukiwikiLikeLinkerPlugin) {
-      [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[[', ']]');
-    }
+    [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[[', ']]');
 
     // if index is not in a pukiwiki link
     // growi link ('[/link]')