소스 검색

WIP: impl RefsContext.parse

Yuki Takei 6 년 전
부모
커밋
531d879557
1개의 변경된 파일55개의 추가작업 그리고 25개의 파일을 삭제
  1. 55 25
      packages/growi-plugin-attachment-refs/src/client/js/util/RefsContext.js

+ 55 - 25
packages/growi-plugin-attachment-refs/src/client/js/util/RefsContext.js

@@ -12,14 +12,14 @@ export default class RefsContext extends TagContext {
   /**
    * @param {object|TagContext|RefsContext} initArgs
    */
-  constructor(initArgs) {
+  constructor(initArgs, fromPagePath) {
     super(initArgs);
 
-    this.fromPagePath = null;
+    this.fromPagePath = fromPagePath;
 
     // initialized after parse()
-    this.isParsed = null;
     this.pagePath = null;
+    this.isParsed = null;
     this.options = {};
   }
 
@@ -28,32 +28,62 @@ export default class RefsContext extends TagContext {
       return;
     }
 
-    const parsedResult = ArgsParser.parse(this.args);
-    this.options = parsedResult.options;
-
-    // determine specifiedPath
-    // order:
-    //   1: refs(prefix=..., ...)
-    //   2: refs(firstArgs, ...)
-    //   3: fromPagePath
-    const specifiedPath = this.options.prefix
-        || ((parsedResult.firstArgsValue === true) ? parsedResult.firstArgsKey : undefined)
-        || this.fromPagePath;
-
-    // resolve pagePath
-    //   when `fromPagePath`=/hoge and `specifiedPath`=./fuga,
-    //        `pagePath` to be /hoge/fuga
-    //   when `fromPagePath`=/hoge and `specifiedPath`=/fuga,
-    //        `pagePath` to be /fuga
-    //   when `fromPagePath`=/hoge and `specifiedPath`=undefined,
-    //        `pagePath` to be /hoge
-    this.pagePath = (specifiedPath !== undefined)
-      ? decodeURIComponent(url.resolve(pathUtils.addTrailingSlash(this.fromPagePath), specifiedPath))
-      : this.fromPagePath;
+    const parsedArgs = ArgsParser.parse(this.args);
+    this.options = parsedArgs.options;
+
+    if (this.method.match(/^(ref|refimg)$/)) {
+      this.parseForSingle(parsedArgs);
+    }
+    else if (this.method.match(/^(refs|refsimg)$/)) {
+      this.parseForMulti(parsedArgs);
+    }
 
     this.isParsed = true;
   }
 
+  /**
+   * parse method for 'ref' and 'refimg'
+   */
+  parseForSingle(parsedArgs) {
+    this.pagePath = this.resolvePath(this.options.page || this.fromPagePath);
+  }
+
+  /**
+   * parse method for 'refs' and 'refsimg'
+   */
+  parseForMulti(parsedArgs) {
+    console.log(parsedArgs);
+    if (this.options.prefix) {
+      this.prefix = this.resolvePath(this.options.prefix);
+    }
+    else {
+      // determine pagePath
+      // order:
+      //   1: ref(page=..., ...)
+      //   2: refs(firstArgs, ...)
+      //   3: constructor argument
+      const specifiedPath = this.options.page
+          || ((parsedArgs.firstArgsValue === true) ? parsedArgs.firstArgsKey : undefined)
+          || this.fromPagePath;
+
+      this.pagePath = (specifiedPath !== undefined)
+        ? decodeURIComponent(url.resolve(pathUtils.addTrailingSlash(this.fromPagePath), specifiedPath))
+        : this.fromPagePath;
+    }
+
+  }
+
+  // resolve pagePath
+  //   when `fromPagePath`=/hoge and `specifiedPath`=./fuga,
+  //        `pagePath` to be /hoge/fuga
+  //   when `fromPagePath`=/hoge and `specifiedPath`=/fuga,
+  //        `pagePath` to be /fuga
+  //   when `fromPagePath`=/hoge and `specifiedPath`=undefined,
+  //        `pagePath` to be /hoge
+  resolvePath(pagePath) {
+    return decodeURIComponent(url.resolve(pathUtils.addTrailingSlash(this.fromPagePath), pagePath));
+  }
+
   getOptDepth() {
     if (this.options.depth === undefined) {
       return undefined;