@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.
- package/dist/folders/commit.cjs +79 -0
- package/dist/folders/commit.cjs.map +1 -0
- package/dist/folders/commit.d.ts +82 -0
- package/dist/folders/commit.d.ts.map +1 -0
- package/dist/folders/commit.js +78 -0
- package/dist/folders/commit.js.map +1 -0
- package/dist/folders/decode.cjs +79 -0
- package/dist/folders/decode.cjs.map +1 -0
- package/dist/folders/decode.d.ts +57 -0
- package/dist/folders/decode.d.ts.map +1 -0
- package/dist/folders/decode.js +77 -0
- package/dist/folders/decode.js.map +1 -0
- package/dist/folders/document.cjs +49 -0
- package/dist/folders/document.cjs.map +1 -0
- package/dist/folders/document.d.ts +56 -0
- package/dist/folders/document.d.ts.map +1 -0
- package/dist/folders/document.js +45 -0
- package/dist/folders/document.js.map +1 -0
- package/dist/folders/index.cjs +37 -0
- package/dist/folders/index.d.ts +10 -0
- package/dist/folders/index.js +10 -0
- package/dist/folders/inheritance.cjs +23 -0
- package/dist/folders/inheritance.cjs.map +1 -0
- package/dist/folders/inheritance.d.ts +19 -0
- package/dist/folders/inheritance.d.ts.map +1 -0
- package/dist/folders/inheritance.js +23 -0
- package/dist/folders/inheritance.js.map +1 -0
- package/dist/folders/invariants.cjs +115 -0
- package/dist/folders/invariants.cjs.map +1 -0
- package/dist/folders/invariants.d.ts +48 -0
- package/dist/folders/invariants.d.ts.map +1 -0
- package/dist/folders/invariants.js +114 -0
- package/dist/folders/invariants.js.map +1 -0
- package/dist/folders/naming.cjs +53 -0
- package/dist/folders/naming.cjs.map +1 -0
- package/dist/folders/naming.d.ts +31 -0
- package/dist/folders/naming.d.ts.map +1 -0
- package/dist/folders/naming.js +51 -0
- package/dist/folders/naming.js.map +1 -0
- package/dist/folders/planner.cjs +187 -0
- package/dist/folders/planner.cjs.map +1 -0
- package/dist/folders/planner.d.ts +100 -0
- package/dist/folders/planner.d.ts.map +1 -0
- package/dist/folders/planner.js +185 -0
- package/dist/folders/planner.js.map +1 -0
- package/dist/folders/removal.cjs +47 -0
- package/dist/folders/removal.cjs.map +1 -0
- package/dist/folders/removal.d.ts +42 -0
- package/dist/folders/removal.d.ts.map +1 -0
- package/dist/folders/removal.js +45 -0
- package/dist/folders/removal.js.map +1 -0
- package/dist/folders/view.cjs +210 -0
- package/dist/folders/view.cjs.map +1 -0
- package/dist/folders/view.d.ts +93 -0
- package/dist/folders/view.d.ts.map +1 -0
- package/dist/folders/view.js +205 -0
- package/dist/folders/view.js.map +1 -0
- package/dist/index.cjs +43 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +12 -1
- package/dist/project.cjs +23 -0
- package/dist/project.cjs.map +1 -0
- package/dist/project.d.ts +13 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +22 -0
- package/dist/project.js.map +1 -0
- package/package.json +7 -4
- package/src/folders/commit.ts +133 -0
- package/src/folders/decode.test.ts +128 -0
- package/src/folders/decode.ts +142 -0
- package/src/folders/document.ts +96 -0
- package/src/folders/index.ts +9 -0
- package/src/folders/inheritance.test.ts +86 -0
- package/src/folders/inheritance.ts +32 -0
- package/src/folders/invariants.test.ts +157 -0
- package/src/folders/invariants.ts +155 -0
- package/src/folders/naming.test.ts +62 -0
- package/src/folders/naming.ts +58 -0
- package/src/folders/planner.test.ts +436 -0
- package/src/folders/planner.ts +276 -0
- package/src/folders/removal.test.ts +142 -0
- package/src/folders/removal.ts +77 -0
- package/src/folders/test_utils.ts +86 -0
- package/src/folders/view.test.ts +422 -0
- package/src/folders/view.ts +324 -0
- package/src/index.ts +1 -0
- package/src/project.ts +21 -0
|
@@ -0,0 +1,42 @@
|
|
|
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/removal.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Everything a folder deletion destroys: the folder, every folder beneath it, and every project
|
|
7
|
+
* and template held anywhere inside.
|
|
8
|
+
*
|
|
9
|
+
* Deleting a project or a template cannot be undone, so this is computed for the dialog and
|
|
10
|
+
* recomputed inside the write, which commits only if the two agree. Without that, a project
|
|
11
|
+
* dropped into the folder between the two reads would be destroyed without ever appearing in the
|
|
12
|
+
* dialog that asked. Every list is in a canonical order for exactly that comparison.
|
|
13
|
+
*/
|
|
14
|
+
export interface FoldersRemoval {
|
|
15
|
+
readonly folders: readonly FolderId[];
|
|
16
|
+
readonly projects: readonly ProjectId[];
|
|
17
|
+
readonly templates: readonly TemplateId[];
|
|
18
|
+
}
|
|
19
|
+
/** One reason a folder cannot be deleted. */
|
|
20
|
+
export type FoldersRemovalIssue = {
|
|
21
|
+
readonly kind: "unknown-folder";
|
|
22
|
+
readonly folder: FolderId;
|
|
23
|
+
};
|
|
24
|
+
/** A removal, or everything standing in the way of one. */
|
|
25
|
+
export type FoldersRemovalPlanResult = {
|
|
26
|
+
readonly ok: true;
|
|
27
|
+
readonly removal: FoldersRemoval;
|
|
28
|
+
} | {
|
|
29
|
+
readonly ok: false;
|
|
30
|
+
readonly issues: readonly FoldersRemovalIssue[];
|
|
31
|
+
};
|
|
32
|
+
/** What deleting this folder would destroy. */
|
|
33
|
+
export declare function planFoldersRemoval(view: FoldersView, folder: FolderId): FoldersRemovalPlanResult;
|
|
34
|
+
/** True when two removals destroy exactly the same things. */
|
|
35
|
+
export declare function foldersRemovalsEqual(a: FoldersRemoval, b: FoldersRemoval): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* The document left once a removal is applied. The projects and templates themselves are deleted
|
|
38
|
+
* by the write path.
|
|
39
|
+
*/
|
|
40
|
+
export declare function applyFoldersRemoval(view: FoldersView, removal: FoldersRemoval): FoldersDocument;
|
|
41
|
+
//#endregion
|
|
42
|
+
//# sourceMappingURL=removal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"removal.d.ts","names":[],"sources":["../../src/folders/removal.ts"],"mappings":";;;;;;;;;;;;;iBAciB;WACN,kBAAkB;WAClB,mBAAmB;WACnB,oBAAoB;;;YAInB;WACD;WACA,QAAQ;;;YAIP;WACG;WAAmB,SAAS;;WAC5B;WAAoB,iBAAiB;;;wBAGpC,mBAAmB,MAAM,aAAa,QAAQ,WAAW;;wBAmBzD,qBAAqB,GAAG,gBAAgB,GAAG;;;;;wBAe3C,oBAAoB,MAAM,aAAa,SAAS,iBAAiB"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { foldersDocumentFromView, foldersSubtree } from "./view.js";
|
|
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 = [...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 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
|
+
export { applyFoldersRemoval, foldersRemovalsEqual, planFoldersRemoval };
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=removal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"removal.js","names":[],"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,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AS,EAAE,CAAC;CAClF,CAAC;AACH"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
const require_project = require("../project.cjs");
|
|
2
|
+
const require_document = require("./document.cjs");
|
|
3
|
+
//#region src/folders/view.ts
|
|
4
|
+
/**
|
|
5
|
+
* Resolve a decoded document against the project and template lists into the view the desktop
|
|
6
|
+
* renders.
|
|
7
|
+
*
|
|
8
|
+
* The read heals rather than fails. An item the document does not mention is at the top level;
|
|
9
|
+
* an assignment naming an item that is gone is ignored; a folder whose parent is missing, or
|
|
10
|
+
* which sits in a cycle, is lifted to the top level instead of being hidden. No input can make a
|
|
11
|
+
* project or a template disappear.
|
|
12
|
+
*/
|
|
13
|
+
function healFolders(decoded, projects, templates = []) {
|
|
14
|
+
return {
|
|
15
|
+
...foldersViewFromDocument(decoded.document, projects, templates),
|
|
16
|
+
writable: decoded.writable,
|
|
17
|
+
problem: decoded.problem
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** The same healing read, for a caller that already holds a document. */
|
|
21
|
+
function foldersViewFromDocument(document, projects, templates = []) {
|
|
22
|
+
const resolution = resolveFolders(document);
|
|
23
|
+
const { folders, byId } = resolution;
|
|
24
|
+
let healed = resolution.healed;
|
|
25
|
+
const placedProjects = place(projects, document.assignments, byId);
|
|
26
|
+
const placedTemplates = place(templates, document.templateAssignments, byId);
|
|
27
|
+
if (placedProjects.healed || placedTemplates.healed) healed = true;
|
|
28
|
+
return {
|
|
29
|
+
folders,
|
|
30
|
+
projects: placedProjects.items,
|
|
31
|
+
templates: placedTemplates.items,
|
|
32
|
+
writable: true,
|
|
33
|
+
healed
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Direct children of a folder, or of the top level when no folder is given. */
|
|
37
|
+
function foldersChildren(view, parent) {
|
|
38
|
+
return {
|
|
39
|
+
folders: view.folders.filter((folder) => folder.parent === parent),
|
|
40
|
+
projects: view.projects.filter((project) => project.folder === parent),
|
|
41
|
+
templates: view.templates.filter((template) => template.folder === parent)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Names already taken directly inside a folder, or at the top level when no folder is given.
|
|
46
|
+
*
|
|
47
|
+
* Folders, projects and templates share one namespace: whatever kind two things beside each other
|
|
48
|
+
* are, they never answer to one name. This is the one list every naming decision is made against.
|
|
49
|
+
*
|
|
50
|
+
* `exclude` names the ids that must not count against themselves — the items being renamed or
|
|
51
|
+
* moved.
|
|
52
|
+
*/
|
|
53
|
+
function foldersSiblingNames(view, parent, exclude = []) {
|
|
54
|
+
const skipped = new Set(exclude);
|
|
55
|
+
const children = foldersChildren(view, parent);
|
|
56
|
+
return [
|
|
57
|
+
...children.folders,
|
|
58
|
+
...children.projects,
|
|
59
|
+
...children.templates
|
|
60
|
+
].filter((item) => !skipped.has(item.id)).map((item) => item.name);
|
|
61
|
+
}
|
|
62
|
+
/** A folder and every folder beneath it, the folder itself first. */
|
|
63
|
+
function foldersSubtree(view, folder) {
|
|
64
|
+
const subtree = [];
|
|
65
|
+
const queue = [folder];
|
|
66
|
+
while (queue.length > 0) {
|
|
67
|
+
const current = queue.shift();
|
|
68
|
+
if (current === void 0) break;
|
|
69
|
+
subtree.push(current);
|
|
70
|
+
for (const candidate of view.folders) if (candidate.parent === current) queue.push(candidate.id);
|
|
71
|
+
}
|
|
72
|
+
return subtree;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The document a healed view stands for — what a write must persist so that the next read finds
|
|
76
|
+
* the tree it just rendered.
|
|
77
|
+
*/
|
|
78
|
+
function foldersDocumentFromView(view) {
|
|
79
|
+
return {
|
|
80
|
+
...require_document.emptyFoldersDocument(),
|
|
81
|
+
folders: view.folders.map((folder) => {
|
|
82
|
+
const description = require_project.normalizeDescription(folder.description);
|
|
83
|
+
return {
|
|
84
|
+
id: folder.id,
|
|
85
|
+
name: folder.name,
|
|
86
|
+
...folder.parent === void 0 ? {} : { parent: folder.parent },
|
|
87
|
+
...description === void 0 ? {} : { description }
|
|
88
|
+
};
|
|
89
|
+
}),
|
|
90
|
+
assignments: Object.fromEntries(view.projects.flatMap((project) => project.folder === void 0 ? [] : [[project.id, project.folder]])),
|
|
91
|
+
templateAssignments: Object.fromEntries(view.templates.flatMap((template) => template.folder === void 0 ? [] : [[template.id, template.folder]]))
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Resolve where each item of one list sits, and report whether doing so had to heal anything: a
|
|
96
|
+
* duplicate id in the list, an assignment to a folder that is not there, or an assignment naming
|
|
97
|
+
* an item the list no longer holds.
|
|
98
|
+
*/
|
|
99
|
+
function place(items, assignments, byId) {
|
|
100
|
+
let healed = false;
|
|
101
|
+
const seen = /* @__PURE__ */ new Set();
|
|
102
|
+
const placed = [];
|
|
103
|
+
for (const item of items) {
|
|
104
|
+
if (seen.has(item.id)) {
|
|
105
|
+
healed = true;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
seen.add(item.id);
|
|
109
|
+
const assigned = assignments[item.id];
|
|
110
|
+
const folder = assigned === void 0 ? void 0 : byId.get(assigned);
|
|
111
|
+
if (assigned !== void 0 && folder === void 0) healed = true;
|
|
112
|
+
placed.push(folder === void 0 ? {
|
|
113
|
+
id: item.id,
|
|
114
|
+
name: item.name,
|
|
115
|
+
ancestors: [],
|
|
116
|
+
path: []
|
|
117
|
+
} : {
|
|
118
|
+
id: item.id,
|
|
119
|
+
name: item.name,
|
|
120
|
+
folder: folder.id,
|
|
121
|
+
ancestors: [...folder.ancestors, folder.id],
|
|
122
|
+
path: [...folder.path]
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
for (const id of Object.keys(assignments)) if (!seen.has(id)) healed = true;
|
|
126
|
+
return {
|
|
127
|
+
items: placed,
|
|
128
|
+
healed
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Place every folder in the tree, lifting to the top level the ones whose stored parent cannot be
|
|
133
|
+
* honoured. A lifted folder keeps its own children, which simply sit one or more levels higher.
|
|
134
|
+
*/
|
|
135
|
+
function resolveFolders(document) {
|
|
136
|
+
const stored = /* @__PURE__ */ new Map();
|
|
137
|
+
let healed = false;
|
|
138
|
+
for (const folder of document.folders) {
|
|
139
|
+
if (stored.has(folder.id)) {
|
|
140
|
+
healed = true;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
stored.set(folder.id, {
|
|
144
|
+
name: folder.name,
|
|
145
|
+
parent: folder.parent,
|
|
146
|
+
description: folder.description
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
150
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
151
|
+
const resolve = (id) => {
|
|
152
|
+
const existing = resolved.get(id);
|
|
153
|
+
if (existing !== void 0) return existing;
|
|
154
|
+
const entry = stored.get(id);
|
|
155
|
+
if (entry === void 0) throw new Error(`folder ${id} is not in the document`);
|
|
156
|
+
if (visiting.has(id)) {
|
|
157
|
+
const lifted = topLevel(id, entry.name, true, entry.description);
|
|
158
|
+
resolved.set(id, lifted);
|
|
159
|
+
healed = true;
|
|
160
|
+
return lifted;
|
|
161
|
+
}
|
|
162
|
+
visiting.add(id);
|
|
163
|
+
try {
|
|
164
|
+
const parentId = entry.parent;
|
|
165
|
+
const parent = parentId === void 0 || !stored.has(parentId) ? void 0 : resolve(parentId);
|
|
166
|
+
const alreadyResolved = resolved.get(id);
|
|
167
|
+
if (alreadyResolved !== void 0) return alreadyResolved;
|
|
168
|
+
const view = parent === void 0 ? topLevel(id, entry.name, parentId !== void 0, entry.description) : {
|
|
169
|
+
id,
|
|
170
|
+
name: entry.name,
|
|
171
|
+
parent: parent.id,
|
|
172
|
+
ancestors: [...parent.ancestors, parent.id],
|
|
173
|
+
path: [...parent.path, entry.name],
|
|
174
|
+
...entry.description === void 0 ? {} : { description: entry.description },
|
|
175
|
+
lifted: false
|
|
176
|
+
};
|
|
177
|
+
if (view.lifted) healed = true;
|
|
178
|
+
resolved.set(id, view);
|
|
179
|
+
return view;
|
|
180
|
+
} finally {
|
|
181
|
+
visiting.delete(id);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
const folders = [];
|
|
185
|
+
for (const id of stored.keys()) folders.push(resolve(id));
|
|
186
|
+
return {
|
|
187
|
+
folders,
|
|
188
|
+
byId: resolved,
|
|
189
|
+
healed
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function topLevel(id, name, lifted, description) {
|
|
193
|
+
return {
|
|
194
|
+
id,
|
|
195
|
+
name,
|
|
196
|
+
ancestors: [],
|
|
197
|
+
path: [name],
|
|
198
|
+
...description === void 0 ? {} : { description },
|
|
199
|
+
lifted
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
exports.foldersChildren = foldersChildren;
|
|
204
|
+
exports.foldersDocumentFromView = foldersDocumentFromView;
|
|
205
|
+
exports.foldersSiblingNames = foldersSiblingNames;
|
|
206
|
+
exports.foldersSubtree = foldersSubtree;
|
|
207
|
+
exports.foldersViewFromDocument = foldersViewFromDocument;
|
|
208
|
+
exports.healFolders = healFolders;
|
|
209
|
+
|
|
210
|
+
//# sourceMappingURL=view.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"view.cjs","names":["emptyFoldersDocument","normalizeDescription"],"sources":["../../src/folders/view.ts"],"sourcesContent":["import type { ProjectId, TemplateId } from \"@milaboratories/pl-model-common\";\nimport { normalizeDescription } from \"../project\";\nimport type { FoldersDecoded, FoldersDocumentProblem } from \"./decode\";\nimport type { FoldersDocument, FolderId } from \"./document\";\nimport { emptyFoldersDocument } from \"./document\";\n\n/** An item as the folder machinery needs to see it: an identity and the label shown to a user. */\nexport interface FoldersItemInput<Id extends string> {\n readonly id: Id;\n readonly name: string;\n}\n\n/** Where an item sits in the tree. */\nexport interface FoldersPlacement {\n /** Absent for an item at the top level. */\n readonly folder?: FolderId;\n /** Containing folders, outermost first, including the folder holding the item. */\n readonly ancestors: readonly FolderId[];\n /**\n * Names of those folders, outermost first. Empty for an item at the top level. This is what\n * tells two identically named items apart in a search across the whole tree.\n */\n readonly path: readonly string[];\n}\n\n/** An item with the folder holding it resolved. */\nexport interface FoldersItemView<Id extends string>\n extends FoldersItemInput<Id>, FoldersPlacement {}\n\nexport type FoldersProjectInput = FoldersItemInput<ProjectId>;\nexport type FoldersTemplateInput = FoldersItemInput<TemplateId>;\nexport type FoldersProjectView = FoldersItemView<ProjectId>;\nexport type FoldersTemplateView = FoldersItemView<TemplateId>;\n\n/** A folder with its place in the tree resolved. */\nexport interface FolderView {\n readonly id: FolderId;\n readonly name: string;\n /** Absent for a top-level folder. */\n readonly parent?: FolderId;\n /** Containing folders, outermost first, excluding this folder. */\n readonly ancestors: readonly FolderId[];\n /** Names of the ancestors followed by this folder's own name. */\n readonly path: readonly string[];\n /** Free text the user wrote about the folder. Absent when there is none. */\n readonly description?: string;\n /** True when the stored parent was unusable and this folder was lifted to the top level. */\n readonly lifted: boolean;\n}\n\n/** The whole tree as the desktop consumes it. */\nexport interface FoldersView {\n readonly folders: readonly FolderView[];\n /** Every project handed in, in the order it was handed in. Nothing here is ever dropped. */\n readonly projects: readonly FoldersProjectView[];\n /** Every template handed in, in the order it was handed in. Nothing here is ever dropped. */\n readonly templates: readonly FoldersTemplateView[];\n /** False when folder writes must be refused. */\n readonly writable: boolean;\n /** Present when the stored document could not be used as it stands. */\n readonly problem?: FoldersDocumentProblem;\n /** True when what the view shows differs from what is stored, because the read healed it. */\n readonly healed: boolean;\n}\n\n/**\n * Resolve a decoded document against the project and template lists into the view the desktop\n * renders.\n *\n * The read heals rather than fails. An item the document does not mention is at the top level;\n * an assignment naming an item that is gone is ignored; a folder whose parent is missing, or\n * which sits in a cycle, is lifted to the top level instead of being hidden. No input can make a\n * project or a template disappear.\n */\nexport function healFolders(\n decoded: FoldersDecoded,\n projects: readonly FoldersProjectInput[],\n templates: readonly FoldersTemplateInput[] = [],\n): FoldersView {\n const view = foldersViewFromDocument(decoded.document, projects, templates);\n return { ...view, writable: decoded.writable, problem: decoded.problem };\n}\n\n/** The same healing read, for a caller that already holds a document. */\nexport function foldersViewFromDocument(\n document: FoldersDocument,\n projects: readonly FoldersProjectInput[],\n templates: readonly FoldersTemplateInput[] = [],\n): FoldersView {\n const resolution = resolveFolders(document);\n const { folders, byId } = resolution;\n let healed = resolution.healed;\n\n const placedProjects = place(projects, document.assignments, byId);\n const placedTemplates = place(templates, document.templateAssignments, byId);\n if (placedProjects.healed || placedTemplates.healed) healed = true;\n\n return {\n folders,\n projects: placedProjects.items,\n templates: placedTemplates.items,\n writable: true,\n healed,\n };\n}\n\n/** Direct children of a folder, or of the top level when no folder is given. */\nexport function foldersChildren(\n view: FoldersView,\n parent?: FolderId,\n): {\n readonly folders: readonly FolderView[];\n readonly projects: readonly FoldersProjectView[];\n readonly templates: readonly FoldersTemplateView[];\n} {\n return {\n folders: view.folders.filter((folder) => folder.parent === parent),\n projects: view.projects.filter((project) => project.folder === parent),\n templates: view.templates.filter((template) => template.folder === parent),\n };\n}\n\n/**\n * Names already taken directly inside a folder, or at the top level when no folder is given.\n *\n * Folders, projects and templates share one namespace: whatever kind two things beside each other\n * are, they never answer to one name. This is the one list every naming decision is made against.\n *\n * `exclude` names the ids that must not count against themselves — the items being renamed or\n * moved.\n */\nexport function foldersSiblingNames(\n view: FoldersView,\n parent?: FolderId,\n exclude: Iterable<string> = [],\n): string[] {\n const skipped = new Set<string>(exclude);\n const children = foldersChildren(view, parent);\n return [...children.folders, ...children.projects, ...children.templates]\n .filter((item) => !skipped.has(item.id))\n .map((item) => item.name);\n}\n\n/** A folder and every folder beneath it, the folder itself first. */\nexport function foldersSubtree(view: FoldersView, folder: FolderId): readonly FolderId[] {\n const subtree: FolderId[] = [];\n const queue: FolderId[] = [folder];\n while (queue.length > 0) {\n const current = queue.shift();\n if (current === undefined) break;\n subtree.push(current);\n for (const candidate of view.folders)\n if (candidate.parent === current) queue.push(candidate.id);\n }\n return subtree;\n}\n\n/**\n * The document a healed view stands for — what a write must persist so that the next read finds\n * the tree it just rendered.\n */\nexport function foldersDocumentFromView(view: FoldersView): FoldersDocument {\n const document = emptyFoldersDocument();\n return {\n ...document,\n folders: view.folders.map((folder) => {\n const description = normalizeDescription(folder.description);\n return {\n id: folder.id,\n name: folder.name,\n ...(folder.parent === undefined ? {} : { parent: folder.parent }),\n ...(description === undefined ? {} : { description }),\n };\n }),\n assignments: Object.fromEntries(\n view.projects.flatMap((project) =>\n project.folder === undefined ? [] : [[project.id, project.folder] as const],\n ),\n ),\n templateAssignments: Object.fromEntries(\n view.templates.flatMap((template) =>\n template.folder === undefined ? [] : [[template.id, template.folder] as const],\n ),\n ),\n };\n}\n\n//\n// Internals\n//\n\n/**\n * Resolve where each item of one list sits, and report whether doing so had to heal anything: a\n * duplicate id in the list, an assignment to a folder that is not there, or an assignment naming\n * an item the list no longer holds.\n */\nfunction place<Id extends string>(\n items: readonly FoldersItemInput<Id>[],\n assignments: Readonly<Record<string, FolderId>>,\n byId: ReadonlyMap<FolderId, FolderView>,\n): { readonly items: FoldersItemView<Id>[]; readonly healed: boolean } {\n let healed = false;\n const seen = new Set<string>();\n const placed: FoldersItemView<Id>[] = [];\n\n for (const item of items) {\n if (seen.has(item.id)) {\n healed = true;\n continue;\n }\n seen.add(item.id);\n\n const assigned = assignments[item.id];\n const folder = assigned === undefined ? undefined : byId.get(assigned);\n if (assigned !== undefined && folder === undefined) healed = true;\n\n placed.push(\n folder === undefined\n ? { id: item.id, name: item.name, ancestors: [], path: [] }\n : {\n id: item.id,\n name: item.name,\n folder: folder.id,\n ancestors: [...folder.ancestors, folder.id],\n path: [...folder.path],\n },\n );\n }\n\n for (const id of Object.keys(assignments)) if (!seen.has(id)) healed = true;\n\n return { items: placed, healed };\n}\n\ninterface FolderResolution {\n readonly folders: readonly FolderView[];\n readonly byId: ReadonlyMap<FolderId, FolderView>;\n readonly healed: boolean;\n}\n\n/**\n * Place every folder in the tree, lifting to the top level the ones whose stored parent cannot be\n * honoured. A lifted folder keeps its own children, which simply sit one or more levels higher.\n */\nfunction resolveFolders(document: FoldersDocument): FolderResolution {\n const stored = new Map<FolderId, { name: string; parent?: FolderId; description?: string }>();\n let healed = false;\n for (const folder of document.folders) {\n if (stored.has(folder.id)) {\n healed = true;\n continue;\n }\n stored.set(folder.id, {\n name: folder.name,\n parent: folder.parent,\n description: folder.description,\n });\n }\n\n const resolved = new Map<FolderId, FolderView>();\n const visiting = new Set<FolderId>();\n\n const resolve = (id: FolderId): FolderView => {\n const existing = resolved.get(id);\n if (existing !== undefined) return existing;\n\n const entry = stored.get(id);\n if (entry === undefined) throw new Error(`folder ${id} is not in the document`);\n\n if (visiting.has(id)) {\n // A cycle. Cutting it here, at the folder the walk came back to, keeps the rest of the\n // chain attached below this one instead of scattering every folder in the cycle.\n const lifted = topLevel(id, entry.name, true, entry.description);\n resolved.set(id, lifted);\n healed = true;\n return lifted;\n }\n\n visiting.add(id);\n try {\n const parentId = entry.parent;\n const parent =\n parentId === undefined || !stored.has(parentId) ? undefined : resolve(parentId);\n\n const alreadyResolved = resolved.get(id);\n if (alreadyResolved !== undefined) return alreadyResolved;\n\n const view =\n parent === undefined\n ? topLevel(id, entry.name, parentId !== undefined, entry.description)\n : {\n id,\n name: entry.name,\n parent: parent.id,\n ancestors: [...parent.ancestors, parent.id],\n path: [...parent.path, entry.name],\n ...(entry.description === undefined ? {} : { description: entry.description }),\n lifted: false,\n };\n\n if (view.lifted) healed = true;\n resolved.set(id, view);\n return view;\n } finally {\n visiting.delete(id);\n }\n };\n\n const folders: FolderView[] = [];\n for (const id of stored.keys()) folders.push(resolve(id));\n\n return { folders, byId: resolved, healed };\n}\n\nfunction topLevel(id: FolderId, name: string, lifted: boolean, description?: string): FolderView {\n return {\n id,\n name,\n ancestors: [],\n path: [name],\n ...(description === undefined ? {} : { description }),\n lifted,\n };\n}\n"],"mappings":";;;;;;;;;;;;AA0EA,SAAgB,YACd,SACA,UACA,YA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AI,oBAAoB,KAAA,GAAW,OAAO;GAE1C,MAAM,OACJ,WAAW,KAAA,IACP,SAAS,IAAI,MAAM,MAAM,aAAa,KAAA,GAAW,MAAM,WAAW,IAClE;IACE;IACA,MAAM,MAAM;IACZ,QAAQ,OAAO;IACf,WAAW,CAAC,GAAG,OAAO,WAAW,OAAO,EAAE;IAC1C,MAAM,CAAC,GAAG,OAAO,MAAM,MAAM,IAAI;IACjC,GAAI,MAAM,gBAAgB,KAAA,IAAY,CAAC,IAAI,EAAE,aAAa,MAAM,YAAY;IAC5E,QAAQ;GACV;GAEN,IAAI,KAAK,QAAQ,SAAS;GAC1B,SAAS,IAAI,IAAI,IAAI;GACrB,OAAO;EACT,UAAU;GACR,SAAS,OAAO,EAAE;EACpB;CACF;CAEA,MAAM,UAAwB,CAAC;CAC/B,KAAK,MAAM,MAAM,OAAO,KAAK,GAAG,QAAQ,KAAK,QAAQ,EAAE,CAAC;CAExD,OAAO;EAAE;EAAS,MAAM;EAAU;CAAO;AAC3C;AAEA,SAAS,SAAS,IAAc,MAAc,QAAiB,aAAkC;CAC/F,OAAO;EACL;EACA;EACA,WAAW,CAAC;EACZ,MAAM,CAAC,IAAI;EACX,GAAI,gBAAgB,KAAA,IAAY,CAAC,IAAI,EAAE,YAAY;EACnD;CACF;AACF"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { FolderId, FoldersDocument } from "./document.js";
|
|
2
|
+
import { FoldersDecoded, FoldersDocumentProblem } from "./decode.js";
|
|
3
|
+
import { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
|
|
4
|
+
//#region src/folders/view.d.ts
|
|
5
|
+
/** An item as the folder machinery needs to see it: an identity and the label shown to a user. */
|
|
6
|
+
export interface FoldersItemInput<Id extends string> {
|
|
7
|
+
readonly id: Id;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
}
|
|
10
|
+
/** Where an item sits in the tree. */
|
|
11
|
+
export interface FoldersPlacement {
|
|
12
|
+
/** Absent for an item at the top level. */
|
|
13
|
+
readonly folder?: FolderId;
|
|
14
|
+
/** Containing folders, outermost first, including the folder holding the item. */
|
|
15
|
+
readonly ancestors: readonly FolderId[];
|
|
16
|
+
/**
|
|
17
|
+
* Names of those folders, outermost first. Empty for an item at the top level. This is what
|
|
18
|
+
* tells two identically named items apart in a search across the whole tree.
|
|
19
|
+
*/
|
|
20
|
+
readonly path: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
/** An item with the folder holding it resolved. */
|
|
23
|
+
export interface FoldersItemView<Id extends string> extends FoldersItemInput<Id>, FoldersPlacement {}
|
|
24
|
+
export type FoldersProjectInput = FoldersItemInput<ProjectId>;
|
|
25
|
+
export type FoldersTemplateInput = FoldersItemInput<TemplateId>;
|
|
26
|
+
export type FoldersProjectView = FoldersItemView<ProjectId>;
|
|
27
|
+
export type FoldersTemplateView = FoldersItemView<TemplateId>;
|
|
28
|
+
/** A folder with its place in the tree resolved. */
|
|
29
|
+
export interface FolderView {
|
|
30
|
+
readonly id: FolderId;
|
|
31
|
+
readonly name: string;
|
|
32
|
+
/** Absent for a top-level folder. */
|
|
33
|
+
readonly parent?: FolderId;
|
|
34
|
+
/** Containing folders, outermost first, excluding this folder. */
|
|
35
|
+
readonly ancestors: readonly FolderId[];
|
|
36
|
+
/** Names of the ancestors followed by this folder's own name. */
|
|
37
|
+
readonly path: readonly string[];
|
|
38
|
+
/** Free text the user wrote about the folder. Absent when there is none. */
|
|
39
|
+
readonly description?: string;
|
|
40
|
+
/** True when the stored parent was unusable and this folder was lifted to the top level. */
|
|
41
|
+
readonly lifted: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** The whole tree as the desktop consumes it. */
|
|
44
|
+
export interface FoldersView {
|
|
45
|
+
readonly folders: readonly FolderView[];
|
|
46
|
+
/** Every project handed in, in the order it was handed in. Nothing here is ever dropped. */
|
|
47
|
+
readonly projects: readonly FoldersProjectView[];
|
|
48
|
+
/** Every template handed in, in the order it was handed in. Nothing here is ever dropped. */
|
|
49
|
+
readonly templates: readonly FoldersTemplateView[];
|
|
50
|
+
/** False when folder writes must be refused. */
|
|
51
|
+
readonly writable: boolean;
|
|
52
|
+
/** Present when the stored document could not be used as it stands. */
|
|
53
|
+
readonly problem?: FoldersDocumentProblem;
|
|
54
|
+
/** True when what the view shows differs from what is stored, because the read healed it. */
|
|
55
|
+
readonly healed: boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Resolve a decoded document against the project and template lists into the view the desktop
|
|
59
|
+
* renders.
|
|
60
|
+
*
|
|
61
|
+
* The read heals rather than fails. An item the document does not mention is at the top level;
|
|
62
|
+
* an assignment naming an item that is gone is ignored; a folder whose parent is missing, or
|
|
63
|
+
* which sits in a cycle, is lifted to the top level instead of being hidden. No input can make a
|
|
64
|
+
* project or a template disappear.
|
|
65
|
+
*/
|
|
66
|
+
export declare function healFolders(decoded: FoldersDecoded, projects: readonly FoldersProjectInput[], templates?: readonly FoldersTemplateInput[]): FoldersView;
|
|
67
|
+
/** The same healing read, for a caller that already holds a document. */
|
|
68
|
+
export declare function foldersViewFromDocument(document: FoldersDocument, projects: readonly FoldersProjectInput[], templates?: readonly FoldersTemplateInput[]): FoldersView;
|
|
69
|
+
/** Direct children of a folder, or of the top level when no folder is given. */
|
|
70
|
+
export declare function foldersChildren(view: FoldersView, parent?: FolderId): {
|
|
71
|
+
readonly folders: readonly FolderView[];
|
|
72
|
+
readonly projects: readonly FoldersProjectView[];
|
|
73
|
+
readonly templates: readonly FoldersTemplateView[];
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Names already taken directly inside a folder, or at the top level when no folder is given.
|
|
77
|
+
*
|
|
78
|
+
* Folders, projects and templates share one namespace: whatever kind two things beside each other
|
|
79
|
+
* are, they never answer to one name. This is the one list every naming decision is made against.
|
|
80
|
+
*
|
|
81
|
+
* `exclude` names the ids that must not count against themselves — the items being renamed or
|
|
82
|
+
* moved.
|
|
83
|
+
*/
|
|
84
|
+
export declare function foldersSiblingNames(view: FoldersView, parent?: FolderId, exclude?: Iterable<string>): string[];
|
|
85
|
+
/** A folder and every folder beneath it, the folder itself first. */
|
|
86
|
+
export declare function foldersSubtree(view: FoldersView, folder: FolderId): readonly FolderId[];
|
|
87
|
+
/**
|
|
88
|
+
* The document a healed view stands for — what a write must persist so that the next read finds
|
|
89
|
+
* the tree it just rendered.
|
|
90
|
+
*/
|
|
91
|
+
export declare function foldersDocumentFromView(view: FoldersView): FoldersDocument;
|
|
92
|
+
//#endregion
|
|
93
|
+
//# sourceMappingURL=view.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"view.d.ts","names":[],"sources":["../../src/folders/view.ts"],"mappings":";;;;;iBAOiB,iBAAiB;WACvB,IAAI;WACJ;;;iBAIM;;WAEN,SAAS;;WAET,oBAAoB;;;;;WAKpB;;;iBAIM,gBAAgB,2BACvB,iBAAiB,KAAK;YAEpB,sBAAsB,iBAAiB;YACvC,uBAAuB,iBAAiB;YACxC,qBAAqB,gBAAgB;YACrC,sBAAsB,gBAAgB;;iBAGjC;WACN,IAAI;WACJ;;WAEA,SAAS;;WAET,oBAAoB;;WAEpB;;WAEA;;WAEA;;;iBAIM;WACN,kBAAkB;;WAElB,mBAAmB;;WAEnB,oBAAoB;;WAEpB;;WAEA,UAAU;;WAEV;;;;;;;;;;;wBAYK,YACd,SAAS,gBACT,mBAAmB,uBACnB,qBAAoB,yBACnB;;wBAMa,wBACd,UAAU,iBACV,mBAAmB,uBACnB,qBAAoB,yBACnB;;wBAmBa,gBACd,MAAM,aACN,SAAS;WAEA,kBAAkB;WAClB,mBAAmB;WACnB,oBAAoB;;;;;;;;;;;wBAkBf,oBACd,MAAM,aACN,SAAS,UACT,UAAS;;wBAUK,eAAe,MAAM,aAAa,QAAQ,oBAAoB;;;;;wBAiB9D,wBAAwB,MAAM,cAAc"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { normalizeDescription } from "../project.js";
|
|
2
|
+
import { emptyFoldersDocument } from "./document.js";
|
|
3
|
+
//#region src/folders/view.ts
|
|
4
|
+
/**
|
|
5
|
+
* Resolve a decoded document against the project and template lists into the view the desktop
|
|
6
|
+
* renders.
|
|
7
|
+
*
|
|
8
|
+
* The read heals rather than fails. An item the document does not mention is at the top level;
|
|
9
|
+
* an assignment naming an item that is gone is ignored; a folder whose parent is missing, or
|
|
10
|
+
* which sits in a cycle, is lifted to the top level instead of being hidden. No input can make a
|
|
11
|
+
* project or a template disappear.
|
|
12
|
+
*/
|
|
13
|
+
function healFolders(decoded, projects, templates = []) {
|
|
14
|
+
return {
|
|
15
|
+
...foldersViewFromDocument(decoded.document, projects, templates),
|
|
16
|
+
writable: decoded.writable,
|
|
17
|
+
problem: decoded.problem
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** The same healing read, for a caller that already holds a document. */
|
|
21
|
+
function foldersViewFromDocument(document, projects, templates = []) {
|
|
22
|
+
const resolution = resolveFolders(document);
|
|
23
|
+
const { folders, byId } = resolution;
|
|
24
|
+
let healed = resolution.healed;
|
|
25
|
+
const placedProjects = place(projects, document.assignments, byId);
|
|
26
|
+
const placedTemplates = place(templates, document.templateAssignments, byId);
|
|
27
|
+
if (placedProjects.healed || placedTemplates.healed) healed = true;
|
|
28
|
+
return {
|
|
29
|
+
folders,
|
|
30
|
+
projects: placedProjects.items,
|
|
31
|
+
templates: placedTemplates.items,
|
|
32
|
+
writable: true,
|
|
33
|
+
healed
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Direct children of a folder, or of the top level when no folder is given. */
|
|
37
|
+
function foldersChildren(view, parent) {
|
|
38
|
+
return {
|
|
39
|
+
folders: view.folders.filter((folder) => folder.parent === parent),
|
|
40
|
+
projects: view.projects.filter((project) => project.folder === parent),
|
|
41
|
+
templates: view.templates.filter((template) => template.folder === parent)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Names already taken directly inside a folder, or at the top level when no folder is given.
|
|
46
|
+
*
|
|
47
|
+
* Folders, projects and templates share one namespace: whatever kind two things beside each other
|
|
48
|
+
* are, they never answer to one name. This is the one list every naming decision is made against.
|
|
49
|
+
*
|
|
50
|
+
* `exclude` names the ids that must not count against themselves — the items being renamed or
|
|
51
|
+
* moved.
|
|
52
|
+
*/
|
|
53
|
+
function foldersSiblingNames(view, parent, exclude = []) {
|
|
54
|
+
const skipped = new Set(exclude);
|
|
55
|
+
const children = foldersChildren(view, parent);
|
|
56
|
+
return [
|
|
57
|
+
...children.folders,
|
|
58
|
+
...children.projects,
|
|
59
|
+
...children.templates
|
|
60
|
+
].filter((item) => !skipped.has(item.id)).map((item) => item.name);
|
|
61
|
+
}
|
|
62
|
+
/** A folder and every folder beneath it, the folder itself first. */
|
|
63
|
+
function foldersSubtree(view, folder) {
|
|
64
|
+
const subtree = [];
|
|
65
|
+
const queue = [folder];
|
|
66
|
+
while (queue.length > 0) {
|
|
67
|
+
const current = queue.shift();
|
|
68
|
+
if (current === void 0) break;
|
|
69
|
+
subtree.push(current);
|
|
70
|
+
for (const candidate of view.folders) if (candidate.parent === current) queue.push(candidate.id);
|
|
71
|
+
}
|
|
72
|
+
return subtree;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The document a healed view stands for — what a write must persist so that the next read finds
|
|
76
|
+
* the tree it just rendered.
|
|
77
|
+
*/
|
|
78
|
+
function foldersDocumentFromView(view) {
|
|
79
|
+
return {
|
|
80
|
+
...emptyFoldersDocument(),
|
|
81
|
+
folders: view.folders.map((folder) => {
|
|
82
|
+
const description = normalizeDescription(folder.description);
|
|
83
|
+
return {
|
|
84
|
+
id: folder.id,
|
|
85
|
+
name: folder.name,
|
|
86
|
+
...folder.parent === void 0 ? {} : { parent: folder.parent },
|
|
87
|
+
...description === void 0 ? {} : { description }
|
|
88
|
+
};
|
|
89
|
+
}),
|
|
90
|
+
assignments: Object.fromEntries(view.projects.flatMap((project) => project.folder === void 0 ? [] : [[project.id, project.folder]])),
|
|
91
|
+
templateAssignments: Object.fromEntries(view.templates.flatMap((template) => template.folder === void 0 ? [] : [[template.id, template.folder]]))
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Resolve where each item of one list sits, and report whether doing so had to heal anything: a
|
|
96
|
+
* duplicate id in the list, an assignment to a folder that is not there, or an assignment naming
|
|
97
|
+
* an item the list no longer holds.
|
|
98
|
+
*/
|
|
99
|
+
function place(items, assignments, byId) {
|
|
100
|
+
let healed = false;
|
|
101
|
+
const seen = /* @__PURE__ */ new Set();
|
|
102
|
+
const placed = [];
|
|
103
|
+
for (const item of items) {
|
|
104
|
+
if (seen.has(item.id)) {
|
|
105
|
+
healed = true;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
seen.add(item.id);
|
|
109
|
+
const assigned = assignments[item.id];
|
|
110
|
+
const folder = assigned === void 0 ? void 0 : byId.get(assigned);
|
|
111
|
+
if (assigned !== void 0 && folder === void 0) healed = true;
|
|
112
|
+
placed.push(folder === void 0 ? {
|
|
113
|
+
id: item.id,
|
|
114
|
+
name: item.name,
|
|
115
|
+
ancestors: [],
|
|
116
|
+
path: []
|
|
117
|
+
} : {
|
|
118
|
+
id: item.id,
|
|
119
|
+
name: item.name,
|
|
120
|
+
folder: folder.id,
|
|
121
|
+
ancestors: [...folder.ancestors, folder.id],
|
|
122
|
+
path: [...folder.path]
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
for (const id of Object.keys(assignments)) if (!seen.has(id)) healed = true;
|
|
126
|
+
return {
|
|
127
|
+
items: placed,
|
|
128
|
+
healed
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Place every folder in the tree, lifting to the top level the ones whose stored parent cannot be
|
|
133
|
+
* honoured. A lifted folder keeps its own children, which simply sit one or more levels higher.
|
|
134
|
+
*/
|
|
135
|
+
function resolveFolders(document) {
|
|
136
|
+
const stored = /* @__PURE__ */ new Map();
|
|
137
|
+
let healed = false;
|
|
138
|
+
for (const folder of document.folders) {
|
|
139
|
+
if (stored.has(folder.id)) {
|
|
140
|
+
healed = true;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
stored.set(folder.id, {
|
|
144
|
+
name: folder.name,
|
|
145
|
+
parent: folder.parent,
|
|
146
|
+
description: folder.description
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
150
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
151
|
+
const resolve = (id) => {
|
|
152
|
+
const existing = resolved.get(id);
|
|
153
|
+
if (existing !== void 0) return existing;
|
|
154
|
+
const entry = stored.get(id);
|
|
155
|
+
if (entry === void 0) throw new Error(`folder ${id} is not in the document`);
|
|
156
|
+
if (visiting.has(id)) {
|
|
157
|
+
const lifted = topLevel(id, entry.name, true, entry.description);
|
|
158
|
+
resolved.set(id, lifted);
|
|
159
|
+
healed = true;
|
|
160
|
+
return lifted;
|
|
161
|
+
}
|
|
162
|
+
visiting.add(id);
|
|
163
|
+
try {
|
|
164
|
+
const parentId = entry.parent;
|
|
165
|
+
const parent = parentId === void 0 || !stored.has(parentId) ? void 0 : resolve(parentId);
|
|
166
|
+
const alreadyResolved = resolved.get(id);
|
|
167
|
+
if (alreadyResolved !== void 0) return alreadyResolved;
|
|
168
|
+
const view = parent === void 0 ? topLevel(id, entry.name, parentId !== void 0, entry.description) : {
|
|
169
|
+
id,
|
|
170
|
+
name: entry.name,
|
|
171
|
+
parent: parent.id,
|
|
172
|
+
ancestors: [...parent.ancestors, parent.id],
|
|
173
|
+
path: [...parent.path, entry.name],
|
|
174
|
+
...entry.description === void 0 ? {} : { description: entry.description },
|
|
175
|
+
lifted: false
|
|
176
|
+
};
|
|
177
|
+
if (view.lifted) healed = true;
|
|
178
|
+
resolved.set(id, view);
|
|
179
|
+
return view;
|
|
180
|
+
} finally {
|
|
181
|
+
visiting.delete(id);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
const folders = [];
|
|
185
|
+
for (const id of stored.keys()) folders.push(resolve(id));
|
|
186
|
+
return {
|
|
187
|
+
folders,
|
|
188
|
+
byId: resolved,
|
|
189
|
+
healed
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function topLevel(id, name, lifted, description) {
|
|
193
|
+
return {
|
|
194
|
+
id,
|
|
195
|
+
name,
|
|
196
|
+
ancestors: [],
|
|
197
|
+
path: [name],
|
|
198
|
+
...description === void 0 ? {} : { description },
|
|
199
|
+
lifted
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
export { foldersChildren, foldersDocumentFromView, foldersSiblingNames, foldersSubtree, foldersViewFromDocument, healFolders };
|
|
204
|
+
|
|
205
|
+
//# sourceMappingURL=view.js.map
|