budibase/packages/bbui/src/Markdown/MarkdownEditor.svelte

56 lines
1.1 KiB
Svelte
Raw Normal View History

2022-02-02 16:17:59 +01:00
<script>
import SpectrumMDE from "./SpectrumMDE.svelte"
import { createEventDispatcher, onMount } from "svelte"
export let value = null
2022-02-02 16:17:59 +01:00
export let height = "300px"
export let placeholder = null
export let id = null
2022-02-02 16:17:59 +01:00
const dispatch = createEventDispatcher()
let latestValue
let mde
// Ensure the value is updated if the value prop changes outside the editor's
// control
2022-02-02 16:17:59 +01:00
$: checkValue(value)
const checkValue = val => {
if (mde && val !== latestValue) {
mde.value(val)
}
}
const debounce = (fn, interval) => {
let timeout
return () => {
clearTimeout(timeout)
timeout = setTimeout(fn, interval)
}
}
const update = () => {
latestValue = mde.value()
dispatch("change", latestValue)
}
// Debounce the update function to avoid spamming it constantly
const debouncedUpdate = debounce(update, 250)
2022-02-02 16:17:59 +01:00
onMount(() => {
mde.codemirror.on("change", debouncedUpdate)
2022-02-02 16:17:59 +01:00
})
</script>
<SpectrumMDE
bind:mde
scroll={true}
{height}
{id}
2022-02-02 16:17:59 +01:00
easyMDEOptions={{
initialValue: value,
placeholder,
}}
/>