cycyexcel
English0.2.0

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?.("文档示例禁止删除行");
    }
  }
);
名称类型说明
seriesmode按顺序执行所有 tap。
bailmode按顺序执行,某个 tap 取消后停止。
waterfallmode上一个 tap 的返回值是下一个 tap 的输入。
parallelmodetap 之间不依赖彼此副作用。

注册签名

ts
const dispose = sheet.hooks.tap(
  "beforeCommand",
  { name: "audit", priority: 10 },
  (context) => {
    // series/bail: handler 只处理 context,返回值被忽略。
    // waterfall: handler 的第二个参数是 context,返回值传给下一个 tap。
  }
);

// 不需要时手动移除;sheet dispose 也会统一清理。
dispose();
名称类型说明
hookNamestring注册表中的固定名称;未知名称会抛 unknown-hook。
meta{ name: string; priority?: number }name 用于日志和错误定位;priority 越大越先执行,默认 0。
handlercontext => void 或 (value, context) => TValue只有 waterfall 使用第二个参数并消费返回值;其他模式返回值忽略。
返回值() => voidDisposer,调用后注销这个 tap。

当前 Hook

名称类型说明
beforeInitseriescontext: {}。插件 setup 期间、核心完成主要装配前触发。handler 返回 void;抛错会中止初始化。
beforeDestroyseriescontext: {}。dispose() 前触发。handler 返回 void;单个 tap 异常被隔离并转发到 onHookError。
beforeCommandbailcontext: { command: { type, params }, transactionId, cancel(reason?), cancelled, reason }。handler 返回 void;调用 cancel() 会阻止 Command 进入计划。
beforeTransactionCommitbailcontext: { commands, patches, model, transactionId, cancel(reason?), cancelled, reason }。handler 返回 void;取消后整个事务不提交。
beforeRowsChangebailcontext: { operation: 'insert' | 'delete' | 'move', start, count, target?, cancel(reason?), cancelled, reason }。move 时有 target。返回 void;cancel() 阻止行结构变更。
beforeColumnsChangebailcontext: { operation: 'insert' | 'delete' | 'move', start, count, target?, cancel(reason?), cancelled, reason }。返回 void;cancel() 阻止列结构变更。
resolveColumnwaterfallhandler: (column: RuntimeColumn, { definition, index }) => RuntimeColumn。必须返回列配置;异常被隔离,当前列回退到上一个值。
beforeContextMenuOpenbailcontext: { context: ContextMenuContext, cancel(reason?), cancelled, reason }。返回 void;cancel() 后菜单不打开。
extendContextMenuwaterfallhandler: (items: ContextMenuItem[], context: ContextMenuContext) => ContextMenuItem[]。返回最终菜单项数组;异常被隔离。
onHookErrorseriescontext: { error: unknown, hookName: string, handlerName: string }。返回 void;自身异常不会再递归进入本 Hook。

beforeCommand 审计

每次写入或插入行均先触发 beforeCommand。

等待操作…