Skip to content
Open
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
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,37 @@
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"@rollup/plugin-typescript": "^8.2.5",
"@storybook/addon-essentials": "^8.1.3",
"@storybook/addon-svelte-csf": "^4.1.3",
"@storybook/svelte": "^8.1.3",
"@storybook/svelte-vite": "^8.1.3",
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"@storybook/addon-essentials": "^8.6.14",
"@storybook/addon-svelte-csf": "^5.0.8",
"@storybook/svelte": "8",
"@storybook/svelte-vite": "8",
"@sveltejs/vite-plugin-svelte": "^6.1.3",
"@tinymce/beehive-flow": "^0.19.0",
"@tinymce/eslint-plugin": "^2.3.1",
"@tsconfig/svelte": "^5.0.4",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"babel-loader": "^8.2.2",
"eslint": "^8",
"eslint-plugin-svelte": "^2.39.3",
"eslint-plugin-svelte": "^3.11.0",
"gh-pages": "^6.1.1",
"react": "^18",
"react-dom": "^18",
"rollup": "^2.56.2",
"rollup-plugin-css-only": "^3.1.0",
"rollup-plugin-execute": "^1.1.1",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^7.1.5",
"rollup-plugin-svelte": "^7.2.3",
"rollup-plugin-terser": "^7.0.0",
"storybook": "^8.1.6",
"svelte": "^4.2.17",
"svelte-check": "^3.8.0",
"svelte-loader": "^3.2.0",
"svelte-preprocess": "^5.1.4",
"storybook": "8",
"svelte": "^5.38.3",
"svelte-check": "^4.3.1",
"svelte-loader": "^3.2.4",
"svelte-preprocess": "^6.0.3",
"tinymce": "^8.0.0",
"tslib": "^2.6.2",
"typescript": "^5.4.5",
"vite": "^5.2.13",
"typescript": "^5.9.2",
"vite": "^7.1.3",
"webpack": "^5.76.2"
},
"resolutions": {
Expand Down
80 changes: 49 additions & 31 deletions src/main/component/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@see {@link https://www.tiny.cloud/docs/tinymce/7/svelte-ref/} for the TinyMCE Svelte Technical Reference.
-->

<script lang="ts" context="module">
<script lang="ts" module>
declare let global: { tinymce: TinyMCE }
declare let window: Window & { tinymce: TinyMCE }

Expand Down Expand Up @@ -62,32 +62,52 @@
</script>

<script lang="ts">
import { createEventDispatcher, onDestroy, onMount } from 'svelte';
import { onDestroy, onMount } from 'svelte';
import type { TinyMCE, Editor as TinyMCEEditor } from 'tinymce';
type EditorOptions = Parameters<TinyMCE['init']>[0];
type Channel = `${'4' | '5' | '6' | '7' | '8'}${'' | '-dev' | '-testing' | `.${number}` | `.${number}.${number}`}`;

import { bindHandlers } from './Utils';
export let id: string = uuid('tinymce-svelte'); // default values
export let inline: boolean | undefined = undefined;
export let disabled: boolean = false;
export let readonly: boolean = false;
export let apiKey: string = 'no-api-key';
export let licenseKey: string | undefined = undefined;
export let channel: Channel = '8';
export let scriptSrc: string | undefined = undefined;
export let conf: EditorOptions = {};
export let modelEvents: string = 'change input undo redo';
export let value: string = '';
export let text: string = '';
export let cssClass: string = 'tinymce-wrapper';

let container: HTMLElement;
let element: HTMLElement;
let editorRef: TinyMCEEditor | null;
let lastVal = value;
let disablindCache = disabled;
let readonlyCache = readonly;
import { bindHandlers, type EventHandlers } from './Utils';

interface Props extends Partial<EventHandlers> {
id?: string; // default values
inline?: boolean | undefined;
disabled?: boolean;
readonly?: boolean;
apiKey?: string;
licenseKey?: string | undefined;
channel?: Channel;
scriptSrc?: string | undefined;
conf?: EditorOptions;
modelEvents?: string;
value?: string;
text?: string;
cssClass?: string;
}

let {
id = uuid('tinymce-svelte'),
inline = undefined,
disabled = false,
readonly = false,
apiKey = 'no-api-key',
licenseKey = undefined,
channel = '8',
scriptSrc = undefined,
conf = {},
modelEvents = 'change input undo redo',
value = $bindable(''),
text = $bindable(''),
cssClass = 'tinymce-wrapper',
...eventHandlers
}: Props = $props();

let container: HTMLElement | undefined = $state();
let element: HTMLElement | undefined = $state();
let editorRef: TinyMCEEditor | null = $state(null);
let lastVal = $state(value);
let disablindCache = $state(disabled);
let readonlyCache = $state(readonly);

const setReadonly = (editor: TinyMCEEditor, readonlyValue: boolean) => {
if (typeof editor.mode?.set === 'function') {
Expand All @@ -103,9 +123,7 @@
}
}

const dispatch = createEventDispatcher();

$: {
$effect(() => {
if (editorRef && lastVal !== value) {
editorRef.setContent(value);
text = editorRef.getContent({format: 'text'});
Expand All @@ -118,7 +136,7 @@
disablindCache = disabled;
setDisabled(editorRef, disabled);
}
}
});

const getTinymce = (): TinyMCE | null => {
const getSink = (): { tinymce: TinyMCE } => {
Expand Down Expand Up @@ -152,22 +170,22 @@
}
});
});
bindHandlers(editor, dispatch);
bindHandlers(editor, eventHandlers);
if (typeof conf.setup === 'function') {
conf.setup(editor);
}
},
};
element.style.visibility = '';
element!.style.visibility = '';
void getTinymce()?.init(finalInit);
};

