Remap frame port references and commit import cycles atomically
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import type { Json } from './types.ts';
|
||||
import { fault } from './values.ts';
|
||||
|
||||
/** Own JSON fields only. A * segment enumerates an array, never object keys. */
|
||||
export function* copySlots(data: Json, path: readonly string[]): Generator<{parent: Record<string, Json>; key: string}> {
|
||||
const work: {value: Json; depth: number}[] = [{value: data, depth: 0}];
|
||||
while (work.length) {
|
||||
const {value, depth} = work.pop()!, key = path[depth];
|
||||
if (key === '*') {
|
||||
if (!Array.isArray(value)) throw fault('invalid_copy_reference');
|
||||
if (depth === path.length - 1) { for (let i = 0; i < value.length; i++) yield {parent: value as unknown as Record<string, Json>, key: String(i)}; }
|
||||
else for (let i = value.length - 1; i >= 0; i--) work.push({value: value[i], depth: depth + 1});
|
||||
} else if (value && typeof value === 'object' && Object.hasOwn(value, key)) {
|
||||
if (depth === path.length - 1) yield {parent: value as Record<string, Json>, key};
|
||||
else work.push({value: (value as Record<string, Json>)[key], depth: depth + 1});
|
||||
}
|
||||
}
|
||||
}
|
||||
export function* copyReferenceIds(data: Json, ref: {path: readonly string[]; many?: boolean}): Generator<string> {
|
||||
for (const {parent, key} of copySlots(data, ref.path)) {
|
||||
const value = parent[key]; if (value === null) continue;
|
||||
if (ref.many && !Array.isArray(value)) throw fault('invalid_copy_reference');
|
||||
for (const id of ref.many ? value as Json[] : [value]) {
|
||||
if (typeof id !== 'string' || !id) throw fault('invalid_copy_reference');
|
||||
yield id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ export function builtinComponents(): readonly ComponentDefinition[] {
|
||||
id, dataVersion: 1, structure: 'node', validate: textData,
|
||||
features: [movable(), resizable(), ...(id === 'frame' ? [] : [rotatable()]), editable()],
|
||||
resourceFields: ['image', 'plain-image'].includes(id) ? ['assetId','asset'] : [],
|
||||
copy: { references: [], omit: [['taskId'], ['runId'], ['progress'], ['runStatus']] },
|
||||
copy: { references: id === 'frame' ? [{path:['ports','*','nodeId'],target:'element',external:'reject'}] : [], omit: [['taskId'], ['runId'], ['progress'], ['runStatus']] },
|
||||
})).concat([defineComponent({ id: 'connection', dataVersion: 1, structure: 'connection', validate: data => validJson(data), features: [editable(), connectionEditable()], resourceFields: [], copy: { references: [], omit: [] } })]);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ export function defineComponent(definition: ComponentDefinition): ComponentDefin
|
||||
const references = definition.copy.references, omitted = definition.copy.omit;
|
||||
if (!Array.isArray(references) || !Array.isArray(omitted) || omitted.some(path => !pathValid(path)) || references.some(ref => !pathValid(ref.path) || !['element','canvas'].includes(ref.target) || !['preserve','reject'].includes(ref.external) || ref.many !== undefined && typeof ref.many !== 'boolean')) throw fault('invalid_copy_schema');
|
||||
const paths: readonly (readonly string[])[] = [...references.map(ref => ref.path), ...omitted];
|
||||
for (let i = 0; i < paths.length; i++) for (let j = 0; j < i; j++) if (paths[i].slice(0, Math.min(paths[i].length, paths[j].length)).every((key, index) => key === paths[j][index])) throw fault('conflicting_copy_paths');
|
||||
for (let i = 0; i < paths.length; i++) for (let j = 0; j < i; j++) if (paths[i].slice(0, Math.min(paths[i].length, paths[j].length)).every((key, index) => (key === paths[j][index] || key === '*' || paths[j][index] === '*'))) throw fault('conflicting_copy_paths');
|
||||
}
|
||||
return freeze({ ...definition, copy: definition.copy ? clone(definition.copy) : undefined, features: definition.features.map(feature => defineFeature(feature.id, feature.handlers)), migrations: definition.migrations ? { ...definition.migrations } : undefined, resourceFields: definition.resourceFields ? [...definition.resourceFields] : undefined });
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ export type ReadonlyValueSet<T> = Pick<ReadonlySet<T>, 'size' | 'has' | 'entries
|
||||
export type Capability = 'geometry.move' | 'geometry.resize' | 'geometry.rotate' | 'content.edit' | 'connection.edit';
|
||||
export type CapabilityHandler = (context: { readonly element: ElementRecord; readonly input: Readonly<Record<string, unknown>> }) => readonly FieldChange[];
|
||||
export type Feature = { readonly id: string; readonly handlers: Readonly<Partial<Record<Capability, CapabilityHandler>>> };
|
||||
/** Path segments address own fields; * iterates array entries. many marks a terminal ID array. */
|
||||
export type CopyReference = { readonly path: readonly string[]; readonly target: 'element' | 'canvas'; readonly many?: boolean; readonly external: 'preserve' | 'reject' };
|
||||
export type CopySchema = { readonly references: readonly CopyReference[]; readonly omit: readonly (readonly string[])[] };
|
||||
export type ComponentDefinition = { readonly id: string; readonly dataVersion: number; readonly structure: ElementRecord['structure']; readonly features: readonly Feature[]; readonly validate: (data: Data) => boolean; readonly migrations?: Readonly<Record<number, (data: Data) => Data>>; readonly resourceFields?: readonly string[]; readonly copy?: CopySchema };
|
||||
|
||||
Reference in New Issue
Block a user