'CKEditor4: changing event.data.defaultValue inside a promisse is not working

I am trying to modify the old plugin "pastebase64" to change the original size of pasted images, so I did this new code on pastebase64/plugin.js:

(function () {
    'use strict';

    CKEDITOR.plugins.add('pastebase64', {
        init: init
    });

    function init(editor) {
        if (editor.addFeature) {
            editor.addFeature({
                allowedContent: 'img[alt,id,!src]{width,height};'
            });
        }
        
        CKEDITOR.on('instanceReady', function(event) {
            event.editor.on('paste', resize);
        });

    }
    
    async function resize(event) {
    
        if(event.data.dataTransfer.$.files.length > 0){
        
            let files = event.data.dataTransfer.$.files;
            let imageType = /^image/;
            let files_array = Array.from(files);
            
            files_array.forEach(async file => {
            
                if(file.type.match(imageType)){
                
                    await file.arrayBuffer().then(arrayBuffer => {
                        return new Blob([new Uint8Array(arrayBuffer)], {type: file.type });
                    }).then(blobOriginal => {
                        return blobToBase64(blobOriginal);
                    }).then(base64original => {
                        return createImageBitmap(file, 120,120,120,120)
                    }).then(imageBitmap => {
                    
                        let canvas = document.createElement('canvas');
                        canvas.width = imageBitmap.width;
                        canvas.height = imageBitmap.height;
                        canvas.getContext('bitmaprenderer')
                                .transferFromImageBitmap(imageBitmap);
                        
                        return new Promise(resolve => {
                            canvas.toBlob(blob => resolve(blob));
                        });
                    }).then(blob => {
                        return blobToBase64(blob);
                    }).then(novoBase64 => {
                        event.data.dataValue.replace(event.data.defaultValue,novoBase64);
                    });
                }
            });
        }
    }
    
    function blobToBase64(blob) {
        return new Promise((resolve, _) => {
            const reader = new FileReader();
            reader.onloadend = () => resolve(reader.result);
            reader.readAsDataURL(blob);
        });
    }
    
})();

But nothing happens, the only lines that I was able to change the pasted content was before the first promisse: file.arrayBuffer().then(), after that any chance of replace or even set another value was in vain.

So how can I achieve that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source