EXTEND
Hooks
Hook 参与操作流程,可以观察、转换或取消一次提交;Event 只能观察已经完成的结果。
执行模式
ts
sheet.hooks.tap(
"beforeCommand",
{ name: "audit" },
(context) => {
const { command } = context as { command: { type: string } };
if (command.type === "delete-rows") {
context.cancel?.("文档示例禁止删除行");
}
}
);| 名称 | 类型 | 说明 |
|---|---|---|
| series | mode | 按顺序执行所有 tap。 |
| bail | mode | 按顺序执行,某个 tap 取消后停止。 |
| waterfall | mode | 上一个 tap 的返回值是下一个 tap 的输入。 |
| parallel | mode | tap 之间不依赖彼此副作用。 |
注册签名
ts
const dispose = sheet.hooks.tap(
"beforeCommand",
{ name: "audit", priority: 10 },
(context) => {
// series/bail: handler 只处理 context,返回值被忽略。
// waterfall: handler 的第二个参数是 context,返回值传给下一个 tap。
}
);
// 不需要时手动移除;sheet dispose 也会统一清理。
dispose();| 名称 | 类型 | 说明 |
|---|---|---|
| hookName | string | 注册表中的固定名称;未知名称会抛 unknown-hook。 |
| meta | { name: string; priority?: number } | name 用于日志和错误定位;priority 越大越先执行,默认 0。 |
| handler | context => void 或 (value, context) => TValue | 只有 waterfall 使用第二个参数并消费返回值;其他模式返回值忽略。 |
| 返回值 | () => void | Disposer,调用后注销这个 tap。 |
当前 Hook
| 名称 | 类型 | 说明 |
|---|---|---|
| beforeInit | series | context: {}。插件 setup 期间、核心完成主要装配前触发。handler 返回 void;抛错会中止初始化。 |
| beforeDestroy | series | context: {}。dispose() 前触发。handler 返回 void;单个 tap 异常被隔离并转发到 onHookError。 |
| beforeCommand | bail | context: { command: { type, params }, transactionId, cancel(reason?), cancelled, reason }。handler 返回 void;调用 cancel() 会阻止 Command 进入计划。 |
| beforeTransactionCommit | bail | context: { commands, patches, model, transactionId, cancel(reason?), cancelled, reason }。handler 返回 void;取消后整个事务不提交。 |
| beforeRowsChange | bail | context: { operation: 'insert' | 'delete' | 'move', start, count, target?, cancel(reason?), cancelled, reason }。move 时有 target。返回 void;cancel() 阻止行结构变更。 |
| beforeColumnsChange | bail | context: { operation: 'insert' | 'delete' | 'move', start, count, target?, cancel(reason?), cancelled, reason }。返回 void;cancel() 阻止列结构变更。 |
| resolveColumn | waterfall | handler: (column: RuntimeColumn, { definition, index }) => RuntimeColumn。必须返回列配置;异常被隔离,当前列回退到上一个值。 |
| beforeContextMenuOpen | bail | context: { context: ContextMenuContext, cancel(reason?), cancelled, reason }。返回 void;cancel() 后菜单不打开。 |
| extendContextMenu | waterfall | handler: (items: ContextMenuItem[], context: ContextMenuContext) => ContextMenuItem[]。返回最终菜单项数组;异常被隔离。 |
| onHookError | series | context: { error: unknown, hookName: string, handlerName: string }。返回 void;自身异常不会再递归进入本 Hook。 |
beforeCommand 审计
每次写入或插入行均先触发 beforeCommand。
等待操作…