Search code examples
javascriptsummernote

Summernote onImageUpload doesn't work when i get code content from server


When i create a post my images upload work correctly, but the callbacks get broke on the edit page

Here is my summernote config:

$('#summernote').summernote({
    focus: false,
    lang: 'pt-BR',
    codemirror: { // codemirror options
        theme: 'monokai'
    },
    toolbar: [
        // [groupName, [list of button]]
        ['font', ['style','fontname', 'fontsize'  ]],
        ['style', ['color','bold', 'italic', 'underline', 'clear']],
        ['para', ['ul', 'ol', 'paragraph', 'height']],
        ['insert', ['picture', 'link', 'hr']],
        ['misc', ['codeview']]
    ],
    callbacks: {
        onImageUpload: function (files, editor, welEditable) {
            console.log('oi');

            for (var i = files.length - 1; i >= 0; i--) {
                sendFile(files[i], this)
            }
        },
        onMediaDelete: function ($target, editor, $editable) {
            let url = $target[0].src.split('/imgs/post/')[1]

            $.post({
                url: `http://${window.location.hostname}:3000/remove/foto/${url}`,
                cache: false,
                contentType: false,
                processData: false
            })
            $target.remove()
        }
    }
})

The content fill the editor correctly, but when its setted the callbacks stop working. Getting the content from server:

let content = String({{ data.content | dump | safe }})
if(content.length) $('#summernote').summernote('code', content)

Solution

  • Found a solution

    Getting from server before:

    let content = String({{ data.content | dump | safe }})
    

    Then initializing with method chain:

    $('#summernote').summernote({
        focus: false,
        lang: 'pt-BR',
        code: 'asdpokaposdk',
        codemirror: { // codemirror options
            theme: 'monokai'
        },
        toolbar: [
            // [groupName, [list of button]]
            ['font', ['style','fontname', 'fontsize'  ]],
            ['style', ['color','bold', 'italic', 'underline', 'clear']],
            ['para', ['ul', 'ol', 'paragraph', 'height']],
            ['insert', ['picture', 'link', 'hr']],
            ['misc', ['codeview']]
        ],
        callbacks: {
            onImageUpload: function (files, editor, welEditable) {
                console.log('oi');
    
                for (var i = files.length - 1; i >= 0; i--) {
                    sendFile(files[i], this)
                }
            },
            onMediaDelete: function ($target, editor, $editable) {
                let url = $target[0].src.split('/imgs/post/')[1]
    
                $.post({
                    url: `http://${window.location.hostname}:3000/remove/foto/${url}`,
                    cache: false,
                    contentType: false,
                    processData: false
                })
                $target.remove()
            }
        }
    }).summernote('code', content)