Skip to content

Commit 7392d3a

Browse files
TINY-11909: Add support for readonly mode (#77)
* TINY-11909: first implementation * TINY-11909: add changelog entries * TINY-11909: split disable and readonly stories * TINY-11909: fix check * TINY-11909: improvement * TINY-11909: add check for isDisabledOptionSupported * TINY-11909: improvemente * TINY-11909: refactor * TINY-11909: fix * TINY-11909: refactor * TINY-11909: update version * Update src/main/component/Editor.svelte --------- Co-authored-by: tiny-ben-tran <ben.tran@tiny.cloud>
1 parent d2555e3 commit 7392d3a

4 files changed

Lines changed: 49 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111
- tinymce "^v7.0.0 || ^v6.0.0 || ^v5.0.0" is now an optional peer dependency. #INT-3324
1212

13+
### Changed
14+
- `disabled` property is now mapped to the TinyMCE `disabled` option.
15+
16+
### Added
17+
- Added `readonly` property that maps to the TinyMCE `readonly` option.
18+
1319
## 3.0.0 - 2024-06-05
1420

1521
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tinymce/tinymce-svelte",
3-
"version": "3.0.1-rc",
3+
"version": "3.1.0-rc",
44
"description": "TinyMCE Svelte Component",
55
"private": false,
66
"publishConfig": {

src/main/component/Editor.svelte

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
return prefix + '_' + Math.floor(Math.random() * 1000000000) + String(Date.now());
1313
};
1414
15+
const isDisabledOptionSupported = (editor: TinyMCEEditor): boolean => {
16+
return typeof editor.options.set === 'function' && editor.options.isRegistered('disabled')
17+
};
18+
1519
const createScriptLoader = () => {
1620
let state: {
1721
listeners: Array<() => void>,
@@ -67,6 +71,7 @@
6771
export let id: string = uuid('tinymce-svelte'); // default values
6872
export let inline: boolean | undefined = undefined;
6973
export let disabled: boolean = false;
74+
export let readonly: boolean = false;
7075
export let apiKey: string = 'no-api-key';
7176
export let licenseKey: string | undefined = undefined;
7277
export let channel: Channel = '7';
@@ -82,24 +87,36 @@
8287
let editorRef: TinyMCEEditor | null;
8388
let lastVal = value;
8489
let disablindCache = disabled;
90+
let readonlyCache = readonly;
8591
92+
const setReadonly = (editor: TinyMCEEditor, readonlyValue: boolean) => {
93+
if (typeof editor.mode?.set === 'function') {
94+
editor.mode.set(readonlyValue ? 'readonly' : 'design');
95+
}
96+
}
97+
98+
const setDisabled = (editor: TinyMCEEditor, disabledValue: boolean) => {
99+
if (isDisabledOptionSupported(editor)) {
100+
editor.options.set('disabled', disabledValue);
101+
} else {
102+
editor.mode.set(disabledValue ? 'readonly' : 'design');
103+
}
104+
}
105+
86106
const dispatch = createEventDispatcher();
87-
107+
88108
$: {
89109
if (editorRef && lastVal !== value) {
90110
editorRef.setContent(value);
91111
text = editorRef.getContent({format: 'text'});
92112
}
113+
if (editorRef && readonly !== readonlyCache) {
114+
readonlyCache = readonly;
115+
setReadonly(editorRef, readonly);
116+
}
93117
if (editorRef && disabled !== disablindCache) {
94118
disablindCache = disabled;
95-
if (typeof editorRef.mode?.set === 'function') {
96-
editorRef.mode.set(disabled ? 'readonly' : 'design');
97-
} else {
98-
interface TinyMCEEditor4 extends TinyMCEEditor {
99-
setMode: (input: string) => void
100-
}
101-
(editorRef as TinyMCEEditor4).setMode(disabled ? 'readonly' : 'design');
102-
}
119+
setDisabled(editorRef, disabled);
103120
}
104121
}
105122
@@ -117,9 +134,12 @@
117134
...conf,
118135
target: element,
119136
inline: inline !== undefined ? inline : conf.inline !== undefined ? conf.inline : false,
120-
readonly: disabled,
121137
license_key: licenseKey,
122138
setup: (editor: TinyMCEEditor) => {
139+
editor.on('PreInit', () => {
140+
setDisabled(editor, disabled);
141+
setReadonly(editor, readonly);
142+
});
123143
editorRef = editor;
124144
editor.on('init', () => {
125145
editor.setContent(value);

src/stories/Editor.stories.svelte

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</script>
77

88
<script>
9-
import Editor from '../main/component/Editor.svelte';
109
import { Story } from '@storybook/addon-svelte-csf';
10+
import Editor from '../main/component/Editor.svelte';
1111
1212
const apiKey = 'prsghhxax677rv082a1zj9b7cgjuoaqysf7h8ayxi5ao43ha';
1313
const content = `
@@ -20,11 +20,15 @@ TinyMCE provides a <span style="text-decoration: underline;">full-featured</span
2020
2121
let value = content;
2222
let disabled = true;
23+
let readonly = true;
2324
let text = '';
2425
2526
const toggleDisabled = () => {
2627
disabled = !disabled;
2728
}
29+
const toggleReadonly = () => {
30+
readonly = !readonly;
31+
}
2832
const controls = { channel: '7', conf: { plugins: 'help' } }
2933
</script>
3034

@@ -69,3 +73,10 @@ TinyMCE provides a <span style="text-decoration: underline;">full-featured</span
6973
<Editor {apiKey} {disabled} {value} {...args}/>
7074
</div>
7175
</Story>
76+
77+
<Story name="Readonly" args={controls} let:args>
78+
<div>
79+
<button on:click={toggleReadonly}>{#if readonly}Not Readonly{:else}Readonly{/if}</button>
80+
<Editor {apiKey} {readonly} {value} {...args}/>
81+
</div>
82+
</Story>

0 commit comments

Comments
 (0)