Ver código fonte

Merge branch 'imprv/refactor-to-parse-link' into imprv/show-link-element-preview

# Conflicts:
#	src/client/js/models/Linker.js
yusuketk 5 anos atrás
pai
commit
748dc909e2

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

@@ -11,6 +11,7 @@ import {
 import { debounce } from 'throttle-debounce';
 
 import path from 'path';
+import validator from 'validator';
 import Preview from './Preview';
 
 import AppContainer from '../../services/AppContainer';
@@ -165,17 +166,23 @@ class LinkEditModal extends React.PureComponent {
   }
 
   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}`;
+    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 });
     }
-    catch (err) {
-      markdown = `<div class="alert alert-warning" role="alert"><strong>${err.message}</strong></div>`;
-    }
-    this.setState({ markdown, permalink });
   }
 
   getLinkTextPreview() {
@@ -245,7 +252,11 @@ class LinkEditModal extends React.PureComponent {
       reshapedLink = rootPath === linkInputValue ? '.' : path.relative(rootPath, linkInputValue);
     }
 
-    return new Linker(linkerType, labelInputValue, reshapedLink, isUsePermanentLink, permalink);
+    if (isUsePermanentLink && permalink != null) {
+      reshapedLink = permalink;
+    }
+
+    return new Linker(linkerType, labelInputValue, reshapedLink);
   }
 
   getRootPath(type) {

+ 3 - 12
src/client/js/models/Linker.js

@@ -1,17 +1,13 @@
 export default class Linker {
 
   constructor(
-      type,
-      label,
-      link,
-      isUsePermanentLink = false,
-      permalink = '',
+      type = this.types.markdownLink,
+      label = '',
+      link = '',
   ) {
     this.type = type;
     this.label = label;
     this.link = link;
-    this.isUsePermanentLink = isUsePermanentLink;
-    this.permalink = permalink;
 
     this.generateMarkdownText = this.generateMarkdownText.bind(this);
   }
@@ -72,15 +68,10 @@ export default class Linker {
       link = label;
     }
 
-    const isUsePermanentLink = false;
-    const permalink = '';
-
     return new Linker(
       type,
       label,
       link,
-      isUsePermanentLink,
-      permalink,
     );
   }