Replan structural edits with shared coordinates and lifecycle-safe history
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import type { Applied, Availability, Capability, Change, Command, Commit, CoreSnapshot, Document, ElementRecord, Geometry, HistoryEntry, HistoryState, Id, OperationIntent, Origin, Registry, Result, Snapshot } from './types.ts';
|
||||
import { applyChanges, commonCanvas, createDocument, invert, targetElement, validateDocument } from './document.ts';
|
||||
import { applyChanges, createDocument, invert, targetElement, validateDocument } from './document.ts';
|
||||
import { clone, equal, fault, freeze, readMap, reject, validJson } from './values.ts';
|
||||
|
||||
import { canvasTransform, convertGeometry } from './coordinates.ts';
|
||||
import { removalChanges, repairConnectionOwners } from './structure.ts';
|
||||
|
||||
export type EditorOptions<Runtime = undefined> = {
|
||||
registry: Registry; document: Document; createId?: () => Id; historyLimit?: number;
|
||||
policy?: (command: Command, document: Document) => Availability;
|
||||
@@ -43,6 +46,7 @@ export function createEditor<Runtime = undefined>(options: EditorOptions<Runtime
|
||||
const canvas = document.canvases.get(command.canvasId);
|
||||
if (!canvas) throw fault('canvas_not_loaded', [command.canvasId]);
|
||||
changes = equal(canvas.parent, command.parent) ? [] : [{ kind: 'canvas', id: canvas.id, before: canvas, after: { ...canvas, parent: command.parent } }];
|
||||
if(changes.length)changes=[...changes,...repairConnectionOwners(document,applyChanges(document,changes))];
|
||||
} else if (command.type === 'element.create') {
|
||||
const element = command.element;
|
||||
if (document.elements.has(element.id) || document.tombstones.has(element.id)) throw fault('duplicate_identity', [element.id]);
|
||||
@@ -59,21 +63,23 @@ export function createEditor<Runtime = undefined>(options: EditorOptions<Runtime
|
||||
if (!validJson(command.path)) throw fault('invalid_connection_path', [element.id]);
|
||||
changes = options.registry.handlers.get(element.componentId)!.get('connection.edit')!({ element, input: freeze({ patch: { path: clone(command.path) } }) });
|
||||
for (const change of changes) if (change.kind !== 'field' || change.field !== 'path' || change.target.id !== element.id || change.target.incarnation !== element.incarnation) throw fault('invalid_handler_change', [element.id]);
|
||||
} else if(command.type==='connection.endpoints'){
|
||||
if(element.structure!=='connection'||!options.registry.supports(element,'connection.edit'))throw fault('unsupported_capability',[element.id]);
|
||||
changes=[];
|
||||
for(const [field,endpoint]of [['source',command.source],['target',command.destination]] as const)if(endpoint&&!equal(element[field],endpoint))changes=[...changes,{kind:'field',target:command.target,field,before:element[field],after:clone(endpoint)}];
|
||||
if(changes.length)changes=[...changes,...repairConnectionOwners(document,applyChanges(document,changes))];
|
||||
} else if (command.type === 'element.remove') {
|
||||
changes = [{ kind: 'element', id: element.id, before: element, after: null }];
|
||||
for (const edge of document.elements.values()) if (edge.structure === 'connection' && (edge.source.elementId === element.id || edge.target.elementId === element.id)) changes = [...changes, { kind: 'element', id: edge.id, before: edge, after: null }];
|
||||
changes = removalChanges(document,command.target);
|
||||
} else if (command.type === 'element.move') {
|
||||
if (!options.registry.supports(element, 'geometry.move')) throw fault('unsupported_capability', [element.id]);
|
||||
changes = [{ kind: 'field', target: command.target, field: 'ownerCanvasId', before: element.ownerCanvasId, after: command.canvasId }];
|
||||
if (command.geometry && element.structure === 'node') changes = [...changes, { kind: 'field', target: command.target, field: 'geometry', before: element.geometry, after: command.geometry }];
|
||||
for (const canvas of document.canvases.values()) if (canvas.parent?.entryElementId === element.id) changes = [...changes, { kind: 'canvas', id: canvas.id, before: canvas, after: { ...canvas, parent: { ...canvas.parent, canvasId: command.canvasId } } }];
|
||||
const moved = applyChanges(document, changes);
|
||||
for (const edge of moved.elements.values()) if (edge.structure === 'connection') {
|
||||
const source = moved.elements.get(edge.source.elementId), target = moved.elements.get(edge.target.elementId);
|
||||
if (!source || !target) throw fault('invalid_endpoint', [edge.id]);
|
||||
const owner = commonCanvas(moved, source.ownerCanvasId, target.ownerCanvasId);
|
||||
if (owner !== edge.ownerCanvasId) changes = [...changes, { kind: 'field', target: { id: edge.id, incarnation: edge.incarnation }, field: 'ownerCanvasId', before: edge.ownerCanvasId, after: owner }];
|
||||
if(element.structure==='node'){
|
||||
if([...document.canvases.values()].some(canvas=>canvas.parent?.entryElementId===element.id)&&canvasTransform(document,element.ownerCanvasId).scale!==canvasTransform(document,command.canvasId).scale)throw fault('container_scale_conversion_required',[element.id]);
|
||||
const geometry=command.geometry??(element.ownerCanvasId===command.canvasId?element.geometry:convertGeometry(element.geometry,canvasTransform(document,element.ownerCanvasId),canvasTransform(document,command.canvasId)));
|
||||
if(!equal(geometry,element.geometry))changes=[...changes,{kind:'field',target:command.target,field:'geometry',before:element.geometry,after:geometry}];
|
||||
}
|
||||
for (const canvas of document.canvases.values()) if (canvas.parent?.entryElementId === element.id) changes = [...changes, { kind: 'canvas', id: canvas.id, before: canvas, after: { ...canvas, parent: { ...canvas.parent, canvasId: command.canvasId } } }];
|
||||
changes=[...changes,...repairConnectionOwners(document,applyChanges(document,changes))];
|
||||
} else {
|
||||
const output: Change[] = [];
|
||||
const run = (capability: Capability, patch: unknown) => {
|
||||
@@ -232,19 +238,26 @@ export function createEditor<Runtime = undefined>(options: EditorOptions<Runtime
|
||||
const entry = state.history.entries.get(id)!;
|
||||
const allowed = options.traversalPolicy?.(entry, direction);
|
||||
if (allowed && !allowed.enabled) throw fault(allowed.code, allowed.targets);
|
||||
let changes = direction === 'undo' ? invert(entry.changes) : entry.changes;
|
||||
const generations = new Map<Id, number>();
|
||||
changes = changes.map(change => {
|
||||
if (change.kind === 'element' && !change.before && change.after) {
|
||||
const dead = state.document.tombstones.get(change.id);
|
||||
if (dead !== undefined && change.after.incarnation <= dead) { generations.set(change.id, dead + 1); return freeze({ ...change, after: { ...change.after, incarnation: dead + 1 } }); }
|
||||
}
|
||||
const planned=direction==='undo'?invert(entry.changes):entry.changes;
|
||||
const generations=new Map<Id,Map<number,number>>();
|
||||
const incarnation=(id:Id,value:number)=>{const mapping=generations.get(id);while(mapping?.has(value))value=mapping.get(value)!;return value;};
|
||||
const remap=(change:Change):Change=>{
|
||||
if(change.kind==='field'){const next=incarnation(change.target.id,change.target.incarnation);return next===change.target.incarnation?change:freeze({...change,target:{...change.target,incarnation:next}});}
|
||||
if(change.kind==='element'){const record=(value:ElementRecord|null)=>value?{...value,incarnation:incarnation(value.id,value.incarnation)}:null;return freeze({...change,before:record(change.before),after:record(change.after)});}
|
||||
return change;
|
||||
}).map(change => change.kind === 'field' && generations.has(change.target.id) ? freeze({ ...change, target: { ...change.target, incarnation: generations.get(change.target.id)! } }) : change);
|
||||
const document = applyChanges(state.document, changes);
|
||||
validateDocument(document, options.registry);
|
||||
const entries = new Map(state.history.entries);
|
||||
entries.set(id, freeze({ ...entry, changes: direction === 'undo' ? invert(changes) : changes }));
|
||||
};
|
||||
let document=state.document;const changes:Change[]=[];
|
||||
for(const original of planned){
|
||||
let change=remap(original);
|
||||
if(change.kind==='element'&&!change.before&&change.after){const dead=document.tombstones.get(change.id);if(dead!==undefined&&change.after.incarnation<=dead){const mapping=generations.get(change.id)??new Map<number,number>();mapping.set(change.after.incarnation,dead+1);generations.set(change.id,mapping);change=remap(change);}}
|
||||
document=applyChanges(document,[change]);changes.push(change);
|
||||
}
|
||||
validateDocument(document,options.registry);
|
||||
const entries=new Map(state.history.entries);
|
||||
// Later redo entries belong to the lifecycle restored by this explicit history action.
|
||||
// Frozen operations retain their original identities and incarnations.
|
||||
if(generations.size)for(const [otherId,other]of entries)if(otherId!==id)entries.set(otherId,freeze({...other,changes:other.changes.map(remap)}));
|
||||
entries.set(id,freeze({...entry,changes:direction==='undo'?invert(changes):changes}));
|
||||
const history: HistoryState = Object.freeze({ undo: Object.freeze(direction === 'undo' ? state.history.undo.slice(0, -1) : [...state.history.undo, id]), redo: Object.freeze(direction === 'redo' ? state.history.redo.slice(0, -1) : [...state.history.redo, id]), entries: readMap(entries) });
|
||||
return publish(document, changes, direction, history, id);
|
||||
} catch (error) { return reject(error); }
|
||||
|
||||
Reference in New Issue
Block a user