| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import { Data } from "types"
- import { BaseCommand } from "./command"
-
- class BaseHistory<T> {
- private stack: BaseCommand<T>[] = []
- private pointer = -1
- private maxLength = 100
- private _enabled = true
-
- execute = (data: T, command: BaseCommand<T>) => {
- if (this.disabled) return
- this.stack = this.stack.slice(0, this.pointer + 1)
- this.stack.push(command)
- command.redo(data, true)
- this.pointer++
-
- if (this.stack.length > this.maxLength) {
- this.stack = this.stack.slice(this.stack.length - this.maxLength)
- this.pointer = this.maxLength - 1
- }
-
- this.save(data)
- }
-
- undo = (data: T) => {
- if (this.disabled) return
- if (this.pointer === -1) return
- const command = this.stack[this.pointer]
- command.undo(data)
- this.pointer--
- this.save(data)
- }
-
- redo = (data: T) => {
- if (this.disabled) return
- if (this.pointer === this.stack.length - 1) return
- const command = this.stack[this.pointer + 1]
- command.redo(data, false)
- this.pointer++
- this.save(data)
- }
-
- save = (data: T) => {
- if (typeof window === "undefined") return
- if (typeof localStorage === "undefined") return
-
- localStorage.setItem("glob_aldata_v6", JSON.stringify(data))
- }
-
- disable = () => {
- this._enabled = false
- }
-
- enable = () => {
- this._enabled = true
- }
-
- get disabled() {
- return !this._enabled
- }
- }
-
- export default new BaseHistory<Data>()
|