tldraw/state/commands/history.ts

64 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-05-10 12:16:57 +00:00
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>()