Yuki Takei 6 vuotta sitten
vanhempi
sitoutus
a66911a056

+ 4 - 4
packages/growi-plugin-attachment-refs/src/client-entry.js

@@ -1,10 +1,10 @@
-import PreRenderInterceptor from './client/js/util/Interceptor/PreRenderInterceptor';
-import PostRenderInterceptor from './client/js/util/Interceptor/PostRenderInterceptor';
+import RefsPreRenderInterceptor from './client/js/util/Interceptor/RefsPreRenderInterceptor';
+import RefsPostRenderInterceptor from './client/js/util/Interceptor/RefsPostRenderInterceptor';
 
 export default (appContainer) => {
   // add interceptors
   appContainer.interceptorManager.addInterceptors([
-    new PreRenderInterceptor(),
-    new PostRenderInterceptor(appContainer),
+    new RefsPreRenderInterceptor(),
+    new RefsPostRenderInterceptor(appContainer),
   ]);
 };

+ 6 - 4
packages/growi-plugin-attachment-refs/src/client/js/components/AttachmentList.jsx

@@ -5,7 +5,7 @@ import * as url from 'url';
 
 import { pathUtils } from 'growi-commons';
 import RefsContext from '../util/RefsContext';
-import CacheHelper from '../util/CacheHelper';
+import TagCacheManagerFactory from '../util/TagCacheManagerFactory';
 
 
 export default class AttachmentList extends React.Component {
@@ -18,6 +18,8 @@ export default class AttachmentList extends React.Component {
       isError: false,
       errorMessage: '',
     };
+
+    this.tagCacheManager = TagCacheManagerFactory.getInstance();
   }
 
   // eslint-disable-next-line react/no-deprecated
