Remap frame port references and commit import cycles atomically

This commit is contained in:
2026-11-08 18:09:39 +08:00
parent 1f89f9ebb5
commit 60fad39847
15 changed files with 209 additions and 48 deletions
+15 -17
View File
@@ -1,4 +1,5 @@
import type { CanvasRecord, Change, Data, Document, ElementRecord, Id, Json, Point, Registry, Target } from './types.ts';
import { copySlots } from './copy-paths.ts';
import { ancestors, commonCanvas, targetElement, validateDocumentSteps } from './document.ts';
import { connectionPathValid } from './features.ts';
import { canvasTransform, convertPath, toLocal, toWorld } from './coordinates.ts';
@@ -11,25 +12,22 @@ export function remapCopyData(element: ElementRecord, registry: Registry, elemen
const definition = registry.components.get(element.componentId), schema = definition?.copy;
if (registry.resolve(element) !== 'known' || !schema) throw fault('copy_schema_unavailable', [element.id]);
const data = clone(element.data) as Record<string, Json>;
const locate = (path: readonly string[]) => {
let parent: Json = data;
for (const key of path.slice(0, -1)) { if (!parent || typeof parent !== 'object' || !Object.hasOwn(parent, key)) return; parent = (parent as Record<string, Json>)[key]; }
if (parent && typeof parent === 'object' && Object.hasOwn(parent, path.at(-1)!)) return { parent: parent as Record<string, Json>, key: path.at(-1)! };
};
for (const ref of schema.references) {
const slot = locate(ref.path); if (!slot || slot.parent[slot.key] === null) continue;
const mapped = (value: Json): Json => {
if (typeof value !== 'string' || !value) throw fault('invalid_copy_reference', [element.id]);
const map = ref.target === 'element' ? elements : canvases, known = ref.target === 'element' ? document.elements : document.canvases;
if (map.has(value)) return map.get(value)!;
if (ref.external !== 'preserve' || !known.has(value)) throw fault('copy_external_reference', [element.id, value]);
return value;
};
const value = slot.parent[slot.key];
if (ref.many && !Array.isArray(value)) throw fault('invalid_copy_reference', [element.id]);
slot.parent[slot.key] = ref.many ? (value as Json[]).map(mapped) : mapped(value);
for (const slot of copySlots(data, ref.path)) {
if (slot.parent[slot.key] === null) continue;
const mapped = (value: Json): Json => {
if (typeof value !== 'string' || !value) throw fault('invalid_copy_reference', [element.id]);
const map = ref.target === 'element' ? elements : canvases, known = ref.target === 'element' ? document.elements : document.canvases;
if (map.has(value)) return map.get(value)!;
if (ref.external !== 'preserve' || !known.has(value)) throw fault('copy_external_reference', [element.id, value]);
return value;
};
const value = slot.parent[slot.key];
if (ref.many && !Array.isArray(value)) throw fault('invalid_copy_reference', [element.id]);
slot.parent[slot.key] = ref.many ? (value as Json[]).map(mapped) : mapped(value);
}
}
for (const path of schema.omit) { const slot = locate(path); if (slot) { if (Array.isArray(slot.parent)) throw fault('invalid_copy_omit', [element.id]); delete slot.parent[slot.key]; } }
for (const path of schema.omit) for (const slot of copySlots(data, path)) { if (Array.isArray(slot.parent)) throw fault('invalid_copy_omit', [element.id]); delete slot.parent[slot.key]; }
return data;
}