CKEditor 5 通过外部 url 插入图像

2024-04-15

我想知道如何仅通过 URL 插入图像(用户从其他网站获取它)。我需要实现一个简单的img 源=“”在 CKEditor 5 中。问题是,默认情况下,编辑器要求我上传图像,而我需要插入外部 url。

我读过很多相关主题(1 https://stackoverflow.com/questions/49264105/ckeditor-5-and-image-button, , 3 https://docs.ckeditor.com/ckeditor5/latest/features/image-upload.html)但没有发现和我类似的问题。我什至不需要特殊的按钮,也许我可以以某种方式输入img src =“myurl”(直接在编辑器中输入它对我来说还不起作用)在 CKEditor 中,然后在我应用后使其被感知为 html 代码@Html.Raw(模型.文本)到我从 CKeditor 文本区域存储在数据库中的整个文本。

这就是我将数据从编辑器插入网页后得到的结果。 我认为这是因为出于安全原因标签被视为文本。

附:堆栈溢出图像插入工具允许在我单击时通过其网址上传图像来自网络的链接在对话框中。所以我想要 CKEditor 5 中类似的东西。

将非常感谢任何帮助!


在他们的文档中,关于如何实现此功能有一个非常简单明了的解释:https://ckeditor.com/docs/ckeditor5/latest/framework/guides/creating-simple-plugin.html 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 );
    } );

最终结果:

我希望这有帮助。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CKEditor 5 通过外部 url 插入图像 的相关文章

随机推荐