Browse Source

get preview with permanent link

yusuketk 5 years ago
parent
commit
acea8269c5
1 changed files with 19 additions and 11 deletions
  1. 19 11
      src/client/js/components/PageEditor/LinkEditModal.jsx

+ 19 - 11
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -11,6 +11,7 @@ import {
 import { debounce } from 'throttle-debounce';
 import { debounce } from 'throttle-debounce';
 
 
 import path from 'path';
 import path from 'path';
+import validator from 'validator';
 import Preview from './Preview';
 import Preview from './Preview';
 
 
 import AppContainer from '../../services/AppContainer';
 import AppContainer from '../../services/AppContainer';
@@ -81,6 +82,7 @@ class LinkEditModal extends React.PureComponent {
       show: true,
       show: true,
       labelInputValue: label,
       labelInputValue: label,
       linkerType: type,
       linkerType: type,
+      isUsePermanentLink: false,
     });
     });
   }
   }
 
 
@@ -159,17 +161,23 @@ class LinkEditModal extends React.PureComponent {
   }
   }
 
 
   async getPreview(path) {
   async getPreview(path) {
-    let markdown = '';
-    let permalink = '';
-    try {
-      const res = await this.props.appContainer.apiGet('/pages.get', { path });
-      markdown = res.page.revision.body;
-      permalink = `${window.location.origin}/${res.page.id}`;
-    }
-    catch (err) {
-      markdown = `<div class="alert alert-warning" role="alert"><strong>${err.message}</strong></div>`;
+    if (path.startsWith('/')) {
+      const isPermanentLink = validator.isMongoId(path.slice(1));
+      const pageId = isPermanentLink ? path.slice(1) : null;
+
+      let markdown = '';
+      let permalink = '';
+      try {
+        const { page } = await this.props.appContainer.apiGet('/pages.get', { path, page_id: pageId });
+        markdown = page.revision.body;
+        // 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}` : '';
+      }
+      catch (err) {
+        markdown = `<div class="alert alert-warning" role="alert"><strong>${err.message}</strong></div>`;
+      }
+      this.setState({ markdown, permalink });
     }
     }
-    this.setState({ markdown, permalink });
   }
   }
 
 
   handleChangeTypeahead(selected) {
   handleChangeTypeahead(selected) {
@@ -222,7 +230,7 @@ class LinkEditModal extends React.PureComponent {
     }
     }
 
 
     if (isUsePermanentLink && permalink != null) {
     if (isUsePermanentLink && permalink != null) {
-      reshapedLink = this.permalink;
+      reshapedLink = permalink;
     }
     }
 
 
     return new Linker(linkerType, labelInputValue, reshapedLink);
     return new Linker(linkerType, labelInputValue, reshapedLink);