Skip to content

Commit 78e2b80

Browse files
Merge pull request #64 from tinymce/feature/TINY-11911
TINY-11911: Added new `disabled` option and increased minor version.
2 parents fc6e31a + 0450bff commit 78e2b80

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
"typescript": "~5.6.3",
4949
"webpack": "^5.75.0"
5050
},
51-
"dependencies": {},
52-
"version": "2.1.1-rc",
51+
"dependencies": {
52+
"@tinymce/miniature": "^6.0.0"
53+
},
54+
"version": "2.2.1-rc",
5355
"name": "@tinymce/tinymce-webcomponent"
5456
}

src/demo/html/disabled.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<title>TinyMCE WebComponent Demo: Disabled and Readonly </title>
7+
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
8+
</head>
9+
10+
<body>
11+
<script>
12+
function toggleDisabled() {
13+
var editor = document.getElementById('disabled_state');
14+
if (!editor.hasAttribute('disabled')) {
15+
editor.setAttribute('disabled', '');
16+
} else {
17+
editor.removeAttribute('disabled');
18+
}
19+
}
20+
21+
function toggleReadonly() {
22+
var editor = document.getElementById('readonly_mode');
23+
if (!editor.hasAttribute('readonly')) {
24+
editor.setAttribute('readonly', '');
25+
} else {
26+
editor.removeAttribute('readonly');
27+
}
28+
}
29+
</script>
30+
<h2>TinyMCE WebComponent Demo: Disabled and Readonly</h2>
31+
<div id="ephox-ui">
32+
<h2>Readonly mode</h2>
33+
<button onclick="toggleReadonly()">Toggle readonly</button>
34+
<tinymce-editor id="readonly_mode" readonly></tinymce-editor>
35+
<h2>Disabled state</h2>
36+
<button onclick="toggleDisabled()" plugins="help">Toggle disabled</button>
37+
<tinymce-editor id="disabled_state" disabled></tinymce-editor>
38+
</div>
39+
<script src="../../../node_modules/tinymce/tinymce.js"></script>
40+
<script src="../../../dist/tinymce-webcomponent.js"></script>
41+
</body>
42+
43+
</html>

src/main/ts/component/Editor.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Resolve, Obj, Fun, Global } from '@ephox/katamari';
22
import { TinyMCE, Editor } from 'tinymce';
33
import { ScriptLoader } from '../utils/ScriptLoader';
4+
import { TinyVer } from '@tinymce/miniature';
45
type EditorOptions = Parameters<TinyMCE['init']>[0];
56
type EventHandler = Parameters<Editor['on']>[1];
67

@@ -76,8 +77,10 @@ const configAttributes: Record<string, (v: string) => unknown> = {
7677
promotion: parseBooleanOrString, // boolean
7778
};
7879

79-
const configRenames: Record<string, string> = {
80-
};
80+
const configRenames: Record<string, string> = {};
81+
82+
// Function that checks if the disabled option is supported with the version used
83+
const isDisabledOptionSupported = (tinymce: TinyMCE): boolean => !TinyVer.isLessThan(tinymce, '7.6.0');
8184

8285
class TinyMceEditor extends HTMLElement {
8386
private _status: Status;
@@ -108,7 +111,7 @@ class TinyMceEditor extends HTMLElement {
108111
'on-ObjectSelected', 'on-SetContent', 'on-Show', 'on-Submit', 'on-Undo',
109112
'on-VisualAid' ];
110113

111-
return [ 'form', 'readonly', 'autofocus', 'placeholder' ].concat(nativeEvents).concat(tinyEvents);
114+
return [ 'form', 'readonly', 'autofocus', 'placeholder', 'disabled' ].concat(nativeEvents).concat(tinyEvents);
112115
}
113116

