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

131 lines
2.6 KiB
Svelte
Raw Normal View History

2020-12-29 04:01:30 +02:00
<script>
import { onMount } from 'svelte';
2020-12-29 04:01:30 +02:00
export let onChange;
export let content;
export let disable;
export let docId;
export let placeholder = 'Type something...';
2023-05-20 23:53:28 +00:00
export let errorMessage;
export let container;
2020-12-29 04:01:30 +02:00
const settings = {
modules: {
toolbar: {
modules: {
syntax: true,
toolbar: [
[{ header: [1, 2, 3, false] }],
['bold', 'italic', 'underline', 'strike'],
['link', 'code-block']
]
},
placeholder: 'Type something...',
theme: 'snow'
}
},
theme: 'snow',
placeholder
2021-05-08 11:47:18 +03:00
};
let quill = null;
let isUserTyping = false;
function resetContent(docId) {
if (quill && quill.setText) {
if (content) {
quill.setContents(JSON.parse(content));
} else {
quill.setText('');
}
}
}
function setQuillHTML(html) {
if (quill) {
quill.setHTML(html);
}
}
onMount(async () => {
const { default: Quill } = await import('quill');
Quill.prototype.getHTML = function () {
return this.container.querySelector('.ql-editor').innerHTML;
};
Quill.prototype.setHTML = function (html) {
this.container.querySelector('.ql-editor').innerHTML = html;
};
quill = new Quill(container, settings);
quill.setHTML(content || '');
quill.on('text-change', function () {
onChange(quill.getHTML());
});
quill.getModule('toolbar').container.addEventListener('mousedown', (e) => {
e.preventDefault();
isUserTyping = true;
});
quill.root.addEventListener('focus', () => {
isUserTyping = true;
});
quill.root.addEventListener('blur', () => {
isUserTyping = false;
});
});
$: {
if (quill && quill.setText) {
if (disable) {
quill.disable(disable);
} else {
quill.enable(true);
}
}
}
$: if (!isUserTyping) {
setQuillHTML(content);
}
$: resetContent(docId);
2020-12-29 04:01:30 +02:00
</script>
<div class="p-2 w-full h-full">
<div bind:this={container} />
2023-05-20 23:53:28 +00:00
{#if errorMessage}
<p class="dark:text-white text-red-500 text-sm">
{errorMessage}
</p>
{/if}
2021-05-08 11:47:18 +03:00
</div>
2023-05-20 23:53:28 +00:00
<style>
:global(.dark .ql-stroke) {
stroke: #fff;
}
:global(.dark .ql-snow .ql-picker) {
color: #fff;
}
:global(.dark .ql-editor.ql-blank::before) {
color: #fff;
}
:global(.ql-editor) {
white-space: normal !important;
}
:global(.ql-editor p),
:global(.ql-editor h1),
:global(.ql-editor h2),
:global(.ql-editor h3),
:global(.ql-editor h4) {
line-height: 2.2;
}
2023-05-20 23:53:28 +00:00
</style>