UNPKG

@selenite/graph-editor

Version:

A graph editor for visual programming, based on rete and svelte.

47 lines (46 loc) 1.71 kB
import { HistoryPlugin as BaseHistoryPlugin } from 'rete-history-plugin'; import { get, writable } from 'svelte/store'; export class HistoryPlugin extends BaseHistoryPlugin { canRedo = writable(false); canUndo = writable(false); isUndoing = false; isRedoing = false; lastMoveTime = Date.now(); add(action) { // console.log('time', Date.now() - this.lastMoveTime); if (Date.now() - this.lastMoveTime < 100) return; if (this.isRedoing || this.isUndoing) return; // console.debug('Adding action to history', action); super.add(action); // @ts-expect-error bypass private access error this.canUndo.set(this.history.produced.length > 0); // @ts-expect-error bypass private access error this.canRedo.set(this.history.reserved.length > 0); } execute(action) { this.add(action); action.redo(); } async undo() { this.lastMoveTime = Date.now(); this.isUndoing = true; await super.undo(); // @ts-expect-error bypass private access error this.canUndo.set(this.history.produced.length > 0); // @ts-expect-error bypass private access error this.canRedo.set(this.history.reserved.length > 0); this.isUndoing = false; } async redo() { this.lastMoveTime = Date.now(); this.isRedoing = true; await super.redo(); // @ts-expect-error bypass private access error this.canRedo.set(this.history.reserved.length > 0); // @ts-expect-error bypass private access error this.canUndo.set(this.history.produced.length > 0); this.isRedoing = false; } }