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

give parentPath from edit mode page create

Futa Arai 1 год назад
Родитель
Сommit
d8b0b8ac5d

+ 5 - 1
apps/app/src/components/Navbar/PageEditorModeManager.tsx

@@ -5,6 +5,7 @@ import { useTranslation } from 'next-i18next';
 
 
 import { useCreatePageAndTransit } from '~/client/services/create-page';
+import { apiv3Get } from '~/client/util/apiv3-client';
 import { toastError } from '~/client/util/toastr';
 import { useIsNotFound } from '~/stores/page';
 import { EditorMode, useEditorMode, useIsDeviceLargerThanMd } from '~/stores/ui';
@@ -75,8 +76,11 @@ export const PageEditorModeManager = (props: Props): JSX.Element => {
     }
 
     try {
+      const res = await apiv3Get('/page/non-empty-closest-ancestor', { path });
       await createAndTransit(
-        { path, wip: shouldCreateWipPage(path), origin: Origin.View },
+        {
+          path, parentPath: res.data.nonEmptyClosestAncestor?.path, wip: shouldCreateWipPage(path), origin: Origin.View,
+        },
         { shouldCheckPageExists: true },
       );
     }

+ 20 - 1
apps/app/src/server/routes/apiv3/page/index.ts

@@ -185,7 +185,7 @@ module.exports = (crowi) => {
   const addActivity = generateAddActivityMiddleware(crowi);
 
   const globalNotificationService = crowi.getGlobalNotificationService();
-  const { Page } = crowi.models;
+  const Page = mongoose.model<IPage, PageModel>('Page');
   const { pageService, exportService } = crowi;
 
   const activityEvent = crowi.event('activity');
@@ -246,6 +246,9 @@ module.exports = (crowi) => {
     contentWidth: [
       body('expandContentWidth').isBoolean(),
     ],
+    nonEmptyClosestAncestor: [
+      query('path').isString(),
+    ],
   };
 
   /**
@@ -939,5 +942,21 @@ module.exports = (crowi) => {
 
   router.put('/:pageId/unpublish', unpublishPageHandlersFactory(crowi));
 
+  router.get(
+    '/non-empty-closest-ancestor', accessTokenParser, loginRequiredStrictly, validator.nonEmptyClosestAncestor, apiV3FormValidator, async(req, res) => {
+      const { path } = req.query;
+      console.log('ここ', path);
+
+      try {
+        const nonEmptyClosestAncestor = await Page.findNonEmptyClosestAncestor(path);
+        return res.apiv3({ nonEmptyClosestAncestor });
+      }
+      catch (err) {
+        logger.error('Failed to get non-empty closest ancestor', err);
+        return res.apiv3Err(err, 500);
+      }
+    },
+  );
+
   return router;
 };

+ 2 - 2
apps/app/src/server/service/page-grant.ts

@@ -771,7 +771,7 @@ class PageGrantService implements IPageGrantService {
   getUserRelatedGrantedGroupsSyncronously(userRelatedGroups: PopulatedGrantedGroup[], page: PageDocument): IGrantedGroup[] {
     const userRelatedGroupIds: string[] = userRelatedGroups.map(ug => ug.item._id.toString());
     return page.grantedGroups?.filter((group) => {
-      return userRelatedGroupIds.includes(getIdForRef(group.item));
+      return userRelatedGroupIds.includes(getIdForRef(group.item).toString());
     }) || [];
   }
 
@@ -782,7 +782,7 @@ class PageGrantService implements IPageGrantService {
     const userRelatedGroups = (await this.getUserRelatedGroups(user));
     const userRelatedGroupIds: string[] = userRelatedGroups.map(ug => ug.item._id.toString());
     return page.grantedGroups?.filter((group) => {
-      return !userRelatedGroupIds.includes(getIdForRef(group.item));
+      return !userRelatedGroupIds.includes(getIdForRef(group.item).toString());
     }) || [];
   }