简体   繁体   English

如何实现羽毛笔表情符号污点

[英]How to implement Quill Emoji Blot

I am trying to implement an Emoji Blot to Quill Editor, 我正在尝试将Emoji Blot实施到笔芯编辑器,

But I have some issues with the cursor on editor. 但是我在编辑器上的光标有一些问题。

In order to solve this I added a space after the emoji insertion, but when I try to remove them is needed two backspaces to remove the emoji. 为了解决这个问题,我在插入表情符号后添加了一个空格,但是当我尝试删除它们时,需要两个退格键来删除表情符号。 On the first time the cursor stops at the beginning of the emoji, and on the second time the emoji is removed. 第一次,光标停在表情符号的开头,第二次,删除了表情符号。

Does anyone already did something similar to this? 有人已经做过类似的事情吗? How can I get this code to work properly? 如何使此代码正常工作?

Thanks in any advance. 在此先感谢。

 const Embed = Quill.import("blots/embed"); class EmojiBlot extends Embed { static create(classes) { let node = super.create(); classes.split(" ").forEach(iconClass => { node.classList.add(iconClass); }); return node; } static formats(node) { let format = {}; if (node.hasAttribute("class")) { format.class = node.getAttribute("class"); } return format; } static value(node) { return node.getAttribute("class"); } format(name, value) { if (name === "class") { if (value) { this.domNode.setAttribute(name, value); } else { this.domNode.removeAttribute(name, value); } } else { super.format(name, value); } } } EmojiBlot.blotName = "emoji"; EmojiBlot.tagName = "span"; Quill.register({ "formats/emoji": EmojiBlot }); var myEditor = new Quill("#editor-container", { modules: { toolbar: document.getElementById("toolbar") }, placeholder: "Compose an epic...", theme: "snow" // or 'bubble' }); const insertEmoji = function() { let editorSelection = myEditor.getSelection(); const cursorPosition = editorSelection && editorSelection.index ? editorSelection.index : 0; myEditor.insertEmbed(cursorPosition, "emoji", 'icon icon-smiley'); myEditor.insertText(cursorPosition + 1, ' ') myEditor.setSelection(cursorPosition + 2) }; document.querySelector(".emojiButton").addEventListener("click", insertEmoji); 
 #editor-container { height: 200px; } .icon { display: inline-block; height: 1em; width: 1em; margin: 0 .05em 0 .1em; vertical-align: -0.1em; background-repeat: no-repeat; background-position: center center; background-size: 1em 1em; font-size: 20px; } .icon-smiley { background-image: url("https://twemoji.maxcdn.com/2/svg/1f603.svg"); } 
 <link href="//cdn.quilljs.com/1.3.5/quill.snow.css" rel="stylesheet"/> <script src="//cdn.quilljs.com/1.3.5/quill.js"></script> <div id="editor-container" ></div> <div id="toolbar"> <button class="ql-bold"></button> <button class="ql-italic"></button> <button class="emojiButton">:D</button> </div> 

I changed the emoji tag from a span to an img and also replaced: 我将emoji表情标签从跨度更改为img,并替换为:

myEditor.insertText(cursorPosition + 1, ' ')
myEditor.setSelection(cursorPosition + 2)

With

myEditor.setSelection(cursorPosition + 1);

This solves the issue. 这样就解决了问题。 See an example here: https://jsfiddle.net/nadavrt/Ldgfp5pa/ 在此处查看示例: https//jsfiddle.net/nadavrt/Ldgfp5pa/

As to what is causing the span the misbehave, my guess is that Quill fails to register your Blot's width correctly due to an error in Quill itself. 至于导致跨度异常的原因,我猜是由于鹅毛笔本身存在错误,鹅毛笔无法正确注册印迹的宽度。 I would recommend that you open a bug issue on the project's GitHub page. 我建议您在项目的GitHub页面上打开一个错误问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM