Library only; nothing is mounted. Adds minimal DeviceContext/NoticeContext stubs (owned by W0-breakpoints / W0-registry-notices) that expose the agreed interfaces. Refs #39 #49 #34 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
88 lines
3.2 KiB
TypeScript
88 lines
3.2 KiB
TypeScript
/**
|
|
* Find the actual deletable target for a node. Linked nodes (owned by parent
|
|
* components like ColumnLayout columns or Section inner) are structurally
|
|
* non-deletable in Craft.js — actions.delete() throws on them. When the
|
|
* selected node is an empty linked node and all its linked siblings are also
|
|
* empty (i.e., the whole layout is empty), redirect deletion to the owning
|
|
* parent so the layout itself goes away. Otherwise return null to refuse
|
|
* deletion — protects against nuking a layout that has content in other cols.
|
|
*/
|
|
export function findDeletableTarget(query: any, nodeId: string | null | undefined): string | null {
|
|
if (!nodeId || nodeId === 'ROOT') return null;
|
|
try {
|
|
const node = query.node(nodeId).get();
|
|
const parentId: string | null | undefined = node?.data?.parent;
|
|
if (!parentId) return nodeId;
|
|
const parent = query.node(parentId).get();
|
|
const linkedIds = Object.values((parent?.data?.linkedNodes || {}) as Record<string, string>);
|
|
if (!linkedIds.includes(nodeId)) return nodeId;
|
|
const allEmpty = linkedIds.every((id) => {
|
|
const sib = query.node(id).get();
|
|
return ((sib?.data?.nodes as string[] | undefined) || []).length === 0;
|
|
});
|
|
if (!allEmpty) return null;
|
|
return findDeletableTarget(query, parentId);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// ---- Tree walkers (shared by commands, breadcrumb, palette) ----
|
|
|
|
function nodeData(query: any, id: string): any {
|
|
try {
|
|
return query.node(id).get()?.data ?? null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function getParentId(query: any, id: string): string | null {
|
|
if (!id || id === 'ROOT') return null;
|
|
return nodeData(query, id)?.parent ?? null;
|
|
}
|
|
|
|
/** Children in order: `nodes` first, then linked nodes (Section inner, columns). */
|
|
export function getChildIds(query: any, id: string): string[] {
|
|
const d = nodeData(query, id);
|
|
if (!d) return [];
|
|
return [...((d.nodes as string[] | undefined) || []), ...Object.values((d.linkedNodes || {}) as Record<string, string>)];
|
|
}
|
|
|
|
/** Siblings including the node itself, in order. For a linked node these are
|
|
* the parent's linked values; empty for ROOT / unknown nodes. */
|
|
export function getSiblingIds(query: any, id: string): string[] {
|
|
const parentId = getParentId(query, id);
|
|
if (!parentId) return [];
|
|
const pd = nodeData(query, parentId);
|
|
if (!pd) return [];
|
|
const nodes: string[] = pd.nodes || [];
|
|
if (nodes.includes(id)) return [...nodes];
|
|
const linked = Object.values((pd.linkedNodes || {}) as Record<string, string>);
|
|
return linked.includes(id) ? linked : [];
|
|
}
|
|
|
|
/** Nearest first, ROOT excluded. */
|
|
export function getAncestorIds(query: any, id: string): string[] {
|
|
const out: string[] = [];
|
|
const seen = new Set<string>([id]);
|
|
let cur = getParentId(query, id);
|
|
while (cur && cur !== 'ROOT' && !seen.has(cur)) {
|
|
out.push(cur);
|
|
seen.add(cur);
|
|
cur = getParentId(query, cur);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function getIndexInParent(query: any, id: string): number {
|
|
return getSiblingIds(query, id).indexOf(id);
|
|
}
|
|
|
|
export function getNodeLabel(query: any, id: string): string {
|
|
if (id === 'ROOT') return 'Canvas';
|
|
const d = nodeData(query, id);
|
|
if (!d) return 'Node';
|
|
return d.custom?.label || d.props?.aiName || d.displayName || 'Node';
|
|
}
|