yusuketk 5 лет назад
Родитель
Сommit
7d3739631f
1 измененных файлов с 9 добавлено и 23 удалено
  1. 9 23
      src/client/js/components/PageEditor/LinkEditModal.jsx

+ 9 - 23
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -9,8 +9,6 @@ import {
   PopoverBody,
   PopoverBody,
 } from 'reactstrap';
 } from 'reactstrap';
 
 
-import { debounce } from 'throttle-debounce';
-
 import path from 'path';
 import path from 'path';
 import validator from 'validator';
 import validator from 'validator';
 import Preview from './Preview';
 import Preview from './Preview';
@@ -36,8 +34,6 @@ class LinkEditModal extends React.PureComponent {
       linkInputValue: '',
       linkInputValue: '',
       labelInputValue: '',
       labelInputValue: '',
       linkerType: Linker.types.markdownLink,
       linkerType: Linker.types.markdownLink,
-      markdown: '',
-      previewError: '',
       permalink: '',
       permalink: '',
       linkText: '',
       linkText: '',
       isPreviewOpen: false,
       isPreviewOpen: false,
@@ -59,15 +55,7 @@ class LinkEditModal extends React.PureComponent {
     this.renderPreview = this.renderPreview.bind(this);
     this.renderPreview = this.renderPreview.bind(this);
     this.getRootPath = this.getRootPath.bind(this);
     this.getRootPath = this.getRootPath.bind(this);
     this.toggleIsPreviewOpen = this.toggleIsPreviewOpen.bind(this);
     this.toggleIsPreviewOpen = this.toggleIsPreviewOpen.bind(this);
-    this.generateAndSetPreviewDebounced = debounce(200, this.generateAndSetPreview.bind(this));
-  }
-
-  componentDidUpdate(prevProps, prevState) {
-    const { linkInputValue: prevLinkInputValue } = prevState;
-    const { linkInputValue } = this.state;
-    if (linkInputValue !== prevLinkInputValue) {
-      this.generateAndSetPreviewDebounced(linkInputValue);
-    }
+    this.getPreview = this.getPreview.bind(this);
   }
   }
 
 
   // defaultMarkdownLink is an instance of Linker
   // defaultMarkdownLink is an instance of Linker
@@ -159,22 +147,19 @@ class LinkEditModal extends React.PureComponent {
   }
   }
 
 
   renderPreview() {
   renderPreview() {
-    if (this.state.markdown !== '') {
+    const markdown = this.getPreview(this.state.linkInputValue);
+    if (markdown !== '') {
       return (
       return (
         <div className="linkedit-preview">
         <div className="linkedit-preview">
           <Preview markdown={this.state.markdown} />
           <Preview markdown={this.state.markdown} />
         </div>
         </div>
       );
       );
     }
     }
-    if (this.state.previewError !== '') {
-      return this.state.previewError;
-    }
     return 'Page preview here.';
     return 'Page preview here.';
   }
   }
 
 
-  async generateAndSetPreview(path) {
-    let markdown = '';
-    let previewError = '';
+  async getPreview(path) {
+    let result = '';
     let permalink = '';
     let permalink = '';
 
 
     if (path.startsWith('/')) {
     if (path.startsWith('/')) {
@@ -184,15 +169,16 @@ class LinkEditModal extends React.PureComponent {
 
 
       try {
       try {
         const { page } = await this.props.appContainer.apiGet('/pages.get', { path: pathWithoutFragment, page_id: pageId });
         const { page } = await this.props.appContainer.apiGet('/pages.get', { path: pathWithoutFragment, page_id: pageId });
-        markdown = page.revision.body;
+        result = page.revision.body;
         // create permanent link only if path isn't permanent link because checkbox for isUsePermanentLink is disabled when permalink is ''.
         // create permanent link only if path isn't permanent link because checkbox for isUsePermanentLink is disabled when permalink is ''.
         permalink = !isPermanentLink ? `${window.location.origin}/${page.id}` : '';
         permalink = !isPermanentLink ? `${window.location.origin}/${page.id}` : '';
       }
       }
       catch (err) {
       catch (err) {
-        previewError = err.message;
+        result = err.message;
       }
       }
     }
     }
-    this.setState({ markdown, previewError, permalink });
+    this.setState({ permalink });
+    return result;
   }
   }
 
 
   renderLinkPreview() {
   renderLinkPreview() {