In this example, we will show you how to use the CreativeEditor SDK’s CreativeEngine to undo and redo steps in the editor API.
Functions#
public func createHistory() -> HistoryCreate a history which consists of an undo/redo stack for editing operations. There can be multiple. But only one can be active at a time.
- Returns: The handle to the created history.
public func destroyHistory(_ history: History)Destroy the given history, returns an error if the handle doesn’t refer to a history.
history:: The history to be destroyed.
public func setActiveHistory(_ history: History)Mark the given history as active, returns an error if the handle doesn’t refer to a history. All other histories get cleared from the active state. Undo/redo operations only apply to the active history.
history:: The history to be marked as active.
public func getActiveHistory() -> HistoryGet the handle to the currently active history. If there’s none it will be created.
- Returns: The handle to the active history.
public func addUndoStep() throwsAdds a new history state to the stack, if undoable changes were made.
public func undo() throwsUndo one step in the history if an undo step is available.
public func canUndo() throws -> BoolIf an undo step is available.
- Returns:
trueif an undo step is available.
public func redo() throwsRedo one step in the history if a redo step is available.
public func canRedo() throws -> BoolIf a redo step is available.
- Returns:
trueif a redo step is available.
public var onHistoryUpdated: AsyncStream<Void> { get }Subscribe to changes to the undo/redo history.
Full Code#
Here’s the full code:
// Manage history stackslet newHistory = engine.editor.createHistory()let oldHistory = engine.editor.getActiveHistory()engine.editor.setActiveHistory(newHistory)engine.editor.destroyHistory(oldHistory)
let historyTask = Task { for await _ in engine.editor.onHistoryUpdated { let canUndo = try engine.editor.canUndo() let canRedo = try engine.editor.canRedo() print("History updated: \(canUndo) \(canRedo)") }}
// Push a new state to the undo stacktry engine.editor.addUndoStep()
// Perform an undo, if possible.if try engine.editor.canUndo() { try engine.editor.undo()}
// Perform a redo, if possible.if try engine.editor.canRedo() { try engine.editor.redo()}