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

implement getAncestorsPathsByFromAndToPath

yohei0125 3 лет назад
Родитель
Сommit
12cebb0fb6

+ 10 - 7
packages/app/src/server/service/page-operation.ts

@@ -8,7 +8,9 @@ import { ObjectIdLike } from '../interfaces/mongoose-utils';
 
 const logger = loggerFactory('growi:services:page-operation');
 
-const { isEitherOfPathAreaOverlap, isPathAreaOverlap, isTrashPage } = pagePathUtils;
+const {
+  isEitherOfPathAreaOverlap, isPathAreaOverlap, isTrashPage, collectAncestorPaths,
+} = pagePathUtils;
 const AUTO_UPDATE_INTERVAL_SEC = 5;
 
 const {
@@ -169,12 +171,13 @@ class PageOperationService {
     clearInterval(timerObj);
   }
 
-  async getAncestorsByFromAndToPath(fromPath: string, toPath: string) {
-    const fromAncestors = await this.crowi.getParentAndFillAncestorsBySystem(fromPath);
-    const toAncestors = await this.crowi.getParentAndFillAncestorsBySystem(toPath);
-    console.log({ fromAncestors });
-    console.log({ toAncestors });
-    return;
+  // get all ancestors paths
+  getAncestorsPathsByFromAndToPath(fromPath: string, toPath: string): string[] {
+    const fromAncestorsPaths = collectAncestorPaths(fromPath);
+    const toAncestorsPaths = collectAncestorPaths(toPath);
+    // merge duplicate paths
+    const mergedPaths = Array.from(new Set(toAncestorsPaths.concat(fromAncestorsPaths)));
+    return mergedPaths;
   }
 
 }

+ 16 - 0
packages/core/src/utils/page-path-utils.ts

@@ -287,3 +287,19 @@ export const generateChildrenRegExp = (path: string): RegExp => {
   // ex. /parent/any_child OR /any_level1
   return new RegExp(`^${path}(\\/[^/]+)\\/?$`);
 };
+
+// Sort paths by `/` count in descending order .
+export const sortPathsBySlashCountInDesc = (paths: string[]): string[] => {
+  const pattern = /\//g; // matching all '/'
+  const sortedPaths = paths.sort((_a, _b) => {
+    if (_a === '/') {
+      return 1;
+    }
+    const a = _a.match(pattern);
+    const b = _b.match(pattern);
+    if (a == null || b == null) return 0;
+
+    return b.length - a.length; // Descending
+  });
+  return sortedPaths;
+};