How can I add an attribute to every Block
element in quilljs?
I know that I can extend a Block
, but is it possible to add an attribute even when pressing "Enter" on the keyboard?
let Block = Quill.import('blots/block');
class ParaBlot extends Block {
static create(value) {
let node = super.create();
return node;
}
}
To add an attribute like style or class to every Block element,
We could extend the default block blot element from the parchment lib,
Just like you are already doing, we import the
Block Blot
element and we extend thecreate(...)
method to edit the node's attributes using thesetAttribute
function.Here i'll be adding a border and a customBlock class for example.
Finnaly we override the default BlockBlot with ours
Here is a demo https://codepen.io
This code block has to run before the target Quill instance is created with
new Quill(...)
We should also consider that it isn't able to change formatting dynamically although i believe we could backup the
Delta
and create a fresh new Quill instance with these new defaults.Another good post about this implement-custom-editor-for-quill-blot