<script>
    import Codemirror from "../../libs/CodemirrorMarkdown.svelte";
    import { getErrorMessage } from "./errorMessage";
    import RichEditorFiles from "./RichEditorFiles.svelte";


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

    function insertMedia(e){
        editor.insertMedia(e.detail)
    }
</script>

<div class="mb-3">
    
    <Codemirror bind:this={editor} bind:value editable={!field.readonly || isCreateMode} />
    {#if field.collections.length > 0}
        <RichEditorFiles
                bind:graph
                {record}
                {field}
                {validationErrors}
                on:editor-insert={insertMedia}
        >
        </RichEditorFiles>
    {/if}
    {#if errorMessage}
        <div class="invalid-feedback d-block">
            {errorMessage}
        </div>
    {/if}
</div>
