Просмотр исходного кода

Merge pull request #2385 from weseek/feat/show-link-path-preview

Feat/generate and paste link path
Yuki Takei 5 лет назад
Родитель
Сommit
7b1cb1b9ff
1 измененных файлов с 45 добавлено и 8 удалено
  1. 45 8
      src/client/js/components/PageEditor/LinkEditModal.jsx

+ 45 - 8
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -1,6 +1,8 @@
 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,
@@ -8,10 +10,13 @@ import {
   ModalFooter,
   ModalFooter,
 } from 'reactstrap';
 } from 'reactstrap';
 
 
+import PageContainer from '../../services/PageContainer';
+
 import PagePathAutoComplete from '../PagePathAutoComplete';
 import PagePathAutoComplete from '../PagePathAutoComplete';
 
 
+import { withUnstatedContainers } from '../UnstatedUtils';
 
 
-export default class LinkEditModal extends React.PureComponent {
+class LinkEditModal extends React.PureComponent {
 
 
   constructor(props) {
   constructor(props) {
     super(props);
     super(props);
@@ -23,7 +28,6 @@ export default class LinkEditModal extends React.PureComponent {
       linkInputValue: '',
       linkInputValue: '',
       labelInputValue: '',
       labelInputValue: '',
       linkerType: 'mdLink',
       linkerType: 'mdLink',
-      output: '[label](link)',
     };
     };
 
 
     this.show = this.show.bind(this);
     this.show = this.show.bind(this);
@@ -35,8 +39,8 @@ export default class LinkEditModal extends React.PureComponent {
     this.handleSelecteLinkerType = this.handleSelecteLinkerType.bind(this);
     this.handleSelecteLinkerType = this.handleSelecteLinkerType.bind(this);
     this.toggleIsUseRelativePath = this.toggleIsUseRelativePath.bind(this);
     this.toggleIsUseRelativePath = this.toggleIsUseRelativePath.bind(this);
     this.toggleIsUsePamanentLink = this.toggleIsUsePamanentLink.bind(this);
     this.toggleIsUsePamanentLink = this.toggleIsUsePamanentLink.bind(this);
-    this.showLog = this.showLog.bind(this);
     this.save = this.save.bind(this);
     this.save = this.save.bind(this);
+    this.generateLink = this.generateLink.bind(this);
   }
   }
 
 
   show(defaultLabelInputValue = '') {
   show(defaultLabelInputValue = '') {
@@ -72,10 +76,6 @@ export default class LinkEditModal extends React.PureComponent {
     // TODO GW-2659
     // TODO GW-2659
   }
   }
 
 
-  showLog() {
-    console.log(this.state.linkInputValue);
-  }
-
   handleChangeLabelInput(label) {
   handleChangeLabelInput(label) {
     this.setState({ labelInputValue: label });
     this.setState({ labelInputValue: label });
   }
   }
@@ -88,8 +88,10 @@ export default class LinkEditModal extends React.PureComponent {
   }
   }
 
 
   save() {
   save() {
+    const output = this.generateLink();
+
     if (this.props.onSave != null) {
     if (this.props.onSave != null) {
-      this.props.onSave(this.state.output);
+      this.props.onSave(output);
     }
     }
 
 
     this.hide();
     this.hide();
@@ -104,6 +106,33 @@ export default class LinkEditModal extends React.PureComponent {
     this.setState({ linkInputValue: submitValue });
     this.setState({ linkInputValue: submitValue });
   }
   }
 
 
+
+  generateLink() {
+    const { pageContainer } = this.props;
+    const {
+      linkInputValue,
+      labelInputValue,
+      linkerType,
+      isUseRelativePath,
+    } = this.state;
+
+    let reshapedLink = linkInputValue;
+
+    if (isUseRelativePath && linkInputValue.match(/^\//)) {
+      reshapedLink = path.relative(pageContainer.state.path, linkInputValue);
+    }
+
+    if (linkerType === 'pukiwikiLink') {
+      return `[[${labelInputValue}>${reshapedLink}]]`;
+    }
+    if (linkerType === 'growiLink') {
+      return `[${reshapedLink}]`;
+    }
+    if (linkerType === 'mdLink') {
+      return `[${labelInputValue}](${reshapedLink})`;
+    }
+  }
+
   render() {
   render() {
     return (
     return (
       <Modal isOpen={this.state.show} toggle={this.cancel} size="lg">
       <Modal isOpen={this.state.show} toggle={this.cancel} size="lg">
@@ -221,5 +250,13 @@ export default class LinkEditModal extends React.PureComponent {
 }
 }
 
 
 LinkEditModal.propTypes = {
 LinkEditModal.propTypes = {
+  pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
   onSave: PropTypes.func,
   onSave: PropTypes.func,
 };
 };
+
+/**
+ * Wrapper component for using unstated
+ */
+const LinkEditModalWrapper = withUnstatedContainers(LinkEditModal, [PageContainer]);
+
+export default LinkEditModalWrapper;