docs(canvas): explain gestures, coordinate spaces and creation planning

This commit is contained in:
2026-11-11 23:46:22 +08:00
parent afa30d4b35
commit 62a0755350
9 changed files with 158 additions and 0 deletions
+21
View File
@@ -1,8 +1,14 @@
/**
* 图片元信息异步到达后,把默认尺寸修正合回原创建历史的纯规划器。
* 同时修正后续历史的 before,使 undo/redo 保持可回放;不会改写已经冻结的请求 body。
* 持久授权、Root 版本检查和发布由 coordinator/editor 完成。
*/
import type { Change, CreationSizeEffect, Document, ElementRecord, Geometry, HistoryState, Id, Registry, Target } from './types.ts';
import { applyChanges, invert, validateDocument } from './document.ts';
import { closeNumber, unitScale } from './units.ts';
import { equal, fault, freeze, readMap, validJson } from './values.ts';
/** 校验恢复图像中的尺寸 effect;未知协议与格式非法分别拒绝,不能猜旧效果含义。 */
export function validateCreationSizeEffect(value: unknown): asserts value is CreationSizeEffect {
const effect = value as CreationSizeEffect | undefined;
if (!effect || effect.version !== 1 || effect.kind !== 'creation-size') throw fault('history_effect_interpreter_unavailable');
@@ -15,33 +21,43 @@ export function validateCreationSizeEffect(value: unknown): asserts value is Cre
* durable sizing grant and record a recoverable history effect before adopting it.
* Already-issued operation bodies are deliberately outside this function.
*/
/** 仅原创建仍在 undo、目标仍活且未被用户缩放时,准备默认尺寸与历史修正。
* 返回候选 document/history/changes;原输入始终不变,失败不留下半份历史。 */
export function planCreationSize(document: Document, history: HistoryState, registry: Registry,
entryId: Id, target: Target, size: { readonly width: number; readonly height: number }) {
/** 统一将失败绑定到目标 ID,便于调用方解释为何不能再自动调整。 */
const fail = (code: string): never => { throw fault(code, [target.id]); };
if (![size.width, size.height].every(value => Number.isFinite(value) && value > 0)) fail('invalid_creation_size');
const index = history.undo.indexOf(entryId), entry = history.entries.get(entryId);
const current = document.elements.get(target.id);
if (index < 0 || !entry || current?.incarnation !== target.incarnation || current.structure !== 'node') fail('creation_size_target_changed');
// 必须在指定 entry 中找到唯一一次同 incarnation 的节点创建,不能猜其它创建记录。
const creations = entry!.changes.filter(change => change.kind === 'element' && change.id === target.id && !change.before && change.after?.incarnation === target.incarnation);
if (creations.length !== 1 || creations[0].kind !== 'element' || creations[0].after?.structure !== 'node') fail('creation_size_entry_mismatch');
const created = creations[0].after as ElementRecord & { structure: 'node' };
if (registry.resolve(current!) !== 'known' || !registry.supports(current!, 'geometry.resize')) fail('unsupported_capability');
// 以创建时的视觉尺寸为基准;目标换 owner 后 unitScale 可变,但视觉基线仍可比较。
const units = unitScale(created.data.presentation);
const baseline = { width: created.geometry.width / units, height: created.geometry.height / units };
const ratio = { width: size.width / baseline.width, height: size.height / baseline.height };
/** 同一生命周期的节点才属于本次自动尺寸修正对象。 */
const matches = (element: ElementRecord | undefined) => element?.incarnation === target.incarnation && element.structure === 'node';
/** 比较宽高除 unitScale 后的视觉值,移动/换 owner 不等同于用户主动缩放。 */
const unchanged = (element: ElementRecord | undefined) => matches(element) && element!.structure === 'node'
&& closeNumber(element!.geometry.width / unitScale(element!.data.presentation), baseline.width)
&& closeNumber(element!.geometry.height / unitScale(element!.data.presentation), baseline.height);
if (!unchanged(current)) fail('creation_size_user_resized');
/** 保留位置和旋转,只将需要继承默认修正的宽高按当前 ratio 缩放。 */
const scaled = (geometry: Geometry): Geometry => freeze({ ...geometry, width: geometry.width * ratio.width, height: geometry.height * ratio.height });
// Rewind on private roots. applyChanges intentionally preserves historical
// incarnations here; this is inspection, not a user-visible delete/restore.
let original = document;
// 从创建项起回退已应用历史,得到创建前私有基线;不产生用户可见的删除/恢复代次。
const applied = history.undo.slice(index);
for (const id of [...applied].reverse()) original = applyChanges(original, invert(history.entries.get(id)!.changes));
let amended = original, inheriting = false;
const entries = new Map(history.entries);
// 先按 undo 的正向顺序重建已应用项,再倒序遍历 redo 栈还原未来重做时间线。
const timeline = [...applied, ...[...history.redo].reverse()];
for (let cursor = 0; cursor < timeline.length; cursor++) {
const id = timeline[cursor], source = entries.get(id)!;
@@ -51,6 +67,7 @@ export function planCreationSize(document: Document, history: HistoryState, regi
// Only its before value inherits the asynchronous default correction.
const futureResize = !isApplied && inheriting && matches(end.elements.get(target.id)) && !unchanged(end.elements.get(target.id));
let resizeConsumed = false;
/** redo 中用户显式改过的维度停止继承默认比例;仅修正其 before,不改用户指定的 after。 */
const preserveFutureResize = () => {
if (!futureResize || resizeConsumed) return;
const value = end.elements.get(target.id) as ElementRecord & { structure: 'node' };
@@ -59,11 +76,13 @@ export function planCreationSize(document: Document, history: HistoryState, regi
resizeConsumed = true;
};
const changes: Change[] = [];
// 每条 next 都基于 amended 的实际 before 重建,保证修正后的连续历史仍条件匹配。
for (const change of source.changes) {
let next = change;
if (change.kind === 'element' && change.id === target.id) {
const before = amended.elements.get(target.id) ?? null;
const birth = id === entryId && change === creations[0];
// 从创建点开始继承;遇到删除或其它 incarnation 时停止,不能跨生命周期改尺寸。
if (birth) inheriting = true;
else preserveFutureResize();
const after = change.after && inheriting && change.after.incarnation === target.incarnation && change.after.structure === 'node'
@@ -84,6 +103,7 @@ export function planCreationSize(document: Document, history: HistoryState, regi
}
// Publishable proposal changes only the currently live geometry. Replaying
// redo above was validation; it must never become the working document.
// 上方 replay redo 只为校验/修正历史;当前工作文档仅修改当前活元素,不提前重做未来项。
const after = freeze({ ...(current as ElementRecord & { structure: 'node' }).geometry,
width: (current as ElementRecord & { structure: 'node' }).geometry.width * size.width / baseline.width,
height: (current as ElementRecord & { structure: 'node' }).geometry.height * size.height / baseline.height });
@@ -92,6 +112,7 @@ export function planCreationSize(document: Document, history: HistoryState, regi
]);
const nextDocument = applyChanges(document, changes);
validateDocument(nextDocument, registry);
// 最后用组件自己的 resize handler 对照约束,自动尺寸不能绕过组件 min/max/aspect 政策。
const handler = registry.handlers.get(current!.componentId)?.get('geometry.resize');
const proposed = handler!({ element: current!, input: freeze({ patch: { width: after.width, height: after.height } }) });
for (const change of proposed) if (change.target.id !== target.id || change.target.incarnation !== target.incarnation || change.field !== 'geometry') fail('invalid_handler_change');