Jelajahi Sumber

Moved process

Taichi Masuyama 4 tahun lalu
induk
melakukan
c6be000a09

+ 3 - 65
packages/app/src/server/service/page-operation.ts

@@ -1,10 +1,8 @@
-import { pagePathUtils, pathUtils } from '@growi/core';
-import escapeStringRegexp from 'escape-string-regexp';
+import { pagePathUtils } from '@growi/core';
 
 
 import PageOperation from '~/server/models/page-operation';
 import PageOperation from '~/server/models/page-operation';
 
 
-const { addTrailingSlash } = pathUtils;
-const { isTrashPage } = pagePathUtils;
+const { isOperatable } = pagePathUtils;
 
 
 class PageOperationService {
 class PageOperationService {
 
 
@@ -33,67 +31,7 @@ class PageOperationService {
 
 
     const toPaths = mainOps.map(op => op.toPath).filter((p): p is string => p != null);
     const toPaths = mainOps.map(op => op.toPath).filter((p): p is string => p != null);
 
 
-    if (isRecursively) {
-
-      if (fromPathToOp != null && !isTrashPage(fromPathToOp)) {
-        const flag = toPaths.some(p => this.isEitherOfPathAreaOverlap(p, fromPathToOp));
-        if (flag) return false;
-      }
-
-      if (toPathToOp != null && !isTrashPage(toPathToOp)) {
-        const flag = toPaths.some(p => this.isPathAreaOverlap(p, toPathToOp));
-        if (flag) return false;
-      }
-
-    }
-    else {
-
-      if (fromPathToOp != null && !isTrashPage(fromPathToOp)) {
-        const flag = toPaths.some(p => this.isPathAreaOverlap(p, fromPathToOp));
-        if (flag) return false;
-      }
-
-      if (toPathToOp != null && !isTrashPage(toPathToOp)) {
-        const flag = toPaths.some(p => this.isPathAreaOverlap(p, toPathToOp));
-        if (flag) return false;
-      }
-
-    }
-
-    return true;
-  }
-
-  private isEitherOfPathAreaOverlap(path1: string, path2: string): boolean {
-    if (path1 === path2) {
-      return true;
-    }
-
-    const path1WithSlash = addTrailingSlash(path1);
-    const path2WithSlash = addTrailingSlash(path2);
-
-    const path1Area = new RegExp(`^${escapeStringRegexp(path1WithSlash)}`);
-    const path2Area = new RegExp(`^${escapeStringRegexp(path2WithSlash)}`);
-
-    if (path1Area.test(path2) || path2Area.test(path1)) {
-      return true;
-    }
-
-    return false;
-  }
-
-  private isPathAreaOverlap(pathToTest: string, pathToBeTested: string): boolean {
-    if (pathToTest === pathToBeTested) {
-      return true;
-    }
-
-    const pathWithSlash = addTrailingSlash(pathToTest);
-
-    const pathAreaToTest = new RegExp(`^${escapeStringRegexp(pathWithSlash)}`);
-    if (pathAreaToTest.test(pathToBeTested)) {
-      return true;
-    }
-
-    return false;
+    return isOperatable(isRecursively, fromPathToOp, toPathToOp, toPaths);
   }
   }
 
 
 }
 }

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

@@ -1,6 +1,7 @@
 import nodePath from 'path';
 import nodePath from 'path';
 
 
 import escapeStringRegexp from 'escape-string-regexp';
 import escapeStringRegexp from 'escape-string-regexp';
+import { addTrailingSlash } from './path-utils';
 
 
 /**
 /**
  * Whether path is the top page
  * Whether path is the top page
@@ -194,3 +195,67 @@ export const omitDuplicateAreaPageFromPages = (pages: any[]): any[] => {
     return !isDuplicate;
     return !isDuplicate;
   });
   });
 };
 };
+
+
+const isEitherOfPathAreaOverlap = (path1: string, path2: string): boolean => {
+  if (path1 === path2) {
+    return true;
+  }
+
+  const path1WithSlash = addTrailingSlash(path1);
+  const path2WithSlash = addTrailingSlash(path2);
+
+  const path1Area = new RegExp(`^${escapeStringRegexp(path1WithSlash)}`);
+  const path2Area = new RegExp(`^${escapeStringRegexp(path2WithSlash)}`);
+
+  if (path1Area.test(path2) || path2Area.test(path1)) {
+    return true;
+  }
+
+  return false;
+};
+const isPathAreaOverlap = (pathToTest: string, pathToBeTested: string): boolean => {
+  if (pathToTest === pathToBeTested) {
+    return true;
+  }
+
+  const pathWithSlash = addTrailingSlash(pathToTest);
+
+  const pathAreaToTest = new RegExp(`^${escapeStringRegexp(pathWithSlash)}`);
+  if (pathAreaToTest.test(pathToBeTested)) {
+    return true;
+  }
+
+  return false;
+};
+
+export const isOperatable = (isRecursively: boolean, fromPathToOp: string | null, toPathToOp: string | null, toPathsToTest: string[]): boolean => {
+  if (isRecursively) {
+
+    if (fromPathToOp != null && !isTrashPage(fromPathToOp)) {
+      const flag = toPathsToTest.some(p => isEitherOfPathAreaOverlap(p, fromPathToOp));
+      if (flag) return false;
+    }
+
+    if (toPathToOp != null && !isTrashPage(toPathToOp)) {
+      const flag = toPathsToTest.some(p => isPathAreaOverlap(p, toPathToOp));
+      if (flag) return false;
+    }
+
+  }
+  else {
+
+    if (fromPathToOp != null && !isTrashPage(fromPathToOp)) {
+      const flag = toPathsToTest.some(p => isPathAreaOverlap(p, fromPathToOp));
+      if (flag) return false;
+    }
+
+    if (toPathToOp != null && !isTrashPage(toPathToOp)) {
+      const flag = toPathsToTest.some(p => isPathAreaOverlap(p, toPathToOp));
+      if (flag) return false;
+    }
+
+  }
+
+  return true;
+};