|
|
|
@@ -1,31 +1,38 @@
|
|
|
|
|
import type { Editor } from './editor.ts';
|
|
|
|
|
import type { Availability, Capability, ElementRecord, Geometry, Id, Result, Target } from './types.ts';
|
|
|
|
|
import type { Availability, Capability, ElementRecord, Geometry, Id, Json, Result, Target } from './types.ts';
|
|
|
|
|
import { geometryValid, targetElement } from './document.ts';
|
|
|
|
|
import { equal, fault, freeze, readMap, readSet, reject } from './values.ts';
|
|
|
|
|
import { clone, equal, fault, freeze, readMap, readSet, reject, validJson } from './values.ts';
|
|
|
|
|
|
|
|
|
|
export type ViewState = { readonly id: Id; readonly selection: ReturnType<typeof readSet<Id>>; readonly viewport: { readonly x: number; readonly y: number; readonly zoom: number }; readonly tool: 'select' | 'pan'; readonly modes: ReadonlyMap<Id, string> };
|
|
|
|
|
export type GestureState = { readonly viewId: Id; readonly targets: readonly Target[]; readonly preview: ReadonlyMap<Id, Geometry>; readonly phase: 'active' | 'committing' };
|
|
|
|
|
export type SessionSnapshot = { readonly views: ReadonlyMap<Id, ViewState>; readonly gesture: GestureState | null; readonly lastCancelReason: string | null };
|
|
|
|
|
export type ViewState = { readonly id: Id; readonly canvasId: Id; readonly selection: ReturnType<typeof readSet<Id>>; readonly viewport: { readonly x: number; readonly y: number; readonly zoom: number }; readonly tool: 'select' | 'pan'; readonly modes: ReadonlyMap<Id, string> };
|
|
|
|
|
export type GestureState = { readonly token: number; readonly viewId: Id; readonly targets: readonly Target[]; readonly preview: ReadonlyMap<Id, Geometry>; readonly phase: 'active' | 'committing' };
|
|
|
|
|
export type ConnectionGesture = { readonly token: number; readonly viewId: Id; readonly target: Target; readonly path: Json; readonly phase: 'active' | 'committing' };
|
|
|
|
|
export type SessionSnapshot = { readonly views: ReadonlyMap<Id, ViewState>; readonly gesture: GestureState | null; readonly connectionGesture: ConnectionGesture | null; readonly lastCancelReason: string | null };
|
|
|
|
|
|
|
|
|
|
/** Transient interaction state belongs to views; no pointer event enters the ledger. */
|
|
|
|
|
export function createSessions(editor: Editor) {
|
|
|
|
|
let snapshot: SessionSnapshot = Object.freeze({ views: readMap(new Map<Id, ViewState>()), gesture: null, lastCancelReason: null });
|
|
|
|
|
let baseline = new Map<Id, ElementRecord>(), disposed = false;
|
|
|
|
|
let snapshot: SessionSnapshot = Object.freeze({ views: readMap(new Map<Id, ViewState>()), gesture: null, connectionGesture: null, lastCancelReason: null });
|
|
|
|
|
let baseline = new Map<Id, ElementRecord>(), connectionBaseline: ElementRecord | undefined, disposed = false, nextGestureToken = 0;
|
|
|
|
|
const listeners = new Set<() => void>();
|
|
|
|
|
const notify = () => { for (const listener of [...listeners]) try { listener(); } catch { /* A renderer cannot invalidate model commits. */ } };
|
|
|
|
|
function setView(view: ViewState) { const views = new Map(snapshot.views); views.set(view.id, view); snapshot = Object.freeze({ ...snapshot, views: readMap(views) }); notify(); }
|
|
|
|
|
function cancel(reason = 'canceled') {
|
|
|
|
|
if (!snapshot.gesture) return;
|
|
|
|
|
baseline.clear(); snapshot = Object.freeze({ ...snapshot, gesture: null, lastCancelReason: reason }); notify();
|
|
|
|
|
if (!snapshot.gesture && !snapshot.connectionGesture) return;
|
|
|
|
|
baseline.clear(); connectionBaseline = undefined; snapshot = Object.freeze({ ...snapshot, gesture: null, connectionGesture: null, lastCancelReason: reason }); notify();
|
|
|
|
|
}
|
|
|
|
|
const detachEditor = editor.subscribe(() => {
|
|
|
|
|
const document = editor.getSnapshot().document;
|
|
|
|
|
const views = new Map(snapshot.views); let changed = false;
|
|
|
|
|
for (const [id, view] of views) {
|
|
|
|
|
const canvasMissing = !document.canvases.has(view.canvasId);
|
|
|
|
|
const selection = [...view.selection].filter(target => document.elements.has(target)), modes = new Map([...view.modes].filter(([target]) => document.elements.has(target)));
|
|
|
|
|
if (selection.length !== view.selection.size || modes.size !== view.modes.size) { views.set(id, freeze({ ...view, selection: readSet(selection), modes: readMap(modes) })); changed = true; }
|
|
|
|
|
if (canvasMissing || selection.length !== view.selection.size || modes.size !== view.modes.size) { views.set(id, freeze({ ...view, canvasId: canvasMissing ? [...document.canvases.values()].find(canvas => !canvas.parent)!.id : view.canvasId, selection: readSet(canvasMissing ? [] : selection), modes: readMap(canvasMissing ? new Map() : modes) })); changed = true; }
|
|
|
|
|
}
|
|
|
|
|
if (changed) { snapshot = Object.freeze({ ...snapshot, views: readMap(views) }); notify(); }
|
|
|
|
|
if (snapshot.connectionGesture?.phase === 'active') {
|
|
|
|
|
const current = document.elements.get(snapshot.connectionGesture.target.id), before = connectionBaseline;
|
|
|
|
|
if (!current || current.structure !== 'connection' || before?.structure !== 'connection' || current.incarnation !== before.incarnation || current.ownerCanvasId !== before.ownerCanvasId || !equal(current.path, before.path)) cancel('target_changed');
|
|
|
|
|
else if (!editor.can({ type: 'connection.path', target: snapshot.connectionGesture.target, path: current.path }).enabled) cancel('permission_changed');
|
|
|
|
|
}
|
|
|
|
|
if (snapshot.gesture?.phase !== 'active') return;
|
|
|
|
|
for (const target of snapshot.gesture.targets) {
|
|
|
|
|
const before = baseline.get(target.id), current = document.elements.get(target.id);
|
|
|
|
@@ -64,10 +71,17 @@ export function createSessions(editor: Editor) {
|
|
|
|
|
}
|
|
|
|
|
return Object.freeze({
|
|
|
|
|
getSnapshot: () => snapshot,
|
|
|
|
|
attach(id: Id) {
|
|
|
|
|
attach(id: Id, canvasId = [...editor.getSnapshot().document.canvases.values()].find(canvas => !canvas.parent)!.id) {
|
|
|
|
|
if (disposed || snapshot.views.has(id)) throw fault('duplicate_view', [id]);
|
|
|
|
|
setView(freeze({ id, selection: readSet<Id>([]), viewport: { x: 0, y: 0, zoom: 1 }, tool: 'select', modes: readMap(new Map<Id, string>()) }));
|
|
|
|
|
return () => { if (snapshot.gesture?.viewId === id) cancel('view_detached'); const views = new Map(snapshot.views); views.delete(id); snapshot = Object.freeze({ ...snapshot, views: readMap(views) }); notify(); };
|
|
|
|
|
if (!editor.getSnapshot().document.canvases.has(canvasId)) throw fault('canvas_missing', [canvasId]);
|
|
|
|
|
setView(freeze({ id, canvasId, selection: readSet<Id>([]), viewport: { x: 0, y: 0, zoom: 1 }, tool: 'select', modes: readMap(new Map<Id, string>()) }));
|
|
|
|
|
return () => { if ((snapshot.gesture?.viewId === id || snapshot.connectionGesture?.viewId === id)) cancel('view_detached'); const views = new Map(snapshot.views); views.delete(id); snapshot = Object.freeze({ ...snapshot, views: readMap(views) }); notify(); };
|
|
|
|
|
},
|
|
|
|
|
canvas(id: Id, canvasId: Id) {
|
|
|
|
|
const current = view(id);
|
|
|
|
|
if (!editor.getSnapshot().document.canvases.has(canvasId)) throw fault('canvas_missing', [canvasId]);
|
|
|
|
|
if ((snapshot.gesture?.viewId === id || snapshot.connectionGesture?.viewId === id)) cancel('canvas_changed');
|
|
|
|
|
setView(freeze({ ...current, canvasId, selection: readSet<Id>([]), modes: readMap(new Map<Id, string>()) }));
|
|
|
|
|
},
|
|
|
|
|
select(id: Id, ids: readonly Id[]) {
|
|
|
|
|
const current = view(id), document = editor.getSnapshot().document;
|
|
|
|
@@ -83,13 +97,45 @@ export function createSessions(editor: Editor) {
|
|
|
|
|
begin(viewId: Id, targets: readonly Target[]): Availability {
|
|
|
|
|
try {
|
|
|
|
|
view(viewId);
|
|
|
|
|
if (snapshot.gesture || !targets.length || new Set(targets.map(target => target.id)).size !== targets.length) throw fault('gesture_busy');
|
|
|
|
|
if (snapshot.gesture || snapshot.connectionGesture || !targets.length || new Set(targets.map(target => target.id)).size !== targets.length) throw fault('gesture_busy');
|
|
|
|
|
baseline = new Map(targets.map(target => { const element = targetElement(editor.getSnapshot().document, target); if (element.structure !== 'node' || editor.registry.resolve(element) !== 'known') throw fault('unsupported_component', [target.id]); return [target.id, element]; }));
|
|
|
|
|
for (const target of targets) { const allowed = editor.can({ type: 'element.geometry', target, patch: {} }); if (!allowed.enabled) throw fault(allowed.code, allowed.targets); }
|
|
|
|
|
snapshot = Object.freeze({ ...snapshot, lastCancelReason: null, gesture: freeze({ viewId, targets: targets.map(target => ({ ...target })), preview: readMap(new Map<Id, Geometry>()), phase: 'active' as const }) }); notify();
|
|
|
|
|
snapshot = Object.freeze({ ...snapshot, lastCancelReason: null, gesture: freeze({ token: ++nextGestureToken, viewId, targets: targets.map(target => ({ ...target })), preview: readMap(new Map<Id, Geometry>()), phase: 'active' as const }) }); notify();
|
|
|
|
|
return { enabled: true };
|
|
|
|
|
} catch (error) { const result = reject(error); return { enabled: false, code: result.code, targets: result.targets }; }
|
|
|
|
|
},
|
|
|
|
|
beginConnection(viewId: Id, target: Target): Availability {
|
|
|
|
|
try {
|
|
|
|
|
view(viewId);
|
|
|
|
|
if (snapshot.gesture || snapshot.connectionGesture) throw fault('gesture_busy');
|
|
|
|
|
const element = targetElement(editor.getSnapshot().document, target);
|
|
|
|
|
if (element.structure !== 'connection') throw fault('unsupported_capability', [target.id]);
|
|
|
|
|
const allowed = editor.can({ type: 'connection.path', target, path: element.path });
|
|
|
|
|
if (!allowed.enabled) throw fault(allowed.code, allowed.targets);
|
|
|
|
|
connectionBaseline = element;
|
|
|
|
|
snapshot = Object.freeze({ ...snapshot, lastCancelReason: null, connectionGesture: freeze({ token: ++nextGestureToken, viewId, target: { ...target }, path: element.path, phase: 'active' as const }) }); notify();
|
|
|
|
|
return { enabled: true };
|
|
|
|
|
} catch (error) { const result = reject(error); return { enabled: false, code: result.code, targets: result.targets }; }
|
|
|
|
|
},
|
|
|
|
|
updateConnection(viewId: Id, path: Json): Availability {
|
|
|
|
|
try {
|
|
|
|
|
view(viewId);
|
|
|
|
|
if (snapshot.connectionGesture?.viewId !== viewId) throw fault('gesture_not_owned');
|
|
|
|
|
if (!validJson(path)) throw fault('invalid_connection_path');
|
|
|
|
|
snapshot = Object.freeze({ ...snapshot, connectionGesture: freeze({ ...snapshot.connectionGesture, path: clone(path) }) }); notify();
|
|
|
|
|
return { enabled: true };
|
|
|
|
|
} catch (error) { const result = reject(error); return { enabled: false, code: result.code, targets: result.targets }; }
|
|
|
|
|
},
|
|
|
|
|
commitConnection(viewId: Id): Result {
|
|
|
|
|
try {
|
|
|
|
|
view(viewId);
|
|
|
|
|
const gesture = snapshot.connectionGesture;
|
|
|
|
|
if (!gesture || gesture.viewId !== viewId) throw fault('gesture_not_owned');
|
|
|
|
|
snapshot = Object.freeze({ ...snapshot, connectionGesture: freeze({ ...gesture, phase: 'committing' as const }) });
|
|
|
|
|
const result = editor.dispatch({ type: 'connection.path', target: gesture.target, path: gesture.path }, { label: 'Edit connection' });
|
|
|
|
|
cancel(result.status === 'rejected' ? result.code : 'committed'); return result;
|
|
|
|
|
} catch (error) { cancel('commit_failed'); return reject(error); }
|
|
|
|
|
},
|
|
|
|
|
update,
|
|
|
|
|
commit(viewId: Id): Result {
|
|
|
|
|
try {
|
|
|
|
|