Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CKEditor 5 insert image by external url

I am wondering how to insert an image by it's URL only (a user gets it from some other website). I need to implement a simple img src="" in CKEditor 5. The problem is that by default, the editor requires me to upload an image while I need to insert an external url.

I have read many related topics (1, 2, 3) but did not find a problem similar to mine. I even do not need the special button, maybe I can somehow just type img src="myurl" (directly typing it inside the editor did not work for me yet) inside CKEditor and then make it to be perceived like an html code after I apply @Html.Raw(Model.Text) to the whole text I have stored in database from CKeditor textarea.

That is what I get after inserting data from the editor to a webpage. I think it is because tags are perceived as text due to security reasons.

enter image description here

P.S. Stack overflow image insertion tool allows to upload image by its url when I click link from the web in dialog. So I want something similar in CKEditor 5.

Will be very grateful for any help!

like image 993
Bogdan Avatar asked Aug 01 '18 21:08

Bogdan


People also ask

How do I import images into CKEditor 5?

Paste or drop an image directly into the editor. You can also use the "Insert image" button in the toolbar. Paste the media URL in the input. CKEditor 5 offers multiple ways to include images in your rich content.

How do I upload an image using CKEditor?

To upload a new image open the upload panel in the image browser. Open the Image info tab and click Browse server. A new window will open where you see all your uploaded images. Open the Settings to choose another upload path.


1 Answers

There's a very simple and concise explanation on how to implement this feature, on their documentation: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/creating-simple-plugin.html

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';

import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';

import imageIcon from '@ckeditor/ckeditor5-core/theme/icons/image.svg';

class InsertImage extends Plugin {
    init() {
        const editor = this.editor;

        editor.ui.componentFactory.add( 'insertImage', locale => {
            const view = new ButtonView( locale );

            view.set( {
                label: 'Insert image',
                icon: imageIcon,
                tooltip: true
            } );

            // Callback executed once the image is clicked.
            view.on( 'execute', () => {
                const imageUrl = prompt( 'Image URL' );

                editor.model.change( writer => {
                    const imageElement = writer.createElement( 'image', {
                        src: imageUrl
                    } );

                    // Insert the image in the current selection location.
                    editor.model.insertContent( imageElement, editor.model.document.selection );
                } );
            } );

            return view;
        } );
    }
}

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        plugins: [ Essentials, Paragraph, Bold, Italic, Image, InsertImage, ImageCaption ],
        toolbar: [ 'bold', 'italic', 'insertImage' ]
    } )
    .then( editor => {
        console.log( 'Editor was initialized', editor );
    } )
    .catch( error => {
        console.error( error.stack );
    } );

The final result:

enter image description here

I hope it was helpful. :)

like image 164
Luís Assunção Avatar answered Sep 29 '22 22:09

Luís Assunção