Preserve visual units across scaled canvas and container moves
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import type { Document, Geometry, Id, Json, Point } from './types.ts';
|
||||
import { ancestors } from './document.ts';
|
||||
import { equal, fault } from './values.ts';
|
||||
import { fault } from './values.ts';
|
||||
import { closeNumber, closeScale, unitScale } from './units.ts';
|
||||
import { connectionPathValid } from './features.ts';
|
||||
export type Transform = { x: number; y: number; scale: number; rotation: number };
|
||||
export const sameTransform=(a:Transform,b:Transform)=>closeNumber(a.x,b.x)&&closeNumber(a.y,b.y)&&closeScale(a.scale,b.scale)&&Math.abs(((a.rotation-b.rotation+540)%360)-180)<1e-9;
|
||||
const origin: Transform = { x: 0, y: 0, scale: 1, rotation: 0 };
|
||||
const rotate = (point: Point, angle: number): Point => { const a = angle * Math.PI / 180; return { x: point.x * Math.cos(a) - point.y * Math.sin(a), y: point.x * Math.sin(a) + point.y * Math.cos(a) }; };
|
||||
export function toWorld(transform: Transform, point: Point): Point { const value = rotate(point, transform.rotation); return { x: transform.x + value.x * transform.scale, y: transform.y + value.y * transform.scale }; }
|
||||
@@ -23,10 +25,10 @@ export function transformPath(path: Json, transform: Transform, inverse = false)
|
||||
|
||||
/** Format-1 child content origin: the same inset, scale and rotation used by renderer projection. */
|
||||
export function childCanvasTransform(parent:Transform,geometry:Geometry,presentation:Json|undefined):Transform {
|
||||
const data=object(presentation),scale=typeof data.contentScale==='number'&&data.contentScale>0?data.contentScale:1;
|
||||
const offset=rotate({x:24-geometry.width/2,y:36-geometry.height/2},geometry.rotation);
|
||||
const data=object(presentation),units=unitScale(presentation),scale=(typeof data.contentScale==='number'&&data.contentScale>0?data.contentScale:1)*units;
|
||||
const offset=rotate({x:24*units-geometry.width/2,y:36*units-geometry.height/2},geometry.rotation);
|
||||
const start=toWorld(parent,{x:geometry.x+geometry.width/2+offset.x,y:geometry.y+geometry.height/2+offset.y});
|
||||
return{...start,scale:parent.scale*scale,rotation:parent.rotation+geometry.rotation};
|
||||
return{...start,scale:parent.scale*scale,rotation:((parent.rotation+geometry.rotation)%360+360)%360};
|
||||
}
|
||||
export function canvasTransform(document:Document,canvasId:Id):Transform {
|
||||
let result:Transform={...origin};
|
||||
@@ -38,12 +40,12 @@ export function canvasTransform(document:Document,canvasId:Id):Transform {
|
||||
return result;
|
||||
}
|
||||
export function convertGeometry(geometry:Geometry,from:Transform,to:Transform):Geometry {
|
||||
if(equal(from,to))return geometry;
|
||||
if(sameTransform(from,to))return geometry;
|
||||
const center=toLocal(to,toWorld(from,{x:geometry.x+geometry.width/2,y:geometry.y+geometry.height/2})),ratio=from.scale/to.scale,width=geometry.width*ratio,height=geometry.height*ratio;
|
||||
return{x:center.x-width/2,y:center.y-height/2,width,height,rotation:((geometry.rotation+from.rotation-to.rotation)%360+360)%360};
|
||||
}
|
||||
export function convertPath(path:Json,from:Transform,to:Transform):Json {
|
||||
if(path===null||equal(from,to))return path;
|
||||
if(path===null||sameTransform(from,to))return path;
|
||||
if(!connectionPathValid(path))throw fault('unsupported_coordinate_path');
|
||||
return transformPath(transformPath(path,from),to,true);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { applyChanges, createDocument, invert, targetElement, validateDocument }
|
||||
import { clone, equal, fault, freeze, readMap, reject, validJson } from './values.ts';
|
||||
|
||||
import { canvasTransform, convertGeometry } from './coordinates.ts';
|
||||
import { unitScale, withUnitScale } from './units.ts';
|
||||
import { removalChanges, repairConnectionOwners } from './structure.ts';
|
||||
|
||||
export type EditorOptions<Runtime = undefined> = {
|
||||
@@ -74,7 +75,8 @@ export function createEditor<Runtime = undefined>(options: EditorOptions<Runtime
|
||||
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(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 source=canvasTransform(document,element.ownerCanvasId),destination=canvasTransform(document,command.canvasId),presentation=withUnitScale(element.data.presentation,unitScale(element.data.presentation)*source.scale/destination.scale);
|
||||
if(!equal(presentation,element.data.presentation))changes=[...changes,{kind:'field',target:command.target,field:'data.presentation',before:element.data.presentation??null,beforePresent:Object.hasOwn(element.data,'presentation'),after:presentation??null,afterPresent:presentation!==undefined}];
|
||||
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}];
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { CapabilityHandler, ComponentDefinition, Data, FieldChange, Geometry, Json } from './types.ts';
|
||||
import { defineComponent, defineFeature } from './registry.ts';
|
||||
import { presentationUnitsValid, unitScale } from './units.ts';
|
||||
import { equal, fault, validJson } from './values.ts';
|
||||
|
||||
const geometryHandler = (allowed: readonly (keyof Geometry)[], constrain?: (geometry: Geometry) => Geometry): CapabilityHandler => ({ element, input }) => {
|
||||
@@ -18,7 +19,7 @@ export const resizable = (inputOptions: { min?: number; max?: number; aspect?: b
|
||||
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;
|
||||
const units=unitScale(element.data.presentation),min=(options.min??1)*units,max=(options.max??100000)*units;
|
||||
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;
|
||||
@@ -41,7 +42,7 @@ export const editable = () => defineFeature('content.edit', {
|
||||
});
|
||||
},
|
||||
});
|
||||
const textData = (data: Data) => validJson(data) && typeof data.title === 'string' && typeof data.text === 'string';
|
||||
const textData = (data: Data) => presentationUnitsValid(data.presentation) && 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,
|
||||
|
||||
@@ -7,3 +7,4 @@ export * from './editor.ts';
|
||||
export * from './gestures.ts';
|
||||
export * from './coordinates.ts';
|
||||
export * from './structure.ts';
|
||||
export * from './units.ts';
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { Json } from './types.ts';
|
||||
import { fault } from './values.ts';
|
||||
|
||||
export type PresentationUnits = { readonly unitScale?: number };
|
||||
|
||||
/** Reserved presentation metadata, independent of canvas ownership. Absence means one visual unit per owner unit. */
|
||||
export function presentationUnitsValid(value:Json|undefined):boolean {
|
||||
if(!value||typeof value!=='object'||Array.isArray(value))return true;
|
||||
return value.unitScale===undefined||typeof value.unitScale==='number'&&Number.isFinite(value.unitScale)&&value.unitScale>0;
|
||||
}
|
||||
export function unitScale(value:Json|undefined):number {
|
||||
if(!presentationUnitsValid(value))throw fault('invalid_presentation_units');
|
||||
return value&&typeof value==='object'&&!Array.isArray(value)&&typeof value.unitScale==='number'?value.unitScale:1;
|
||||
}
|
||||
export const closeNumber=(a:number,b:number)=>Math.abs(a-b)<=Number.EPSILON*64*Math.max(1,Math.abs(a),Math.abs(b));
|
||||
export const closeScale=(a:number,b:number)=>Math.abs(a-b)<=Number.EPSILON*64*Math.max(Number.MIN_VALUE,Math.abs(a),Math.abs(b));
|
||||
export function presentationRecord(value:Json|undefined):Readonly<Record<string,Json>> {
|
||||
if(value===undefined||value===null)return{};
|
||||
if(typeof value!=='object'||Array.isArray(value))throw fault('unsupported_presentation_units');
|
||||
unitScale(value);return value;
|
||||
}
|
||||
/** Normalize only the framework-owned unit field; all other presentation properties survive. */
|
||||
export function withUnitScale(value:Json|undefined,scale:number):Json|undefined {
|
||||
if(!Number.isFinite(scale)||scale<=0)throw fault('invalid_presentation_units');
|
||||
const normalized=closeNumber(scale,1)?1:scale;
|
||||
if(closeScale(unitScale(value),normalized))return value;
|
||||
const fields=new Map(Object.entries(presentationRecord(value)));
|
||||
if(normalized===1)fields.delete('unitScale');else fields.set('unitScale',normalized);
|
||||
return fields.size?Object.fromEntries(fields):undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user