Skip to content

TINY-11908: Add new readonly prop #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added
- The readonly prop now maps to the readonly editor option. See: [Editor important options: readonly](https://www.tiny.cloud/docs/tinymce/latest/editor-important-options/#readonly)

### Changed
- The disabled prop now maps to the disabled editor option. See: [Editor important options: disabled](https://www.tiny.cloud/docs/tinymce/latest/editor-important-options/#disabled)

## 6.1.0 - 2024-10-22

### Changed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This package is a thin wrapper around [TinyMCE](https://github.com/tinymce/tinym

### Support

Version 7.0 is intended to support the tinymce version 7.6 and above.
Version 4.0 is intended to support Vue 3. For Vue 2.x and below please use previous versions of the wrapper.

### Issues
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tinymce/tinymce-vue",
"version": "6.1.1-rc",
"version": "7.0.0-rc",
"description": "Official TinyMCE Vue 3 Component",
"private": false,
"repository": {
Expand Down
5 changes: 4 additions & 1 deletion src/main/ts/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ const mergePlugins = (initPlugins: string | string[] | undefined, inputPlugins?:
const isNullOrUndefined = (value: any): value is null | undefined =>
value === null || value === undefined;

const isDisabledOptionSupported = (editor: TinyMCEEditor): boolean => typeof editor.options.set === 'function' && editor.options.isRegistered('disabled');

export {
bindHandlers,
bindModelHandlers,
Expand All @@ -163,5 +165,6 @@ export {
uuid,
isTextarea,
mergePlugins,
isNullOrUndefined
isNullOrUndefined,
isDisabledOptionSupported
};
56 changes: 38 additions & 18 deletions src/main/ts/components/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { ScriptLoader } from '../ScriptLoader';
import { getTinymce } from '../TinyMCE';
import { isTextarea, mergePlugins, uuid, isNullOrUndefined, initEditor } from '../Utils';
import { isTextarea, mergePlugins, uuid, isNullOrUndefined, initEditor, isDisabledOptionSupported } from '../Utils';
import { editorProps, IPropTypes } from './EditorPropTypes';
import { h, defineComponent, onMounted, ref, Ref, toRefs, nextTick, watch, onBeforeUnmount, onActivated, onDeactivated } from 'vue';
import type { Editor as TinyMCEEditor, EditorEvent, TinyMCE } from 'tinymce';
Expand All @@ -34,9 +34,9 @@ export const Editor = defineComponent({
props: editorProps,
setup: (props: IPropTypes, ctx) => {
let conf = props.init ? { ...props.init, ...defaultInitValues } : { ...defaultInitValues };
const { disabled, modelValue, tagName } = toRefs(props);
const { disabled, readonly, modelValue, tagName } = toRefs(props);
const element: Ref<Element | null> = ref(null);
let vueEditor: any = null;
let vueEditor: TinyMCEEditor | null = null;
const elementId: string = props.id || uuid('tiny-vue');
const inlineEditor: boolean = (props.init && props.init.inline) || props.inline;
const modelBind = !!ctx.attrs['onUpdate:modelValue'];
Expand All @@ -52,7 +52,8 @@ export const Editor = defineComponent({
const content = getContent(mounting);
const finalInit = {
...conf,
readonly: props.disabled,
readonly: props.readonly,
disabled: props.disabled,
target: element.value,
plugins: mergePlugins(conf.plugins, props.plugins),
toolbar: props.toolbar || (conf.toolbar),
Expand All @@ -72,21 +73,36 @@ export const Editor = defineComponent({
getTinymce().init(finalInit);
mounting = false;
};
watch(readonly, (isReadonly) => {
if (vueEditor !== null && isDisabledOptionSupported(vueEditor)) {
if (typeof vueEditor.mode?.set === 'function') {
vueEditor.mode.set(isReadonly ? 'readonly' : 'design');
} else {
(vueEditor as any).setMode(isReadonly ? 'readonly' : 'design');
}
}
});
watch(disabled, (disable) => {
if (vueEditor !== null) {
if (typeof vueEditor.mode?.set === 'function') {
vueEditor.mode.set(disable ? 'readonly' : 'design');
if (isDisabledOptionSupported(vueEditor)) {
vueEditor.options.set('disabled', disable);
} else {
vueEditor.setMode(disable ? 'readonly' : 'design');
if (typeof vueEditor.mode?.set === 'function') {
vueEditor.mode.set(disable ? 'readonly' : 'design');
} else {
(vueEditor as any).setMode(disable ? 'readonly' : 'design');
}
}
}
});
watch(tagName, (_) => {
if (!modelBind) {
cache = vueEditor.getContent();
if (vueEditor) {
if (!modelBind) {
cache = vueEditor.getContent();
}
getTinymce()?.remove(vueEditor);
nextTick(() => initWrapper());
}
getTinymce()?.remove(vueEditor);
nextTick(() => initWrapper());
});
onMounted(() => {
if (getTinymce() !== null) {
Expand Down Expand Up @@ -116,17 +132,21 @@ export const Editor = defineComponent({
}
});
onDeactivated(() => {
if (!modelBind) {
cache = vueEditor.getContent();
if (vueEditor) {
if (!modelBind) {
cache = vueEditor.getContent();
}
getTinymce()?.remove(vueEditor);
}
getTinymce()?.remove(vueEditor);
});
}
const rerender = (init: EditorOptions) => {
cache = vueEditor.getContent();
getTinymce()?.remove(vueEditor);
conf = { ...conf, ...init, ...defaultInitValues };
nextTick(() => initWrapper());
if (vueEditor) {
cache = vueEditor.getContent();
getTinymce()?.remove(vueEditor);
conf = { ...conf, ...init, ...defaultInitValues };
nextTick(() => initWrapper());
}
};
ctx.expose({
rerender,
Expand Down
2 changes: 2 additions & 0 deletions src/main/ts/components/EditorPropTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface IPropTypes {
toolbar: string[] | string;
modelValue: string;
disabled: boolean;
readonly: boolean;
tinymceScriptSrc: string;
}

Expand All @@ -43,6 +44,7 @@ export const editorProps: CopyProps<IPropTypes> = {
toolbar: [ String, Array ],
modelValue: String,
disabled: Boolean,
readonly: Boolean,
tinymceScriptSrc: String,
outputFormat: {
type: String,
Expand Down
12 changes: 10 additions & 2 deletions src/stories/Editor.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Story } from '@storybook/vue3';
import { onBeforeMount, ref } from 'vue';
import { ScriptLoader } from '../main/ts/ScriptLoader';

import type { EditorEvent, Editor as TinyMCEEditor } from 'tinymce';
import { Editor } from '../main/ts/components/Editor';
import type { Editor as TinyMCEEditor, EditorEvent } from 'tinymce';

const apiKey = 'qagffr3pkuv17a8on1afax661irst1hbr4e6tbv888sz91jc';
const content = `
Expand Down Expand Up @@ -159,24 +159,32 @@ export const Disable: Story = (args) => ({
const cc = args.channel || lastChannel;
const conf = getConf(args.conf);
const disabled = ref(false);
const readonly = ref(false);
const toggleDisabled = (_) => {
disabled.value = !disabled.value;
}
const toggleReadonly = (_) => {
readonly.value = !readonly.value;
}
return {
apiKey,
content,
cloudChannel: cc,
conf,
disabled,
toggleDisabled
readonly,
toggleDisabled,
toggleReadonly
}
},
template: `
<div>
<button @click="toggleDisabled">{{ disabled ? 'enable' : 'disable' }}</button>
<button @click="toggleReadonly">{{ readonly ? 'design' : 'readonly' }}</button>
<editor
api-key="${apiKey}"
v-bind:disabled="disabled"
v-bind:readonly="readonly"
:init="conf"
v-model="content"
/>
Expand Down