Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/fix modification observer disable #1340

Merged
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: 1 addition & 1 deletion dist/editor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- `Fix` - blocks.getBlockByIndex() API method now returns void for indexes out of range [#1270](https://github.com/codex-team/editor.js/issues/1270)
- `Fix` - Fixed the `onChange` callback issue. This method didn't be called for native inputs before some contentedtable element changed [#843](https://github.com/codex-team/editor.js/issues/843)
- `Fix` - Fixed the `Tab` key behavior when the caret is not set inside contenteditable element, but the block is selected [#1302](https://github.com/codex-team/editor.js/issues/1302).
- `Fix` - Fixed the `onChange` callback issue. This method didn't be called after the callback throws an exception [#1339](https://github.com/codex-team/editor.js/issues/1339)

### 2.18

Expand Down
9 changes: 6 additions & 3 deletions src/components/modules/modificationsObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class ModificationsObserver extends Module {
/**
* Allows to temporary disable mutations handling
*/
private disabled: boolean;
private disabled = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use strict mode of TypeScript.
impact-blue#21 (comment)

Changes of this file are not related to #1339,
but these changes are more safety for TypeScript.


/**
* Used to prevent several mutation callback execution
Expand All @@ -37,7 +37,10 @@ export default class ModificationsObserver extends Module {
*/
private mutationDebouncer = _.debounce(() => {
this.updateNativeInputs();
this.config.onChange(this.Editor.API.methods);

if (typeof this.config.onChange === 'function') {
this.config.onChange(this.Editor.API.methods);
}
}, ModificationsObserver.DebounceTimer);

/**
Expand Down Expand Up @@ -127,7 +130,7 @@ export default class ModificationsObserver extends Module {
* @param {MutationRecord[]} mutationList - list of mutations
* @param {MutationObserver} observer - observer instance
*/
private mutationHandler(mutationList: MutationRecord[], observer): void {
private mutationHandler(mutationList: MutationRecord[], observer: MutationObserver): void {
/**
* Skip mutations in stealth mode
*/
Expand Down
18 changes: 10 additions & 8 deletions src/components/modules/saver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ export default class Saver extends Module {
*/
ModificationsObserver.disable();

blocks.forEach((block: Block) => {
chainData.push(this.getSavedData(block));
});

const extractedData = await Promise.all(chainData);
const sanitizedData = await Sanitizer.sanitizeBlocks(extractedData);
try {
blocks.forEach((block: Block) => {
chainData.push(this.getSavedData(block));
});

ModificationsObserver.enable();
const extractedData = await Promise.all(chainData);
const sanitizedData = await Sanitizer.sanitizeBlocks(extractedData);

return this.makeOutput(sanitizedData);
return this.makeOutput(sanitizedData);
} finally {
ModificationsObserver.enable();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even throws an exception, enable modification observer finally.

}
}

/**
Expand Down