Update rich text field height to be set dynamically

This commit is contained in:
Andrew Kingston 2022-02-03 10:46:41 +00:00
parent 1ec51d2562
commit 5109975433
3 changed files with 44 additions and 21 deletions

View File

@ -1,6 +1,6 @@
<script> <script>
import SpectrumMDE from "./SpectrumMDE.svelte" import SpectrumMDE from "./SpectrumMDE.svelte"
import { createEventDispatcher, onMount } from "svelte" import { createEventDispatcher } from "svelte"
export let value = null export let value = null
export let height = "300px" export let height = "300px"
@ -15,6 +15,7 @@
// Ensure the value is updated if the value prop changes outside the editor's // Ensure the value is updated if the value prop changes outside the editor's
// control // control
$: checkValue(value) $: checkValue(value)
$: mde?.codemirror.on("change", debouncedUpdate)
const checkValue = val => { const checkValue = val => {
if (mde && val !== latestValue) { if (mde && val !== latestValue) {
@ -37,19 +38,17 @@
// Debounce the update function to avoid spamming it constantly // Debounce the update function to avoid spamming it constantly
const debouncedUpdate = debounce(update, 250) const debouncedUpdate = debounce(update, 250)
onMount(() => {
mde.codemirror.on("change", debouncedUpdate)
})
</script> </script>
<SpectrumMDE {#key height}
bind:mde <SpectrumMDE
scroll={true} bind:mde
{height} scroll={true}
{id} {height}
easyMDEOptions={{ {id}
initialValue: value, easyMDEOptions={{
placeholder, initialValue: value,
}} placeholder,
/> }}
/>
{/key}

View File

@ -1,6 +1,5 @@
<script> <script>
import SpectrumMDE from "./SpectrumMDE.svelte" import SpectrumMDE from "./SpectrumMDE.svelte"
import { onMount } from "svelte"
export let value export let value
export let height export let height
@ -9,11 +8,11 @@
// Keep the value up to date // Keep the value up to date
$: mde && mde.value(value || "") $: mde && mde.value(value || "")
$: {
onMount(() => { if (mde && !mde.isPreviewActive()) {
// Turn on preview mode mde.togglePreview()
mde.togglePreview() }
}) }
</script> </script>
<div class="markdown-viewer" style="height:{height};"> <div class="markdown-viewer" style="height:{height};">

View File

@ -1,4 +1,6 @@
<script> <script>
import { getContext, setContext } from "svelte"
import { writable } from "svelte/store"
import { CoreRichTextField } from "@budibase/bbui" import { CoreRichTextField } from "@budibase/bbui"
import Field from "./Field.svelte" import Field from "./Field.svelte"
@ -11,6 +13,28 @@
let fieldState let fieldState
let fieldApi let fieldApi
const component = getContext("component")
const newContext = writable($component)
setContext("component", newContext)
// Extract the settings height so we can pass it on to the rich text field.
// We then wipe the height style so that the field will automatically size
// itself based on the height of the rich text field.
let height
$: {
height = $component.styles?.normal?.height
newContext.set({
...$component,
styles: {
...$component.styles,
normal: {
...$component.styles.normal,
height: undefined,
},
},
})
}
</script> </script>
<Field <Field
@ -31,6 +55,7 @@
error={fieldState.error} error={fieldState.error}
id={fieldState.fieldId} id={fieldState.fieldId}
{placeholder} {placeholder}
{height}
/> />
{/if} {/if}
</Field> </Field>