I want to add a couple of pre-built labels like
<div class="label"> Label Text <span>x</span><div>
to the html content in the quill editor. Add such a tag should not be a problem in itself. However I don't want the user to be able to edit the text inside the label. The cursor should not even be allowed to be placed inside the label. On delete the whole div
should be deleted. The whole label should act like an image in some sense.
Is it possible ?
You should be able to achieve this by writing your own Blot:
class Label extends Parchment.Embed {
static create(value) {
const node = super.create(value);
node.innerText = value;
// Remember to set this so users can't edit
// the label's content
node.contentEditable = 'false';
this._addRemovalButton(node);
return node;
}
static value(node) {
// Only return the text of the first child
// node (ie the text node), otherwise the
// value includes the contents of the button
return node.childNodes[0].textContent;
}
static _addRemovalButton(node) {
const button = document.createElement('button');
button.innerText = 'x';
button.onclick = () => node.remove();
button.contentEditable = 'false';
node.appendChild(button);
// Extra span forces the cursor to the end of
// the label, otherwise it appears inside the
// removal button
const span = document.createElement('span');
span.innerText = ' ';
node.appendChild(span);
}
}
Label.blotName = 'label';
Label.tagName = 'SPAN';
Label.className = 'ql-label';
You register it with Quill:
Quill.register(Label);
And finally, you can use it in a similar way to an image
or other embeds:
quill.updateContents(
new Delta().insert({ label: 'foo' })
);
NB: Any styling you need can be applied with the .ql-label
class:
.ql-label {
background-color: lightgrey;
padding: 0.3em;
margin: 0 0.2em;
}
.ql-label button {
margin-left: 0.3em;
}
Finally finally: a working example.
来源:https://stackoverflow.com/questions/39601992/how-to-add-a-non-editable-tag-to-content-in-quill-editor