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

96 lines
2.7 KiB
Svelte
Raw Normal View History

2023-09-11 13:15:50 +01:00
<script lang="ts">
2023-09-05 13:21:44 +00:00
import { PUBLIC_TINYMCE_API_KEY } from '$env/static/public';
import Editor from '@tinymce/tinymce-svelte';
import { globalStore } from '$lib/utils/store/app';
import { addMathPlugin } from '$lib/utils/functions/tinymce/plugins';
2020-12-29 04:01:30 +02:00
export let id = '';
2023-12-04 19:39:39 +03:00
export let value: string | undefined = '';
export let onChange = (html: string) => {};
2023-09-05 13:21:44 +00:00
export let height = 300;
export let placeholder = '';
2023-11-01 09:45:40 +00:00
export let editorWindowRef: Window | undefined = undefined;
export let maxHeight: number | undefined = undefined;
2020-12-29 04:01:30 +02:00
2023-09-05 13:21:44 +00:00
const apiKey = PUBLIC_TINYMCE_API_KEY;
2023-12-04 19:39:39 +03:00
let unmount = false;
function getTinymce() {
const getSink = () => {
return typeof window !== 'undefined' ? window : global;
};
const sink = getSink();
return sink && sink.tinymce ? sink.tinymce : null;
}
2023-09-05 13:21:44 +00:00
// editor configuration
let conf = {
plugins: 'lists, link, emoticons, code, media, 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: '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;
editor.on('Change', function () {
2023-09-05 13:21:44 +00:00
const html = editor.getContent();
if (onChange) {
onChange(html);
}
// backup in case the data doesn't get to our backend, we should store the note to avoid data loss
if (id) {
localStorage.setItem(id, html);
}
2023-09-05 13:21:44 +00:00
});
}
2021-05-08 11:47:18 +03: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';
}
unmount = true;
setTimeout(() => {
unmount = false;
}, 200);
}
2023-09-11 13:15:50 +01:00
$: value = !value ? '' : value;
$: handleModeChange($globalStore.isDark);
2020-12-29 04:01:30 +02:00
</script>
2023-09-05 13:21:44 +00:00
<div>
{#if !unmount}
2023-12-04 19:39:39 +03:00
<Editor bind:value {apiKey} bind:conf />
{/if}
2021-05-08 11:47:18 +03:00
</div>