docs(core): clarify creation sizing and structural planning boundaries
This commit is contained in:
@@ -22,12 +22,14 @@ export function validateCreationSizeEffect(value: unknown): asserts value is Cre
|
||||
* Already-issued operation bodies are deliberately outside this function.
|
||||
*/
|
||||
/** 仅原创建仍在 undo、目标仍活且未被用户缩放时,准备默认尺寸与历史修正。
|
||||
* 返回候选 document/history/changes;原输入始终不变,失败不留下半份历史。 */
|
||||
* 返回候选 document/history/changes,不发布状态;输入须来自已捕获的不可变文档与历史。
|
||||
* 这里按 geometry.resize 能力判定,不限定 componentId 必须是 image;图片授权条件由外层 grant 决定。 */
|
||||
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');
|
||||
// 创建项必须仍在已应用 undo 时间线上;已撤销到 redo、被淘汰或目标换代的尺寸结果不能采用。
|
||||
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');
|
||||
@@ -40,7 +42,7 @@ export function planCreationSize(document: Document, history: HistoryState, regi
|
||||
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 };
|
||||
/** 同一生命周期的节点才属于本次自动尺寸修正对象。 */
|
||||
/** 调用处已按 target.id 取记录,这里再限制 incarnation 与 node 结构;不单独比较元素 ID。 */
|
||||
const matches = (element: ElementRecord | undefined) => element?.incarnation === target.incarnation && element.structure === 'node';
|
||||
/** 比较宽高除 unitScale 后的视觉值,移动/换 owner 不等同于用户主动缩放。 */
|
||||
const unchanged = (element: ElementRecord | undefined) => matches(element) && element!.structure === 'node'
|
||||
@@ -52,9 +54,11 @@ export function planCreationSize(document: Document, history: HistoryState, regi
|
||||
// Rewind on private roots. applyChanges intentionally preserves historical
|
||||
// incarnations here; this is inspection, not a user-visible delete/restore.
|
||||
let original = document;
|
||||
// 从创建项起回退已应用历史,得到创建前私有基线;不产生用户可见的删除/恢复代次。
|
||||
// 从创建项起回退全部已应用项,得到创建前私有基线;此处用 applyChanges 而非 planHistoryChanges,
|
||||
// 因而允许检查途中存在历史代次/墓碑组合,不把它们作为最终文档发布。
|
||||
const applied = history.undo.slice(index);
|
||||
for (const id of [...applied].reverse()) original = applyChanges(original, invert(history.entries.get(id)!.changes));
|
||||
// original 追踪旧时间线,amended 追踪修正后的时间线;inheriting 只在匹配创建之后开启。
|
||||
let amended = original, inheriting = false;
|
||||
const entries = new Map(history.entries);
|
||||
// 先按 undo 的正向顺序重建已应用项,再倒序遍历 redo 栈还原未来重做时间线。
|
||||
@@ -63,11 +67,12 @@ export function planCreationSize(document: Document, history: HistoryState, regi
|
||||
const id = timeline[cursor], source = entries.get(id)!;
|
||||
const end = applyChanges(original, source.changes), isApplied = cursor < applied.length;
|
||||
if (isApplied && !unchanged(end.elements.get(target.id))) fail('creation_size_user_resized');
|
||||
// A future explicit resize remains a resize to the user's chosen dimensions.
|
||||
// Only its before value inherits the asynchronous default correction.
|
||||
// A future resize preserves each dimension explicitly changed by the user.
|
||||
// Its before value and unchanged dimensions may inherit the default correction.
|
||||
const futureResize = !isApplied && inheriting && matches(end.elements.get(target.id)) && !unchanged(end.elements.get(target.id));
|
||||
let resizeConsumed = false;
|
||||
/** redo 中用户显式改过的维度停止继承默认比例;仅修正其 before,不改用户指定的 after。 */
|
||||
/** redo 中按宽/高分别判断:被用户改过的维度将 ratio 置 1,保留其 after;
|
||||
* before 重取修正时间线,未被用户改变的另一维仍可继承默认比例。每项最多消费一次。 */
|
||||
const preserveFutureResize = () => {
|
||||
if (!futureResize || resizeConsumed) return;
|
||||
const value = end.elements.get(target.id) as ElementRecord & { structure: 'node' };
|
||||
@@ -76,9 +81,10 @@ export function planCreationSize(document: Document, history: HistoryState, regi
|
||||
resizeConsumed = true;
|
||||
};
|
||||
const changes: Change[] = [];
|
||||
// 每条 next 都基于 amended 的实际 before 重建,保证修正后的连续历史仍条件匹配。
|
||||
// 目标整记录/geometry 变化从 amended 重取 before;其它变化保留原条件,继续用 applyChanges 校验。
|
||||
for (const change of source.changes) {
|
||||
let next = change;
|
||||
// 整记录变化也要重取真实 before;其它对象/字段沿用原变化,保证整条时间线仍按原次序检查。
|
||||
if (change.kind === 'element' && change.id === target.id) {
|
||||
const before = amended.elements.get(target.id) ?? null;
|
||||
const birth = id === entryId && change === creations[0];
|
||||
@@ -98,6 +104,7 @@ export function planCreationSize(document: Document, history: HistoryState, regi
|
||||
amended = applyChanges(amended, [next]);
|
||||
changes.push(next);
|
||||
}
|
||||
// 整项完成后再推进旧时间线;只有变化内容确实不同才替换 entries 中这一项,operationIds/标签保持。
|
||||
original = end;
|
||||
if (!equal(changes, source.changes)) entries.set(id, freeze({ ...source, changes }));
|
||||
}
|
||||
@@ -112,10 +119,12 @@ export function planCreationSize(document: Document, history: HistoryState, regi
|
||||
]);
|
||||
const nextDocument = applyChanges(document, changes);
|
||||
validateDocument(nextDocument, registry);
|
||||
// 最后用组件自己的 resize handler 对照约束,自动尺寸不能绕过组件 min/max/aspect 政策。
|
||||
// 调用已安装 resize handler,把其生成变化应用后的目标记录与直接候选比较;不一致就拒绝。
|
||||
// 这能执行组件实际实现的限制,不凭 capability 名称宣称所有组件都有 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');
|
||||
if (!equal(applyChanges(document, proposed).elements.get(target.id), nextDocument.elements.get(target.id))) fail('creation_size_constraint');
|
||||
// 仅返回当前几何 changes,历史 entries 是整条修正时间线;两栈顺序沿用,不把上方 redo 检查变成实际重做。
|
||||
return Object.freeze({ document: nextDocument, changes, history: Object.freeze({ undo: history.undo, redo: history.redo, entries: readMap(entries) }) });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user