@milaboratories/pl-model-middle-layer 1.33.0 → 1.34.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.d.ts +1 -1
- package/dist/folders/index.d.ts +1 -1
- package/dist/folders/inheritance.cjs +4 -4
- package/dist/folders/inheritance.cjs.map +1 -1
- package/dist/folders/inheritance.d.ts +4 -4
- package/dist/folders/inheritance.js +4 -4
- package/dist/folders/inheritance.js.map +1 -1
- package/dist/folders/invariants.cjs +9 -9
- package/dist/folders/invariants.cjs.map +1 -1
- package/dist/folders/invariants.d.ts +5 -3
- package/dist/folders/invariants.d.ts.map +1 -1
- package/dist/folders/invariants.js +9 -9
- package/dist/folders/invariants.js.map +1 -1
- package/dist/folders/naming.cjs +5 -4
- package/dist/folders/naming.cjs.map +1 -1
- package/dist/folders/naming.d.ts +5 -4
- package/dist/folders/naming.d.ts.map +1 -1
- package/dist/folders/naming.js +5 -4
- package/dist/folders/naming.js.map +1 -1
- package/dist/folders/planner.cjs +8 -3
- package/dist/folders/planner.cjs.map +1 -1
- package/dist/folders/planner.d.ts.map +1 -1
- package/dist/folders/planner.js +8 -3
- package/dist/folders/planner.js.map +1 -1
- package/dist/folders/view.cjs +7 -5
- package/dist/folders/view.cjs.map +1 -1
- package/dist/folders/view.d.ts +8 -3
- package/dist/folders/view.d.ts.map +1 -1
- package/dist/folders/view.js +7 -5
- package/dist/folders/view.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +2 -2
- package/src/folders/inheritance.test.ts +11 -6
- package/src/folders/inheritance.ts +7 -6
- package/src/folders/invariants.test.ts +15 -6
- package/src/folders/invariants.ts +15 -11
- package/src/folders/naming.ts +5 -4
- package/src/folders/planner.test.ts +20 -6
- package/src/folders/planner.ts +10 -7
- package/src/folders/view.test.ts +17 -6
- package/src/folders/view.ts +15 -5
|
@@ -1 +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,Q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U,MAAM,cAAc,UAAU,OAAO,KAAK,EAAE;GAC5E,OAAO,aAAa,KAAA,IAChB,KAAA,IACA;IAAE,MAAM,SAAS;IAAM,cAAc,SAAS;GAAO;EAC3D;CACF;AACF;;;;;;AAOA,SAAS,iBAAiB,GAAY,GAAoB;CACxD,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"}
|
|
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 // Each kind is named against its own kind only: a moved folder never becomes \"X (Copy)\" for a\n // project called X in the destination.\n const movedIds = moved.map(({ item }) => item.id);\n const taken: Record<FoldersItem[\"kind\"], string[]> = {\n folder: foldersSiblingNames(view, destination, movedIds, { kind: \"folder\" }),\n project: foldersSiblingNames(view, destination, movedIds, { kind: \"project\" }),\n template: foldersSiblingNames(view, destination, movedIds, { kind: \"template\" }),\n };\n\n const entries: FoldersMoveEntry[] = [];\n for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {\n const resulting = foldersUniqueName(name, taken[item.kind]);\n taken[item.kind].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,IAAI,UAAU,KAAA,GAAW;GACvB,OAAO,KAAK;IAAE,MAAM;IAAgB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AE,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"}
|
package/dist/folders/view.cjs
CHANGED
|
@@ -44,20 +44,22 @@ function foldersChildren(view, parent) {
|
|
|
44
44
|
/**
|
|
45
45
|
* Names already taken directly inside a folder, or at the top level when no folder is given.
|
|
46
46
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
47
|
+
* Each kind has its own namespace: two folders, two projects or two templates beside each other
|
|
48
|
+
* never answer to one name, but a folder, a project and a template may. With `kind` given, only the
|
|
49
|
+
* names of that kind are listed — the list a name for an item of that kind is checked against.
|
|
50
|
+
* Without it, the names of every kind are listed.
|
|
49
51
|
*
|
|
50
52
|
* `exclude` names the ids that must not count against themselves — the items being renamed or
|
|
51
53
|
* moved.
|
|
52
54
|
*/
|
|
53
|
-
function foldersSiblingNames(view, parent, exclude = []) {
|
|
55
|
+
function foldersSiblingNames(view, parent, exclude = [], options = {}) {
|
|
54
56
|
const skipped = new Set(exclude);
|
|
55
57
|
const children = foldersChildren(view, parent);
|
|
56
|
-
return [
|
|
58
|
+
return (options.kind === "folder" ? children.folders : options.kind === "project" ? children.projects : options.kind === "template" ? children.templates : [
|
|
57
59
|
...children.folders,
|
|
58
60
|
...children.projects,
|
|
59
61
|
...children.templates
|
|
60
|
-
].filter((item) => !skipped.has(item.id)).map((item) => item.name);
|
|
62
|
+
]).filter((item) => !skipped.has(item.id)).map((item) => item.name);
|
|
61
63
|
}
|
|
62
64
|
/** A folder and every folder beneath it, the folder itself first. */
|
|
63
65
|
function foldersSubtree(view, folder) {
|
|
@@ -1 +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,YAA6C,CAAC,GACjC;CAEb,OAAO;EAAE,GADI,wBAAwB,QAAQ,UAAU,UAAU,SAClD;EAAG,UAAU,QAAQ;EAAU,SAAS,QAAQ;CAAQ;AACzE;;AAGA,SAAgB,wBACd,UACA,UACA,YAA6C,CAAC,GACjC;CACb,MAAM,aAAa,eAAe,QAAQ;CAC1C,MAAM,EAAE,SAAS,SAAS;CAC1B,IAAI,SAAS,WAAW;CAExB,MAAM,iBAAiB,MAAM,UAAU,SAAS,aAAa,IAAI;CACjE,MAAM,kBAAkB,MAAM,WAAW,SAAS,qBAAqB,IAAI;CAC3E,IAAI,eAAe,UAAU,gBAAgB,QAAQ,SAAS;CAE9D,OAAO;EACL;EACA,UAAU,eAAe;EACzB,WAAW,gBAAgB;EAC3B,UAAU;EACV;CACF;AACF;;AAGA,SAAgB,gBACd,MACA,QAKA;CACA,OAAO;EACL,SAAS,KAAK,QAAQ,QAAQ,WAAW,OAAO,WAAW,MAAM;EACjE,UAAU,KAAK,SAAS,QAAQ,YAAY,QAAQ,WAAW,MAAM;EACrE,WAAW,KAAK,UAAU,QAAQ,aAAa,SAAS,WAAW,MAAM;CAC3E;AACF;;;;;;;;;;AAWA,SAAgB,oBACd,MACA,QAC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,KAAA,IAAY,CAAC,IAAI,EAAE,YAAY;EACnD;CACF;AACF"}
|
|
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\";\nimport type { FoldersItem } from \"./planner\";\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 * Each kind has its own namespace: two folders, two projects or two templates beside each other\n * never answer to one name, but a folder, a project and a template may. With `kind` given, only the\n * names of that kind are listed — the list a name for an item of that kind is checked against.\n * Without it, the names of every kind are listed.\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 options: { readonly kind?: FoldersItem[\"kind\"] } = {},\n): string[] {\n const skipped = new Set<string>(exclude);\n const children = foldersChildren(view, parent);\n const listed: readonly { readonly id: string; readonly name: string }[] =\n options.kind === \"folder\"\n ? children.folders\n : options.kind === \"project\"\n ? children.projects\n : options.kind === \"template\"\n ? children.templates\n : [...children.folders, ...children.projects, ...children.templates];\n return listed.filter((item) => !skipped.has(item.id)).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":";;;;;;;;;;;;AA2EA,SAAgB,YACd,SACA,UACA,YAA6C,CAAC,GACjC;CAEb,OAAO;EAAE,GADI,wBAAwB,QAAQ,UAAU,UAAU,SAClD;EAAG,UAAU,QAAQ;EAAU,SAAS,QAAQ;CAAQ;AACzE;;AAGA,SAAgB,wBACd,UACA,UACA,YAA6C,CAAC,GACjC;CACb,MAAM,aAAa,eAAe,QAAQ;CAC1C,MAAM,EAAE,SAAS,SAAS;CAC1B,IAAI,SAAS,WAAW;CAExB,MAAM,iBAAiB,MAAM,UAAU,SAAS,aAAa,IAAI;CACjE,MAAM,kBAAkB,MAAM,WAAW,SAAS,qBAAqB,IAAI;CAC3E,IAAI,eAAe,UAAU,gBAAgB,QAAQ,SAAS;CAE9D,OAAO;EACL;EACA,UAAU,eAAe;EACzB,WAAW,gBAAgB;EAC3B,UAAU;EACV;CACF;AACF;;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AAS;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"}
|
package/dist/folders/view.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { FolderId, FoldersDocument } from "./document.js";
|
|
2
2
|
import { FoldersDecoded, FoldersDocumentProblem } from "./decode.js";
|
|
3
|
+
import { FoldersItem } from "./planner.js";
|
|
3
4
|
import { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
|
|
4
5
|
//#region src/folders/view.d.ts
|
|
5
6
|
/** An item as the folder machinery needs to see it: an identity and the label shown to a user. */
|
|
@@ -75,13 +76,17 @@ export declare function foldersChildren(view: FoldersView, parent?: FolderId): {
|
|
|
75
76
|
/**
|
|
76
77
|
* Names already taken directly inside a folder, or at the top level when no folder is given.
|
|
77
78
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
79
|
+
* Each kind has its own namespace: two folders, two projects or two templates beside each other
|
|
80
|
+
* never answer to one name, but a folder, a project and a template may. With `kind` given, only the
|
|
81
|
+
* names of that kind are listed — the list a name for an item of that kind is checked against.
|
|
82
|
+
* Without it, the names of every kind are listed.
|
|
80
83
|
*
|
|
81
84
|
* `exclude` names the ids that must not count against themselves — the items being renamed or
|
|
82
85
|
* moved.
|
|
83
86
|
*/
|
|
84
|
-
export declare function foldersSiblingNames(view: FoldersView, parent?: FolderId, exclude?: Iterable<string
|
|
87
|
+
export declare function foldersSiblingNames(view: FoldersView, parent?: FolderId, exclude?: Iterable<string>, options?: {
|
|
88
|
+
readonly kind?: FoldersItem["kind"];
|
|
89
|
+
}): string[];
|
|
85
90
|
/** A folder and every folder beneath it, the folder itself first. */
|
|
86
91
|
export declare function foldersSubtree(view: FoldersView, folder: FolderId): readonly FolderId[];
|
|
87
92
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view.d.ts","names":[],"sources":["../../src/folders/view.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"view.d.ts","names":[],"sources":["../../src/folders/view.ts"],"mappings":";;;;;;iBAQiB,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;;;;;;;;;;;;;wBAoBf,oBACd,MAAM,aACN,SAAS,UACT,UAAS,kBACT;WAAoB,OAAO;;;wBAgBb,eAAe,MAAM,aAAa,QAAQ,oBAAoB;;;;;wBAiB9D,wBAAwB,MAAM,cAAc"}
|
package/dist/folders/view.js
CHANGED
|
@@ -44,20 +44,22 @@ function foldersChildren(view, parent) {
|
|
|
44
44
|
/**
|
|
45
45
|
* Names already taken directly inside a folder, or at the top level when no folder is given.
|
|
46
46
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
47
|
+
* Each kind has its own namespace: two folders, two projects or two templates beside each other
|
|
48
|
+
* never answer to one name, but a folder, a project and a template may. With `kind` given, only the
|
|
49
|
+
* names of that kind are listed — the list a name for an item of that kind is checked against.
|
|
50
|
+
* Without it, the names of every kind are listed.
|
|
49
51
|
*
|
|
50
52
|
* `exclude` names the ids that must not count against themselves — the items being renamed or
|
|
51
53
|
* moved.
|
|
52
54
|
*/
|
|
53
|
-
function foldersSiblingNames(view, parent, exclude = []) {
|
|
55
|
+
function foldersSiblingNames(view, parent, exclude = [], options = {}) {
|
|
54
56
|
const skipped = new Set(exclude);
|
|
55
57
|
const children = foldersChildren(view, parent);
|
|
56
|
-
return [
|
|
58
|
+
return (options.kind === "folder" ? children.folders : options.kind === "project" ? children.projects : options.kind === "template" ? children.templates : [
|
|
57
59
|
...children.folders,
|
|
58
60
|
...children.projects,
|
|
59
61
|
...children.templates
|
|
60
|
-
].filter((item) => !skipped.has(item.id)).map((item) => item.name);
|
|
62
|
+
]).filter((item) => !skipped.has(item.id)).map((item) => item.name);
|
|
61
63
|
}
|
|
62
64
|
/** A folder and every folder beneath it, the folder itself first. */
|
|
63
65
|
function foldersSubtree(view, folder) {
|
package/dist/folders/view.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view.js","names":[],"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":";;;;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,IAAI,EAAE;GACvC,IAAI,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"}
|
|
1
|
+
{"version":3,"file":"view.js","names":[],"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\";\nimport type { FoldersItem } from \"./planner\";\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 * Each kind has its own namespace: two folders, two projects or two templates beside each other\n * never answer to one name, but a folder, a project and a template may. With `kind` given, only the\n * names of that kind are listed — the list a name for an item of that kind is checked against.\n * Without it, the names of every kind are listed.\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 options: { readonly kind?: FoldersItem[\"kind\"] } = {},\n): string[] {\n const skipped = new Set<string>(exclude);\n const children = foldersChildren(view, parent);\n const listed: readonly { readonly id: string; readonly name: string }[] =\n options.kind === \"folder\"\n ? children.folders\n : options.kind === \"project\"\n ? children.projects\n : options.kind === \"template\"\n ? children.templates\n : [...children.folders, ...children.projects, ...children.templates];\n return listed.filter((item) => !skipped.has(item.id)).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":";;;;;;;;;;;;AA2EA,SAAgB,YACd,SACA,UACA,YAA6C,CAAC,GACjC;CAEb,OAAO;EAAE,GADI,wBAAwB,QAAQ,UAAU,UAAU,SAClD;EAAG,UAAU,QAAQ;EAAU,SAAS,QAAQ;CAAQ;AACzE;;AAGA,SAAgB,wBACd,UACA,UACA,YAA6C,CAAC,GACjC;CACb,MAAM,aAAa,eAAe,QAAQ;CAC1C,MAAM,EAAE,SAAS,SAAS;CAC1B,IAAI,SAAS,WAAW;CAExB,MAAM,iBAAiB,MAAM,UAAU,SAAS,aAAa,IAAI;CACjE,MAAM,kBAAkB,MAAM,WAAW,SAAS,qBAAqB,IAAI;CAC3E,IAAI,eAAe,UAAU,gBAAgB,QAAQ,SAAS;CAE9D,OAAO;EACL;EACA,UAAU,eAAe;EACzB,WAAW,gBAAgB;EAC3B,UAAU;EACV;CACF;AACF;;AAGA,SAAgB,gBACd,MACA,QAKA;CACA,OAAO;EACL,SAAS,KAAK,QAAQ,QAAQ,WAAW,OAAO,WAAW,MAAM;EACjE,UAAU,KAAK,SAAS,QAAQ,YAAY,QAAQ,WAAW,MAAM;EACrE,WAAW,KAAK,UAAU,QAAQ,aAAa,SAAS,WAAW,MAAM;CAC3E;AACF;;;;;;;;;;;;AAaA,SAAgB,oBACd,MACA,QACA,UAA4B,CAAC,GAC7B,UAAmD,CAAC,GAC1C;CACV,MAAM,UAAU,IAAI,IAAY,OAAO;CACvC,MAAM,WAAW,gBAAgB,MAAM,MAAM;CAS7C,QAPE,QAAQ,SAAS,WACb,SAAS,UACT,QAAQ,SAAS,YACf,SAAS,WACT,QAAQ,SAAS,aACf,SAAS,YACT;EAAC,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_info.js";
|
|
|
12
12
|
import { BlockCalculationStatus, BlockStateOverview, ProjectOverview } from "./project_overview.js";
|
|
13
13
|
import { FOLDERS_SCHEMA_VERSION, FolderEntry, FolderId, FoldersDocument, asFolderId, emptyFoldersDocument } from "./folders/document.js";
|
|
14
14
|
import { FoldersDecoded, FoldersDocumentProblem, FoldersStoredDocument, decodeFoldersDocument, decodeStoredFoldersDocument, encodeFoldersDocument } from "./folders/decode.js";
|
|
15
|
+
import { FoldersItem, FoldersLabelRename, FoldersLeafItem, FoldersMoveApplication, FoldersMoveEntry, FoldersMoveIssue, FoldersMovePlan, FoldersMovePlanResult, applyFoldersMove, foldersMovePlansEqual, planFoldersMove } from "./folders/planner.js";
|
|
15
16
|
import { FolderView, FoldersItemInput, FoldersItemView, FoldersPlacement, FoldersProjectInput, FoldersProjectView, FoldersTemplateInput, FoldersTemplateView, FoldersView, foldersChildren, foldersDocumentFromView, foldersSiblingNames, foldersSubtree, foldersViewFromDocument, healFolders } from "./folders/view.js";
|
|
16
17
|
import { FoldersViolation, formatFoldersViolation, validateFoldersDocument } from "./folders/invariants.js";
|
|
17
18
|
import { foldersNameBlank, foldersNameTaken, foldersUniqueName } from "./folders/naming.js";
|
|
18
|
-
import { FoldersItem, FoldersLabelRename, FoldersLeafItem, FoldersMoveApplication, FoldersMoveEntry, FoldersMoveIssue, FoldersMovePlan, FoldersMovePlanResult, applyFoldersMove, foldersMovePlansEqual, planFoldersMove } from "./folders/planner.js";
|
|
19
19
|
import { FoldersRemoval, FoldersRemovalIssue, FoldersRemovalPlanResult, applyFoldersRemoval, foldersRemovalsEqual, planFoldersRemoval } from "./folders/removal.js";
|
|
20
20
|
import { FoldersEdit, FoldersMoveOutcome, FoldersRemovalOutcome, commitFoldersMove, commitFoldersRemoval } from "./folders/commit.js";
|
|
21
21
|
import { inheritedFolder } from "./folders/inheritance.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milaboratories/pl-model-middle-layer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.34.0",
|
|
4
4
|
"description": "Common model between middle layer and non-block UI code",
|
|
5
5
|
"files": [
|
|
6
6
|
"./dist/**/*",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"@vitest/coverage-istanbul": "^4.1.3",
|
|
29
29
|
"typescript": "7.0.2",
|
|
30
30
|
"vitest": "^4.1.3",
|
|
31
|
-
"@milaboratories/ts-configs": "1.4.0",
|
|
32
31
|
"@milaboratories/build-configs": "2.0.2",
|
|
32
|
+
"@milaboratories/ts-configs": "1.4.0",
|
|
33
33
|
"@milaboratories/ts-builder": "1.7.2"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
@@ -15,11 +15,16 @@ describe("what a new item inherits from the project it was made from", () => {
|
|
|
15
15
|
expect(inheritedFolder(tree, pid("p1"), template("t1"), "Run (Copy)")).toBe(fid("a"));
|
|
16
16
|
});
|
|
17
17
|
|
|
18
|
-
test("a template is held to the
|
|
19
|
-
const tree = view(
|
|
18
|
+
test("a template is held to the names of the templates in that folder only", () => {
|
|
19
|
+
const tree = view(
|
|
20
|
+
[{ id: "a", name: "Samples" }],
|
|
21
|
+
[{ id: "p1", name: "Run", folder: "a" }],
|
|
22
|
+
[{ id: "t2", name: "Pilot", folder: "a" }],
|
|
23
|
+
);
|
|
20
24
|
|
|
21
|
-
// "Run" is
|
|
22
|
-
expect(inheritedFolder(tree, pid("p1"), template("t1"), "Run")).
|
|
25
|
+
// "Run" is the source project's own name, which a template may carry beside it.
|
|
26
|
+
expect(inheritedFolder(tree, pid("p1"), template("t1"), "Run")).toBe(fid("a"));
|
|
27
|
+
expect(inheritedFolder(tree, pid("p1"), template("t1"), "pilot")).toBeUndefined();
|
|
23
28
|
});
|
|
24
29
|
|
|
25
30
|
test("a source at the top level leaves the new item at the top level", () => {
|
|
@@ -46,7 +51,7 @@ describe("what a new item inherits from the project it was made from", () => {
|
|
|
46
51
|
expect(inheritedFolder(tree, pid("p1"), project("p3"), "Run (Copy)")).toBeUndefined();
|
|
47
52
|
});
|
|
48
53
|
|
|
49
|
-
test("a child folder's name
|
|
54
|
+
test("a child folder's name does not block a project: each kind has its own namespace", () => {
|
|
50
55
|
const tree = view(
|
|
51
56
|
[
|
|
52
57
|
{ id: "a", name: "Samples" },
|
|
@@ -55,7 +60,7 @@ describe("what a new item inherits from the project it was made from", () => {
|
|
|
55
60
|
[{ id: "p1", name: "Run", folder: "a" }],
|
|
56
61
|
);
|
|
57
62
|
|
|
58
|
-
expect(inheritedFolder(tree, pid("p1"), project("p2"), "Run (Copy)")).
|
|
63
|
+
expect(inheritedFolder(tree, pid("p1"), project("p2"), "Run (Copy)")).toBe(fid("a"));
|
|
59
64
|
});
|
|
60
65
|
|
|
61
66
|
test("a name taken elsewhere in the tree does not block the placement", () => {
|
|
@@ -12,9 +12,9 @@ import { foldersSiblingNames } from "./view";
|
|
|
12
12
|
* top level reads as the app having lost them. `undefined` means leave it at the top level:
|
|
13
13
|
*
|
|
14
14
|
* - the source is itself at the top level, or is no longer in the tree at all;
|
|
15
|
-
* - the
|
|
16
|
-
* suffixing it here would disagree with what it reported,
|
|
17
|
-
* a placement would be the worse trade.
|
|
15
|
+
* - an item of the created one's kind already carries the name where the source sits. The
|
|
16
|
+
* creating caller chose that name, so suffixing it here would disagree with what it reported,
|
|
17
|
+
* and refusing the whole creation over a placement would be the worse trade.
|
|
18
18
|
*/
|
|
19
19
|
export function inheritedFolder(
|
|
20
20
|
view: FoldersView,
|
|
@@ -25,8 +25,9 @@ export function inheritedFolder(
|
|
|
25
25
|
const folder = view.projects.find((candidate) => candidate.id === source)?.folder;
|
|
26
26
|
if (folder === undefined) return undefined;
|
|
27
27
|
|
|
28
|
-
// Scoped to the destination, because the rule is: names are
|
|
29
|
-
// globally. The item being created is left out in
|
|
30
|
-
|
|
28
|
+
// Scoped to the destination and to the created item's kind, because the rule is: names are
|
|
29
|
+
// unique among one kind within a parent, never globally. The item being created is left out in
|
|
30
|
+
// case the view already lists it.
|
|
31
|
+
const siblings = foldersSiblingNames(view, folder, [created.id], { kind: created.kind });
|
|
31
32
|
return foldersNameTaken(name, siblings) ? undefined : folder;
|
|
32
33
|
}
|
|
@@ -102,30 +102,39 @@ describe("validateFoldersDocument", () => {
|
|
|
102
102
|
).toEqual([]);
|
|
103
103
|
});
|
|
104
104
|
|
|
105
|
-
test("a folder and a project inside the same parent share
|
|
105
|
+
test("a folder and a project inside the same parent may share a name", () => {
|
|
106
106
|
expect(kinds(document([{ id: "a", name: "Runs" }], { p1: "gone-nowhere" }), [])).toEqual([]);
|
|
107
107
|
|
|
108
108
|
expect(kinds(document([{ id: "a", name: "Runs" }]), [{ id: pid("p1"), name: "runs" }])).toEqual(
|
|
109
|
-
[
|
|
109
|
+
[],
|
|
110
110
|
);
|
|
111
111
|
});
|
|
112
112
|
|
|
113
|
-
test("
|
|
113
|
+
test("two projects inside the same parent may not", () => {
|
|
114
|
+
expect(
|
|
115
|
+
kinds(document([]), [
|
|
116
|
+
{ id: pid("p1"), name: "Runs" },
|
|
117
|
+
{ id: pid("p2"), name: "runs" },
|
|
118
|
+
]),
|
|
119
|
+
).toEqual(["duplicate-name"]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("a template shares its name with nothing but other templates", () => {
|
|
114
123
|
const withTemplates = (templates: { id: string; name: string }[]) =>
|
|
115
124
|
validateFoldersDocument(
|
|
116
125
|
document([{ id: "a", name: "Runs" }], { p1: "a" }, { t1: "a", t2: "a" }),
|
|
117
126
|
[{ id: pid("p1"), name: "Pilot" }],
|
|
118
127
|
templates.map((template) => ({ id: tid(template.id), name: template.name })),
|
|
119
|
-
)
|
|
128
|
+
);
|
|
120
129
|
|
|
121
130
|
expect(withTemplates([{ id: "t1", name: "Snapshot" }])).toEqual([]);
|
|
122
|
-
expect(withTemplates([{ id: "t1", name: "pilot" }])).toEqual([
|
|
131
|
+
expect(withTemplates([{ id: "t1", name: "pilot" }])).toEqual([]);
|
|
123
132
|
expect(
|
|
124
133
|
withTemplates([
|
|
125
134
|
{ id: "t1", name: "Snapshot" },
|
|
126
135
|
{ id: "t2", name: "Snapshot" },
|
|
127
136
|
]),
|
|
128
|
-
).toEqual(["duplicate-name"]);
|
|
137
|
+
).toEqual([{ kind: "duplicate-name", item: "template", parent: "a", name: "Snapshot" }]);
|
|
129
138
|
});
|
|
130
139
|
|
|
131
140
|
test("an assignment naming a folder that is not there is reported", () => {
|
|
@@ -2,6 +2,7 @@ import type { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
|
|
|
2
2
|
import { asProjectId, asTemplateId } from "@milaboratories/pl-model-common";
|
|
3
3
|
import type { FoldersDocument, FolderId } from "./document";
|
|
4
4
|
import { foldersNameBlank, foldersNameTaken } from "./naming";
|
|
5
|
+
import type { FoldersItem } from "./planner";
|
|
5
6
|
import type { FoldersProjectInput, FoldersTemplateInput } from "./view";
|
|
6
7
|
|
|
7
8
|
/** One way a document breaks the rules the write path enforces. */
|
|
@@ -16,6 +17,8 @@ export type FoldersViolation =
|
|
|
16
17
|
| { readonly kind: "cycle"; readonly folder: FolderId }
|
|
17
18
|
| {
|
|
18
19
|
readonly kind: "duplicate-name";
|
|
20
|
+
/** Which kind of item the name is used twice among. */
|
|
21
|
+
readonly item: FoldersItem["kind"];
|
|
19
22
|
readonly parent?: FolderId;
|
|
20
23
|
readonly name: string;
|
|
21
24
|
}
|
|
@@ -37,9 +40,8 @@ export type FoldersViolation =
|
|
|
37
40
|
* write that would persist any of them is refused, because a document no reader can render as
|
|
38
41
|
* stored is a document someone has to repair by hand.
|
|
39
42
|
*
|
|
40
|
-
* Project and template names are optional. Without them
|
|
41
|
-
*
|
|
42
|
-
* should. The template list also tells an assignment for a template that is gone — which a read
|
|
43
|
+
* Project and template names are optional. Without them only folder names can be checked for
|
|
44
|
+
* duplicates, so a write path that can supply them should. The template list also tells an assignment for a template that is gone — which a read
|
|
43
45
|
* prunes — from one naming a folder that does not exist, which is a broken document.
|
|
44
46
|
*/
|
|
45
47
|
export function validateFoldersDocument(
|
|
@@ -71,21 +73,23 @@ export function validateFoldersDocument(
|
|
|
71
73
|
|
|
72
74
|
// The comparison is the naming rule's, not a second one written here: a document is invalid in
|
|
73
75
|
// exactly the cases the rule would have refused, or the write path and the checker would drift.
|
|
76
|
+
// Each kind of item has a namespace of its own inside a parent.
|
|
74
77
|
const namesByParent = new Map<string, string[]>();
|
|
75
|
-
const claim = (parent: FolderId | undefined, name: string) => {
|
|
76
|
-
const key = parent ?? ""
|
|
78
|
+
const claim = (item: FoldersItem["kind"], parent: FolderId | undefined, name: string) => {
|
|
79
|
+
const key = `${item}:${parent ?? ""}`;
|
|
77
80
|
const names = namesByParent.get(key) ?? [];
|
|
78
81
|
namesByParent.set(key, names);
|
|
79
|
-
if (foldersNameTaken(name, names))
|
|
82
|
+
if (foldersNameTaken(name, names))
|
|
83
|
+
violations.push({ kind: "duplicate-name", item, parent, name });
|
|
80
84
|
names.push(name);
|
|
81
85
|
};
|
|
82
86
|
|
|
83
|
-
for (const folder of byId.values()) claim(folder.parent, folder.name);
|
|
87
|
+
for (const folder of byId.values()) claim("folder", folder.parent, folder.name);
|
|
84
88
|
|
|
85
89
|
const knownProjects = new Set<string>((projects ?? []).map((project) => project.id));
|
|
86
90
|
for (const project of projects ?? []) {
|
|
87
91
|
const parent = document.assignments[project.id];
|
|
88
|
-
if (parent === undefined || byId.has(parent)) claim(parent, project.name);
|
|
92
|
+
if (parent === undefined || byId.has(parent)) claim("project", parent, project.name);
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
for (const [project, folder] of Object.entries(document.assignments))
|
|
@@ -98,7 +102,7 @@ export function validateFoldersDocument(
|
|
|
98
102
|
|
|
99
103
|
for (const template of templates ?? []) {
|
|
100
104
|
const parent = document.templateAssignments[template.id];
|
|
101
|
-
if (parent === undefined || byId.has(parent)) claim(parent, template.name);
|
|
105
|
+
if (parent === undefined || byId.has(parent)) claim("template", parent, template.name);
|
|
102
106
|
}
|
|
103
107
|
|
|
104
108
|
const knownTemplates = new Set<string>((templates ?? []).map((template) => template.id));
|
|
@@ -126,8 +130,8 @@ export function formatFoldersViolation(violation: FoldersViolation): string {
|
|
|
126
130
|
return `folder ${violation.folder} sits in a cycle of parents`;
|
|
127
131
|
case "duplicate-name":
|
|
128
132
|
return violation.parent === undefined
|
|
129
|
-
? `the name "${violation.name}" is used more than once at the top level`
|
|
130
|
-
: `the name "${violation.name}" is used more than once inside folder ${violation.parent}`;
|
|
133
|
+
? `the ${violation.item} name "${violation.name}" is used more than once at the top level`
|
|
134
|
+
: `the ${violation.item} name "${violation.name}" is used more than once inside folder ${violation.parent}`;
|
|
131
135
|
case "unknown-assignment-folder":
|
|
132
136
|
return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;
|
|
133
137
|
case "unknown-template-assignment-folder":
|
package/src/folders/naming.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
* The one naming rule for folders, projects and templates.
|
|
3
3
|
*
|
|
4
4
|
* Scope is always the children of one parent — a folder, or the top level, which is simply the
|
|
5
|
-
* folder with no parent
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* folder with no parent — and one kind of item. No two folders, no two projects and no two
|
|
6
|
+
* templates inside the same parent can carry the same name, while a folder, a project and a
|
|
7
|
+
* template beside each other may: the list shows each kind under its own heading, so they are
|
|
8
|
+
* never mistaken for one another. Comparison ignores case and surrounding whitespace: `Samples`,
|
|
9
|
+
* `samples` and ` Samples ` are the same thing to the person looking at them.
|
|
9
10
|
*
|
|
10
11
|
* The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
|
|
11
12
|
* human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
|