<script>
    import { getContext } from "svelte";
    import { htmlurl, presetUrl } from "../../files/imageserver";
    import Codemirror from "../../libs/CodemirrorMarkdown.svelte";
    import { getErrorMessage } from "./errorMessage";
    import RichEditorFiles from "./RichEditorFiles.svelte";
    const channel = getContext("channel");
    export let value;
    export let field;

    export let record;
    export let isCreateMode;
    // export let id;
    export let validationErrors;
    $: errorMessage = getErrorMessage(validationErrors, field.name);
    let editor;

    function onFilesInserted(e) {
        const presetPath = e.detail.preset.path;
        editor.insertMedia(channel, e.detail.files, presetPath);
    }
</script>

<div class="mb-3">
    <RichEditorFiles {record} {field} {validationErrors} {onFilesInserted}
    ></RichEditorFiles>
    <Codemirror
        bind:this={editor}
        bind:value
        editable={!field.readonly || isCreateMode}
    />

    {#if errorMessage}
        <div class="invalid-feedback d-block">
            {errorMessage}
        </div>
    {/if}
</div>
