Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS CKeditor5 upload images

Having trouble with uploading images using CKeditor5 in Vuejs.

First having tried Simple upload Adapter which gave me the following error:

Reason: CKEditorError: ckeditor-duplicated-modules: Some CKEditor 5 modules are duplicated. Read more: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-ckeditor-duplicated-modules

I tried making a upload adapter. As a uploadadapter I took the example and modified the url. The uploadadapter.js file looks like the following:

    export default class UploadAdapter {
        constructor( loader ) {
            // The file loader instance to use during the upload.
            this.loader = loader;
        }
    
        // Starts the upload process.
        upload() {
            return this.loader.file
                .then( file => new Promise( ( resolve, reject ) => {
                    this._initRequest();
                    this._initListeners( resolve, reject, file );
                    this._sendRequest( file );
                } ) );
        }
    
        // Aborts the upload process.
        abort() {
            if ( this.xhr ) {
                this.xhr.abort();
            }
        }
    
        // Initializes the XMLHttpRequest object using the URL passed to the constructor.
        _initRequest() {
            const xhr = this.xhr = new XMLHttpRequest();
    
            xhr.open( 'POST', '<url here>', true );
            xhr.responseType = 'json';
        }
    
        // Initializes XMLHttpRequest listeners.
        _initListeners( resolve, reject, file ) {
            const xhr = this.xhr;
            const loader = this.loader;
            const genericErrorText = `Couldn't upload file: ${ file.name }.`;
    
            xhr.addEventListener( 'error', () => reject( genericErrorText ) );
            xhr.addEventListener( 'abort', () => reject() );
            xhr.addEventListener( 'load', () => {
                const response = xhr.response;
    
                if ( !response || response.error ) {
                    return reject( response && response.error ? response.error.message : genericErrorText );
                }
   
                resolve( {
                    default: response.url
                } );
            } );
    
            if ( xhr.upload ) {
                xhr.upload.addEventListener( 'progress', evt => {
                    if ( evt.lengthComputable ) {
                        loader.uploadTotal = evt.total;
                        loader.uploaded = evt.loaded;
                    }
                } );
            }
        }
    
        // Prepares the data and sends the request.
        _sendRequest( file ) {
            // Prepare the form data.
            const data = new FormData();
    
            data.append( 'upload', file );
    
            // Send the request.
            this.xhr.send( data );
        }
    }

The Vue component:

    <template>
        <form @submit.prevent="store">
            <ckeditor
                :editor="editor"
                v-model="form.content"
                :error-messages="errors.content"
                :config="editorConfig"
            />
        </form>
    </template>
    
    <script>
        import CKEditor from '@ckeditor/ckeditor5-vue';
        import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
        import UploadAdapter from '../../UploadAdapter';
    
        export default {
            data()
            {
                return {
                    form: {
                        content: null,
                    },
                    editor: ClassicEditor,
                    editorConfig: {
                        toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', '|', 'insertTable', '|', 'imageUpload', 'mediaEmbed', '|', 'undo', 'redo' ],
                        table: {
                            toolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
                        },
                        extraPlugin: [this.uploader],
                        language: 'nl',
                    },
                }
            },
    
            methods: {
                store()
                {
                    // Some code
                },
    
                uploader(editor)
                {
                    editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
                        return new UploadAdapter( loader );
                    };
                },
            },
    
            components: {
                ckeditor: CKEditor.component
            }
        }
    </script>

However each time when trying to upload a file the following warning is returned:

filerepository-no-upload-adapter: Upload adapter is not defined. Read more: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-filerepository-no-upload-adapter

Have looked at the url but it just sends me in circles thus making no progress. What I'm looking for is an example that at least sends a file to the server without errors/ warnings. If the uploadadapter can be scraped and something else except CKfinder can be used that's fine. For now I guess the problem is most likely to be in the Vue component.

like image 454
SuperDJ Avatar asked Dec 05 '19 08:12

SuperDJ


People also ask

How do I upload files to ckeditor5?

Use the image. upload. types configuration option to define the allowed image MIME types that can be uploaded to CKEditor 5. By default, users are allowed to upload jpeg , png , gif , bmp , webp and tiff files, but you can customize this behavior to accept, for example, SVG files.


1 Answers

use extraPlugins instead of extraPlugin.

like image 160
meer Avatar answered Oct 18 '22 02:10

meer