@@ -25,7 +27,7 @@ export default class AttachmentList extends React.Component {
     const pluginContext = this.props.refContext || this.props.refsContext;
 
     // get state object cache
-    const stateCache = CacheHelper.getStateCache(pluginContext);
+    const stateCache = this.tagCacheManager.getStateCache(pluginContext);
 
     // check cache exists
     if (stateCache != null) {
@@ -98,6 +100,6 @@ export default class AttachmentList extends React.Component {
 
 AttachmentList.propTypes = {
   appContainer: PropTypes.object.isRequired,
-  refContext: PropTypes.instanceOf(PropTypes.instanceOf(Object)),
-  refsContext: PropTypes.instanceOf(PropTypes.instanceOf(RefsContext)),
+  // refContext: PropTypes.instanceOf(PropTypes.instanceOf(Object)),
+  // refsContext: PropTypes.instanceOf(PropTypes.instanceOf(RefsContext)),
 };

+ 0 - 65
packages/growi-plugin-attachment-refs/src/client/js/util/CacheHelper.js

@@ -1,65 +0,0 @@
-import { LocalStorageManager } from 'growi-commons';
-
-const REFS_STATE_CACHE_NS = 'refs-state-cache';
-
-export default class CacheHelper {
-
-  /**
-   * generate cache key for storing to storage
-   *
-   * @param {TagContext} tagContext
-   */
-  static generateCacheKey(tagContext) {
-    // return `${lsxContext.fromPagePath}__${lsxContext.args}`;
-    return `${tagContext.method}__${tagContext.args}`;
-  }
-
-  /**
-   *
-   *
-   * @static
-   * @param {TagContext} tagContext
-   * @returns
-   */
-  static getStateCache(tagContext) {
-    const localStorageManager = LocalStorageManager.getInstance();
-
-    const key = CacheHelper.generateCacheKey(tagContext);
-    const stateCache = localStorageManager.retrieveFromSessionStorage(REFS_STATE_CACHE_NS, key);
-
-    // if (stateCache != null && stateCache.nodeTree != null) {
-    //   // instanciate PageNode
-    //   stateCache.nodeTree = stateCache.nodeTree.map((obj) => {
-    //     return PageNode.instanciateFrom(obj);
-    //   });
-    // }
-
-    return stateCache;
-  }
-
-  /**
-   * store state object of React Component with specified key
-   *
-   * @static
-   * @param {TagContext} tagContext
-   * @param {object} state state object of React Component
-   */
-  static cacheState(tagContext, state) {
-    const localStorageManager = LocalStorageManager.getInstance();
-    const key = CacheHelper.generateCacheKey(tagContext);
-    localStorageManager.saveToSessionStorage(REFS_STATE_CACHE_NS, key, state);
-  }
-
-  /**
-   * clear all state caches
-   *
-   * @static
-   *
-   * @memberOf LsxCacheHelper
-   */
-  static clearAllStateCaches() {
-    const localStorageManager = LocalStorageManager.getInstance();
-    localStorageManager.saveToSessionStorage(REFS_STATE_CACHE_NS, {});
-  }
-
-}

+ 5 - 7
packages/growi-plugin-attachment-refs/src/client/js/util/Interceptor/PostRenderInterceptor.js → packages/growi-plugin-attachment-refs/src/client/js/util/Interceptor/RefsPostRenderInterceptor.js

@@ -11,7 +11,7 @@ import AttachmentList from '../../components/AttachmentList';
  *
  *  render React DOM
  */
-export default class PostRenderInterceptor extends BasicInterceptor {
+export default class RefsPostRenderInterceptor extends BasicInterceptor {
 
   constructor(appContainer) {
     super();
@@ -34,15 +34,13 @@ export default class PostRenderInterceptor extends BasicInterceptor {
   process(contextName, ...args) {
     const context = Object.assign(args[0]); // clone
 
-    // forEach keys of tagContextMap
-    Object.keys(context.tagContextMap).forEach((domId) => {
+    // forEach keys of refsContextMap
+    Object.keys(context.refsContextMap).forEach((domId) => {
       const elem = document.getElementById(domId);
 
       if (elem) {
-        // get TagContext instance from context
-        const tagContext = context.tagContextMap[domId] || {};
-        // create RefsContext instance
-        const refsContext = new RefsContext(tagContext);
+        // instanciate RefsContext from context
+        const refsContext = new RefsContext(context.refsContextMap[domId] || {});
         refsContext.fromPagePath = context.currentPagePath;
 
         this.renderReactDOM(refsContext, elem);

+ 11 - 6
packages/growi-plugin-attachment-refs/src/client/js/util/Interceptor/PreRenderInterceptor.js → packages/growi-plugin-attachment-refs/src/client/js/util/Interceptor/RefsPreRenderInterceptor.js

@@ -1,13 +1,13 @@
 import { customTagUtils, BasicInterceptor } from 'growi-commons';
 
-import CacheHelper from '../CacheHelper';
+import TagCacheManagerFactory from '../TagCacheManagerFactory';
 
 /**
  * The interceptor for refs
  *
  *  replace refs tag to a React target element
  */
-export default class PreRenderInterceptor extends BasicInterceptor {
+export default class RefsPreRenderInterceptor extends BasicInterceptor {
 
   /**
    * @inheritdoc
@@ -19,6 +19,13 @@ export default class PreRenderInterceptor extends BasicInterceptor {
     );
   }
 
+  /**
+   * @inheritdoc
+   */
+  isProcessableParallel() {
+    return false;
+  }
+
   /**
    * @inheritdoc
    */
@@ -27,13 +34,11 @@ export default class PreRenderInterceptor extends BasicInterceptor {
     const parsedHTML = context.parsedHTML;
     this.initializeCache(contextName);
 
-    context.lsxContextMap = {};
-
     const tagPattern = /ref|refs|refimg|refsimg/;
     const result = customTagUtils.findTagAndReplace(tagPattern, parsedHTML);
 
     context.parsedHTML = result.html;
-    context.tagContextMap = result.tagContextMap;
+    context.refsContextMap = result.tagContextMap;
 
     return context;
   }
@@ -47,7 +52,7 @@ export default class PreRenderInterceptor extends BasicInterceptor {
    */
   initializeCache(contextName) {
     if (contextName === 'preRenderHtml') {
-      CacheHelper.clearAllStateCaches();
+      TagCacheManagerFactory.getInstance().clearAllStateCaches();
     }
   }
 

+ 21 - 0
packages/growi-plugin-attachment-refs/src/client/js/util/TagCacheManagerFactory.js

@@ -0,0 +1,21 @@
+import { TagCacheManager } from 'growi-commons';
+
+const STATE_CACHE_NS = 'refs-state-cache';
+
+let _instance;
+export default class TagCacheManagerFactory {
+
+  static getInstance() {
+    if (_instance == null) {
+      // create generateCacheKey implementation
+      const generateCacheKey = (refsContext) => {
+        return `${refsContext.method}__${refsContext.fromPagePath}__${refsContext.args}`;
+      };
+
+      _instance = new TagCacheManager(STATE_CACHE_NS, generateCacheKey);
+    }
+
+    return _instance;
+  }
+
+}