Files
classroomio/apps/dashboard/src/lib/components/TextEditor/index.svelte
T

119 lines
3.1 KiB
Svelte
Raw Normal View History

2023-09-11 13:15:50 +01:00
<script lang="ts">
2024-05-14 15:22:30 +01:00
import Editor from './TinymceSvelte/index.svelte';
import { globalStore } from '$lib/utils/store/app';
import { addMathPlugin } from '$lib/utils/functions/tinymce/plugins';
2025-11-01 15:49:29 +01:00
import { untrack } from 'svelte';
2020-12-29 04:01:30 +02:00
2025-11-01 15:49:29 +01:00
interface Props {
id?: string;
value?: string | undefined;
onChange?: any;
height?: number;
placeholder?: string;
editorWindowRef?: Window | undefined;
maxHeight?: number | undefined;
}
2020-12-29 04:01:30 +02:00
2025-11-01 15:49:29 +01:00
let {
id = '',
value = $bindable(),
onChange = (_html: string) => {},
height = 300,
placeholder = '',
editorWindowRef = $bindable(undefined),
maxHeight = undefined
}: Props = $props();
let unmount = $state(false);
2024-04-19 05:45:48 +01:00
let editorChangeHandlerId;
function getTinymce() {
const getSink = () => {
return typeof window !== 'undefined' ? window : global;
};
const sink = getSink();
2024-05-14 15:22:30 +01:00
const res = sink && sink.tinymce ? sink.tinymce : null;
console.log({ res });
return res;
}
2023-09-05 13:21:44 +00:00
// editor configuration
2025-11-01 15:49:29 +01:00
let conf = $state({
plugins: 'lists, link, emoticons, code, media, searchreplace, eqneditor',
2023-09-05 13:21:44 +00:00
toolbar: [
{ name: 'history', items: ['undo', 'redo'] },
{ name: 'styles', items: ['styles'] },
{ name: 'formatting', items: ['bold', 'italic'] },
{ name: 'alignment', items: ['alignleft', 'aligncenter', 'alignright', 'alignjustify'] },
{ name: 'indentation', items: ['outdent', 'indent'] },
{ name: 'list', items: ['numlist', 'bullist'] },
{ name: 'link', items: ['link'] },
{ name: 'insert', items: ['emoticons'] },
{ name: 'code', items: ['code'] },
{ name: 'media', items: ['media'] },
{ name: 'search', items: ['searchreplace'] },
{ name: 'eqneditor', items: ['eqneditor'] }
2023-09-05 13:21:44 +00:00
],
lists_indent_on_tab: false,
min_height: height,
max_height: maxHeight,
2023-09-05 13:21:44 +00:00
placeholder: placeholder,
skin: 'oxide-dark',
content_css: 'dark',
extended_valid_elements: '*[.*]',
2024-04-15 21:37:59 +01:00
license_key: 'gpl',
setup: () => {
addMathPlugin(getTinymce());
},
2023-09-11 13:15:50 +01:00
init_instance_callback: function (editor: any) {
2023-09-05 13:21:44 +00:00
editorWindowRef = editor.iframeElement?.contentWindow;
2024-04-19 05:45:48 +01:00
editor.on('Paste Change input Undo Redo', function () {
clearTimeout(editorChangeHandlerId);
const html = editor.getContent();
2024-04-19 05:45:48 +01:00
editorChangeHandlerId = setTimeout(function () {
if (onChange) {
onChange(html);
}
if (id) {
localStorage.setItem(id, html);
}
}, 1000);
2023-09-05 13:21:44 +00:00
});
}
2025-11-01 15:49:29 +01:00
});
2023-09-11 13:15:50 +01:00
function handleModeChange(isDark: boolean) {
if (isDark) {
conf.content_css = 'dark';
conf.skin = 'oxide-dark';
} else {
conf.content_css = 'light';
conf.skin = 'oxide';
}
2025-11-01 15:49:29 +01:00
untrack(() => {
unmount = true;
setTimeout(() => {
unmount = false;
}, 200);
});
}
2025-11-01 15:49:29 +01:00
$effect(() => {
handleModeChange($globalStore.isDark);
});
2020-12-29 04:01:30 +02:00
</script>
2024-05-14 15:22:30 +01:00
<svelte:head>
<link rel="stylesheet" href="https://unpkg.com/katex@0.12.0/dist/katex.min.css" />
</svelte:head>
2023-09-05 13:21:44 +00:00
<div>
{#if !unmount}
2024-05-14 15:22:30 +01:00
<Editor bind:value bind:conf />
{/if}
2021-05-08 11:47:18 +03:00
</div>