Skip to content

Commit

Permalink
feat: undo redo rich text tracking [TOL-1987] (#1657)
Browse files Browse the repository at this point in the history
* feat: undo redo rich text tracking

* test: tracking undo and redo
  • Loading branch information
YvesRijckaert committed May 17, 2024
1 parent aba605c commit 7f72d28
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
20 changes: 20 additions & 0 deletions cypress/component/rich-text/RichTextEditor.Tracking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,4 +967,24 @@ describe('Rich Text Editor - Tracking', { viewportHeight: 2000, viewportWidth: 1
cy.get('@onAction').should('have.callCount', 3);
});
});

describe('Undo / redo', () => {
it('tracks via toolbar', () => {
richText.editor.click().type('some text.').click();
richText.toolbar.undo.click();
cy.get('@onAction').should('be.calledWithExactly', ...action('undo', 'toolbar-icon'));
richText.toolbar.redo.click();
cy.get('@onAction').should('be.calledWithExactly', ...action('redo', 'toolbar-icon'));
cy.get('@onAction').should('have.callCount', 2);
});

it('tracks via shortcut', () => {
richText.editor.click().type('some text.').click();
richText.editor.click().type(`{${mod}}z`);
cy.get('@onAction').should('be.calledWithExactly', ...action('undo', 'shortcut'));
richText.editor.type(`{${mod}}{shift}z`);
cy.get('@onAction').should('be.calledWithExactly', ...action('redo', 'shortcut'));
cy.get('@onAction').should('have.callCount', 2);
});
});
});
6 changes: 5 additions & 1 deletion packages/rich-text/src/Toolbar/components/ButtonRedo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import { ToolbarButton } from '../../plugins/shared/ToolbarButton';
export const ButtonRedo = () => {
const editor = useContentfulEditor();

const onClickHandler = () => {
editor.redo('toolbar');
};

return (
<ToolbarButton
title="Redo"
testId="redo-toolbar-button"
onClick={editor.redo}
onClick={onClickHandler}
isActive={false}
isDisabled={editor.history.redos.length === 0}
>
Expand Down
6 changes: 5 additions & 1 deletion packages/rich-text/src/Toolbar/components/ButtonUndo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import { ToolbarButton } from '../../plugins/shared/ToolbarButton';
export const ButtonUndo = () => {
const editor = useContentfulEditor();

const onClickHandler = () => {
editor.undo('toolbar');
};

return (
<ToolbarButton
title="Undo"
testId="undo-toolbar-button"
onClick={editor.undo}
onClick={onClickHandler}
isActive={false}
isDisabled={editor.history.undos.length === 0}
>
Expand Down
8 changes: 8 additions & 0 deletions packages/rich-text/src/internal/types/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export type Value = Element[];
export type ReactEditor = p.TReactEditor<Value>;
export interface PlateEditor extends p.PlateEditor<Value> {
tracking: TrackingPluginActions;
undo: {
(): void;
(source: 'toolbar' | 'shortcut'): void;
};
redo: {
(): void;
(source: 'toolbar' | 'shortcut'): void;
};
}

export type Node = p.ElementOf<PlateEditor> | p.TextOf<PlateEditor>;
Expand Down
22 changes: 21 additions & 1 deletion packages/rich-text/src/plugins/Tracking/createTrackingPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export type RichTextTrackingActionName =
// Copy & Paste
| 'paste'
| 'invalidTablePaste'
// Undo & Redo
| 'undo'
| 'redo'
// Hyperlinks
| 'cancelCreateHyperlinkDialog'
| 'cancelEditHyperlinkDialog'
Expand Down Expand Up @@ -136,7 +139,7 @@ export const createTrackingPlugin = (onAction: RichTextTrackingActionHandler): P
return {
key: 'TrackingPlugin',
withOverrides: (editor) => {
const { insertData } = editor;
const { insertData, undo, redo } = editor;

editor.tracking = trackingActions;

Expand All @@ -161,6 +164,23 @@ export const createTrackingPlugin = (onAction: RichTextTrackingActionHandler): P
insertData(data);
};

editor.undo = (source?: 'toolbar' | 'shortcut') => {
undo();
if (source === 'toolbar') {
editor.tracking.onToolbarAction('undo');
} else {
editor.tracking.onShortcutAction('undo');
}
};

editor.redo = (source?: 'toolbar' | 'shortcut') => {
redo();
if (source === 'toolbar') {
editor.tracking.onToolbarAction('redo');
} else {
editor.tracking.onShortcutAction('redo');
}
};
return editor;
},
};
Expand Down

0 comments on commit 7f72d28

Please sign in to comment.