当我从服务器获取代码内容时,Summernote onImageUpload不起作用

时间:2018-05-04 03:35:18

标签: javascript summernote

当我创建帖子时,我的图片上传工作正常,但回调在编辑页面上破了

这是我的summernote配置:

$('#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()
        }
    }
})

内容正确填充编辑器,但在设置时,回调停止工作。 从服务器获取内容:

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

1 个答案:

答案 0 :(得分:0)

找到解决方案

之前从服务器获取:

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

然后用方法链初始化:

$('#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)