Skip to content

Commit 4c821ea

Browse files
committed
TINY-11906: Enable support for readonly mode
1 parent 4ce80bb commit 4c821ea

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/main/ts/components/Editor.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,14 @@ export interface IProps {
9595
toolbar: NonNullable<EditorOptions['toolbar']>;
9696
/**
9797
* @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).
98+
* @description Whether the editor should be "disabled".
9999
*/
100100
disabled: boolean;
101+
/**
102+
* @see {@link https://www.tiny.cloud/docs/tinymce/7/react-ref/#readonly React Tech Ref - readonly}
103+
* @description Whether the editor should be readonly.
104+
*/
105+
readonly: boolean;
101106
/**
102107
* @see {@link https://www.tiny.cloud/docs/tinymce/7/react-ref/#textareaname React Tech Ref - textareaName}
103108
* @description Set the `name` attribute of the `textarea` element used for the editor in forms. Only valid in iframe mode.
@@ -209,9 +214,9 @@ export class Editor extends React.Component<IAllProps> {
209214
}
210215
});
211216
}
212-
if (this.props.disabled !== prevProps.disabled) {
213-
const disabled = this.props.disabled ?? false;
214-
setMode(this.editor, disabled ? 'readonly' : 'design');
217+
if (this.props.readonly !== prevProps.readonly) {
218+
const readonly = this.props.readonly ?? false;
219+
setMode(this.editor, readonly ? 'readonly' : 'design');
215220
}
216221
}
217222
}
@@ -440,7 +445,7 @@ export class Editor extends React.Component<IAllProps> {
440445
...this.props.init as Omit<InitOptions, OmittedInitProps>,
441446
selector: undefined,
442447
target,
443-
readonly: this.props.disabled,
448+
disabled: this.props.disabled,
444449
inline: this.inline,
445450
plugins: mergePlugins(this.props.init?.plugins, this.props.plugins),
446451
toolbar: this.props.toolbar ?? this.props.init?.toolbar,
@@ -477,8 +482,8 @@ export class Editor extends React.Component<IAllProps> {
477482
editor.undoManager.add();
478483
editor.setDirty(false);
479484
}
480-
const disabled = this.props.disabled ?? false;
481-
setMode(this.editor, disabled ? 'readonly' : 'design');
485+
const readonly = this.props.readonly ?? false;
486+
setMode(this.editor, readonly ? 'readonly' : 'design');
482487
// ensure existing init_instance_callback is called
483488
if (this.props.init && isFunction(this.props.init.init_instance_callback)) {
484489
this.props.init.init_instance_callback(editor);

src/stories/Editor.stories.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ export const ToggleDisabledProp: StoryObj<Editor> = {
145145
}
146146
};
147147

148+
export const ToggleReadonlyProp: StoryObj<Editor> = {
149+
render: () => {
150+
const [ readonly, setReadonly ] = React.useState(true);
151+
const toggleReadonly = () => setReadonly((prev) => !prev);
152+
return (
153+
<div>
154+
<Editor
155+
apiKey={apiKey}
156+
initialValue={initialValue}
157+
readonly={readonly}
158+
/>
159+
<button onClick={toggleReadonly}>
160+
{readonly ? 'Enable Editor' : 'Disable Editor'}
161+
</button>
162+
</div>
163+
);
164+
}
165+
};
166+
148167
export const CloudChannelSetTo5Dev: StoryObj<Editor> = {
149168
name: 'Cloud Channel Set To "6-dev"',
150169
render: () => (

0 commit comments

Comments
 (0)