export class HistoryService { constructor() { this.history = { records: [], currentRecordIndex: -1, state: { pre: 0, next: 0, }, } } getCurrentRecordIndex() { return this.history.currentRecordIndex } getHistoryRecord() { if (this.history.currentRecordIndex == null || this.history.records.length == 0) { return null } else { return this.history.records[this.history.currentRecordIndex] } } getHistoryRecords() { return this.history.records } getHistoryState() { return this.history.state } addHistoryRecord(item) { const len = this.history.records.length if (len == 0) { this.history.records.push(item) this.history.currentRecordIndex = 0 } else if (this.history.currentRecordIndex + 1 == len) { this.history.records.push(item) ++this.history.currentRecordIndex } // 覆盖 else { const records = this.history.records.slice(0, this.history.currentRecordIndex + 1) records.push(item) this.history.records = records ++this.history.currentRecordIndex } } setHistoryState(pre, next) { this.history.state.pre = pre this.history.state.next = next } undoHistoryRecord() { --this.history.currentRecordIndex } redoHistoryRecord() { ++this.history.currentRecordIndex } clearHistoryRecord() { this.history.records = [] this.setHistoryState(0, 0) } hasRecords() { return this.history.records.length > 0 } } const historyService = new HistoryService() window.historyService = historyService export { historyService }