@milaboratories/pl-model-middle-layer 1.32.2 → 1.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/dist/folders/commit.cjs +79 -0
  2. package/dist/folders/commit.cjs.map +1 -0
  3. package/dist/folders/commit.d.ts +82 -0
  4. package/dist/folders/commit.d.ts.map +1 -0
  5. package/dist/folders/commit.js +78 -0
  6. package/dist/folders/commit.js.map +1 -0
  7. package/dist/folders/decode.cjs +79 -0
  8. package/dist/folders/decode.cjs.map +1 -0
  9. package/dist/folders/decode.d.ts +57 -0
  10. package/dist/folders/decode.d.ts.map +1 -0
  11. package/dist/folders/decode.js +77 -0
  12. package/dist/folders/decode.js.map +1 -0
  13. package/dist/folders/document.cjs +49 -0
  14. package/dist/folders/document.cjs.map +1 -0
  15. package/dist/folders/document.d.ts +56 -0
  16. package/dist/folders/document.d.ts.map +1 -0
  17. package/dist/folders/document.js +45 -0
  18. package/dist/folders/document.js.map +1 -0
  19. package/dist/folders/index.cjs +37 -0
  20. package/dist/folders/index.d.ts +10 -0
  21. package/dist/folders/index.js +10 -0
  22. package/dist/folders/inheritance.cjs +23 -0
  23. package/dist/folders/inheritance.cjs.map +1 -0
  24. package/dist/folders/inheritance.d.ts +19 -0
  25. package/dist/folders/inheritance.d.ts.map +1 -0
  26. package/dist/folders/inheritance.js +23 -0
  27. package/dist/folders/inheritance.js.map +1 -0
  28. package/dist/folders/invariants.cjs +115 -0
  29. package/dist/folders/invariants.cjs.map +1 -0
  30. package/dist/folders/invariants.d.ts +48 -0
  31. package/dist/folders/invariants.d.ts.map +1 -0
  32. package/dist/folders/invariants.js +114 -0
  33. package/dist/folders/invariants.js.map +1 -0
  34. package/dist/folders/naming.cjs +53 -0
  35. package/dist/folders/naming.cjs.map +1 -0
  36. package/dist/folders/naming.d.ts +31 -0
  37. package/dist/folders/naming.d.ts.map +1 -0
  38. package/dist/folders/naming.js +51 -0
  39. package/dist/folders/naming.js.map +1 -0
  40. package/dist/folders/planner.cjs +187 -0
  41. package/dist/folders/planner.cjs.map +1 -0
  42. package/dist/folders/planner.d.ts +100 -0
  43. package/dist/folders/planner.d.ts.map +1 -0
  44. package/dist/folders/planner.js +185 -0
  45. package/dist/folders/planner.js.map +1 -0
  46. package/dist/folders/removal.cjs +47 -0
  47. package/dist/folders/removal.cjs.map +1 -0
  48. package/dist/folders/removal.d.ts +42 -0
  49. package/dist/folders/removal.d.ts.map +1 -0
  50. package/dist/folders/removal.js +45 -0
  51. package/dist/folders/removal.js.map +1 -0
  52. package/dist/folders/view.cjs +210 -0
  53. package/dist/folders/view.cjs.map +1 -0
  54. package/dist/folders/view.d.ts +93 -0
  55. package/dist/folders/view.d.ts.map +1 -0
  56. package/dist/folders/view.js +205 -0
  57. package/dist/folders/view.js.map +1 -0
  58. package/dist/index.cjs +43 -2
  59. package/dist/index.d.ts +12 -2
  60. package/dist/index.js +12 -1
  61. package/dist/project.cjs +23 -0
  62. package/dist/project.cjs.map +1 -0
  63. package/dist/project.d.ts +13 -0
  64. package/dist/project.d.ts.map +1 -1
  65. package/dist/project.js +22 -0
  66. package/dist/project.js.map +1 -0
  67. package/package.json +7 -4
  68. package/src/folders/commit.ts +133 -0
  69. package/src/folders/decode.test.ts +128 -0
  70. package/src/folders/decode.ts +142 -0
  71. package/src/folders/document.ts +96 -0
  72. package/src/folders/index.ts +9 -0
  73. package/src/folders/inheritance.test.ts +86 -0
  74. package/src/folders/inheritance.ts +32 -0
  75. package/src/folders/invariants.test.ts +157 -0
  76. package/src/folders/invariants.ts +155 -0
  77. package/src/folders/naming.test.ts +62 -0
  78. package/src/folders/naming.ts +58 -0
  79. package/src/folders/planner.test.ts +436 -0
  80. package/src/folders/planner.ts +276 -0
  81. package/src/folders/removal.test.ts +142 -0
  82. package/src/folders/removal.ts +77 -0
  83. package/src/folders/test_utils.ts +86 -0
  84. package/src/folders/view.test.ts +422 -0
  85. package/src/folders/view.ts +324 -0
  86. package/src/index.ts +1 -0
  87. package/src/project.ts +21 -0
