Skip to content

Commit

Permalink
Throw error only if read-only is enabled from the start (#1337)
Browse files Browse the repository at this point in the history
* Throw error only if read-only is enabled from the start

* update modules
  • Loading branch information
gohabereg authored Oct 2, 2020
1 parent 274cb76 commit b836359
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/editor.js

Large diffs are not rendered by default.

26 changes: 22 additions & 4 deletions src/components/modules/readonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { CriticalError } from '../errors/critical';
* @property {boolean} readOnlyEnabled - read-only state
*/
export default class ReadOnly extends Module {
/**
* Array of tools name which don't support read-only mode
*/
private toolsDontSupportReadOnly: string[] = [];

/**
* Value to track read-only state
*
Expand Down Expand Up @@ -41,10 +46,10 @@ export default class ReadOnly extends Module {
}
});

if (toolsDontSupportReadOnly.length > 0) {
throw new CriticalError(
`To enable read-only mode all connected tools should support it. Tools ${toolsDontSupportReadOnly.join(', ')} don't support read-only mode.`
);
this.toolsDontSupportReadOnly = toolsDontSupportReadOnly;

if (this.config.readOnly && toolsDontSupportReadOnly.length > 0) {
this.throwCriticalError();
}

this.readOnlyEnabled = this.config.readOnly;
Expand All @@ -57,6 +62,10 @@ export default class ReadOnly extends Module {
* @param {boolean} state - (optional) read-only state or toggle
*/
public async toggle(state = !this.readOnlyEnabled): Promise<boolean> {
if (this.toolsDontSupportReadOnly.length > 0) {
this.throwCriticalError();
}

this.readOnlyEnabled = state;

for (const name in this.Editor) {
Expand All @@ -83,4 +92,13 @@ export default class ReadOnly extends Module {

return this.readOnlyEnabled;
}

/**
* Throws an error about tools which don't support read-only mode
*/
private throwCriticalError(): never {
throw new CriticalError(
`To enable read-only mode all connected tools should support it. Tools ${this.toolsDontSupportReadOnly.join(', ')} don't support read-only mode.`
);
}
}

0 comments on commit b836359

Please sign in to comment.