Skip to content

Commit

Permalink
feat: Expose undo/redo actions to host app
Browse files Browse the repository at this point in the history
Enable navigating editor history from the host app.
  • Loading branch information
dcalhoun committed Jan 8, 2025
1 parent 3bd48fc commit ae41b33
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ios/Sources/GutenbergKit/Sources/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
return EditorTitleAndContent(title: title, content: content)
}

/// Steps backwards in the editor history state
public func undo() {
evaluate("editor.undo();")
}

/// Steps forwards in the editor history state
public func redo() {
evaluate("editor.redo();")
}

/// Enables code editor.
public var isCodeEditorEnabled: Bool = false {
didSet {
Expand Down
12 changes: 11 additions & 1 deletion src/components/visual-editor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function VisualEditor({ post }) {
const postContentRef = useRef(post.content);
const { addEntities, editEntityRecord, receiveEntityRecords } =
useDispatch(coreStore);
const { setEditedPost, setupEditor } = useDispatch(editorStore);
const { setEditedPost, setupEditor, undo, redo } = useDispatch(editorStore);
const { getEditedPostAttribute, getEditedPostContent } =
useSelect(editorStore);

Expand Down Expand Up @@ -171,6 +171,16 @@ function VisualEditor({ post }) {
};
};

editor.undo = () => {
// Do not return the `Promise` return value to avoid host errors.
undo();
};

editor.redo = () => {
// Do not return the `Promise` return value to avoid host errors.
redo();
};

const blockEditorSettings = useBlockEditorSettings(
editorSettings,
post.type,
Expand Down

0 comments on commit ae41b33

Please sign in to comment.