用于操作记录,提供撤销与反撤销。
const recorder = {
record: [],
max: 5,
currentIndex: 0,
push(...values) {
this.record.push(...values)
this.clean()
},
clean() {
while (this.record.length > this.max) {
this.record.shift()
}
this.currentIndex = this.record.length - 1
},
forward() {
this.currentIndex =
this.currentIndex < this.record.length - 1
? this.currentIndex + 1
: this.currentIndex
return this.record[this.currentIndex]
},
back() {
this.currentIndex =
this.currentIndex > 0
? this.currentIndex - 1
: this.currentIndex
return this.record[this.currentIndex]
}
}