javascript - 自定义嵌入印迹无法重新实例化已经存在的对象
问题描述
QuillJS 自定义内联印迹
我正在尝试在编辑器中插入一个自定义印迹(使用印迹/嵌入),并且该部分的工作原理与操场上显示的一样。但是,如果我尝试重新实例化提交的内容,它会在给类的参数中返回 undefined。
我创建了一个显示这种行为的代码笔。其内容如下所示。
HTML
<div id="editor-container"><br/><span class="proc-link" style="text-decoration: underline;background-color : lightgreen;" data-proc="value"><span contenteditable="false">Test</span></span></div>
CSS
#editor-container {
height: 375px;
}
JS
var Embed = Quill.import('blots/embed');
class ProcLink extends Embed {
static create(value) {
let node = super.create(value);
// give it some margin
node.setAttribute('style', "text-decoration: underline;background-color : lightgreen;");
node.setAttribute('data-proc', value.value);
node.innerHTML = value.text;
return node;
}
}
ProcLink.blotName = 'proc-link';
ProcLink.className = 'proc-link';
ProcLink.tagName = 'span';
Quill.register({
'formats/proc-link': ProcLink
});
var quill = new Quill('#editor-container', {
modules: {
toolbar: [
[{ header: [1, 2, false] }],
['bold', 'italic', 'underline']
]
},
placeholder: 'Compose an epic...',
theme: 'snow' // or 'bubble'
});
var index = quill.getSelection(true).index;//quill is the reference to my instantiated quill obj
var cObj = {text : 'Test', value : 'value'};
quill.insertEmbed(0,"proc-link",cObj)
解决方案
我认为你应该使用value(node)
方法来实现这一点。它返回有关相关印迹的 DOM 信息。它提供了重新实例化。
class ProcLink extends Embed {
static create(value) {
let node = super.create(value);
// give it some margin
node.setAttribute('style', "text-decoration: underline;background-color : lightgreen;");
node.setAttribute('data-proc', value.value);
node.innerHTML = value.text;
return node;
}
static value(node) {
return {
value: node.attributes.dataProc,
text: node.innerHTML,
style: node.attributes.style,
}
}
}
推荐阅读
- ffmpeg - 使用 ffmpeg 的最小 SRT 流示例
- java - Admob 横幅不会显示
- c# - 使用 CSharp.SyntaxFactory 时,如何为 Roslyn 正确指定 nameof 的 SyntaxNodes?
- node.js - 遍历数组以在 Sendgrid 交易电子邮件模板中发送?
- r - 在 R 中用条件绘制一个点
- javascript - 使用 JavaScript 仅查找和替换 HTML 中的整个单词?
- c# - 如何在c#中将颜色作为参数传递?
- c++ - GLUT 环面与相机相撞
- python - Python:如何创建多个函数重复工作并且每个函数工作的次数不同?
- typescript - 打字稿。定义泛型
键作为字符串