UndoManagerPlugin
Example — Source Code — API Reference
UndoManagerPlugin adds support for undo/redo operations in the viewer. It can be used to manage the history of changes made to the scene, objects, materials, etc.
It uses the JSUndoManager(from ts-browser-helpers) to maintain a common undo/redo history across the viewer and other plugins.
The plugin is used automatically by TweakpaneUiPlugin, BlueprintJsUiPlugin, PickingPlugin, TransformControlsPlugin, and other plugins that support undo/redo operations. It is not required to be added to the viewer explicitly when using any of the UI plugins, but can be added if you want to use it directly.
Keys. Mod+Z undoes, Mod+Shift+Z and Mod+Y redo (Mod: Cmd on a Mac, Ctrl on other platforms); a held key repeats them. The keys are letters, so they follow the printed key on any layout, and they do nothing while the plugin is disabled. They are actions (undo, redo in UndoManagerPlugin.actions), registered in viewer.actions: a UI lists them, and the app changes or removes their keys there (setShortcut). A key typed in a text field is left to the field, for its own text undo; a checkbox or a button has no text, so the key undoes there.
viewer.addPlugin(UndoManagerPlugin)
let undoManager: JSUndoManager | undefined
// listen for plugin to be added/removed/changed
viewer.forPlugin<UndoManagerPlugin>('UndoManagerPlugin', (um)=> {
undoManager = um.undoManager
}, ()=> undoManager = undefined)
// on button click
undoManager.record({
// my command
})Undo and Redo
undo() and redo() step through the shared history. The keys are the actions undo and redo in viewer.actions (see Keys above): Mod+Z to undo, Mod+Shift+Z or Mod+Y to redo. The hot keys of JSUndoManager are off (bindHotKeys: false), so a key typed in a text field stays with the field.
const undoPlugin = viewer.getPlugin(UndoManagerPlugin)!
undoPlugin.undo()
undoPlugin.redo()
undoPlugin.enabled = false // stop recording and ignore undo/redo and their keys
undoPlugin.limit = 200 // maximum number of steps kept in the history, default 1000Commands
A command is either an object with undo and redo functions, or an object with a type naming a preset registered in undoManager.presets, which turns it into undo/redo functions when it is undone or redone. The plugin registers two presets, ThreeViewerUM_set and ThreeViewerUM_action (see undoCommandTypes), used by setValue and performAction below.
undoManager.record(command) adds a step without running it, the change is expected to be done already. recordUndo(command) records a preset command the same way, and merges consecutive setValue commands (see below).
const obj = viewer.scene.getObjectByName('box')!
const oldPosition = obj.position.clone()
obj.position.set(1, 0, 0)
const newPosition = obj.position.clone()
undoPlugin.undoManager?.record({
undo: () => { obj.position.copy(oldPosition); obj.setDirty() },
redo: () => { obj.position.copy(newPosition); obj.setDirty() },
})setValue
setValue(binding, value, props, uid) sets binding[0][binding[1]] = value and records a ThreeViewerUM_set step that restores the previous value on undo.
Consecutive setValue calls with the same uid within undoEditingWaitTime (2000 ms) are merged into a single step (the last step is replaced, keeping the first value), as long as the previous call was not final. A call is final unless props.last is false, so pass last: false while the value is still changing (e.g. dragging a slider) and last: true when done.
const material = obj.material as PhysicalMaterial
const uid = 'material-roughness' // any value that identifies the property, like the ui config
undoPlugin.setValue([material, 'roughness'], 0.3, {last: false}, uid)
undoPlugin.setValue([material, 'roughness'], 0.4, {last: false}, uid)
undoPlugin.setValue([material, 'roughness'], 0.5, {last: true}, uid)
// one step in the history, undo sets roughness back to its value before the first callsetValues(bindings, defaults, values, props, uid) does the same for several bindings in one step, defaults are the values used for a binding whose target property is not defined.
undoPlugin.setValues([[material, 'roughness'], [material, 'metalness']], [1, 0], [0.5, 1], {last: true}, 'material-rm')performAction
performAction(target, fn, args, uid) calls fn (with this as target, if not undefined) and records a ThreeViewerUM_action step. fn returns either
- an undo function, then redo calls
fnagain, or - an object
{undo, redo?, action?}. Ifactionis set, it is called right away and is used asredowhenredois not set.
Nothing is recorded if no undo function is returned.
await undoPlugin.performAction(undefined, () => {
const child = new Mesh2(new BoxGeometry(), new PhysicalMaterial())
return {
action: () => viewer.scene.addObject(child),
undo: () => child.removeFromParent(),
}
}, [], 'add-box')Using in plugins
Other plugins get the shared manager with viewer.forPlugin, which calls the first function when the plugin is added (or right away if it is already added) and the second when it is removed. Pass the calling plugin as the last argument to also unmount when it is removed. This is how PickingPlugin, TransformControlsPlugin and others record their steps.
class MyPlugin extends AViewerPluginSync {
public static readonly PluginType = 'MyPlugin'
undoManager?: JSUndoManager
onAdded(viewer: ThreeViewer) {
super.onAdded(viewer)
viewer.forPlugin<UndoManagerPlugin>('UndoManagerPlugin', (um)=> {
this.undoManager = um.undoManager
}, ()=> this.undoManager = undefined, this)
}
}