From e684c39aea6f81a9836b46c38b5198fffd964e69 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 19 Jan 2024 11:44:02 -0700 Subject: [PATCH 1/9] Add an async `__unstablePreSavePost` hook; resolving with false prevents saving --- packages/editor/src/store/actions.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index a0330321bac8f7..5660dd6c45008d 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -181,6 +181,15 @@ export const savePost = dispatch.editPost( { content }, { undoIgnore: true } ); } + const preSaveOK = await applyFilters( + 'editor.__unstablePreSavePost', + Promise.resolve( true ), + options + ); + if ( ! preSaveOK ) { + return; + } + const previousRecord = select.getCurrentPost(); const edits = { id: previousRecord.id, From f2e65b8823ae840a7c933f7f8ce8464f4f7c16fb Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 19 Jan 2024 12:05:32 -0700 Subject: [PATCH 2/9] Simplify preSave check --- packages/editor/src/store/actions.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 5660dd6c45008d..a1ca923c7d8dac 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -181,12 +181,13 @@ export const savePost = dispatch.editPost( { content }, { undoIgnore: true } ); } - const preSaveOK = await applyFilters( - 'editor.__unstablePreSavePost', - Promise.resolve( true ), - options - ); - if ( ! preSaveOK ) { + if ( + ! ( await applyFilters( + 'editor.__unstablePreSavePost', + Promise.resolve( true ), + options + ) ) + ) { return; } From bcae1749dc8a94108c3d1723278fc3db833967e4 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Wed, 8 May 2024 13:41:39 -0600 Subject: [PATCH 3/9] Filter edits & enable throwing Error in `__unstablePreSavePost` filter --- packages/editor/src/store/actions.js | 53 +++++++++++++++------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 62c30d96ed5352..73adf439996a8b 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -183,18 +183,8 @@ export const savePost = dispatch.editPost( { content }, { undoIgnore: true } ); } - if ( - ! ( await applyFilters( - 'editor.__unstablePreSavePost', - Promise.resolve( true ), - options - ) ) - ) { - return; - } - const previousRecord = select.getCurrentPost(); - const edits = { + let edits = { id: previousRecord.id, ...registry .select( coreStore ) @@ -205,23 +195,38 @@ export const savePost = ), content, }; - dispatch( { type: 'REQUEST_POST_UPDATE_START', options } ); - await registry - .dispatch( coreStore ) - .saveEntityRecord( - 'postType', - previousRecord.type, + let error; + try { + edits = await applyFilters( + 'editor.__unstablePreSavePost', edits, options ); + } catch ( err ) { + error = err; + } - let error = registry - .select( coreStore ) - .getLastEntitySaveError( - 'postType', - previousRecord.type, - previousRecord.id - ); + dispatch( { type: 'REQUEST_POST_UPDATE_START', options } ); + if ( ! error ) { + error = await registry + .dispatch( coreStore ) + .saveEntityRecord( + 'postType', + previousRecord.type, + edits, + options + ); + } + + if ( ! error ) { + error = registry + .select( coreStore ) + .getLastEntitySaveError( + 'postType', + previousRecord.type, + previousRecord.id + ); + } if ( ! error ) { await applyFilters( From ff7d6b430a139ae0ffcb77d64796b8c6919ffe2c Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 16 Jul 2024 14:52:34 -0600 Subject: [PATCH 4/9] Pass Promise.resolve( edits ) instead of edits --- packages/editor/src/store/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 73adf439996a8b..bc7e3c4db1ed9c 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -213,7 +213,7 @@ export const savePost = .saveEntityRecord( 'postType', previousRecord.type, - edits, + Promise.resolve( edits ), options ); } From 4ead1ff2876457b9007f26e7d200e6cb9c7a0760 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 16 Jul 2024 14:56:30 -0600 Subject: [PATCH 5/9] Move REQUEST_POST_UPDATE_START to before preSave async filter --- packages/editor/src/store/actions.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index bc7e3c4db1ed9c..4c8859cbc23f86 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -195,6 +195,8 @@ export const savePost = ), content, }; + + dispatch( { type: 'REQUEST_POST_UPDATE_START', options } ); let error; try { edits = await applyFilters( @@ -206,7 +208,6 @@ export const savePost = error = err; } - dispatch( { type: 'REQUEST_POST_UPDATE_START', options } ); if ( ! error ) { error = await registry .dispatch( coreStore ) From d5d7bfce716af2a2565052782bd29599ef07e55f Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 16 Jul 2024 15:07:15 -0600 Subject: [PATCH 6/9] Update saveEntityRecord error handling to use try/catch --- packages/editor/src/store/actions.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 4c8859cbc23f86..983a6036baf965 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -209,14 +209,21 @@ export const savePost = } if ( ! error ) { - error = await registry - .dispatch( coreStore ) - .saveEntityRecord( - 'postType', - previousRecord.type, - Promise.resolve( edits ), - options - ); + try { + error = await registry + .dispatch( coreStore ) + .saveEntityRecord( + 'postType', + previousRecord.type, + Promise.resolve( edits ), + options + ); + } catch ( err ) { + error = + err.message && err.code !== 'unknown_error' + ? err.message + : __( 'An error occurred while updating.' ); + } } if ( ! error ) { From 7764b36ce1b293214238498eafbcd0cd4f90e9d9 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Thu, 1 Aug 2024 11:53:54 -0600 Subject: [PATCH 7/9] Use explicit false check for error --- packages/editor/src/store/actions.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 983a6036baf965..33605fdd81c397 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -197,7 +197,7 @@ export const savePost = }; dispatch( { type: 'REQUEST_POST_UPDATE_START', options } ); - let error; + let error = false; try { edits = await applyFilters( 'editor.__unstablePreSavePost', @@ -208,7 +208,7 @@ export const savePost = error = err; } - if ( ! error ) { + if ( false !== error ) { try { error = await registry .dispatch( coreStore ) @@ -226,7 +226,7 @@ export const savePost = } } - if ( ! error ) { + if ( false !== error ) { error = registry .select( coreStore ) .getLastEntitySaveError( @@ -236,7 +236,7 @@ export const savePost = ); } - if ( ! error ) { + if ( false !== error ) { await applyFilters( 'editor.__unstableSavePost', Promise.resolve(), From 2a0d9f29b18d6472c448b85aee0c0d15c33f0440 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Thu, 1 Aug 2024 12:34:09 -0600 Subject: [PATCH 8/9] =?UTF-8?q?reduce=20changes,=20test=20functionality,?= =?UTF-8?q?=20don=E2=80=99t=20pass=20data=20to=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/editor/src/store/actions.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 33605fdd81c397..83f4de2d328bf3 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -184,7 +184,7 @@ export const savePost = } const previousRecord = select.getCurrentPost(); - let edits = { + const edits = { id: previousRecord.id, ...registry .select( coreStore ) @@ -197,25 +197,26 @@ export const savePost = }; dispatch( { type: 'REQUEST_POST_UPDATE_START', options } ); + let error = false; try { - edits = await applyFilters( + error = await applyFilters( 'editor.__unstablePreSavePost', - edits, + Promise.resolve( false ), options ); } catch ( err ) { error = err; } - if ( false !== error ) { + if ( ! error ) { try { - error = await registry + await registry .dispatch( coreStore ) .saveEntityRecord( 'postType', previousRecord.type, - Promise.resolve( edits ), + edits, options ); } catch ( err ) { @@ -226,7 +227,7 @@ export const savePost = } } - if ( false !== error ) { + if ( ! error ) { error = registry .select( coreStore ) .getLastEntitySaveError( @@ -236,7 +237,7 @@ export const savePost = ); } - if ( false !== error ) { + if ( ! error ) { await applyFilters( 'editor.__unstableSavePost', Promise.resolve(), From 05b48529c8e2012b24704af3a803e076855c501b Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Thu, 1 Aug 2024 12:36:53 -0600 Subject: [PATCH 9/9] whitespace --- packages/editor/src/store/actions.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 83f4de2d328bf3..59faa6b5b73624 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -195,7 +195,6 @@ export const savePost = ), content, }; - dispatch( { type: 'REQUEST_POST_UPDATE_START', options } ); let error = false;