Files
site-builder/craft/src/utils/canvas-summary.ts
T

33 lines
1.4 KiB
TypeScript
Raw Normal View History

import { SerializedNodes } from '@craftjs/core';
export function summarizeCanvas(state: SerializedNodes, maxChars = 6000): string {
const root = state['ROOT'];
if (!root) return '(empty canvas)';
const lines: string[] = [];
const visit = (id: string, depth: number) => {
const node = state[id];
if (!node) return;
const indent = ' '.repeat(depth);
const type = typeof node.type === 'object' ? (node.type as any).resolvedName : String(node.type);
const props = node.props || {};
const aiName = (props as any).aiName ?? '';
const nodeId = (props as any).node_id ?? id;
const interesting: string[] = [];
for (const [k, v] of Object.entries(props)) {
if (k === 'aiName' || k === 'node_id' || k === 'style') continue;
if (v == null) continue;
const repr = typeof v === 'string' ? v : JSON.stringify(v);
const truncated = repr.length > 60 ? repr.slice(0, 57) + '…' : repr;
interesting.push(`${k}=${truncated}`);
if (interesting.length >= 3) break;
}
lines.push(`${indent}- ${type} id=${nodeId} name="${aiName}" {${interesting.join(', ')}}`);
if (type === 'HtmlBlock') return;
for (const childId of node.nodes || []) visit(childId, depth + 1);
};
visit('ROOT', 0);
let out = lines.join('\n');
if (out.length > maxChars) out = out.slice(0, maxChars - 30) + '\n… (truncated)';
return out;
}