Explorar el Código

Enable to edit empty pages

Taichi Masuyama hace 4 años
padre
commit
cdd55e4bd0

+ 19 - 13
packages/app/src/client/app.jsx

@@ -177,19 +177,25 @@ const renderMainComponents = () => {
 
 // extract context before rendering main components
 const elem = document.getElementById('page-context');
-ReactDOM.render(
-  <I18nextProvider i18n={i18n}>
-    <ErrorBoundary>
-      <SWRConfig value={swrGlobalConfiguration}>
-        <Provider inject={injectableContainers}>
-          {componentMappings['page-context']}
-        </Provider>
-      </SWRConfig>
-    </ErrorBoundary>
-  </I18nextProvider>,
-  elem,
-  renderMainComponents,
-);
+
+if (elem != null) {
+  ReactDOM.render(
+    <I18nextProvider i18n={i18n}>
+      <ErrorBoundary>
+        <SWRConfig value={swrGlobalConfiguration}>
+          <Provider inject={injectableContainers}>
+            {componentMappings['page-context']}
+          </Provider>
+        </SWRConfig>
+      </ErrorBoundary>
+    </I18nextProvider>,
+    elem,
+    renderMainComponents,
+  );
+}
+else {
+  renderMainComponents();
+}
 
 
 // initialize scrollpos-styler

+ 13 - 1
packages/app/src/server/models/obsolete-page.js

@@ -939,12 +939,24 @@ export const getPageSchema = (crowi) => {
       }
     }
 
+    /*
+     * update empty page if exists, if not, create a new page
+     */
+    let page;
+    const emptyPage = await Page.findOne({ path, isEmpty: true });
+    if (emptyPage != null) {
+      page = emptyPage;
+      page.isEmpty = false;
+    }
+    else {
+      page = new Page();
+    }
+
     let parent = parentId;
     if (isV5Compatible && parent == null && !isTopPage(path)) {
       parent = await Page.getParentIdAndFillAncestors(path);
     }
 
-    const page = new Page();
     page.path = path;
     page.creator = user;
     page.lastUpdateUser = user;

+ 10 - 10
packages/app/src/server/routes/apiv3/pages.js

@@ -192,7 +192,7 @@ module.exports = (crowi) => {
   async function createPageAction({
     path, body, user, options,
   }) {
-    const createdPage = Page.create(path, body, user, options);
+    const createdPage = await Page.create(path, body, user, options);
     return createdPage;
   }
 
@@ -267,21 +267,21 @@ module.exports = (crowi) => {
     // check whether path starts slash
     path = pathUtils.addHeadingSlash(path);
 
-    // check page existence
-    const isExist = await Page.count({ path }) > 0;
-    if (isExist) {
-      return res.apiv3Err(new ErrorV3('Failed to post page', 'page_exists'), 500);
-    }
-
     const options = {};
     if (grant != null) {
       options.grant = grant;
       options.grantUserGroupId = grantUserGroupId;
     }
 
-    const createdPage = await createPageAction({
-      path, body, user: req.user, options,
-    });
+    let createdPage;
+    try {
+      createdPage = await createPageAction({
+        path, body, user: req.user, options,
+      });
+    }
+    catch (err) {
+      return res.apiv3Error(err);
+    }
 
     const savedTags = await saveTagsAction({ createdPage, pageTags });
 

+ 1 - 1
packages/app/src/server/routes/index.js

@@ -139,7 +139,7 @@ module.exports = function(crowi, app) {
   // my drafts
   app.get('/me/drafts'                , loginRequiredStrictly, me.drafts.list);
 
-  app.get('/:id([0-9a-z]{24})'       , loginRequired , page.showPage);
+  app.get('/:id([0-9a-z]{24})'       , loginRequired , page.showPage, page.notFound);
   app.get('/_r/:id([0-9a-z]{24})'    , loginRequired , page.redirector); // alias
   app.get('/attachment/:id([0-9a-z]{24})' , certifySharedFile , loginRequired, attachment.api.get);
   app.get('/attachment/profile/:id([0-9a-z]{24})' , loginRequired, attachment.api.get);

+ 18 - 1
packages/app/src/server/routes/page.js

@@ -299,6 +299,12 @@ module.exports = function(crowi, app) {
       next();
     }
 
+    if (page.isEmpty) {
+      req.isEmpty = true;
+      req.pagePath = page.path;
+      return next();
+    }
+
     const renderVars = {};
 
     // populate
@@ -357,6 +363,13 @@ module.exports = function(crowi, app) {
       return next();
     }
 
+    // empty page
+    if (page.isEmpty) {
+      req.isEmpty = true;
+      req.pagePath = page.path;
+      return next(); // to page.notFound
+    }
+
     const { path } = page; // this must exist
 
     if (page.redirectTo) {
@@ -503,7 +516,7 @@ module.exports = function(crowi, app) {
   /* eslint-enable no-else-return */
 
   actions.notFound = async function(req, res) {
-    const path = getPathFromRequest(req);
+    const path = req.pagePath || getPathFromRequest(req);
 
     let view;
     const renderVars = { path };
@@ -586,6 +599,10 @@ module.exports = function(crowi, app) {
     }
 
     if (pages.length === 1) {
+      if (pages[0].isEmpty) {
+        return next();
+      }
+
       const url = new URL('https://dummy.origin');
       url.pathname = `/${pages[0]._id}`;
       Object.entries(req.query).forEach(([key, value], i) => {