@@ -2,9 +2,12 @@ import * as React from 'react';
2
2
import type { Bookmark , EditorEvent , TinyMCE , Editor as TinyMCEEditor } from 'tinymce' ;
3
3
import { IEvents } from '../Events' ;
4
4
import { ScriptItem , ScriptLoader } from '../ScriptLoader2' ;
5
- import { getTinymce } from '../TinyMCE' ;
6
- import { configHandlers , isBeforeInputEventAvailable , isFunction , isInDoc , isTextareaOrInput , mergePlugins , setMode , uuid } from '../Utils' ;
5
+ import { configHandlers , isBeforeInputEventAvailable ,
6
+ isFunction , isInDoc , isTextareaOrInput , mergePlugins ,
7
+ setMode , uuid , isDisabledOptionSupported ,
8
+ getTinymceOrError } from '../Utils' ;
7
9
import { EditorPropTypes , IEditorPropTypes } from './EditorPropTypes' ;
10
+ import { getTinymce } from '../TinyMCE' ;
8
11
9
12
const changeEvents = 'change keyup compositionend setcontent CommentChange' ;
10
13
@@ -14,14 +17,15 @@ interface DoNotUse<T extends string> {
14
17
__brand : T ;
15
18
}
16
19
17
- type OmittedInitProps = 'selector' | 'target' | 'readonly' | 'license_key' ;
20
+ type OmittedInitProps = 'selector' | 'target' | 'readonly' | 'disabled' | ' license_key';
18
21
19
22
type EditorOptions = Parameters < TinyMCE [ 'init' ] > [ 0 ] ;
20
23
21
24
export type InitOptions = Omit < OmitStringIndexSignature < EditorOptions > , OmittedInitProps > & {
22
25
selector ?: DoNotUse < 'selector prop is handled internally by the component' > ;
23
26
target ?: DoNotUse < 'target prop is handled internally by the component' > ;
24
- readonly ?: DoNotUse < 'readonly prop is overridden by the component, use the `disabled` prop instead' > ;
27
+ readonly ?: DoNotUse < 'readonly prop is overridden by the component' > ;
28
+ disabled ?: DoNotUse < 'disabled prop is overridden by the component' > ;
25
29
license_key ?: DoNotUse < 'license_key prop is overridden by the integration, use the `licenseKey` prop instead' > ;
26
30
} & { [ key : string ] : unknown } ;
27
31
@@ -95,9 +99,14 @@ export interface IProps {
95
99
toolbar : NonNullable < EditorOptions [ 'toolbar' ] > ;
96
100
/**
97
101
* @see {@link https://www.tiny.cloud/docs/tinymce/7/react-ref/#disabled React Tech Ref - disabled }
98
- * @description Whether the editor should be " disabled" (read-only) .
102
+ * @description Whether the editor should be disabled.
99
103
*/
100
104
disabled : boolean ;
105
+ /**
106
+ * @see {@link https://www.tiny.cloud/docs/tinymce/7/react-ref/#readonly React Tech Ref - readonly }
107
+ * @description Whether the editor should be readonly.
108
+ */
109
+ readonly : boolean ;
101
110
/**
102
111
* @see {@link https://www.tiny.cloud/docs/tinymce/7/react-ref/#textareaname React Tech Ref - textareaName }
103
112
* @description Set the `name` attribute of the `textarea` element used for the editor in forms. Only valid in iframe mode.
@@ -209,9 +218,18 @@ export class Editor extends React.Component<IAllProps> {
209
218
}
210
219
} ) ;
211
220
}
221
+
222
+ if ( this . props . readonly !== prevProps . readonly ) {
223
+ const readonly = this . props . readonly ?? false ;
224
+ setMode ( this . editor , readonly ? 'readonly' : 'design' ) ;
225
+ }
226
+
212
227
if ( this . props . disabled !== prevProps . disabled ) {
213
- const disabled = this . props . disabled ?? false ;
214
- setMode ( this . editor , disabled ? 'readonly' : 'design' ) ;
228
+ if ( isDisabledOptionSupported ( this . view ) ) {
229
+ this . editor . options . set ( 'disabled' , this . props . disabled ) ;
230
+ } else {
231
+ setMode ( this . editor , this . props . disabled ? 'readonly' : 'design' ) ;
232
+ }
215
233
}
216
234
}
217
235
}
@@ -431,16 +449,16 @@ export class Editor extends React.Component<IAllProps> {
431
449
return ;
432
450
}
433
451
434
- const tinymce = getTinymce ( this . view ) ;
435
- if ( ! tinymce ) {
436
- throw new Error ( 'tinymce should have been loaded into global scope' ) ;
437
- }
452
+ const tinymce = getTinymceOrError ( this . view ) ;
438
453
439
454
const finalInit : EditorOptions = {
440
455
...this . props . init as Omit < InitOptions , OmittedInitProps > ,
441
456
selector : undefined ,
442
457
target,
443
- readonly : this . props . disabled ,
458
+ ...isDisabledOptionSupported ( this . view )
459
+ ? { disabled : this . props . disabled , readonly : this . props . readonly }
460
+ : { readonly : this . props . disabled || this . props . readonly }
461
+ ,
444
462
inline : this . inline ,
445
463
plugins : mergePlugins ( this . props . init ?. plugins , this . props . plugins ) ,
446
464
toolbar : this . props . toolbar ?? this . props . init ?. toolbar ,
@@ -477,8 +495,7 @@ export class Editor extends React.Component<IAllProps> {
477
495
editor . undoManager . add ( ) ;
478
496
editor . setDirty ( false ) ;
479
497
}
480
- const disabled = this . props . disabled ?? false ;
481
- setMode ( this . editor , disabled ? 'readonly' : 'design' ) ;
498
+
482
499
// ensure existing init_instance_callback is called
483
500
if ( this . props . init && isFunction ( this . props . init . init_instance_callback ) ) {
484
501
this . props . init . init_instance_callback ( editor ) ;
0 commit comments