HistoryController.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { set } from "lodash";
  2. import { StateRoot } from "queenjs";
  3. type RecordOptions = { combine?: boolean };
  4. class State extends StateRoot {
  5. currLen = 0; //操作栈的长度
  6. maxLen = 100; //操作栈总长度
  7. opIndex = -1; //操作栈的指针
  8. canUndo = this.computed((state) => {
  9. return state.opIndex >= 0;
  10. });
  11. canRedo = this.computed((state) => {
  12. return state.opIndex < state.currLen - 1;
  13. });
  14. }
  15. export class HistoryController {
  16. state = new State().reactive();
  17. queue: GroupAction[] = [];
  18. cacheGroupAction = new GroupAction();
  19. // 添加缓存记录
  20. record(action: Action, options?: RecordOptions) {
  21. this.cacheGroupAction.record(action, options);
  22. }
  23. // 保存缓存记录到历史栈中
  24. submit(action?: Action) {
  25. const { state, queue, cacheGroupAction } = this;
  26. if (action) this.record(action);
  27. if (!cacheGroupAction.actions.length) return;
  28. // 将缓存操作记录保存到当前指针的下一栈中
  29. queue[++state.opIndex] = cacheGroupAction;
  30. // 设置栈的长度为指针的长度,舍弃后面的记录
  31. queue.length = state.opIndex + 1;
  32. // 若栈长度超过上限, 舍弃之前的记录
  33. if (queue.length > state.maxLen) {
  34. queue.splice(0, queue.length - state.maxLen);
  35. state.opIndex = state.maxLen - 1;
  36. }
  37. // 更新当前长度状态
  38. state.currLen = queue.length;
  39. // 更新当前缓存GroupAction
  40. this.cacheGroupAction = new GroupAction();
  41. console.log(this);
  42. }
  43. undo() {
  44. if (!this.state.canUndo) return;
  45. this.queue[this.state.opIndex--].undo();
  46. console.log(this)
  47. }
  48. redo() {
  49. if (!this.state.canRedo) return;
  50. this.queue[++this.state.opIndex].redo();
  51. console.log(this)
  52. }
  53. //清除操作
  54. clear() {
  55. this.queue = [];
  56. this.state.currLen = 0;
  57. this.state.opIndex = -1;
  58. this.cacheGroupAction = new GroupAction();
  59. }
  60. }
  61. export class Action {
  62. constructor(
  63. public type: "set" | "delete",
  64. public root: any,
  65. public path: string,
  66. public value?: any,
  67. public oldValue?: any
  68. ) {}
  69. undo() {
  70. set(this.root, this.path, this.oldValue);
  71. }
  72. redo() {
  73. set(this.root, this.path, this.value);
  74. }
  75. }
  76. export class GroupAction {
  77. actions: Action[] = [];
  78. record(action: Action, options?: RecordOptions) {
  79. const lastAction = this.actions.at(-1);
  80. if (
  81. options?.combine &&
  82. lastAction?.root === action.root &&
  83. lastAction?.path === action.path
  84. ) {
  85. this.actions[this.actions.length - 1] = action;
  86. } else {
  87. this.actions.push(action);
  88. }
  89. }
  90. undo() {
  91. [...this.actions].reverse().forEach((act) => act.undo());
  92. }
  93. redo() {
  94. this.actions.forEach((d) => d.redo());
  95. }
  96. }