page-operation.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { pagePathUtils } from '@growi/core';
  2. import PageOperation from '~/server/models/page-operation';
  3. const { isEitherOfPathAreaOverlap, isPathAreaOverlap, isTrashPage } = pagePathUtils;
  4. class PageOperationService {
  5. crowi: any;
  6. constructor(crowi) {
  7. this.crowi = crowi;
  8. }
  9. // TODO: Remove this code when resuming feature is implemented
  10. async init():Promise<void> {
  11. await PageOperation.deleteMany({});
  12. }
  13. /**
  14. * Check if the operation is operatable
  15. * @param isRecursively Boolean that determines whether the operation is recursive or not
  16. * @param fromPathToOp The path to operate from
  17. * @param toPathToOp The path to operate to
  18. * @returns boolean
  19. */
  20. async canOperate(isRecursively: boolean, fromPathToOp: string | null, toPathToOp: string | null): Promise<boolean> {
  21. const mainOps = await PageOperation.findMainOps();
  22. if (mainOps.length === 0) {
  23. return true;
  24. }
  25. const toPaths = mainOps.map(op => op.toPath).filter((p): p is string => p != null);
  26. if (isRecursively) {
  27. if (fromPathToOp != null && !isTrashPage(fromPathToOp)) {
  28. const flag = toPaths.some(p => isEitherOfPathAreaOverlap(p, fromPathToOp));
  29. if (flag) return false;
  30. }
  31. if (toPathToOp != null && !isTrashPage(toPathToOp)) {
  32. const flag = toPaths.some(p => isPathAreaOverlap(p, toPathToOp));
  33. if (flag) return false;
  34. }
  35. }
  36. else {
  37. if (fromPathToOp != null && !isTrashPage(fromPathToOp)) {
  38. const flag = toPaths.some(p => isPathAreaOverlap(p, fromPathToOp));
  39. if (flag) return false;
  40. }
  41. if (toPathToOp != null && !isTrashPage(toPathToOp)) {
  42. const flag = toPaths.some(p => isPathAreaOverlap(p, toPathToOp));
  43. if (flag) return false;
  44. }
  45. }
  46. return true;
  47. }
  48. }
  49. export default PageOperationService;