From c9508aa89a61c1330ad17fb86c9bbd281fd3ab48 Mon Sep 17 00:00:00 2001 From: Sion612 <167093684+Sion612@users.noreply.github.com> Date: Wed, 23 Sep 2026 21:57:28 +0800 Subject: [PATCH 1/2] fix(saver): propagate save failures to callers --- src/components/modules/saver.ts | 2 + test/cypress/tests/modules/Saver.cy.ts | 80 ++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/components/modules/saver.ts b/src/components/modules/saver.ts index c02192835..ad21dc6a6 100644 --- a/src/components/modules/saver.ts +++ b/src/components/modules/saver.ts @@ -44,6 +44,8 @@ export default class Saver extends Module { return this.makeOutput(sanitizedData); } catch (e) { _.logLabeled(`Saving failed due to the Error %o`, 'error', e); + + throw e; } } diff --git a/test/cypress/tests/modules/Saver.cy.ts b/test/cypress/tests/modules/Saver.cy.ts index dcebe6f5e..ab192591e 100644 --- a/test/cypress/tests/modules/Saver.cy.ts +++ b/test/cypress/tests/modules/Saver.cy.ts @@ -1,8 +1,88 @@ import type EditorJS from '../../../../types/index'; import Header from '@editorjs/header'; +import ToolMock from '../../fixtures/tools/ToolMock'; describe('Saver module', function () { describe('save()', function () { + ['throws', 'rejects'].forEach((failureMode) => { + it(`should reject with the original error when a tool's save() ${failureMode}`, function () { + const saveError = new Error('Tool failed to save'); + const save = cy.stub(ToolMock.prototype, 'save'); + + if (failureMode === 'throws') { + save.throws(saveError); + } else { + save.rejects(saveError); + } + + cy.createEditor({ + tools: { + failingTool: ToolMock, + }, + data: { + blocks: [ + { + type: 'failingTool', + data: { text: 'Unsaved content' }, + }, + ], + }, + }).then((editor: EditorJS) => { + return editor.save().then( + (data) => { + assert.fail(`Expected save() to reject, but it resolved with ${JSON.stringify(data)}`); + }, + (error) => { + expect(error).to.equal(saveError); + } + ); + }); + }); + }); + + it('should skip invalid blocks and save valid blocks', function () { + cy.createEditor({ + data: { + blocks: [ + { + type: 'paragraph', + data: { text: '' }, + }, + { + id: 'valid-block', + type: 'paragraph', + data: { text: 'Valid content' }, + }, + ], + }, + }).then(async (editor: EditorJS) => { + const data = await editor.save(); + + expect(data.blocks).to.deep.equal([ + { + id: 'valid-block', + type: 'paragraph', + data: { text: 'Valid content' }, + }, + ]); + }); + }); + + it('should still reject saving in read-only mode', function () { + cy.createEditor({ + readOnly: true, + }).then((editor: EditorJS) => { + return editor.save().then( + () => { + assert.fail('Expected saving in read-only mode to reject'); + }, + (error) => { + expect(error.message).to.equal('Editor\'s content can not be saved in read-only mode'); + } + ); + }); + }); + it('should correctly save block if there are some 3rd party (eg. browser extensions) nodes inserted into the layout', function () { cy.createEditor({ data: { From 637e4fa45f63b21cc8e4063526106946f2263472 Mon Sep 17 00:00:00 2001 From: Peter Savchenko Date: Fri, 27 Nov 2026 18:00:42 +0300 Subject: [PATCH 2/2] fix review comments --- docs/CHANGELOG.md | 2 + src/components/modules/readonly.ts | 64 +++++++++++++++++++---------- test/cypress/tests/readOnly.cy.ts | 65 ++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 21 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 96791b9f4..abf876ec0 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -9,6 +9,8 @@ - `Fix` - `blocks.insertMany()` now triggers `onChange` with a `block-added` event for each inserted block - `Fix` - Inline Tools shortcuts work in every Editor instance on a page and are released when an Editor is destroyed - `Fix` - Focus the contenteditable input when setting the caret, so autofocus works inside iframes +- `Fix` - Save failures now propagate to callers (#3030) +- `Fix` - `readOnly.toggle()` now restores editor to consistent state if save fails ### 2.31.7 diff --git a/src/components/modules/readonly.ts b/src/components/modules/readonly.ts index cee5aa874..c39d24de7 100644 --- a/src/components/modules/readonly.ts +++ b/src/components/modules/readonly.ts @@ -71,19 +71,7 @@ export default class ReadOnly extends Module { this.readOnlyEnabled = state; - for (const name in this.Editor) { - /** - * Verify module has method `toggleReadOnly` method - */ - if (!this.Editor[name].toggleReadOnly) { - continue; - } - - /** - * set or toggle read-only state - */ - this.Editor[name].toggleReadOnly(state); - } + this.toggleModulesReadOnly(state); /** * If new state equals old one, do not re-render blocks @@ -104,17 +92,51 @@ export default class ReadOnly extends Module { */ this.Editor.ModificationsObserver.disable(); - /** - * Save current Editor Blocks and render again - */ - const savedBlocks = await this.Editor.Saver.save(); + try { + /** + * Save current Editor Blocks and render again + */ + const savedBlocks = await this.Editor.Saver.save(); - await this.Editor.BlockManager.clear(); - await this.Editor.Renderer.render(savedBlocks.blocks); + await this.Editor.BlockManager.clear(); + await this.Editor.Renderer.render(savedBlocks.blocks); - this.Editor.ModificationsObserver.enable(); + this.Editor.ModificationsObserver.enable(); - return this.readOnlyEnabled; + return this.readOnlyEnabled; + } catch (error) { + /** + * If save fails, restore the previous read-only state + */ + this.readOnlyEnabled = oldState; + + this.toggleModulesReadOnly(oldState); + + this.Editor.ModificationsObserver.enable(); + + throw error; + } + } + + /** + * Toggles read-only state for all Editor modules that support it + * + * @param state - read-only state to set + */ + private toggleModulesReadOnly(state: boolean): void { + for (const name in this.Editor) { + /** + * Verify module has method `toggleReadOnly` method + */ + if (!this.Editor[name].toggleReadOnly) { + continue; + } + + /** + * set or toggle read-only state + */ + this.Editor[name].toggleReadOnly(state); + } } /** diff --git a/test/cypress/tests/readOnly.cy.ts b/test/cypress/tests/readOnly.cy.ts index f1f8e496c..0913fb6ff 100644 --- a/test/cypress/tests/readOnly.cy.ts +++ b/test/cypress/tests/readOnly.cy.ts @@ -1,5 +1,6 @@ import type { EditorConfig } from '../../../types'; import type EditorJS from '../../../types'; +import ToolMock from '../fixtures/tools/ToolMock'; describe('ReadOnly API spec', () => { /** @@ -53,4 +54,68 @@ describe('ReadOnly API spec', () => { }); }); }); + + it('should restore read-only state when toggle fails due to save error', () => { + const saveError = new Error('Tool failed to save'); + + /** + * Create a tool class that supports read-only mode + */ + class ReadOnlyTool extends ToolMock { + public static isReadOnlySupported = true; + } + + const saveStub = cy.stub(ReadOnlyTool.prototype, 'save'); + + saveStub.throws(saveError); + + cy.createEditor({ + tools: { + failingTool: ReadOnlyTool, + }, + data: { + blocks: [ + { + type: 'failingTool', + data: { text: 'Unsaved content' }, + }, + ], + }, + }).then((editor: EditorJS) => { + expect(editor.readOnly.isEnabled).to.be.false; + + return editor.readOnly.toggle(true).then( + () => { + assert.fail('Expected readOnly.toggle() to reject, but it resolved'); + }, + (error) => { + /** + * The toggle should reject with the original error + */ + expect(error).to.equal(saveError); + + /** + * The editor should not be in read-only mode after the failure + */ + expect(editor.readOnly.isEnabled).to.be.false; + + /** + * Restore the save method so it works now + */ + saveStub.restore(); + + /** + * editor.save() should work now that the failure is gone + */ + return editor.save(); + } + ).then((data) => { + /** + * Verify the save succeeded + */ + expect(data).to.have.property('blocks'); + expect(data.blocks).to.have.length(1); + }); + }); + }); });