Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
jalners committed May 20, 2022
2 parents 768ddcd + 68e1ca1 commit a67361f
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# WProofreader plugin for CKEditor 5 Changelog

## 2.1.0 – 2022-05-04

* Added functionality that properly disables the plugin for certain editor's modes.

## 2.0.6 – 2021-06-15

Internal changes only (updated dependencies, documentation, etc.).
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@webspellchecker/wproofreader-ckeditor5",
"version": "2.0.6",
"version": "2.1.0",
"description": "Multilingual spelling and grammar checking plugin for CKEditor 5",
"repository": {
"type": "git",
Expand Down
92 changes: 85 additions & 7 deletions src/wproofreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import WProofreaderEditing from './wproofreaderediting';
import WProofreaderUI from './wproofreaderui';
import { ScriptLoader } from './utils/script-loader';

const DISABLE_COMMAND_ID = 'WProofreaderToggleCommandDisabling';
const DISABLE_INSTANCES_ID = 'InstancesDisabling';

/**
* Initializes and creates instances of the {@code WEBSPELLCHECKER}.
*/
Expand All @@ -27,6 +30,14 @@ export default class WProofreader extends Plugin {
constructor(editor) {
super(editor);

/**
* Flag indicating whether the {@code WProofreaderToggle} command is enabled.
*
* @observable
* @member {Boolean}
*/
this.set('isToggleCommandEnabled', true);

this._instances = [];

this._collaborationPluginNames = [
Expand All @@ -44,6 +55,8 @@ export default class WProofreader extends Plugin {
init() {
this._userOptions = this._getUserOptions();
this._setTheme();
this._setAutoStartup();
this._setIsEnabled(this._userOptions.autoStartup, DISABLE_INSTANCES_ID);

this._loadWscbundle()
.then(() => {
Expand All @@ -52,6 +65,11 @@ export default class WProofreader extends Plugin {
.catch((error) => {
throw new Error(error);
});

this.bind('isToggleCommandEnabled').to(
this.editor.commands.get('WProofreaderToggle'), 'isEnabled',
(isEnabled) => this._handleToggleCommandEnabled(isEnabled)
);
}

/**
Expand Down Expand Up @@ -90,6 +108,24 @@ export default class WProofreader extends Plugin {
}
}

/**
* Checks if the autoStartup option exists otherwise sets {@code true} value.
* @private
*/
_setAutoStartup() {
if (!this._userOptions.hasOwnProperty('autoStartup')) {
this._userOptions.autoStartup = true;
}
}

/**
* Configures the {@code isEnabled} state of the plugin.
* @private
*/
_setIsEnabled(enable, disableId) {
enable ? this.clearForceDisabled(disableId) : this.forceDisabled(disableId);
}

/**
* Loads {@code wscbundle} script.
* @private
Expand Down Expand Up @@ -198,6 +234,7 @@ export default class WProofreader extends Plugin {
_onToggle(instance) {
const enable = !instance.isDisabled();

this._setIsEnabled(enable, DISABLE_INSTANCES_ID);
this._syncToggle(enable);
}

Expand All @@ -206,13 +243,35 @@ export default class WProofreader extends Plugin {
* @private
*/
_syncToggle(enable) {
const options = { ignoreCallback: true };

this._instances.forEach((instance) => {
enable ? instance.enable(options) : instance.disable(options);
enable ? this._enableInstance(instance) : this._disableInstance(instance);
});
}

/**
* Enables an instance of the {@code WEBSPELLCHECKER}.
* @private
*/
_enableInstance(instance) {
const options = { ignoreCallback: true };

if (!this.isEnabled) {
return;
}

instance.enable(options);
}

/**
* Disables an instance of the {@code WEBSPELLCHECKER}.
* @private
*/
_disableInstance(instance) {
const options = { ignoreCallback: true };

instance.disable(options);
}

/**
* Creates an instance of the {@code WEBSPELLCHECKER}.
* @private
Expand Down Expand Up @@ -244,6 +303,10 @@ export default class WProofreader extends Plugin {
return;
}

if (!this.isEnabled) {
this._disableInstance(instance);
}

this._instances.push(instance);
}

Expand All @@ -257,6 +320,18 @@ export default class WProofreader extends Plugin {
});
}

/**
* Handles an enabled state of the {@code WProofreaderToggle} command.
* @private
*/
_handleToggleCommandEnabled(isEnabled) {
this._setIsEnabled(isEnabled, DISABLE_COMMAND_ID);
this._syncToggle(isEnabled);

// Method should return a boolean value to correct work of the bind functionality.
return isEnabled;
}

/**
* Returns available static actions of the {@code WEBSPELLCHECKER}.
* @public
Expand All @@ -276,11 +351,14 @@ export default class WProofreader extends Plugin {
* @public
*/
toggle() {
const options = { ignoreCallback: true };
if (this._instances.length === 0) {
return;
}

this._instances.forEach((instance) => {
instance.isDisabled() ? instance.enable(options) : instance.disable(options);
});
const enable = this.isInstancesEnabled();

this._setIsEnabled(!enable, DISABLE_INSTANCES_ID);
this._syncToggle(!enable);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/wproofreaderui.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export default class WProofreaderUI extends Plugin {
editor.execute(evt.source.commandParam);
});

// The dropdown view should be disabled if the WProofreaderToggle command is disabled.
dropdownView.bind('isEnabled').to(editor.commands.get('WProofreaderToggle'));

return dropdownView;
});
}
Expand Down
Loading

0 comments on commit a67361f

Please sign in to comment.