114117
constructor() {
@@ -201,6 +204,9 @@ class TinyMceEditor extends HTMLElement {
201204
if (this.readonly) {
202205
config.readonly = true;
203206
}
207+
if (this.disabled) {
208+
config.disabled = true;
209+
}
204210
if (this.autofocus) {
205211
config.auto_focus = true;
206212
}
@@ -242,6 +248,10 @@ class TinyMceEditor extends HTMLElement {
242248
const baseConfig = this._getConfig();
243249
const conf: EditorOptions = {
244250
...baseConfig,
251+
...{
252+
disabled: this.hasAttribute('disabled'),
253+
readonly: this.hasAttribute('readonly')
254+
},
245255
target,
246256
setup: (editor: Editor) => {
247257
this._editor = editor;
@@ -252,6 +262,12 @@ class TinyMceEditor extends HTMLElement {
252262
// this assignment ensures the attribute is in sync with the editor
253263
this.readonly = this.readonly;
254264
});
265+
266+
editor.on('DisabledStateChange', (_e: unknown) => {
267+
// this assignment ensures the attribute is in sync with the editor
268+
this.disabled = this.disabled;
269+
});
270+
255271
Obj.each(this._eventHandlers, (handler, event) => {
256272
if (handler !== undefined) {
257273
editor.on(event, handler);
@@ -293,6 +309,8 @@ class TinyMceEditor extends HTMLElement {
293309
if (oldValue !== newValue) {
294310
if (attribute === 'form') {
295311
this._updateForm();
312+
} else if (attribute === 'disabled') {
313+
this.disabled = newValue !== null;
296314
} else if (attribute === 'readonly') {
297315
this.readonly = newValue !== null;
298316
} else if (attribute === 'autofocus') {
@@ -355,6 +373,27 @@ class TinyMceEditor extends HTMLElement {
355373
}
356374
}
357375

376+
get disabled(): boolean {
377+
return this._editor
378+
? this._editor.options.get('disabled')
379+
: this.hasAttribute('disabled');
380+
}
381+
382+
set disabled(value: boolean) {
383+
const tinymce = this._getTinymce?.();
384+
const isVersionNewer = tinymce ? isDisabledOptionSupported(tinymce) : true;
385+
386+
if (this._editor && this._status === Status.Ready && isVersionNewer) {
387+
this._editor.options.set('disabled', value);
388+
}
389+
390+
if (value && !this.hasAttribute('disabled')) {
391+
this.setAttribute('disabled', '');
392+
} else if (!value && this.hasAttribute('disabled')) {
393+
this.removeAttribute('disabled');
394+
}
395+
}
396+
358397
get placeholder(): string | null {
359398
return this.getAttribute('placeholder');
360399
}

yarn.lock

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@
638638
"@deno/shim-deno-test" "^0.4.0"
639639
which "^2.0.2"
640640

641-
"@ephox/agar@^8.0.1":
641+
"@ephox/agar@^8.0.0", "@ephox/agar@^8.0.1":
642642
version "8.0.1"
643643
resolved "https://registry.yarnpkg.com/@ephox/agar/-/agar-8.0.1.tgz#6772ed7abd90adee5b08bc6f8baf0cb4f22b8d1c"
644644
integrity sha512-2LR/u0oos9GadRoLFDVE6NRDV8Tl9+DYQyouVYn0+7afAYdouyw+nXaoTVG5nnvMG+ofsmldKOC82nJWu0a4kQ==
@@ -758,14 +758,25 @@
758758
dependencies:
759759
"@ephox/dispute" "^1.0.3"
760760

761+
"@ephox/mcagar@^9.0.0":
762+
version "9.0.1"
763+
resolved "https://registry.yarnpkg.com/@ephox/mcagar/-/mcagar-9.0.1.tgz#d740d02628063e8df86ca8cdd8aa307654b1c69b"
764+
integrity sha512-iNMP8mkz8AtgiRceU4TAnstQje3RG8yxM/QoWKkriCeh6n5fe1sSfexFRVW3fwZ0bUrjTlyawSqoH0yhESqJjQ==
765+
dependencies:
766+
"@ephox/agar" "^8.0.1"
767+
"@ephox/katamari" "^9.1.6"
768+
"@ephox/sand" "^6.0.10"
769+
"@ephox/sugar" "^9.3.1"
770+
fast-check "^2.0.0"
771+
761772
"@ephox/sand@^6.0.10":
762773
version "6.0.10"
763774
resolved "https://registry.yarnpkg.com/@ephox/sand/-/sand-6.0.10.tgz#1bd3e03ce339ab282fa3ac03591badc13f7d69b1"
764775
integrity sha512-q/2Xcjl1O5c9/HHxQiEKGZkGsC68WUjVP5ciDNKqrZd1s1BhA3VWbaubQxxBRt3SwKyBCukfS5gL53qkqTfbkg==
765776
dependencies:
766777
"@ephox/katamari" "^9.1.6"
767778

768-
"@ephox/sugar@^9.2.1", "@ephox/sugar@^9.3.1":
779+
"@ephox/sugar@^9.2.1", "@ephox/sugar@^9.3.0", "@ephox/sugar@^9.3.1":
769780
version "9.3.1"
770781
resolved "https://registry.yarnpkg.com/@ephox/sugar/-/sugar-9.3.1.tgz#082dbe885d41e657eff7ef6275d648f3dff88334"
771782
integrity sha512-NQMqDN0zC+IZGuhBuz/7RxncfJsmTBhCHgaqkHOJ5d1E0CIL9afIizBYQ0JSHS3osAb9woGGtwPobjCx3/Srpw==
@@ -1544,6 +1555,16 @@
15441555
resolve "^1.17.0"
15451556
typescript "^4.9.5"
15461557

1558+
"@tinymce/miniature@^6.0.0":
1559+
version "6.0.0"
1560+
resolved "https://registry.yarnpkg.com/@tinymce/miniature/-/miniature-6.0.0.tgz#d11ca91292fa614926faf399166898874baedd92"
1561+
integrity sha512-bLpaTTgbyMHP3V8bucSxvCTVTUjRRk05zDgnlD5Hj2eo6hEyHT1FCqpuhdVLHx9mbCydrlAIhhsY8Ex6Hp8Qqg==
1562+
dependencies:
1563+
"@ephox/agar" "^8.0.0"
1564+
"@ephox/katamari" "^9.1.5"
1565+
"@ephox/mcagar" "^9.0.0"
1566+
"@ephox/sugar" "^9.3.0"
1567+
15471568
"@tootallnate/quickjs-emscripten@^0.23.0":
15481569
version "0.23.0"
15491570
resolved "https://registry.yarnpkg.com/@tootallnate/quickjs-emscripten/-/quickjs-emscripten-0.23.0.tgz#db4ecfd499a9765ab24002c3b696d02e6d32a12c"
@@ -7035,9 +7056,9 @@ thunky@^1.0.2:
70357056
integrity sha512-eHY7nBftgThBqOyHGVN+l8gF0BucP09fMo0oO/Lb0w1OF80dJv+lDVpXG60WMQvkcxAkNybKsrEIE3ZtKGmPrA==
70367057

70377058
tinymce@^7.4.1:
7038-
version "7.4.1"
7039-
resolved "https://registry.yarnpkg.com/tinymce/-/tinymce-7.4.1.tgz#cae3160a7d5850e3069d6d79625b1c74918f8af9"
7040-
integrity sha512-g1Ieaio5YU+jLEQZkQyxTT8EY/im+TC/CFBPlqDBCNdsF8YQOeLMot+K6vmFOAXhNc85KhP1rC9Dn2X+iBFDGg==
7059+
version "7.9.0"
7060+
resolved "https://registry.yarnpkg.com/tinymce/-/tinymce-7.9.0.tgz#92e2260629de77e864e0a66e61c4a6193cf9cc32"
7061+
integrity sha512-tTrUmUGWqy1BY1WwDYh4WiuHm23LiRTcE1Xq3WLO8HKFzde/d0bTF/hXWOa97zqGh0ndJHx/nysQaNC9Gcd16g==
70417062

70427063
tinyrainbow@^1.2.0:
70437064
version "1.2.0"

0 commit comments

Comments
 (0)