@@ -0,0 +1,51 @@
1
+ //#region src/folders/naming.ts
2
+ /**
3
+ * The one naming rule for folders, projects and templates.
4
+ *
5
+ * Scope is always the children of one parent — a folder, or the top level, which is simply the
6
+ * folder with no parent. Folders, projects and templates share that one namespace, so no two of
7
+ * them inside the same parent can carry the same name. Comparison ignores case and surrounding
8
+ * whitespace: `Samples`, `samples` and ` Samples ` are the same thing to the person looking at
9
+ * them.
10
+ *
11
+ * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
12
+ * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
13
+ * an error instead: a text field that quietly disagrees with what was typed is worse than one
14
+ * that says no.
15
+ *
16
+ * Names already stored are never rewritten. Accounts predating folders may hold collisions,
17
+ * because nothing enforced uniqueness before.
18
+ */
19
+ /** True when a sibling already carries this name, compared as the rule above compares names. */
20
+ function foldersNameTaken(name, siblingNames) {
21
+ const wanted = normalizeName(name);
22
+ for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;
23
+ return false;
24
+ }
25
+ /** True when a name has nothing in it but whitespace; a folder cannot be called that. */
26
+ function foldersNameBlank(name) {
27
+ return name.trim() === "";
28
+ }
29
+ /**
30
+ * The name to use for an item landing beside `siblingNames`.
31
+ *
32
+ * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and
33
+ * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.
34
+ */
35
+ function foldersUniqueName(desiredName, siblingNames) {
36
+ const taken = /* @__PURE__ */ new Set();
37
+ for (const sibling of siblingNames) taken.add(normalizeName(sibling));
38
+ if (!taken.has(normalizeName(desiredName))) return desiredName;
39
+ const base = desiredName.replace(COPY_SUFFIX, "");
40
+ let candidate = `${base} (Copy)`;
41
+ for (let index = 2; taken.has(normalizeName(candidate)); index++) candidate = `${base} (Copy ${index})`;
42
+ return candidate;
43
+ }
44
+ const COPY_SUFFIX = / \(Copy(?: \d+)?\)$/;
45
+ function normalizeName(name) {
46
+ return name.trim().toLowerCase();
47
+ }
48
+ //#endregion
49
+ export { foldersNameBlank, foldersNameTaken, foldersUniqueName };
50
+
51
+ //# sourceMappingURL=naming.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"naming.js","names":[],"sources":["../../src/folders/naming.ts"],"sourcesContent":["/**\n * The one naming rule for folders, projects and templates.\n *\n * Scope is always the children of one parent — a folder, or the top level, which is simply the\n * folder with no parent. Folders, projects and templates share that one namespace, so no two of\n * them inside the same parent can carry the same name. Comparison ignores case and surrounding\n * whitespace: `Samples`, `samples` and ` Samples ` are the same thing to the person looking at\n * them.\n *\n * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a\n * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with\n * an error instead: a text field that quietly disagrees with what was typed is worse than one\n * that says no.\n *\n * Names already stored are never rewritten. Accounts predating folders may hold collisions,\n * because nothing enforced uniqueness before.\n */\n\n/** True when a sibling already carries this name, compared as the rule above compares names. */\nexport function foldersNameTaken(name: string, siblingNames: Iterable<string>): boolean {\n const wanted = normalizeName(name);\n for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;\n return false;\n}\n\n/** True when a name has nothing in it but whitespace; a folder cannot be called that. */\nexport function foldersNameBlank(name: string): boolean {\n return name.trim() === \"\";\n}\n\n/**\n * The name to use for an item landing beside `siblingNames`.\n *\n * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and\n * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.\n */\nexport function foldersUniqueName(desiredName: string, siblingNames: Iterable<string>): string {\n const taken = new Set<string>();\n for (const sibling of siblingNames) taken.add(normalizeName(sibling));\n\n if (!taken.has(normalizeName(desiredName))) return desiredName;\n\n const base = desiredName.replace(COPY_SUFFIX, \"\");\n let candidate = `${base} (Copy)`;\n for (let index = 2; taken.has(normalizeName(candidate)); index++)\n candidate = `${base} (Copy ${index})`;\n return candidate;\n}\n\n//\n// Internals\n//\n\nconst COPY_SUFFIX = / \\(Copy(?: \\d+)?\\)$/;\n\nfunction normalizeName(name: string): string {\n return name.trim().toLowerCase();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,iBAAiB,MAAc,cAAyC;CACtF,MAAM,SAAS,cAAc,IAAI;CACjC,KAAK,MAAM,WAAW,cAAc,IAAI,cAAc,OAAO,MAAM,QAAQ,OAAO;CAClF,OAAO;AACT;;AAGA,SAAgB,iBAAiB,MAAuB;CACtD,OAAO,KAAK,KAAK,MAAM;AACzB;;;;;;;AAQA,SAAgB,kBAAkB,aAAqB,cAAwC;CAC7F,MAAM,wBAAQ,IAAI,IAAY;CAC9B,KAAK,MAAM,WAAW,cAAc,MAAM,IAAI,cAAc,OAAO,CAAC;CAEpE,IAAI,CAAC,MAAM,IAAI,cAAc,WAAW,CAAC,GAAG,OAAO;CAEnD,MAAM,OAAO,YAAY,QAAQ,aAAa,EAAE;CAChD,IAAI,YAAY,GAAG,KAAK;CACxB,KAAK,IAAI,QAAQ,GAAG,MAAM,IAAI,cAAc,SAAS,CAAC,GAAG,SACvD,YAAY,GAAG,KAAK,SAAS,MAAM;CACrC,OAAO;AACT;AAMA,MAAM,cAAc;AAEpB,SAAS,cAAc,MAAsB;CAC3C,OAAO,KAAK,KAAK,CAAC,CAAC,YAAY;AACjC"}
@@ -0,0 +1,187 @@
1
+ const require_view = require("./view.cjs");
2
+ const require_naming = require("./naming.cjs");
3
+ //#region src/folders/planner.ts
4
+ /**
5
+ * Plan a move of any mix of folders, projects and templates into one destination.
6
+ *
7
+ * The only function that decides what a moved item ends up called. The preview and the write both
8
+ * call it, over the same view, so the two cannot drift apart.
9
+ *
10
+ * A folder moves with everything inside it. When the selection holds a folder together with
11
+ * something inside it, that item travels with the folder and is not planned on its own: moving it
12
+ * separately as well would pull it out of the subtree it belongs to.
13
+ */
14
+ function planFoldersMove(view, items, destination) {
15
+ const issues = [];
16
+ const destinationKnown = destination === void 0 || view.folders.some((folder) => folder.id === destination);
17
+ if (destination !== void 0 && !destinationKnown) issues.push({
18
+ kind: "unknown-destination",
19
+ folder: destination
20
+ });
21
+ const located = [];
22
+ const seen = /* @__PURE__ */ new Set();
23
+ for (const item of items) {
24
+ const key = `${item.kind}:${item.id}`;
25
+ if (seen.has(key)) continue;
26
+ seen.add(key);
27
+ const known = locate(view, item);
28
+ if (known === void 0) {
29
+ issues.push({
30
+ kind: "unknown-item",
31
+ item
32
+ });
33
+ continue;
34
+ }
35
+ located.push({
36
+ item,
37
+ ...known
38
+ });
39
+ }
40
+ const carried = new Set(located.flatMap(({ item }) => item.kind === "folder" ? require_view.foldersSubtree(view, item.id) : []));
41
+ const moved = located.filter(({ sourceFolder }) => sourceFolder === void 0 || !carried.has(sourceFolder));
42
+ if (destination !== void 0 && destinationKnown) for (const { item } of moved) {
43
+ if (item.kind !== "folder") continue;
44
+ if (require_view.foldersSubtree(view, item.id).includes(destination)) issues.push({
45
+ kind: "into-own-descendant",
46
+ folder: item.id,
47
+ destination
48
+ });
49
+ }
50
+ if (issues.length > 0) return {
51
+ ok: false,
52
+ issues
53
+ };
54
+ const taken = require_view.foldersSiblingNames(view, destination, moved.map(({ item }) => item.id));
55
+ const entries = [];
56
+ for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {
57
+ const resulting = require_naming.foldersUniqueName(name, taken);
58
+ taken.push(resulting);
59
+ entries.push({
60
+ item,
61
+ currentName: name,
62
+ name: resulting,
63
+ renamed: resulting !== name,
64
+ ...sourceFolder === void 0 ? {} : { sourceFolder }
65
+ });
66
+ }
67
+ return {
68
+ ok: true,
69
+ plan: {
70
+ ...destination === void 0 ? {} : { destination },
71
+ entries
72
+ }
73
+ };
74
+ }
75
+ /** True when two plans say exactly the same thing. */
76
+ function foldersMovePlansEqual(a, b) {
77
+ if (a.destination !== b.destination) return false;
78
+ if (a.entries.length !== b.entries.length) return false;
79
+ return a.entries.every((entry, index) => {
80
+ const other = b.entries[index];
81
+ if (other === void 0) return false;
82
+ return entry.item.kind === other.item.kind && entry.item.id === other.item.id && entry.currentName === other.currentName && entry.name === other.name && entry.renamed === other.renamed && entry.sourceFolder === other.sourceFolder;
83
+ });
84
+ }
85
+ /**
86
+ * The document a plan produces, plus the project and template label renames it implies.
87
+ *
88
+ * Applying a plan is arithmetic on an already validated plan; whether that plan may be committed
89
+ * is the write path's decision.
90
+ */
91
+ function applyFoldersMove(view, movePlan) {
92
+ const folderParents = /* @__PURE__ */ new Map();
93
+ const folderNames = /* @__PURE__ */ new Map();
94
+ const projectFolders = /* @__PURE__ */ new Map();
95
+ const templateFolders = /* @__PURE__ */ new Map();
96
+ const labelRenames = [];
97
+ for (const entry of movePlan.entries) switch (entry.item.kind) {
98
+ case "folder":
99
+ folderParents.set(entry.item.id, movePlan.destination);
100
+ folderNames.set(entry.item.id, entry.name);
101
+ break;
102
+ case "project":
103
+ projectFolders.set(entry.item.id, movePlan.destination);
104
+ if (entry.renamed) labelRenames.push({
105
+ item: entry.item,
106
+ name: entry.name
107
+ });
108
+ break;
109
+ case "template":
110
+ templateFolders.set(entry.item.id, movePlan.destination);
111
+ if (entry.renamed) labelRenames.push({
112
+ item: entry.item,
113
+ name: entry.name
114
+ });
115
+ break;
116
+ }
117
+ const folders = view.folders.map((folder) => ({
118
+ ...folder,
119
+ parent: folderParents.has(folder.id) ? folderParents.get(folder.id) : folder.parent,
120
+ name: folderNames.get(folder.id) ?? folder.name
121
+ }));
122
+ const projects = view.projects.map((project) => ({
123
+ ...project,
124
+ folder: projectFolders.has(project.id) ? projectFolders.get(project.id) : project.folder
125
+ }));
126
+ const templates = view.templates.map((template) => ({
127
+ ...template,
128
+ folder: templateFolders.has(template.id) ? templateFolders.get(template.id) : template.folder
129
+ }));
130
+ return {
131
+ document: require_view.foldersDocumentFromView({
132
+ ...view,
133
+ folders,
134
+ projects,
135
+ templates
136
+ }),
137
+ labelRenames
138
+ };
139
+ }
140
+ /** The name and containing folder of an item the view holds, whichever kind it is. */
141
+ function locate(view, item) {
142
+ switch (item.kind) {
143
+ case "folder": {
144
+ const folder = view.folders.find((candidate) => candidate.id === item.id);
145
+ return folder === void 0 ? void 0 : {
146
+ name: folder.name,
147
+ sourceFolder: folder.parent
148
+ };
149
+ }
150
+ case "project": {
151
+ const project = view.projects.find((candidate) => candidate.id === item.id);
152
+ return project === void 0 ? void 0 : {
153
+ name: project.name,
154
+ sourceFolder: project.folder
155
+ };
156
+ }
157
+ case "template": {
158
+ const template = view.templates.find((candidate) => candidate.id === item.id);
159
+ return template === void 0 ? void 0 : {
160
+ name: template.name,
161
+ sourceFolder: template.folder
162
+ };
163
+ }
164
+ }
165
+ }
166
+ /**
167
+ * Folders, then projects, then templates; within a kind by name, then by id. The order of the
168
+ * items a caller hands in must not change the plan, or a preview and its write could differ over
169
+ * nothing.
170
+ */
171
+ function byCanonicalOrder(a, b) {
172
+ if (a.item.kind !== b.item.kind) return KIND_ORDER[a.item.kind] - KIND_ORDER[b.item.kind];
173
+ const byName = a.name.toLowerCase().localeCompare(b.name.toLowerCase());
174
+ if (byName !== 0) return byName;
175
+ return String(a.item.id).localeCompare(String(b.item.id));
176
+ }
177
+ const KIND_ORDER = {
178
+ folder: 0,
179
+ project: 1,
180
+ template: 2
181
+ };
182
+ //#endregion
183
+ exports.applyFoldersMove = applyFoldersMove;
184
+ exports.foldersMovePlansEqual = foldersMovePlansEqual;
185
+ exports.planFoldersMove = planFoldersMove;
186
+
187
+ //# sourceMappingURL=planner.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"planner.cjs","names":["foldersSubtree","foldersSiblingNames","foldersUniqueName","foldersDocumentFromView"],"sources":["../../src/folders/planner.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { foldersUniqueName } from \"./naming\";\nimport type { FoldersView } from \"./view\";\nimport { foldersDocumentFromView, foldersSiblingNames, foldersSubtree } from \"./view\";\n\n/** A folder, a project or a stored template, as something a move acts on. */\nexport type FoldersItem =\n | { readonly kind: \"folder\"; readonly id: FolderId }\n | { readonly kind: \"project\"; readonly id: ProjectId }\n | { readonly kind: \"template\"; readonly id: TemplateId };\n\n/** A tree item that is not a folder: a project or a stored template. */\nexport type FoldersLeafItem = Exclude<FoldersItem, { readonly kind: \"folder\" }>;\n\n/** What one item is called after the move, and what it was called before. */\nexport interface FoldersMoveEntry {\n readonly item: FoldersItem;\n readonly currentName: string;\n /** The name the item carries once the plan is applied. */\n readonly name: string;\n /** True when the destination already held that name and the item is being renamed. */\n readonly renamed: boolean;\n /** Folder the item is coming from; absent when it is at the top level. */\n readonly sourceFolder?: FolderId;\n}\n\n/**\n * Every item of a move and its resulting name.\n *\n * A plan is computed for the preview and recomputed inside the write, and the write commits only\n * when the two are identical, so a plan confirmed against state that has since changed is refused\n * rather than applied. Entries are in a canonical order for exactly that comparison.\n */\nexport interface FoldersMovePlan {\n /** Folder everything lands in; absent for the top level. */\n readonly destination?: FolderId;\n readonly entries: readonly FoldersMoveEntry[];\n}\n\n/** One reason a move cannot be planned. */\nexport type FoldersMoveIssue =\n | { readonly kind: \"unknown-destination\"; readonly folder: FolderId }\n | { readonly kind: \"unknown-item\"; readonly item: FoldersItem }\n | {\n readonly kind: \"into-own-descendant\";\n readonly folder: FolderId;\n readonly destination: FolderId;\n };\n\n/** A plan, or everything standing in the way of one. */\nexport type FoldersMovePlanResult =\n | { readonly ok: true; readonly plan: FoldersMovePlan }\n | { readonly ok: false; readonly issues: readonly FoldersMoveIssue[] };\n\n/**\n * Plan a move of any mix of folders, projects and templates into one destination.\n *\n * The only function that decides what a moved item ends up called. The preview and the write both\n * call it, over the same view, so the two cannot drift apart.\n *\n * A folder moves with everything inside it. When the selection holds a folder together with\n * something inside it, that item travels with the folder and is not planned on its own: moving it\n * separately as well would pull it out of the subtree it belongs to.\n */\nexport function planFoldersMove(\n view: FoldersView,\n items: readonly FoldersItem[],\n destination?: FolderId,\n): FoldersMovePlanResult {\n const issues: FoldersMoveIssue[] = [];\n\n const destinationKnown =\n destination === undefined || view.folders.some((folder) => folder.id === destination);\n if (destination !== undefined && !destinationKnown)\n issues.push({ kind: \"unknown-destination\", folder: destination });\n\n const located: Located[] = [];\n const seen = new Set<string>();\n for (const item of items) {\n const key = `${item.kind}:${item.id}`;\n if (seen.has(key)) continue;\n seen.add(key);\n\n const known = locate(view, item);\n if (known === undefined) {\n issues.push({ kind: \"unknown-item\", item });\n continue;\n }\n located.push({ item, ...known });\n }\n\n const carried = new Set<FolderId>(\n located.flatMap(({ item }) => (item.kind === \"folder\" ? foldersSubtree(view, item.id) : [])),\n );\n const moved = located.filter(\n ({ sourceFolder }) => sourceFolder === undefined || !carried.has(sourceFolder),\n );\n\n if (destination !== undefined && destinationKnown)\n for (const { item } of moved) {\n if (item.kind !== \"folder\") continue;\n if (foldersSubtree(view, item.id).includes(destination))\n issues.push({ kind: \"into-own-descendant\", folder: item.id, destination });\n }\n\n if (issues.length > 0) return { ok: false, issues };\n\n const taken = foldersSiblingNames(\n view,\n destination,\n moved.map(({ item }) => item.id),\n );\n\n const entries: FoldersMoveEntry[] = [];\n for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {\n const resulting = foldersUniqueName(name, taken);\n taken.push(resulting);\n entries.push({\n item,\n currentName: name,\n name: resulting,\n renamed: resulting !== name,\n ...(sourceFolder === undefined ? {} : { sourceFolder }),\n });\n }\n\n return {\n ok: true,\n plan: { ...(destination === undefined ? {} : { destination }), entries },\n };\n}\n\n/** True when two plans say exactly the same thing. */\nexport function foldersMovePlansEqual(a: FoldersMovePlan, b: FoldersMovePlan): boolean {\n if (a.destination !== b.destination) return false;\n if (a.entries.length !== b.entries.length) return false;\n return a.entries.every((entry, index) => {\n const other = b.entries[index];\n if (other === undefined) return false;\n return (\n entry.item.kind === other.item.kind &&\n entry.item.id === other.item.id &&\n entry.currentName === other.currentName &&\n entry.name === other.name &&\n entry.renamed === other.renamed &&\n entry.sourceFolder === other.sourceFolder\n );\n });\n}\n\n/**\n * A project or a template the move gives a new name. Their names live in their own metadata, not\n * in the folder document, so the write path has to carry these out separately; a folder's name is\n * in the document and is renamed there.\n */\nexport interface FoldersLabelRename {\n readonly item: FoldersLeafItem;\n readonly name: string;\n}\n\n/** What applying a plan changes. */\nexport interface FoldersMoveApplication {\n /** The document to persist. */\n readonly document: FoldersDocument;\n /** Projects and templates whose label the move changes. */\n readonly labelRenames: readonly FoldersLabelRename[];\n}\n\n/**\n * The document a plan produces, plus the project and template label renames it implies.\n *\n * Applying a plan is arithmetic on an already validated plan; whether that plan may be committed\n * is the write path's decision.\n */\nexport function applyFoldersMove(\n view: FoldersView,\n movePlan: FoldersMovePlan,\n): FoldersMoveApplication {\n const folderParents = new Map<FolderId, FolderId | undefined>();\n const folderNames = new Map<FolderId, string>();\n const projectFolders = new Map<ProjectId, FolderId | undefined>();\n const templateFolders = new Map<TemplateId, FolderId | undefined>();\n const labelRenames: FoldersLabelRename[] = [];\n\n for (const entry of movePlan.entries) {\n switch (entry.item.kind) {\n case \"folder\":\n folderParents.set(entry.item.id, movePlan.destination);\n folderNames.set(entry.item.id, entry.name);\n break;\n case \"project\":\n projectFolders.set(entry.item.id, movePlan.destination);\n if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });\n break;\n case \"template\":\n templateFolders.set(entry.item.id, movePlan.destination);\n if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });\n break;\n }\n }\n\n const folders = view.folders.map((folder) => ({\n ...folder,\n parent: folderParents.has(folder.id) ? folderParents.get(folder.id) : folder.parent,\n name: folderNames.get(folder.id) ?? folder.name,\n }));\n\n const projects = view.projects.map((project) => ({\n ...project,\n folder: projectFolders.has(project.id) ? projectFolders.get(project.id) : project.folder,\n }));\n\n const templates = view.templates.map((template) => ({\n ...template,\n folder: templateFolders.has(template.id) ? templateFolders.get(template.id) : template.folder,\n }));\n\n const document = foldersDocumentFromView({ ...view, folders, projects, templates });\n return { document, labelRenames };\n}\n\n//\n// Internals\n//\n\n/** An item of the selection, with what the view says about it. */\ninterface Located {\n readonly item: FoldersItem;\n readonly name: string;\n /** Folder holding the item; absent when it is at the top level. */\n readonly sourceFolder?: FolderId;\n}\n\n/** The name and containing folder of an item the view holds, whichever kind it is. */\nfunction locate(\n view: FoldersView,\n item: FoldersItem,\n): { readonly name: string; readonly sourceFolder?: FolderId } | undefined {\n switch (item.kind) {\n case \"folder\": {\n const folder = view.folders.find((candidate) => candidate.id === item.id);\n return folder === undefined ? undefined : { name: folder.name, sourceFolder: folder.parent };\n }\n case \"project\": {\n const project = view.projects.find((candidate) => candidate.id === item.id);\n return project === undefined\n ? undefined\n : { name: project.name, sourceFolder: project.folder };\n }\n case \"template\": {\n const template = view.templates.find((candidate) => candidate.id === item.id);\n return template === undefined\n ? undefined\n : { name: template.name, sourceFolder: template.folder };\n }\n }\n}\n\n/**\n * Folders, then projects, then templates; within a kind by name, then by id. The order of the\n * items a caller hands in must not change the plan, or a preview and its write could differ over\n * nothing.\n */\nfunction byCanonicalOrder(a: Located, b: Located): number {\n if (a.item.kind !== b.item.kind) return KIND_ORDER[a.item.kind] - KIND_ORDER[b.item.kind];\n const byName = a.name.toLowerCase().localeCompare(b.name.toLowerCase());\n if (byName !== 0) return byName;\n return String(a.item.id).localeCompare(String(b.item.id));\n}\n\nconst KIND_ORDER: Readonly<Record<FoldersItem[\"kind\"], number>> = {\n folder: 0,\n project: 1,\n template: 2,\n};\n"],"mappings":";;;;;;;;;;;;;AAiEA,SAAgB,gBACd,MACA,OACA,aACuB;CACvB,MAAM,SAA6B,CAAC;CAEpC,MAAM,mBACJ,gBAAgB,KAAA,KAAa,KAAK,QAAQ,MAAM,WAAW,OAAO,OAAO,WAAW;CACtF,IAAI,gBAAgB,KAAA,KAAa,CAAC,kBAChC,OAAO,KAAK;EAAE,MAAM;EAAuB,QAAQ;CAAY,CAAC;CAElE,MAAM,UAAqB,CAAC;CAC5B,MAAM,uBAAO,IAAI,IAAY;CAC7B,KAAK,MAAM,QAAQ,OAAO;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D,IAAI,EAAE,KAAK,SAAS,EAAE,KAAK,MAAM,OAAO,WAAW,EAAE,KAAK,QAAQ,WAAW,EAAE,KAAK;CACpF,MAAM,SAAS,EAAE,KAAK,YAAY,CAAC,CAAC,cAAc,EAAE,KAAK,YAAY,CAAC;CACtE,IAAI,WAAW,GAAG,OAAO;CACzB,OAAO,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,cAAc,OAAO,EAAE,KAAK,EAAE,CAAC;AAC1D;AAEA,MAAM,aAA4D;CAChE,QAAQ;CACR,SAAS;CACT,UAAU;AACZ"}
@@ -0,0 +1,100 @@
1
+ import { FolderId, FoldersDocument } from "./document.js";
2
+ import { FoldersView } from "./view.js";
3
+ import { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
4
+ //#region src/folders/planner.d.ts
5
+ /** A folder, a project or a stored template, as something a move acts on. */
6
+ export type FoldersItem = {
7
+ readonly kind: "folder";
8
+ readonly id: FolderId;
9
+ } | {
10
+ readonly kind: "project";
11
+ readonly id: ProjectId;
12
+ } | {
13
+ readonly kind: "template";
14
+ readonly id: TemplateId;
15
+ };
16
+ /** A tree item that is not a folder: a project or a stored template. */
17
+ export type FoldersLeafItem = Exclude<FoldersItem, {
18
+ readonly kind: "folder";
19
+ }>;
20
+ /** What one item is called after the move, and what it was called before. */
21
+ export interface FoldersMoveEntry {
22
+ readonly item: FoldersItem;
23
+ readonly currentName: string;
24
+ /** The name the item carries once the plan is applied. */
25
+ readonly name: string;
26
+ /** True when the destination already held that name and the item is being renamed. */
27
+ readonly renamed: boolean;
28
+ /** Folder the item is coming from; absent when it is at the top level. */
29
+ readonly sourceFolder?: FolderId;
30
+ }
31
+ /**
32
+ * Every item of a move and its resulting name.
33
+ *
34
+ * A plan is computed for the preview and recomputed inside the write, and the write commits only
35
+ * when the two are identical, so a plan confirmed against state that has since changed is refused
36
+ * rather than applied. Entries are in a canonical order for exactly that comparison.
37
+ */
38
+ export interface FoldersMovePlan {
39
+ /** Folder everything lands in; absent for the top level. */
40
+ readonly destination?: FolderId;
41
+ readonly entries: readonly FoldersMoveEntry[];
42
+ }
43
+ /** One reason a move cannot be planned. */
44
+ export type FoldersMoveIssue = {
45
+ readonly kind: "unknown-destination";
46
+ readonly folder: FolderId;
47
+ } | {
48
+ readonly kind: "unknown-item";
49
+ readonly item: FoldersItem;
50
+ } | {
51
+ readonly kind: "into-own-descendant";
52
+ readonly folder: FolderId;
53
+ readonly destination: FolderId;
54
+ };
55
+ /** A plan, or everything standing in the way of one. */
56
+ export type FoldersMovePlanResult = {
57
+ readonly ok: true;
58
+ readonly plan: FoldersMovePlan;
59
+ } | {
60
+ readonly ok: false;
61
+ readonly issues: readonly FoldersMoveIssue[];
62
+ };
63
+ /**
64
+ * Plan a move of any mix of folders, projects and templates into one destination.
65
+ *
66
+ * The only function that decides what a moved item ends up called. The preview and the write both
67
+ * call it, over the same view, so the two cannot drift apart.
68
+ *
69
+ * A folder moves with everything inside it. When the selection holds a folder together with
70
+ * something inside it, that item travels with the folder and is not planned on its own: moving it
71
+ * separately as well would pull it out of the subtree it belongs to.
72
+ */
73
+ export declare function planFoldersMove(view: FoldersView, items: readonly FoldersItem[], destination?: FolderId): FoldersMovePlanResult;
74
+ /** True when two plans say exactly the same thing. */
75
+ export declare function foldersMovePlansEqual(a: FoldersMovePlan, b: FoldersMovePlan): boolean;
76
+ /**
77
+ * A project or a template the move gives a new name. Their names live in their own metadata, not
78
+ * in the folder document, so the write path has to carry these out separately; a folder's name is
79
+ * in the document and is renamed there.
80
+ */
81
+ export interface FoldersLabelRename {
82
+ readonly item: FoldersLeafItem;
83
+ readonly name: string;
84
+ }
85
+ /** What applying a plan changes. */
86
+ export interface FoldersMoveApplication {
87
+ /** The document to persist. */
88
+ readonly document: FoldersDocument;
89
+ /** Projects and templates whose label the move changes. */
90
+ readonly labelRenames: readonly FoldersLabelRename[];
91
+ }
92
+ /**
93
+ * The document a plan produces, plus the project and template label renames it implies.
94
+ *
95
+ * Applying a plan is arithmetic on an already validated plan; whether that plan may be committed
96
+ * is the write path's decision.
97
+ */
98
+ export declare function applyFoldersMove(view: FoldersView, movePlan: FoldersMovePlan): FoldersMoveApplication;
99
+ //#endregion
100
+ //# sourceMappingURL=planner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"planner.d.ts","names":[],"sources":["../../src/folders/planner.ts"],"mappings":";;;;;YAOY;WACG;WAAyB,IAAI;;WAC7B;WAA0B,IAAI;;WAC9B;WAA2B,IAAI;;;YAGlC,kBAAkB,QAAQ;WAAwB;;;iBAG7C;WACN,MAAM;WACN;;WAEA;;WAEA;;WAEA,eAAe;;;;;;;;;iBAUT;;WAEN,cAAc;WACd,kBAAkB;;;YAIjB;WACG;WAAsC,QAAQ;;WAC9C;WAA+B,MAAM;;WAErC;WACA,QAAQ;WACR,aAAa;;;YAIhB;WACG;WAAmB,MAAM;;WACzB;WAAoB,iBAAiB;;;;;;;;;;;;wBAYpC,gBACd,MAAM,aACN,gBAAgB,eAChB,cAAc,WACb;;wBAiEa,sBAAsB,GAAG,iBAAiB,GAAG;;;;;;iBAsB5C;WACN,MAAM;WACN;;;iBAIM;;WAEN,UAAU;;WAEV,uBAAuB;;;;;;;;wBASlB,iBACd,MAAM,aACN,UAAU,kBACT"}
@@ -0,0 +1,185 @@
1
+ import { foldersDocumentFromView, foldersSiblingNames, foldersSubtree } from "./view.js";
2
+ import { foldersUniqueName } from "./naming.js";
3
+ //#region src/folders/planner.ts
4
+ /**
5
+ * Plan a move of any mix of folders, projects and templates into one destination.
6
+ *
7
+ * The only function that decides what a moved item ends up called. The preview and the write both
8
+ * call it, over the same view, so the two cannot drift apart.
9
+ *
10
+ * A folder moves with everything inside it. When the selection holds a folder together with
11
+ * something inside it, that item travels with the folder and is not planned on its own: moving it
12
+ * separately as well would pull it out of the subtree it belongs to.
13
+ */
14
+ function planFoldersMove(view, items, destination) {
15
+ const issues = [];
16
+ const destinationKnown = destination === void 0 || view.folders.some((folder) => folder.id === destination);
17
+ if (destination !== void 0 && !destinationKnown) issues.push({
18
+ kind: "unknown-destination",
19
+ folder: destination
20
+ });
21
+ const located = [];
22
+ const seen = /* @__PURE__ */ new Set();
23
+ for (const item of items) {
24
+ const key = `${item.kind}:${item.id}`;
25
+ if (seen.has(key)) continue;
26
+ seen.add(key);
27
+ const known = locate(view, item);
28
+ if (known === void 0) {
29
+ issues.push({
30
+ kind: "unknown-item",
31
+ item
32
+ });
33
+ continue;
34
+ }
35
+ located.push({
36
+ item,
37
+ ...known
38
+ });
39
+ }
40
+ const carried = new Set(located.flatMap(({ item }) => item.kind === "folder" ? foldersSubtree(view, item.id) : []));
41
+ const moved = located.filter(({ sourceFolder }) => sourceFolder === void 0 || !carried.has(sourceFolder));
42
+ if (destination !== void 0 && destinationKnown) for (const { item } of moved) {
43
+ if (item.kind !== "folder") continue;
44
+ if (foldersSubtree(view, item.id).includes(destination)) issues.push({
45
+ kind: "into-own-descendant",
46
+ folder: item.id,
47
+ destination
48
+ });
49
+ }
50
+ if (issues.length > 0) return {
51
+ ok: false,
52
+ issues
53
+ };
54
+ const taken = foldersSiblingNames(view, destination, moved.map(({ item }) => item.id));
55
+ const entries = [];
56
+ for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {
57
+ const resulting = foldersUniqueName(name, taken);
58
+ taken.push(resulting);
59
+ entries.push({
60
+ item,
61
+ currentName: name,
62
+ name: resulting,
63
+ renamed: resulting !== name,
64
+ ...sourceFolder === void 0 ? {} : { sourceFolder }
65
+ });
66
+ }
67
+ return {
68
+ ok: true,
69
+ plan: {
70
+ ...destination === void 0 ? {} : { destination },
71
+ entries
72
+ }
73
+ };
74
+ }
75
+ /** True when two plans say exactly the same thing. */
76
+ function foldersMovePlansEqual(a, b) {
77
+ if (a.destination !== b.destination) return false;
78
+ if (a.entries.length !== b.entries.length) return false;
79
+ return a.entries.every((entry, index) => {
80
+ const other = b.entries[index];
81
+ if (other === void 0) return false;
82
+ return entry.item.kind === other.item.kind && entry.item.id === other.item.id && entry.currentName === other.currentName && entry.name === other.name && entry.renamed === other.renamed && entry.sourceFolder === other.sourceFolder;
83
+ });
84
+ }
85
+ /**
86
+ * The document a plan produces, plus the project and template label renames it implies.
87
+ *
88
+ * Applying a plan is arithmetic on an already validated plan; whether that plan may be committed
89
+ * is the write path's decision.
90
+ */
91
+ function applyFoldersMove(view, movePlan) {
92
+ const folderParents = /* @__PURE__ */ new Map();
93
+ const folderNames = /* @__PURE__ */ new Map();
94
+ const projectFolders = /* @__PURE__ */ new Map();
95
+ const templateFolders = /* @__PURE__ */ new Map();
96
+ const labelRenames = [];
97
+ for (const entry of movePlan.entries) switch (entry.item.kind) {
98
+ case "folder":
99
+ folderParents.set(entry.item.id, movePlan.destination);
100
+ folderNames.set(entry.item.id, entry.name);
101
+ break;
102
+ case "project":
103
+ projectFolders.set(entry.item.id, movePlan.destination);
104
+ if (entry.renamed) labelRenames.push({
105
+ item: entry.item,
106
+ name: entry.name
107
+ });
108
+ break;
109
+ case "template":
110
+ templateFolders.set(entry.item.id, movePlan.destination);
111
+ if (entry.renamed) labelRenames.push({
112
+ item: entry.item,
113
+ name: entry.name
114
+ });
115
+ break;
116
+ }
117
+ const folders = view.folders.map((folder) => ({
118
+ ...folder,
119
+ parent: folderParents.has(folder.id) ? folderParents.get(folder.id) : folder.parent,
120
+ name: folderNames.get(folder.id) ?? folder.name
121
+ }));
122
+ const projects = view.projects.map((project) => ({
123
+ ...project,
124
+ folder: projectFolders.has(project.id) ? projectFolders.get(project.id) : project.folder
125
+ }));
126
+ const templates = view.templates.map((template) => ({
127
+ ...template,
128
+ folder: templateFolders.has(template.id) ? templateFolders.get(template.id) : template.folder
129
+ }));
130
+ return {
131
+ document: foldersDocumentFromView({
132
+ ...view,
133
+ folders,
134
+ projects,
135
+ templates
136
+ }),
137
+ labelRenames
138
+ };
139
+ }
140
+ /** The name and containing folder of an item the view holds, whichever kind it is. */
141
+ function locate(view, item) {
142
+ switch (item.kind) {
143
+ case "folder": {
144
+ const folder = view.folders.find((candidate) => candidate.id === item.id);
145
+ return folder === void 0 ? void 0 : {
146
+ name: folder.name,
147
+ sourceFolder: folder.parent
148
+ };
149
+ }
150
+ case "project": {
151
+ const project = view.projects.find((candidate) => candidate.id === item.id);
152
+ return project === void 0 ? void 0 : {
153
+ name: project.name,
154
+ sourceFolder: project.folder
155
+ };
156
+ }
157
+ case "template": {
158
+ const template = view.templates.find((candidate) => candidate.id === item.id);
159
+ return template === void 0 ? void 0 : {
160
+ name: template.name,
161
+ sourceFolder: template.folder
162
+ };
163
+ }
164
+ }
165
+ }
166
+ /**
167
+ * Folders, then projects, then templates; within a kind by name, then by id. The order of the
168
+ * items a caller hands in must not change the plan, or a preview and its write could differ over
169
+ * nothing.
170
+ */
171
+ function byCanonicalOrder(a, b) {
172
+ if (a.item.kind !== b.item.kind) return KIND_ORDER[a.item.kind] - KIND_ORDER[b.item.kind];
173
+ const byName = a.name.toLowerCase().localeCompare(b.name.toLowerCase());
174
+ if (byName !== 0) return byName;
175
+ return String(a.item.id).localeCompare(String(b.item.id));
176
+ }
177
+ const KIND_ORDER = {
178
+ folder: 0,
179
+ project: 1,
180
+ template: 2
181
+ };
182
+ //#endregion
183
+ export { applyFoldersMove, foldersMovePlansEqual, planFoldersMove };
184
+
185
+ //# sourceMappingURL=planner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"planner.js","names":[],"sources":["../../src/folders/planner.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { foldersUniqueName } from \"./naming\";\nimport type { FoldersView } from \"./view\";\nimport { foldersDocumentFromView, foldersSiblingNames, foldersSubtree } from \"./view\";\n\n/** A folder, a project or a stored template, as something a move acts on. */\nexport type FoldersItem =\n | { readonly kind: \"folder\"; readonly id: FolderId }\n | { readonly kind: \"project\"; readonly id: ProjectId }\n | { readonly kind: \"template\"; readonly id: TemplateId };\n\n/** A tree item that is not a folder: a project or a stored template. */\nexport type FoldersLeafItem = Exclude<FoldersItem, { readonly kind: \"folder\" }>;\n\n/** What one item is called after the move, and what it was called before. */\nexport interface FoldersMoveEntry {\n readonly item: FoldersItem;\n readonly currentName: string;\n /** The name the item carries once the plan is applied. */\n readonly name: string;\n /** True when the destination already held that name and the item is being renamed. */\n readonly renamed: boolean;\n /** Folder the item is coming from; absent when it is at the top level. */\n readonly sourceFolder?: FolderId;\n}\n\n/**\n * Every item of a move and its resulting name.\n *\n * A plan is computed for the preview and recomputed inside the write, and the write commits only\n * when the two are identical, so a plan confirmed against state that has since changed is refused\n * rather than applied. Entries are in a canonical order for exactly that comparison.\n */\nexport interface FoldersMovePlan {\n /** Folder everything lands in; absent for the top level. */\n readonly destination?: FolderId;\n readonly entries: readonly FoldersMoveEntry[];\n}\n\n/** One reason a move cannot be planned. */\nexport type FoldersMoveIssue =\n | { readonly kind: \"unknown-destination\"; readonly folder: FolderId }\n | { readonly kind: \"unknown-item\"; readonly item: FoldersItem }\n | {\n readonly kind: \"into-own-descendant\";\n readonly folder: FolderId;\n readonly destination: FolderId;\n };\n\n/** A plan, or everything standing in the way of one. */\nexport type FoldersMovePlanResult =\n | { readonly ok: true; readonly plan: FoldersMovePlan }\n | { readonly ok: false; readonly issues: readonly FoldersMoveIssue[] };\n\n/**\n * Plan a move of any mix of folders, projects and templates into one destination.\n *\n * The only function that decides what a moved item ends up called. The preview and the write both\n * call it, over the same view, so the two cannot drift apart.\n *\n * A folder moves with everything inside it. When the selection holds a folder together with\n * something inside it, that item travels with the folder and is not planned on its own: moving it\n * separately as well would pull it out of the subtree it belongs to.\n */\nexport function planFoldersMove(\n view: FoldersView,\n items: readonly FoldersItem[],\n destination?: FolderId,\n): FoldersMovePlanResult {\n const issues: FoldersMoveIssue[] = [];\n\n const destinationKnown =\n destination === undefined || view.folders.some((folder) => folder.id === destination);\n if (destination !== undefined && !destinationKnown)\n issues.push({ kind: \"unknown-destination\", folder: destination });\n\n const located: Located[] = [];\n const seen = new Set<string>();\n for (const item of items) {\n const key = `${item.kind}:${item.id}`;\n if (seen.has(key)) continue;\n seen.add(key);\n\n const known = locate(view, item);\n if (known === undefined) {\n issues.push({ kind: \"unknown-item\", item });\n continue;\n }\n located.push({ item, ...known });\n }\n\n const carried = new Set<FolderId>(\n located.flatMap(({ item }) => (item.kind === \"folder\" ? foldersSubtree(view, item.id) : [])),\n );\n const moved = located.filter(\n ({ sourceFolder }) => sourceFolder === undefined || !carried.has(sourceFolder),\n );\n\n if (destination !== undefined && destinationKnown)\n for (const { item } of moved) {\n if (item.kind !== \"folder\") continue;\n if (foldersSubtree(view, item.id).includes(destination))\n issues.push({ kind: \"into-own-descendant\", folder: item.id, destination });\n }\n\n if (issues.length > 0) return { ok: false, issues };\n\n const taken = foldersSiblingNames(\n view,\n destination,\n moved.map(({ item }) => item.id),\n );\n\n const entries: FoldersMoveEntry[] = [];\n for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {\n const resulting = foldersUniqueName(name, taken);\n taken.push(resulting);\n entries.push({\n item,\n currentName: name,\n name: resulting,\n renamed: resulting !== name,\n ...(sourceFolder === undefined ? {} : { sourceFolder }),\n });\n }\n\n return {\n ok: true,\n plan: { ...(destination === undefined ? {} : { destination }), entries },\n };\n}\n\n/** True when two plans say exactly the same thing. */\nexport function foldersMovePlansEqual(a: FoldersMovePlan, b: FoldersMovePlan): boolean {\n if (a.destination !== b.destination) return false;\n if (a.entries.length !== b.entries.length) return false;\n return a.entries.every((entry, index) => {\n const other = b.entries[index];\n if (other === undefined) return false;\n return (\n entry.item.kind === other.item.kind &&\n entry.item.id === other.item.id &&\n entry.currentName === other.currentName &&\n entry.name === other.name &&\n entry.renamed === other.renamed &&\n entry.sourceFolder === other.sourceFolder\n );\n });\n}\n\n/**\n * A project or a template the move gives a new name. Their names live in their own metadata, not\n * in the folder document, so the write path has to carry these out separately; a folder's name is\n * in the document and is renamed there.\n */\nexport interface FoldersLabelRename {\n readonly item: FoldersLeafItem;\n readonly name: string;\n}\n\n/** What applying a plan changes. */\nexport interface FoldersMoveApplication {\n /** The document to persist. */\n readonly document: FoldersDocument;\n /** Projects and templates whose label the move changes. */\n readonly labelRenames: readonly FoldersLabelRename[];\n}\n\n/**\n * The document a plan produces, plus the project and template label renames it implies.\n *\n * Applying a plan is arithmetic on an already validated plan; whether that plan may be committed\n * is the write path's decision.\n */\nexport function applyFoldersMove(\n view: FoldersView,\n movePlan: FoldersMovePlan,\n): FoldersMoveApplication {\n const folderParents = new Map<FolderId, FolderId | undefined>();\n const folderNames = new Map<FolderId, string>();\n const projectFolders = new Map<ProjectId, FolderId | undefined>();\n const templateFolders = new Map<TemplateId, FolderId | undefined>();\n const labelRenames: FoldersLabelRename[] = [];\n\n for (const entry of movePlan.entries) {\n switch (entry.item.kind) {\n case \"folder\":\n folderParents.set(entry.item.id, movePlan.destination);\n folderNames.set(entry.item.id, entry.name);\n break;\n case \"project\":\n projectFolders.set(entry.item.id, movePlan.destination);\n if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });\n break;\n case \"template\":\n templateFolders.set(entry.item.id, movePlan.destination);\n if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });\n break;\n }\n }\n\n const folders = view.folders.map((folder) => ({\n ...folder,\n parent: folderParents.has(folder.id) ? folderParents.get(folder.id) : folder.parent,\n name: folderNames.get(folder.id) ?? folder.name,\n }));\n\n const projects = view.projects.map((project) => ({\n ...project,\n folder: projectFolders.has(project.id) ? projectFolders.get(project.id) : project.folder,\n }));\n\n const templates = view.templates.map((template) => ({\n ...template,\n folder: templateFolders.has(template.id) ? templateFolders.get(template.id) : template.folder,\n }));\n\n const document = foldersDocumentFromView({ ...view, folders, projects, templates });\n return { document, labelRenames };\n}\n\n//\n// Internals\n//\n\n/** An item of the selection, with what the view says about it. */\ninterface Located {\n readonly item: FoldersItem;\n readonly name: string;\n /** Folder holding the item; absent when it is at the top level. */\n readonly sourceFolder?: FolderId;\n}\n\n/** The name and containing folder of an item the view holds, whichever kind it is. */\nfunction locate(\n view: FoldersView,\n item: FoldersItem,\n): { readonly name: string; readonly sourceFolder?: FolderId } | undefined {\n switch (item.kind) {\n case \"folder\": {\n const folder = view.folders.find((candidate) => candidate.id === item.id);\n return folder === undefined ? undefined : { name: folder.name, sourceFolder: folder.parent };\n }\n case \"project\": {\n const project = view.projects.find((candidate) => candidate.id === item.id);\n return project === undefined\n ? undefined\n : { name: project.name, sourceFolder: project.folder };\n }\n case \"template\": {\n const template = view.templates.find((candidate) => candidate.id === item.id);\n return template === undefined\n ? undefined\n : { name: template.name, sourceFolder: template.folder };\n }\n }\n}\n\n/**\n * Folders, then projects, then templates; within a kind by name, then by id. The order of the\n * items a caller hands in must not change the plan, or a preview and its write could differ over\n * nothing.\n */\nfunction byCanonicalOrder(a: Located, b: Located): number {\n if (a.item.kind !== b.item.kind) return KIND_ORDER[a.item.kind] - KIND_ORDER[b.item.kind];\n const byName = a.name.toLowerCase().localeCompare(b.name.toLowerCase());\n if (byName !== 0) return byName;\n return String(a.item.id).localeCompare(String(b.item.id));\n}\n\nconst KIND_ORDER: Readonly<Record<FoldersItem[\"kind\"], number>> = {\n folder: 0,\n project: 1,\n template: 2,\n};\n"],"mappings":";;;;;;;;;;;;;AAiEA,SAAgB,gBACd,MACA,OACA,aACuB;CACvB,MAAM,SAA6B,CAAC;CAEpC,MAAM,mBACJ,gBAAgB,KAAA,KAAa,KAAK,QAAQ,MAAM,WAAW,OAAO,OAAO,WAAW;CACtF,IAAI,gBAAgB,KAAA,KAAa,CAAC,kBAChC,OAAO,KAAK;EAAE,MAAM;EAAuB,QAAQ;CAAY,CAAC;CAElE,MAAM,UAAqB,CAAC;CAC5B,MAAM,uBAAO,IAAI,IAAY;CAC7B,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,MAAM,GAAG,KAAK,KAAK,GAAG,KAAK;EACjC,IAAI,KAAK,IAAI,GAAG,GAAG;EACnB,KAAK,IAAI,GAAG;EAEZ,MAAM,QAAQ,OAAO,MAAM,IAAI;EAC/B,IAA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AAO;CACzB,OAAO,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,cAAc,OAAO,EAAE,KAAK,EAAE,CAAC;AAC1D;AAEA,MAAM,aAA4D;CAChE,QAAQ;CACR,SAAS;CACT,UAAU;AACZ"}
@@ -0,0 +1,47 @@
1
+ const require_view = require("./view.cjs");
2
+ //#region src/folders/removal.ts
3
+ /** What deleting this folder would destroy. */
4
+ function planFoldersRemoval(view, folder) {
5
+ if (!view.folders.some((candidate) => candidate.id === folder)) return {
6
+ ok: false,
7
+ issues: [{
8
+ kind: "unknown-folder",
9
+ folder
10
+ }]
11
+ };
12
+ const folders = [...require_view.foldersSubtree(view, folder)].sort();
13
+ const doomed = new Set(folders);
14
+ return {
15
+ ok: true,
16
+ removal: {
17
+ folders,
18
+ projects: view.projects.filter((project) => project.folder !== void 0 && doomed.has(project.folder)).map((project) => project.id).sort(),
19
+ templates: view.templates.filter((template) => template.folder !== void 0 && doomed.has(template.folder)).map((template) => template.id).sort()
20
+ }
21
+ };
22
+ }
23
+ /** True when two removals destroy exactly the same things. */
24
+ function foldersRemovalsEqual(a, b) {
25
+ return a.folders.length === b.folders.length && a.projects.length === b.projects.length && a.templates.length === b.templates.length && a.folders.every((id, index) => id === b.folders[index]) && a.projects.every((id, index) => id === b.projects[index]) && a.templates.every((id, index) => id === b.templates[index]);
26
+ }
27
+ /**
28
+ * The document left once a removal is applied. The projects and templates themselves are deleted
29
+ * by the write path.
30
+ */
31
+ function applyFoldersRemoval(view, removal) {
32
+ const doomedFolders = new Set(removal.folders);
33
+ const doomedProjects = new Set(removal.projects);
34
+ const doomedTemplates = new Set(removal.templates);
35
+ return require_view.foldersDocumentFromView({
36
+ ...view,
37
+ folders: view.folders.filter((folder) => !doomedFolders.has(folder.id)),
38
+ projects: view.projects.filter((project) => !doomedProjects.has(project.id)),
39
+ templates: view.templates.filter((template) => !doomedTemplates.has(template.id))
40
+ });
41
+ }
42
+ //#endregion
43
+ exports.applyFoldersRemoval = applyFoldersRemoval;
44
+ exports.foldersRemovalsEqual = foldersRemovalsEqual;
45
+ exports.planFoldersRemoval = planFoldersRemoval;
46
+
47
+ //# sourceMappingURL=removal.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removal.cjs","names":["foldersSubtree","foldersDocumentFromView"],"sources":["../../src/folders/removal.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport type { FoldersView } from \"./view\";\nimport { foldersDocumentFromView, foldersSubtree } from \"./view\";\n\n/**\n * Everything a folder deletion destroys: the folder, every folder beneath it, and every project\n * and template held anywhere inside.\n *\n * Deleting a project or a template cannot be undone, so this is computed for the dialog and\n * recomputed inside the write, which commits only if the two agree. Without that, a project\n * dropped into the folder between the two reads would be destroyed without ever appearing in the\n * dialog that asked. Every list is in a canonical order for exactly that comparison.\n */\nexport interface FoldersRemoval {\n readonly folders: readonly FolderId[];\n readonly projects: readonly ProjectId[];\n readonly templates: readonly TemplateId[];\n}\n\n/** One reason a folder cannot be deleted. */\nexport type FoldersRemovalIssue = {\n readonly kind: \"unknown-folder\";\n readonly folder: FolderId;\n};\n\n/** A removal, or everything standing in the way of one. */\nexport type FoldersRemovalPlanResult =\n | { readonly ok: true; readonly removal: FoldersRemoval }\n | { readonly ok: false; readonly issues: readonly FoldersRemovalIssue[] };\n\n/** What deleting this folder would destroy. */\nexport function planFoldersRemoval(view: FoldersView, folder: FolderId): FoldersRemovalPlanResult {\n if (!view.folders.some((candidate) => candidate.id === folder))\n return { ok: false, issues: [{ kind: \"unknown-folder\", folder }] };\n\n const folders = [...foldersSubtree(view, folder)].sort();\n const doomed = new Set<FolderId>(folders);\n const projects = view.projects\n .filter((project) => project.folder !== undefined && doomed.has(project.folder))\n .map((project) => project.id)\n .sort();\n const templates = view.templates\n .filter((template) => template.folder !== undefined && doomed.has(template.folder))\n .map((template) => template.id)\n .sort();\n\n return { ok: true, removal: { folders, projects, templates } };\n}\n\n/** True when two removals destroy exactly the same things. */\nexport function foldersRemovalsEqual(a: FoldersRemoval, b: FoldersRemoval): boolean {\n return (\n a.folders.length === b.folders.length &&\n a.projects.length === b.projects.length &&\n a.templates.length === b.templates.length &&\n a.folders.every((id, index) => id === b.folders[index]) &&\n a.projects.every((id, index) => id === b.projects[index]) &&\n a.templates.every((id, index) => id === b.templates[index])\n );\n}\n\n/**\n * The document left once a removal is applied. The projects and templates themselves are deleted\n * by the write path.\n */\nexport function applyFoldersRemoval(view: FoldersView, removal: FoldersRemoval): FoldersDocument {\n const doomedFolders = new Set<FolderId>(removal.folders);\n const doomedProjects = new Set<ProjectId>(removal.projects);\n const doomedTemplates = new Set<TemplateId>(removal.templates);\n return foldersDocumentFromView({\n ...view,\n folders: view.folders.filter((folder) => !doomedFolders.has(folder.id)),\n projects: view.projects.filter((project) => !doomedProjects.has(project.id)),\n templates: view.templates.filter((template) => !doomedTemplates.has(template.id)),\n });\n}\n"],"mappings":";;;AAgCA,SAAgB,mBAAmB,MAAmB,QAA4C;CAChG,IAAI,CAAC,KAAK,QAAQ,MAAM,cAAc,UAAU,OAAO,MAAM,GAC3D,OAAO;EAAE,IAAI;EAAO,QAAQ,CAAC;GAAE,MAAM;GAAkB;EAAO,CAAC;CAAE;CAEnE,MAAM,UAAU,CAAC,GAAGA,aAAAA,eAAe,MAAM,MAAM,CAAC,CAAC,CAAC,KAAK;CACvD,MAAM,SAAS,IAAI,IAAc,OAAO;CAUxC,OAAO;EAAE,IAAI;EAAM,SAAS;GAAE;GAAS,UATtB,KAAK,SACnB,QAAQ,YAAY,QAAQ,WAAW,KAAA,KAAa,OAAO,IAAI,QAAQ,MAAM,CAAC,CAAC,CAC/E,KAAK,YAAY,QAAQ,EAAE,CAAC,CAC5B,KAM2C;GAAG,WAL/B,KAAK,UACpB,QAAQ,aAAa,SAAS,WAAW,KAAA,KAAa,OAAO,IAAI,SAAS,MAAM,CAAC,CAAC,CAClF,KAAK,aAAa,SAAS,EAAE,CAAC,CAC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