Yuki Takei 6 سال پیش
والد
کامیت
ce67e29ebd

+ 33 - 8
packages/growi-plugin-attachment-refs/src/client/js/components/AttachmentList.jsx

@@ -19,7 +19,8 @@ export default class AttachmentList extends React.Component {
     super(props);
     super(props);
 
 
     this.state = {
     this.state = {
-      isLoading: true,
+      isLoading: false,
+      isLoaded: false,
       isError: false,
       isError: false,
       errorMessage: null,
       errorMessage: null,
 
 
@@ -45,7 +46,27 @@ export default class AttachmentList extends React.Component {
       return; // go to render()
       return; // go to render()
     }
     }
 
 
-    refsContext.parse();
+    // parse
+    try {
+      refsContext.parse();
+    }
+    catch (err) {
+      this.setState({
+        isError: true,
+        errorMessage: err.toString(),
+      });
+
+      // store to sessionStorage
+      this.tagCacheManager.cacheState(refsContext, this.state);
+
+      return;
+    }
+
+    this.loadContents();
+  }
+
+  async loadContents() {
+    const { refsContext } = this.props;
 
 
     let res;
     let res;
     try {
     try {
@@ -60,6 +81,7 @@ export default class AttachmentList extends React.Component {
       });
       });
 
 
       this.setState({
       this.setState({
+        isLoaded: true,
         attachments: [res.data.attachment],
         attachments: [res.data.attachment],
       });
       });
     }
     }
@@ -68,6 +90,8 @@ export default class AttachmentList extends React.Component {
         isError: true,
         isError: true,
         errorMessage: err.response.data,
         errorMessage: err.response.data,
       });
       });
+
+      return;
     }
     }
     finally {
     finally {
       this.setState({ isLoading: false });
       this.setState({ isLoading: false });
@@ -97,12 +121,13 @@ export default class AttachmentList extends React.Component {
         </div>
         </div>
       );
       );
     }
     }
-
-    return (
-      this.state.attachments.map((attachment) => {
-        return <Attachment key={attachment._id} attachment={attachment} />;
-      })
-    );
+    if (this.state.isLoaded) {
+      return (
+        this.state.attachments.map((attachment) => {
+          return <Attachment key={attachment._id} attachment={attachment} />;
+        })
+      );
+    }
   }
   }
 
 
   render() {
   render() {

+ 11 - 1
packages/growi-plugin-attachment-refs/src/client/js/util/RefsContext.js

@@ -45,7 +45,17 @@ export default class RefsContext extends TagContext {
    * parse method for 'ref' and 'refimg'
    * parse method for 'ref' and 'refimg'
    */
    */
   parseForSingle(parsedArgs) {
   parseForSingle(parsedArgs) {
-    this.fileName = parsedArgs.firstArgsKey;
+    // determine fileName
+    // order:
+    //   1: ref(file=..., ...)
+    //   2: refs(firstArgs, ...)
+    this.fileName = this.options.file
+      || ((parsedArgs.firstArgsValue === true) ? parsedArgs.firstArgsKey : undefined);
+
+    if (this.fileName == null) {
+      throw new Error('fileName is not specified. set first argument or specify \'file\' option');
+    }
+
     this.pagePath = this.resolvePath(this.options.page || this.fromPagePath);
     this.pagePath = this.resolvePath(this.options.page || this.fromPagePath);
   }
   }
 
 

+ 2 - 2
packages/growi-plugin-attachment-refs/src/server/routes/refs.js

@@ -16,7 +16,7 @@ module.exports = (crowi) => {
   router.get('/ref', async(req, res) => {
   router.get('/ref', async(req, res) => {
     const user = req.user;
     const user = req.user;
     const { pagePath, fileName } = req.query;
     const { pagePath, fileName } = req.query;
-    // const options = JSON.parse(req.query.options);
+    const options = JSON.parse(req.query.options);
 
 
     if (pagePath == null) {
     if (pagePath == null) {
       res.status(400).send('the param \'pagePath\' must be set.');
       res.status(400).send('the param \'pagePath\' must be set.');
@@ -40,7 +40,7 @@ module.exports = (crowi) => {
 
 
     // not found
     // not found
     if (attachment == null) {
     if (attachment == null) {
-      res.status(404).send(`fileName: '${fileName}' is not found.`);
+      res.status(404).send(`attachment (fileName: '${fileName}') is not found.`);
       return;
       return;
     }
     }