Add renderer-independent canvas model, transactions and view sessions
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import type { CapabilityHandler, ComponentDefinition, Data, FieldChange, Geometry } from './types.ts';
|
||||
import { defineComponent, defineFeature } from './registry.ts';
|
||||
import { equal, fault, validJson } from './values.ts';
|
||||
|
||||
const geometryHandler = (allowed: readonly (keyof Geometry)[], constrain?: (geometry: Geometry) => Geometry): CapabilityHandler => ({ element, input }) => {
|
||||
if (element.structure !== 'node') throw fault('unsupported_capability', [element.id]);
|
||||
const patch = input.patch as Partial<Geometry>;
|
||||
if (!patch || Object.values(patch).some(value => typeof value !== 'number' || !Number.isFinite(value)) || Object.keys(patch).some(key => !allowed.includes(key as keyof Geometry))) throw fault('invalid_geometry_patch', [element.id]);
|
||||
const after = constrain ? constrain({ ...element.geometry, ...patch }) : { ...element.geometry, ...patch };
|
||||
return equal(element.geometry, after) ? [] : [{ kind: 'field', target: { id: element.id, incarnation: element.incarnation }, field: 'geometry', before: element.geometry, after }];
|
||||
};
|
||||
export const movable = () => defineFeature('geometry.move', { 'geometry.move': geometryHandler(['x', 'y']) });
|
||||
export const resizable = (inputOptions: { min?: number; max?: number; aspect?: boolean } = {}) => {
|
||||
const options = Object.freeze({ ...inputOptions });
|
||||
if (options.min !== undefined && (!Number.isFinite(options.min) || options.min <= 0) || options.max !== undefined && (!Number.isFinite(options.max) || options.max < (options.min ?? 1))) throw fault('invalid_resize_constraint');
|
||||
return defineFeature('geometry.resize', {
|
||||
'geometry.resize': ({ element, input }) => {
|
||||
if (element.structure !== 'node') throw fault('unsupported_capability', [element.id]);
|
||||
const patch = input.patch as Partial<Geometry>;
|
||||
return geometryHandler(['width', 'height'], geometry => {
|
||||
const min = options.min ?? 1, max = options.max ?? 100000;
|
||||
let width = Math.min(max, Math.max(min, geometry.width)), height = Math.min(max, Math.max(min, geometry.height));
|
||||
if (options.aspect) {
|
||||
const ratio = element.geometry.width / element.geometry.height;
|
||||
if (patch.width !== undefined) height = width / ratio; else width = height * ratio;
|
||||
if (width < min || height < min || width > max || height > max) throw fault('aspect_constraint', [element.id]);
|
||||
}
|
||||
return { ...geometry, width, height };
|
||||
})({ element, input });
|
||||
},
|
||||
});
|
||||
};
|
||||
export const rotatable = () => defineFeature('geometry.rotate', { 'geometry.rotate': geometryHandler(['rotation']) });
|
||||
export const editable = () => defineFeature('content.edit', {
|
||||
'content.edit': ({ element, input }) => {
|
||||
const patch = input.patch as Data;
|
||||
if (!patch || !validJson(patch) || Array.isArray(patch)) throw fault('invalid_data_patch', [element.id]);
|
||||
return Object.entries(patch).flatMap(([key, after]): FieldChange[] => {
|
||||
const before = element.data[key] ?? null;
|
||||
return Object.hasOwn(element.data, key) && equal(before, after) ? [] : [{ kind: 'field', target: { id: element.id, incarnation: element.incarnation }, field: `data.${key}`, before, after, beforePresent: Object.hasOwn(element.data, key), afterPresent: true }];
|
||||
});
|
||||
},
|
||||
});
|
||||
const textData = (data: Data) => validJson(data) && typeof data.title === 'string' && typeof data.text === 'string';
|
||||
export function builtinComponents(): readonly ComponentDefinition[] {
|
||||
return ['text', 'image', 'script', 'plain-text', 'plain-image', 'icon', 'frame'].map(id => defineComponent({
|
||||
id, dataVersion: 1, structure: 'node', validate: textData,
|
||||
features: [movable(), resizable(), ...(id === 'frame' ? [] : [rotatable()]), editable()],
|
||||
resourceFields: ['image', 'plain-image'].includes(id) ? ['asset'] : [],
|
||||
})).concat([defineComponent({ id: 'connection', dataVersion: 1, structure: 'connection', validate: data => validJson(data), features: [editable()] })]);
|
||||
}
|
||||
Reference in New Issue
Block a user