Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 43 additions & 21 deletions src/components/modules/readonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/components/modules/saver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
80 changes: 80 additions & 0 deletions test/cypress/tests/modules/Saver.cy.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
65 changes: 65 additions & 0 deletions test/cypress/tests/readOnly.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { EditorConfig } from '../../../types';
import type EditorJS from '../../../types';
import ToolMock from '../fixtures/tools/ToolMock';

describe('ReadOnly API spec', () => {
/**
Expand Down Expand Up @@ -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);
});
});
});
});
Loading