|
12 | 12 | return prefix + '_' + Math.floor(Math.random() * 1000000000) + String(Date.now()); |
13 | 13 | }; |
14 | 14 |
|
| 15 | + const isDisabledOptionSupported = (editor: TinyMCEEditor): boolean => { |
| 16 | + return typeof editor.options.set === 'function' && editor.options.isRegistered('disabled') |
| 17 | + }; |
| 18 | +
|
15 | 19 | const createScriptLoader = () => { |
16 | 20 | let state: { |
17 | 21 | listeners: Array<() => void>, |
|
67 | 71 | export let id: string = uuid('tinymce-svelte'); // default values |
68 | 72 | export let inline: boolean | undefined = undefined; |
69 | 73 | export let disabled: boolean = false; |
| 74 | + export let readonly: boolean = false; |
70 | 75 | export let apiKey: string = 'no-api-key'; |
71 | 76 | export let licenseKey: string | undefined = undefined; |
72 | 77 | export let channel: Channel = '7'; |
|
82 | 87 | let editorRef: TinyMCEEditor | null; |
83 | 88 | let lastVal = value; |
84 | 89 | let disablindCache = disabled; |
| 90 | + let readonlyCache = readonly; |
85 | 91 | |
| 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 | +
|
86 | 106 | const dispatch = createEventDispatcher(); |
87 | | - |
| 107 | +
|
88 | 108 | $: { |
89 | 109 | if (editorRef && lastVal !== value) { |
90 | 110 | editorRef.setContent(value); |
91 | 111 | text = editorRef.getContent({format: 'text'}); |
92 | 112 | } |
| 113 | + if (editorRef && readonly !== readonlyCache) { |
| 114 | + readonlyCache = readonly; |
| 115 | + setReadonly(editorRef, readonly); |
| 116 | + } |
93 | 117 | if (editorRef && disabled !== disablindCache) { |
94 | 118 | 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); |
103 | 120 | } |
104 | 121 | } |
105 | 122 | |
|
117 | 134 | ...conf, |
118 | 135 | target: element, |
119 | 136 | inline: inline !== undefined ? inline : conf.inline !== undefined ? conf.inline : false, |
120 | | - readonly: disabled, |
121 | 137 | license_key: licenseKey, |
122 | 138 | setup: (editor: TinyMCEEditor) => { |
| 139 | + editor.on('PreInit', () => { |
| 140 | + setDisabled(editor, disabled); |
| 141 | + setReadonly(editor, readonly); |
| 142 | + }); |
123 | 143 | editorRef = editor; |
124 | 144 | editor.on('init', () => { |
125 | 145 | editor.setContent(value); |
|
0 commit comments