2020-12-29 04:01:30 +02:00
|
|
|
<script>
|
2022-12-30 13:08:54 +01:00
|
|
|
import { onMount } from 'svelte';
|
2020-12-29 04:01:30 +02:00
|
|
|
|
2022-12-30 13:08:54 +01: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;
|
2023-08-22 11:01:09 +00:00
|
|
|
export let container;
|
2020-12-29 04:01:30 +02:00
|
|
|
|
2022-12-30 13:08:54 +01:00
|
|
|
const settings = {
|
|
|
|
|
modules: {
|
|
|
|
|
toolbar: {
|
|
|
|
|
modules: {
|
|
|
|
|
syntax: true,
|
|
|
|
|
toolbar: [
|
|
|
|
|
[{ header: [1, 2, 3, false] }],
|
|
|
|
|
['bold', 'italic', 'underline', 'strike'],
|
2023-08-22 11:01:09 +00:00
|
|
|
['link', 'code-block']
|
|
|
|
|
]
|
2022-12-30 13:08:54 +01:00
|
|
|
},
|
|
|
|
|
placeholder: 'Type something...',
|
2023-08-22 11:01:09 +00:00
|
|
|
theme: 'snow'
|
|
|
|
|
}
|
2022-12-30 13:08:54 +01:00
|
|
|
},
|
|
|
|
|
theme: 'snow',
|
2023-08-22 11:01:09 +00:00
|
|
|
placeholder
|
2021-05-08 11:47:18 +03:00
|
|
|
};
|
2022-12-30 13:08:54 +01:00
|
|
|
|
|
|
|
|
let quill = null;
|
2023-08-28 13:10:23 +00:00
|
|
|
let isUserTyping = false;
|
2022-12-30 13:08:54 +01:00
|
|
|
|
|
|
|
|
function resetContent(docId) {
|
|
|
|
|
if (quill && quill.setText) {
|
|
|
|
|
if (content) {
|
|
|
|
|
quill.setContents(JSON.parse(content));
|
|
|
|
|
} else {
|
|
|
|
|
quill.setText('');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 11:01:09 +00:00
|
|
|
function setQuillHTML(html) {
|
|
|
|
|
if (quill) {
|
|
|
|
|
quill.setHTML(html);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 13:08:54 +01:00
|
|
|
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());
|
|
|
|
|
});
|
2023-08-28 13:10:23 +00:00
|
|
|
quill.getModule('toolbar').container.addEventListener('mousedown', (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
isUserTyping = true;
|
|
|
|
|
});
|
|
|
|
|
quill.root.addEventListener('focus', () => {
|
|
|
|
|
isUserTyping = true;
|
|
|
|
|
});
|
|
|
|
|
quill.root.addEventListener('blur', () => {
|
|
|
|
|
isUserTyping = false;
|
|
|
|
|
});
|
2022-12-30 13:08:54 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$: {
|
|
|
|
|
if (quill && quill.setText) {
|
|
|
|
|
if (disable) {
|
|
|
|
|
quill.disable(disable);
|
|
|
|
|
} else {
|
|
|
|
|
quill.enable(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 13:10:23 +00:00
|
|
|
$: if (!isUserTyping) {
|
|
|
|
|
setQuillHTML(content);
|
|
|
|
|
}
|
2023-08-22 11:01:09 +00:00
|
|
|
|
2022-12-30 13:08:54 +01:00
|
|
|
$: resetContent(docId);
|
2020-12-29 04:01:30 +02:00
|
|
|
</script>
|
|
|
|
|
|
2022-12-30 13:08:54 +01:00
|
|
|
<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;
|
|
|
|
|
}
|
2023-08-22 11:01:09 +00:00
|
|
|
|
|
|
|
|
: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>
|