[英]CKEditor 5 Insert text on button click - Pure JS
I'm trying to create a sort of dropdown menu that a user can insert tokens into a CKEditor with.我正在尝试创建一种下拉菜单,用户可以使用该下拉菜单将令牌插入到 CKEditor 中。 I've found that I can insert text in an editor with this code:我发现我可以使用以下代码在编辑器中插入文本:
editorInstance.model.change( writer => {
let pos = editorInstance.model.document.selection.getFirstPosition();
writer.insertText( "TEST", pos,0);
});
It works when I put it in the ClassicEditor.create.then
for testing but it does nothing when I place this in a $().click
event.当我把它放在ClassicEditor.create.then
进行测试时它起作用,但是当我把它放在$().click
事件中时它什么也不做。 I can log something from inside the callback so I know the code is executed but nothing is inserted.我可以从回调内部记录一些东西,所以我知道代码已执行但没有插入任何内容。
I've been trying to get this working for hours now but all the examples I can find is in angular or any other frameworks.我一直试图让这个工作几个小时,但我能找到的所有例子都是 angular 或任何其他框架。
I Solved the issue you faced as follows我解决了您遇到的问题如下
first defined new editor that is null and Then instatiate Ckeditor 5首先定义为空的新编辑器,然后安装 Ckeditor 5
let neditor = null;
ClassicEditor.create(document.querySelector('#editor'))
.then(editor => {
neditor = editor;
})
.catch(error => {
console.error(error);
});
using pure js使用纯js
document.addEventListener("click", (event) => {
var button = event.target;
if (event.target.type === 'button' && event.target.className.includes('testtokenbutton')) {
neditor.model.change(writer => {
const insertPosition = neditor.model.document.selection.getFirstPosition();
writer.insertText(button.id, insertPosition);
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.