Skip to content

PinghuaZhuang/command-stack

Repository files navigation

CommandStack

常见的 redo, undo操作. 继承 events.

publish license Commit Verison

Quick start

pnpm add @zstark/command-stack
const cs = new CommandStack({ /* handlers */ });

// 记录
cs.dispatch({
    type: 'create.shape',
    context: { /* any */ },
});

// redo undo
cs.redo();
cs.undo();

exmaple

Live Demo

pnpm instsall
pnpm build:example
pnpm dev

Options

  • handlers?: Record<ActionTypeEnum, handler>
  • options?: { maxLength: number }

Methods

  • redo: () => void. 下一步
  • undo: () => void. 上一步
  • dispatch: (action: Action) => void. 记录行为
  • excute () => void. 执行操作. 一般用不到

Action

type Action = {
    type: string; // 操作标识
    context: any; // 上下文
}

TODO

  • handlers 支持异步.
    • excute
  • 合并操作??