onMount(() => {
if (getTinymce() !== null) {
init();
} else {
const script = scriptSrc ? scriptSrc : `https://cdn.tiny.cloud/1/${apiKey}/tinymce/${channel}/tinymce.min.js`;
scriptLoader.load(container.ownerDocument, script, () => {
scriptLoader.load(container!.ownerDocument, script, () => {
init();
});
}
Expand Down
20 changes: 11 additions & 9 deletions src/main/component/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TinyMCE } from "tinymce";

const validEvents = [
export const validEvents = [
'Activate',
'AddUndo',
'BeforeAddUndo',
Expand Down Expand Up @@ -67,16 +68,17 @@ const validEvents = [
'Show',
'Submit',
'Undo',
'VisualAid' ];
'VisualAid'] as const;

const bindHandlers = (editor, dispatch) => {
validEvents.forEach( (eventName) => {
export type EventHandlers = {
[K in typeof validEvents[number] as Lowercase<K>]: (event: any, editor: TinyMCE) => void;
};


const bindHandlers = (editor, eventHandlers: Partial<EventHandlers>) => {
validEvents.forEach((eventName) => {
editor.on(eventName, (e) => {
dispatch(eventName.toLowerCase(), {
eventName,
event: e,
editor
});
eventHandlers[eventName.toLowerCase()]?.(e, editor);
});
});
};
Expand Down
90 changes: 52 additions & 38 deletions src/stories/Editor.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script context="module">
<script module>
export const meta = {
title: 'Editor',
component: Editor
Expand All @@ -18,10 +18,10 @@ TinyMCE provides a <span style="text-decoration: underline;">full-featured</span
<strong><span style="font-size: 14pt;"><span style="color: #7e8c8d; font-weight: 600;">No matter what you're building, TinyMCE has got you covered.</span></span></strong>
</p>`.trim();

let value = content;
let disabled = true;
let readonly = true;
let text = '';
let value = $state(content);
let disabled = $state(true);
let readonly = $state(true);
let text = $state('');

const toggleDisabled = () => {
disabled = !disabled;
Expand All @@ -32,51 +32,65 @@ TinyMCE provides a <span style="text-decoration: underline;">full-featured</span
const controls = { channel: '8' }
</script>

<Story name="Iframe" args={{ ...controls, inline: false }} let:args>
<Editor {apiKey} {value} {...args}/>
<Story name="Iframe" args={{ ...controls, inline: false }} >
{#snippet children({ args })}
<Editor {apiKey} {value} {...args}/>
{/snippet}
</Story>

<Story name="Inline" args={{ ...controls, inline: true }} let:args>
<div style="padding-top:100px;">
<Editor {apiKey} {value} {...args} />
</div>
<Story name="Inline" args={{ ...controls, inline: true }} >
{#snippet children({ args })}
<div style="padding-top:100px;">
<Editor {apiKey} {value} {...args} />
</div>
{/snippet}
</Story>

<Story name="Value binding" args={controls} let:args>
<div>
<Editor {apiKey} bind:value {...args}/>
<Story name="Value binding" args={controls} >
{#snippet children({ args })}
<div>
{@html value}
<Editor {apiKey} bind:value {...args}/>
<div>
{@html value}
</div>
</div>
</div>
{/snippet}
</Story>

<Story name="Input binding" args={controls} let:args>
<div>
<Editor {apiKey} bind:value {...args}/>
<textarea style="width:100%;height:200px" bind:value></textarea>
</div>
<Story name="Input binding" args={controls} >
{#snippet children({ args })}
<div>
<Editor {apiKey} bind:value {...args}/>
<textarea style="width:100%;height:200px" bind:value></textarea>
</div>
{/snippet}
</Story>

<Story name="Text binding" args={controls} let:args>
<div>
<Editor {apiKey} bind:value bind:text {...args}/>
<div>{text}</div>
<div>{@html value}</div>
<textarea style="width:100%;height:200px" bind:value={text}></textarea>
</div>
<Story name="Text binding" args={controls} >
{#snippet children({ args })}
<div>
<Editor {apiKey} bind:value bind:text {...args}/>
<div>{text}</div>
<div>{@html value}</div>
<textarea style="width:100%;height:200px" bind:value={text}></textarea>
</div>
{/snippet}
</Story>

<Story name="Disabling" args={controls} let:args>
<div>
<button on:click={toggleDisabled}>{#if disabled}Enable{:else}Disable{/if}</button>
<Editor {apiKey} {disabled} {value} {...args}/>
</div>
<Story name="Disabling" args={controls} >
{#snippet children({ args })}
<div>
<button onclick={toggleDisabled}>{#if disabled}Enable{:else}Disable{/if}</button>
<Editor {apiKey} {disabled} {value} {...args}/>
</div>
{/snippet}
</Story>

<Story name="Readonly" args={controls} let:args>
<div>
<button on:click={toggleReadonly}>{#if readonly}Not Readonly{:else}Readonly{/if}</button>
<Editor {apiKey} {readonly} {value} {...args}/>
</div>
<Story name="Readonly" args={controls} >
{#snippet children({ args })}
<div>
<button onclick={toggleReadonly}>{#if readonly}Not Readonly{:else}Readonly{/if}</button>
<Editor {apiKey} {readonly} {value} {...args}/>
</div>
{/snippet}
</Story>
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"sourceMap": true,
"types": ["svelte"],
"ignoreDeprecations": "5.0",
"noImplicitAny": false
"noImplicitAny": false,
"target": "ES2022"
},

"include": ["src/**/*"],
Expand Down
Loading