I have still no idea why the question has downvotes, but however here is the solution:
(我仍然不知道为什么这个问题有争议,但是这里是解决方案:)
Import the embed blot - important: not "block", not "embed", "block/embed"!
(导入嵌入污点 -重要:不要“阻止??”,不要“嵌入”,“阻止/嵌入”!)
var Embed = Quill.import('blots/block/embed');
Define a new class that extends that Embed
(定义一个扩展该Embed的新类)
class Hr extends Embed {
static create(value) {
let node = super.create(value);
// give it some margin
node.setAttribute('style', "height:0px; margin-top:10px; margin-bottom:10px;");
return node;
}
}
Define your tag
(定义标签)
Hr.blotName = 'hr'; //now you can use .ql-hr classname in your toolbar
Hr.className = 'my-hr';
Hr.tagName = 'hr';
Write a custom handler for the <hr> button
(为<hr>按钮编写一个自定义处理程序)
var customHrHandler = function(){
// get the position of the cursor
var range = quill.getSelection();
if (range) {
// insert the <hr> where the cursor is
quill.insertEmbed(range.index,"hr","null")
}
}
Register your new format
(注册您的新格式)
Quill.register({
'formats/hr': Hr
});
Instantiate your Editor with the right selectors in your HTML
(在HTML中使用正确的选择器实例化您的编辑器)
var quill = new Quill('#editor', {
modules: {
toolbar: { container: '#toolbar-container',
handlers: {
'hr': customHrHandler
}
}
},
theme: 'snow'
});
The HTML part remains the same.
(HTML部分保持不变。)
The whole <hr> functionality can be done similar to the <img> format .(整个<hr>功能可以类似于<img>格式来完成。)
Thank you, you helpful community.
(谢谢您,对我们有帮助的社区。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…