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

refactor to ensure that specity currentPagePath and fromPagePath separately

Yuki Takei 9 лет назад
Родитель
Сommit
9b0ee0adea

+ 1 - 4
packages/growi-plugin-lsx/src/lib/routes/lsx.js

@@ -7,10 +7,7 @@ module.exports = (crowi, app) => {
     , actions = {};
 
   actions.renderHtml = (req, res) => {
-    var currentPath = req.query.currentPath;
-    var args = req.query.args;
-
-    lsx.renderHtml(req.user, currentPath, args)
+    lsx.renderHtml(req.user, req.query.fromPagePath, req.query.args)
       .then((html) => {
         return res.json(ApiResponse.success({html}));
       })

+ 4 - 4
packages/growi-plugin-lsx/src/lib/util/LsxPageListRenderer.js

@@ -8,13 +8,13 @@ class LsxPageListRenderer {
     this.app = app;
   }
 
-  renderHtml(user, currentPath, args) {
-    debug(`rendering html for currentPath='${currentPath}', args='${args}'`);
+  renderHtml(user, fromPagePath, args) {
+    debug(`rendering html for fromPagePath='${fromPagePath}', args='${args}'`);
 
     // TODO try-catch
 
     // initialize
-    let lsxPrefix = args || currentPath;
+    let lsxPrefix = args || fromPagePath;
     let lsxOptions = {};
 
     // if args is a String like 'key1=value1, key2=value2, ...'
@@ -35,7 +35,7 @@ class LsxPageListRenderer {
     }
 
     // resolve path
-    const pagePath = path.resolve(currentPath, lsxPrefix);
+    const pagePath = path.resolve(fromPath, lsxPrefix);
     const queryOptions = {}
 
     // find pages

+ 9 - 5
packages/growi-plugin-lsx/src/resource/js/components/Lsx.jsx

@@ -14,7 +14,10 @@ export class Lsx extends React.Component {
   }
 
   componentDidMount() {
-    this.props.crowi.apiGet('/plugins/lsx', {currentPath: this.props.currentPath, args: this.props.lsxArgs})
+    const fromPagePath = this.props.fromPagePath;
+    const args = this.props.lsxArgs;
+
+    this.props.crowi.apiGet('/plugins/lsx', {fromPagePath, args})
       .then((res) => {
         if (res.ok) {
           this.setState({ html: res.html });
@@ -40,15 +43,15 @@ export class Lsx extends React.Component {
     if (this.state.isLoading) {
       return (
         <div>
-          <i class="fa fa-spinner fa-pulse fa-fw"></i>
-          <span class="lsx-blink">{this.props.tagExpression}</span>
+          <i className="fa fa-spinner fa-pulse fa-fw"></i>
+          <span className="lsx-blink">{this.props.tagExpression}</span>
         </div>
       );
     }
     if (this.isError) {
       return (
         <div>
-          <i class="fa fa-exclamation-triangle fa-fw"></i>
+          <i className="fa fa-exclamation-triangle fa-fw"></i>
           {this.props.tagExpression} (-> <small>{this.state.message})</small>
         </div>
       )
@@ -62,7 +65,8 @@ export class Lsx extends React.Component {
 
 Lsx.propTypes = {
   crowi: React.PropTypes.object.isRequired,
+  currentPagePath: React.PropTypes.string,
   tagExpression: React.PropTypes.string.isRequired,
-  currentPath: React.PropTypes.string.isRequired,
+  fromPagePath: React.PropTypes.string.isRequired,
   lsxArgs: React.PropTypes.string.isRequired,
 };

+ 10 - 4
packages/growi-plugin-lsx/src/resource/js/util/Interceptor/LsxPostRenderInterceptor.js

@@ -4,6 +4,7 @@ import ReactDOM from 'react-dom';
 import BasicInterceptor from '../../../../lib/util/BasicInterceptor';
 
 import { Lsx } from '../../components/Lsx';
+import { LsxLoadingContext } from '../LsxLoadingContext';
 
 /**
  * The interceptor for lsx
@@ -22,7 +23,10 @@ export class LsxPostRenderInterceptor extends BasicInterceptor {
    * @inheritdoc
    */
   isInterceptWhen(contextName) {
-    return contextName === 'postRenderPreview';
+    return (
+      contextName === 'postRender' ||
+      contextName === 'postRenderPreview'
+    );
   }
 
   /**
@@ -35,13 +39,15 @@ export class LsxPostRenderInterceptor extends BasicInterceptor {
       const elem = document.getElementById(renderId);
 
       if (elem) {
-        const context = contexts[renderId];
-        const props = Object.assign(context, {crowi: this.crowi});
+        const context = new LsxLoadingContext(contexts[renderId]);
 
         // render
         ReactDOM.render(
           <Lsx crowi={this.crowi}
-              tagExpression={context.tagExpression} currentPath={context.currentPath} lsxArgs={context.lsxArgs} />,
+              currentPagePath={context.currentPagePath}
+              tagExpression={context.tagExpression}
+              fromPagePath={context.fromPagePath}
+              lsxArgs={context.lsxArgs} />,
           elem
         );
       }

+ 7 - 5
packages/growi-plugin-lsx/src/resource/js/util/LsxLoadingContext.js

@@ -3,14 +3,16 @@
  */
 export class LsxLoadingContext {
 
+  currentPagePath;
   tagExpression;
-  currentPath;
+  fromPagePath;
   lsxArgs;
 
-  constructor(tagExpression, currentPath, lsxArgs) {
-    this.tagExpression = tagExpression;
-    this.currentPath = currentPath;
-    this.lsxArgs = lsxArgs;
+  constructor(obj) {
+    this.currentPagePath = obj.currentPagePath;
+    this.tagExpression = obj.tagExpression;
+    this.fromPagePath = obj.fromPagePath;
+    this.lsxArgs = obj.lsxArgs;
   }
 
 }

+ 5 - 3
packages/growi-plugin-lsx/src/resource/js/util/PreProcessor/LsxPreProcessor.js

@@ -1,7 +1,7 @@
 import { LsxCache } from '../LsxCache';
 import { LsxLoadingContext } from '../LsxLoadingContext';
 
-// TODO change to PostProcessor
+// TODO change to PreInterceptor
 export class LsxPreProcessor {
 
   constructor(crowi) {
@@ -11,7 +11,9 @@ export class LsxPreProcessor {
 
   process(markdown) {
     // get current path from window.location
-    let currentPath = window.location.pathname;
+    const currentPagePath = window.location.pathname;
+    // TODO retrieve from args for interceptor
+    const fromPagePath = currentPagePath;
 
     return markdown
       // see: https://regex101.com/r/NQq3s9/2
@@ -34,7 +36,7 @@ export class LsxPreProcessor {
 
         // store contexts
         let contexts = JSON.parse(sessionStorage.getItem('lsx-loading-contexts')) || {};
-        contexts[renderId] = new LsxLoadingContext(tagExpression, currentPath, lsxArgs);
+        contexts[renderId] = new LsxLoadingContext({currentPagePath, tagExpression, fromPagePath, lsxArgs});
         sessionStorage.setItem('lsx-loading-contexts', JSON.stringify(contexts));
 
         return this.createReactTargetDom(renderId, tagExpression);