Skip to content

Commit 74c22a5

Browse files
TINY-11907: Simplify disabled option check and update tests (#419)
1 parent a01aee5 commit 74c22a5

File tree

3 files changed

+37
-40
lines changed

3 files changed

+37
-40
lines changed

tinymce-angular-component/src/main/ts/editor/editor.component.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class EditorComponent extends Events implements AfterViewInit, ControlVal
8181
public set disabled(val) {
8282
this._disabled = val;
8383
if (this._editor) {
84-
if (DisabledUtils.isDisabledOptionSupported()) {
84+
if (DisabledUtils.isDisabledOptionSupported(this._editor)) {
8585
this._editor.options.set('disabled', val ?? false);
8686
} else {
8787
setMode(this._editor, val ? 'readonly' : 'design');
@@ -190,10 +190,8 @@ export class EditorComponent extends Events implements AfterViewInit, ControlVal
190190
selector: undefined,
191191
target: this._element,
192192
inline: this.inline,
193-
...( DisabledUtils.isDisabledOptionSupported()
194-
? { disabled: this.disabled, readonly: this.readonly }
195-
: { readonly: this.disabled || this.readonly }
196-
),
193+
disabled: this.disabled,
194+
readonly: this.readonly,
197195
license_key: this.licenseKey,
198196
plugins: mergePlugins((this.init && this.init.plugins) as string, this.plugins),
199197
toolbar: this.toolbar || (this.init && this.init.toolbar),
@@ -209,6 +207,14 @@ export class EditorComponent extends Events implements AfterViewInit, ControlVal
209207
if (this.init && typeof this.init.setup === 'function') {
210208
this.init.setup(editor);
211209
}
210+
211+
if (this.disabled === true) {
212+
if (DisabledUtils.isDisabledOptionSupported(editor)) {
213+
this._editor.options.set('disabled', this.disabled);
214+
} else {
215+
this._editor.mode.set('readonly');
216+
}
217+
}
212218
}
213219
};
214220

tinymce-angular-component/src/main/ts/utils/DisabledUtils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import { getTinymce } from '../TinyMCE';
2-
import { TinyMCE } from 'tinymce';
1+
import { Editor } from 'tinymce';
32

4-
const isDisabledOptionSupported = () => {
5-
const tiny: TinyMCE = getTinymce();
6-
// Disabled option is supported since Tiny 7.6.0
7-
return Number(tiny.majorVersion) > 7 || (Number(tiny.majorVersion) === 7 && Number(tiny.minorVersion) >= 6);
8-
};
3+
const isDisabledOptionSupported = (editor: Editor) => editor.options && editor.options.isRegistered('disabled');
94

105
export {
116
isDisabledOptionSupported

tinymce-angular-component/src/test/ts/browser/DisabledPropertyTest.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ describe('DisabledPropertyTest', () => {
2323
const assertDisabledOption = (editor: Editor, expected: boolean) =>
2424
Assertions.assertEq(`TinyMCE should have disabled option set to ${expected}`, expected, editor.options.get('disabled'));
2525

26-
eachVersionContext([ '7.5.0' ], () => {
26+
eachVersionContext([ '5', '6', '7.5.0', ], () => {
2727
const createFixture = editorHook(EditorComponent);
2828

29-
it(`Component 'disabled' property is mapped to editor 'readonly' property`, async () => {
29+
it(`Component 'disabled' property is mapped to editor 'readonly' mode`, async () => {
3030
const { editor } = await createFixture({ disabled: true });
3131
assertReadonlyMode(editor);
3232
});
3333

34-
it(`Toggling component's 'disabled' property is mapped to editor 'readonly' property`, async () => {
34+
it(`Toggling component's 'disabled' property is mapped to editor 'readonly' mode`, async () => {
3535
const fixture = await createFixture();
3636
const { editor } = fixture;
3737

@@ -46,6 +46,26 @@ describe('DisabledPropertyTest', () => {
4646
assertDesignMode(editor);
4747
});
4848

49+
it(`Setting the 'readonly' property causing readonly mode`, async () => {
50+
const { editor } = await createFixture({ readonly: true });
51+
assertReadonlyMode(editor);
52+
});
53+
54+
it(`Toggling component's 'readonly' property is mapped to editor 'readonly' mode`, async () => {
55+
const fixture = await createFixture();
56+
const { editor } = fixture;
57+
58+
assertDesignMode(editor);
59+
60+
fixture.componentRef.setInput('readonly', true);
61+
fixture.detectChanges();
62+
assertReadonlyMode(editor);
63+
64+
fixture.componentRef.setInput('readonly', false);
65+
fixture.detectChanges();
66+
assertDesignMode(editor);
67+
});
68+
4969
it(`[disabled]=true [readonly]=false triggers readonly mode`, async () => {
5070
const { editor } = await createFixture({ disabled: true, readonly: false });
5171
assertReadonlyMode(editor);
@@ -67,7 +87,7 @@ describe('DisabledPropertyTest', () => {
6787
assertDesignMode(editor);
6888
});
6989

70-
it(`Toggling component's 'disabled' property is mapped to editor 'disabled' property`, async () => {
90+
it(`Toggling component's 'disabled' property is mapped to editor 'disabled' option`, async () => {
7191
const fixture = await createFixture();
7292
const { editor } = fixture;
7393

@@ -86,30 +106,6 @@ describe('DisabledPropertyTest', () => {
86106
});
87107
});
88108

89-
eachVersionContext([ '4', '5', '6', '7' ], () => {
90-
const createFixture = editorHook(EditorComponent);
91-
92-
it(`Setting the 'readonly' property causing readonly mode`, async () => {
93-
const { editor } = await createFixture({ readonly: true });
94-
assertReadonlyMode(editor);
95-
});
96-
97-
it(`Toggling component's 'readonly' property is mapped to editor 'readonly' mode`, async () => {
98-
const fixture = await createFixture();
99-
const { editor } = fixture;
100-
101-
assertDesignMode(editor);
102-
103-
fixture.componentRef.setInput('readonly', true);
104-
fixture.detectChanges();
105-
assertReadonlyMode(editor);
106-
107-
fixture.componentRef.setInput('readonly', false);
108-
fixture.detectChanges();
109-
assertDesignMode(editor);
110-
});
111-
});
112-
113109
context('With version 7', () => {
114110
@Component({
115111
imports: [ FormsModule, EditorComponent ],

0 commit comments

Comments
 (0)