Bog*_*dan 6 javascript image ckeditor asp.net-core ckeditor5
我想知道如何仅通过其 URL 插入图像(用户从其他网站获取)。我需要在 CKEditor 5 中实现一个简单的img src=""。问题是,默认情况下,编辑器要求我上传图像,而我需要插入外部 url。
我已经阅读了许多相关主题(1、2、3),但没有发现与我类似的问题。我什至不需要特殊按钮,也许我可以以某种方式在 CKEditor 中输入img src="myurl"(直接在编辑器中输入它对我不起作用)然后让它在我之后被感知为一个 html 代码将@Html.Raw(Model.Text)应用于我从 CKeditor textarea 存储在数据库中的整个文本。
这就是我将数据从编辑器插入网页后得到的结果。我认为这是因为出于安全原因,标签被视为文本。
PS Stack 溢出图像插入工具允许当我在对话框中单击来自网络的链接时通过其 url 上传图像。所以我想要 CKEditor 5 中类似的东西。
将非常感谢任何帮助!
关于如何实现此功能,在他们的文档中有一个非常简单明了的解释: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 );
} );
Run Code Online (Sandbox Code Playgroud)
最终结果:
我希望它有帮助。:)