Reputation: 189
I'm in the process of implementing a custom upload adapter for CKEditor 5 using VueJS3. Basing on the example provided in this StackOverflow question, I did the following:
import UploadAdapter from '@/Shared/UploadAdapter';
Uploader()
function in the component's methods and also included the extraPlugins: [this.uploader]
in ck's editorConfig. (find code of vue component below)Now the problem is I'm getting this strange error that tells me ReferenceError: $ is not defined when I drag and drop an image into the editor.
All search results on google point to this error being caused by lack of jquery. This confuses me because I'm using vuejs 3 and not jquery. Nevertheless I installed jquery using npm -i jquery
and imported it into my vue component but still no luck.
I need help pointing out what triggers the ReferenceError: $ is not defined
.
Here is my UploadAdapter.vue file
<script>
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', '/editor/file/upload', true );
xhr.setRequestHeader('x-csrf-token', $('meta[name="csrf-token"]').attr('content'));
//_token: '{{csrf_token()}}'
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 );
}
}
</script>
And here is my vue component
<template>
<app-layout>
<template #header>
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
Test Editor
</h2>
</template>
<div class="py-2 md:py-12">
<ckeditor
:editor="editor"
v-model="form.description"
:config="editorConfig"
tag-name="textarea" />
</div>
</app-layout>
</template>
<script>
import CKEditor from '@ckeditor/ckeditor5-vue';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import UploadAdapter from '@/Shared/UploadAdapter';
//import SimpleUploadAdapter from '@ckeditor/ckeditor5-build-classic';
export default {
components: {
ckeditor: CKEditor.component
},
data() {
return {
form: this.$inertia.form({
description: '',
}),
editor: ClassicEditor,
editorConfig: {
toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', '|', 'insertTable', '|', 'imageUpload', 'mediaEmbed', '|', 'undo', 'redo' ],
table: {
toolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
},
extraPlugins: [this.uploader],
language: 'en',
},
}
},
methods: {
uploader(editor) {
editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
return new UploadAdapter( loader );
};
},
}
}
</script>
<style>
.ck-editor__editable {
min-height: 300px;
border-bottom-left-radius: 0.375rem !important;
border-bottom-right-radius: 0.375rem !important;
}
</style>
Thank you!
Upvotes: 2
Views: 1362
Reputation: 396
It looks like you are attempting to use jQuery in UpoadAdapter.vue
to select the content from the csrf-token meta tag.
xhr.setRequestHeader('x-csrf-token', $('meta[name="csrf-token"]').attr('content'));
Two things you could try
UploadAdapter.vue
: (I would advise against this while using Vue)import { $ } from 'jQuery';
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
if (csrfToken) {
xhr.setRequestHeader('x-csrf-token', csrfToken);
}
Upvotes: 1