From 7de1d6ad2117f391c1434780d548d5c8994e11d3 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 10 Jan 2022 23:45:47 +0200 Subject: [PATCH 1/2] [History]: Fix redo after update/publish with transient edits --- packages/core-data/src/reducer.js | 9 ++++++++ .../specs/editor/various/undo.test.js | 21 +++++++++++++++++ .../specs/site-editor/template-revert.test.js | 23 +++++++++++-------- test/emptytheme/block-templates/index.html | 15 ++++++------ 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index 130407e6f277a..693c4e58ef46e 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -431,7 +431,16 @@ export function undo( state = UNDO_INITIAL_STATE, action ) { // to continue as if we were creating an explicit undo level. This // will result in an extra undo level being appended with the flattened // undo values. + // We also have to take into account if the `lastEditAction` had opted out + // of being tracked in undo history, like the action that persists the latest + // content right before saving. In that case we have to update the `lastEditAction` + // to avoid returning early before applying the existing flattened undos. isCreateUndoLevel = true; + if ( ! lastEditAction.meta.undo ) { + lastEditAction.meta.undo = { + edits: {}, + }; + } action = lastEditAction; } else { return nextState; diff --git a/packages/e2e-tests/specs/editor/various/undo.test.js b/packages/e2e-tests/specs/editor/various/undo.test.js index 8c2f8ecfa9105..9402ce8624c1a 100644 --- a/packages/e2e-tests/specs/editor/various/undo.test.js +++ b/packages/e2e-tests/specs/editor/various/undo.test.js @@ -420,4 +420,25 @@ describe( 'undo', () => { // Expect "1". expect( await getEditedPostContent() ).toMatchSnapshot(); } ); + + it( 'should be able to undo and redo when transient changes have been made and we update/publish', async () => { + // Typing consecutive characters in a `Paragraph` block updates the same + // block attribute as in the previous action and results in transient edits + // and skipping `undo` history steps. + const text = 'tonis'; + await clickBlockAppender(); + await page.keyboard.type( text ); + await publishPost(); + await pressKeyWithModifier( 'primary', 'z' ); + expect( await getEditedPostContent() ).toBe( '' ); + await page.waitForSelector( + '.editor-history__redo[aria-disabled="false"]' + ); + await page.click( '.editor-history__redo[aria-disabled="false"]' ); + expect( await getEditedPostContent() ).toMatchInlineSnapshot( ` + " +

tonis

+ " + ` ); + } ); } ); diff --git a/packages/e2e-tests/specs/site-editor/template-revert.test.js b/packages/e2e-tests/specs/site-editor/template-revert.test.js index 34e1cba4dbfcb..3189fa120c5bf 100644 --- a/packages/e2e-tests/specs/site-editor/template-revert.test.js +++ b/packages/e2e-tests/specs/site-editor/template-revert.test.js @@ -22,29 +22,31 @@ const { disableWelcomeGuide, } = siteEditor; -const assertSaveButtonIsDisabled = () => +const assertSaveButtonIsDisabled = async () => page.waitForSelector( '.edit-site-save-button__button[aria-disabled="true"]' ); -const assertSaveButtonIsEnabled = () => +const assertSaveButtonIsEnabled = async () => page.waitForSelector( '.edit-site-save-button__button[aria-disabled="false"]' ); -const waitForNotice = () => page.waitForSelector( '.components-snackbar' ); - const clickButtonInNotice = async () => { const selector = '.components-snackbar button'; await page.waitForSelector( selector ); await page.click( selector ); }; -const clickUndoInHeaderToolbar = () => +const clickUndoInHeaderToolbar = async () => page.click( '.edit-site-header__toolbar button[aria-label="Undo"]' ); -const clickRedoInHeaderToolbar = () => - page.click( '.edit-site-header__toolbar button[aria-label="Redo"]' ); +const clickRedoInHeaderToolbar = async () => { + await page.waitForSelector( + '.edit-site-header__toolbar button[aria-label="Redo"][aria-disabled="false"]' + ); + return page.click( '.edit-site-header__toolbar button[aria-label="Redo"]' ); +}; const undoRevertInHeaderToolbar = async () => { await clickUndoInHeaderToolbar(); @@ -72,7 +74,9 @@ const save = async () => { const revertTemplate = async () => { await page.click( '.edit-site-document-actions__get-info' ); await page.click( '.edit-site-template-details__revert-button' ); - await waitForNotice(); + await page.waitForXPath( + '//*[contains(@class, "components-snackbar") and contains(text(), "Template reverted")]' + ); await assertSaveButtonIsEnabled(); }; @@ -164,7 +168,7 @@ describe( 'Template Revert', () => { expect( contentBefore ).toBe( contentAfter ); } ); - it.skip( 'should show the original content after revert, clicking undo then redo in the header toolbar', async () => { + it( 'should show the original content after revert, clicking undo then redo in the header toolbar', async () => { const contentBefore = await getEditedPostContent(); await addDummyText(); @@ -172,7 +176,6 @@ describe( 'Template Revert', () => { await revertTemplate(); await save(); await undoRevertInHeaderToolbar(); - // there's a bug which probably also exists where the redo button stays disabled. await clickRedoInHeaderToolbar(); const contentAfter = await getEditedPostContent(); diff --git a/test/emptytheme/block-templates/index.html b/test/emptytheme/block-templates/index.html index 356981eb585b6..1a91eca128d33 100644 --- a/test/emptytheme/block-templates/index.html +++ b/test/emptytheme/block-templates/index.html @@ -1,9 +1,10 @@ - - - - - - - \ No newline at end of file + +
+ + + + +
+ From dcf91aa7eddc6e6ed62ade7069b92ccb165925ef Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 11 Jan 2022 00:54:36 +0200 Subject: [PATCH 2/2] test --- test/emptytheme/block-templates/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/emptytheme/block-templates/index.html b/test/emptytheme/block-templates/index.html index 1a91eca128d33..464f1f429afd6 100644 --- a/test/emptytheme/block-templates/index.html +++ b/test/emptytheme/block-templates/index.html @@ -1,6 +1,6 @@ - +