Split GraphCanvas (1076 -> 831 lines) into EdgeLayer and graphMenus; the port and marker menus were the same list and are now one function. Value specs of the inspector and the list tab share valueSpecs.ts, and the remaining copies (compound commands, type glyphs, typed asset lookup, name rule, play/editor world setup, marquee) are merged. Four copies had already drifted apart; merging them fixes: - "Disconnect N links" in the port menu is one undo step, not N - "+ Add collider" is hidden on a placed container instance, as in the hierarchy menu, instead of always failing - a bool property coerced from the text "false" becomes false - a port command on an unknown node type reports instead of crashing Comment-only edits leave the bundle byte-identical. README corrected in 35 places (step limit, jump-run clock, suite duration, 409 body, ...). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
116 lines
4.4 KiB
TypeScript
116 lines
4.4 KiB
TypeScript
import {
|
||
coerceFieldValue,
|
||
type PropertyDef,
|
||
type StructDef,
|
||
type StructFieldDef,
|
||
structOf,
|
||
} from '../../core/properties/PropertySchema';
|
||
import type { Command } from '../../core/commands/Command';
|
||
import type { PropertyValue } from '../../core/model/Properties';
|
||
import type { EditorState, ListSlot } from '../state';
|
||
import type { FieldHandlers } from './fieldFactory';
|
||
|
||
/**
|
||
* The `openList` half of a row's handlers, for the sections that know where their value lives.
|
||
* Empty for every other kind, so spreading it into the handlers is always safe.
|
||
*/
|
||
export function listOpener(
|
||
state: EditorState,
|
||
def: PropertyDef,
|
||
slot: () => ListSlot,
|
||
): Pick<FieldHandlers, 'openList'> {
|
||
return def.kind === 'list' ? { openList: () => state.openList(slot()) } : {};
|
||
}
|
||
|
||
/**
|
||
* The `structFieldHandlers` half of a row's handlers. Each field writes the whole bundle, so it
|
||
* reads the bundle at that moment rather than from a snapshot: sharing one would make the second
|
||
* field edited undo the first.
|
||
*/
|
||
export function structFields<Target>(
|
||
host: { state: EditorState },
|
||
def: PropertyDef,
|
||
spec: PreviewedValue<Target>,
|
||
fieldSlot?: (field: string) => ListSlot,
|
||
): Pick<FieldHandlers, 'structFieldHandlers'> {
|
||
return def.kind === 'struct' ? { structFieldHandlers: structFieldsOf(host, def, spec, fieldSlot) } : {};
|
||
}
|
||
|
||
/** `fieldSlot` is what a `list` field needs: its own address, so the List tab can be opened on it. */
|
||
export function structFieldsOf<Target>(
|
||
host: { state: EditorState },
|
||
def: StructDef,
|
||
spec: PreviewedValue<Target>,
|
||
fieldSlot?: (field: string) => ListSlot,
|
||
): (field: StructFieldDef) => FieldHandlers {
|
||
return (field) => ({
|
||
...previewingHandlers<Target>(host, {
|
||
...spec,
|
||
toModel: (input, target) => ({
|
||
...structOf(def.fields, (spec.current ?? spec.read)(target)),
|
||
[field.key]: coerceFieldValue(input, field),
|
||
}),
|
||
}),
|
||
...(field.kind === 'list' && fieldSlot
|
||
? { openList: () => host.state.openList(fieldSlot(field.key)) }
|
||
: {}),
|
||
});
|
||
}
|
||
|
||
/** Where an inspector row finds the value it edits, and what committing it means. */
|
||
export interface PreviewedValue<Target, Model = PropertyValue> {
|
||
/** The model object holding the value, or undefined when the selection is gone – then the row does nothing. */
|
||
target: () => Target | undefined;
|
||
/** The stored value; undefined means the key is not set on the target at all. */
|
||
read: (target: Target) => Model | undefined;
|
||
/**
|
||
* The value the row displays: the stored one resolved against what it inherits. Only a compound
|
||
* value needs it – writing one struct field rebuilds the whole bundle, and an override starts out
|
||
* unset, so seeding from `read` would drop every untouched field to empty. Defaults to `read`.
|
||
*/
|
||
current?: (target: Target) => Model | undefined;
|
||
write: (target: Target, value: Model) => void;
|
||
/** Unsets the key again; needed wherever `read` can return undefined. */
|
||
remove?: (target: Target) => void;
|
||
/** The widget value as the model stores it (default: unchanged). Called on the target as it is at that moment. */
|
||
toModel?: (input: PropertyValue, target: Target) => Model;
|
||
commit: (value: Model) => Command;
|
||
}
|
||
|
||
/**
|
||
* Row handlers that preview into the model and restore the pre-preview value (or its absence, via
|
||
* `remove`) before committing, since a command records whatever it finds at execute time as its undo target.
|
||
*/
|
||
export function previewingHandlers<Target, Model = PropertyValue>(
|
||
host: { state: EditorState },
|
||
spec: PreviewedValue<Target, Model>,
|
||
): FieldHandlers {
|
||
const toModel = spec.toModel ?? ((input: PropertyValue) => input as unknown as Model);
|
||
let previewed = false;
|
||
let before: Model | undefined;
|
||
return {
|
||
onInput: (input) => {
|
||
const target = spec.target();
|
||
if (!target) return;
|
||
if (!previewed) {
|
||
previewed = true;
|
||
before = spec.read(target);
|
||
}
|
||
spec.write(target, toModel(input, target));
|
||
host.state.preview();
|
||
},
|
||
onChange: (input) => {
|
||
const target = spec.target();
|
||
if (!target) return;
|
||
if (previewed) {
|
||
if (before === undefined) spec.remove?.(target);
|
||
else spec.write(target, before);
|
||
}
|
||
previewed = false;
|
||
before = undefined;
|
||
// Built after the restore: `toModel` may fill the rest of a compound value from the target.
|
||
host.state.execute(spec.commit(toModel(input, target)));
|
||
},
|
||
};
|
||
}
|