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

79 lines
2.1 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 { appStore } from '$lib/utils/store/app';
2020-12-29 04:01:30 +02:00
export let id = '';
2023-09-11 13:15:50 +01:00
export let value: string;
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;
let tinmycEditor: any;
let unmount = false
2023-09-05 13:21:44 +00:00
// editor configuration
let conf = {
plugins: 'lists',
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'] }
],
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',
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;
tinmycEditor = editor;
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($appStore.isDark);
2020-12-29 04:01:30 +02:00
</script>
2023-09-05 13:21:44 +00:00
<div>
{#if !unmount}
<Editor bind:value {apiKey} bind:conf />
{/if}
2021-05-08 11:47:18 +03:00
</div>