Quill custom buttons and drop-down lists

Clock
25.10.2024
Clock
18.12.2024
Clock
3 minutes
An eye
586
Hearts
0
Connected dots
0
Connected dots
0
Connected dots
0

How to add a custom button for quill toolbar

Adding a button to the toolbar is very simple. Quill has a special dictionary where it stores icons for its interface. To access this dictionary, import:
var icons = Quill.import("ui/icons");
Now you can get and save any SVG icon. To add your own icon, you need to save its content. To read the content of any file, I created the corresponding readFile function:
function readFile(file) {
var rawFile = new XMLHttpRequest();
var result = ""
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if(rawFile.readyState === 4) {
if(rawFile.status === 200 || rawFile.status == 0) {
result = rawFile.responseText;
}
}
}
rawFile.send(null);
return result
}

icons["attention"] = readFile(`${PATH}PostEditor/img/blockAttention.svg`)
After that, you can use it. But how, you ask? Quill uses special names. In order for the icon to be applied, you need to add the appropriate class selector. Specifically, in my example, it will look like this:
<button class="ql-attention"></button>
To make the button at least somewhat useful, you can attach a special handler to it. This handler has exactly the same name as the button name. This is how this handler is added:
const quill = new Quill('#editor', {
modules: {
toolbar: {
container: '#toolbar-container',
handlers: {
//... other handlers
'attention': (value) => {
// value in this case means either true or false
if (value){
// some logic
}else{
// some logic
}
}
}
}
},
theme: 'snow',
});

How to add a drop-down list for quill toolbar

The drop-down list is a bit more complicated, and I'll explain why. The thing is that quill actively relies on CSS selectors, and without proper style settings, you won't get anywhere. Plus, I think you might need a couple of my tricks to make the selectors work correctly.
This is important. I don't import quill styles because they look weird on my site. And the principles of the WYSIWYG editor are violated. In short, what you see is what will be displayed on the final page. In my case, when I wrote in the editor, the final article looks different. And to remove this effect, I completely got rid of the styles from QuillJS
The HTML code will look like this:
<span class="ql-formats">
<select class="ql-type">
<option id="type-1" value="type-1" selected>Type One</option>
<option id="type-2" value="type-2">Type Two</option>
<option id="type-3" value="type-3">Type Three</option>
</select>
</span>
Next, you need to add some styles. You may not have to add them, because this may be a feature of MY site, but if nothing is displayed, add these styles:
select.ql-type {
background-color: #000;
border: 2px solid white;
padding: 5px;
color: var(--ui_text_color);
}
select.ql-type > option{
background-color: #fff;
}
.ql-type{
/* By default quill will add display: none */
display: block !important;
max-width: 150px;
}
Now everything works, but not really. You can notice how the default element does not change. Perhaps somewhere deep in the source code of quill this can be somehow fixed and configured, but I did not find such an opportunity. Therefore, let's setup a handler, just like for the first case with a single button:
const quill = new Quill('#editor', {
modules: {
toolbar: {
container: '#toolbar-container',
handlers: {
// ... other handlers
'type': (value)=>{
// First, remove all selected attribute
document.querySelector(`#${value}`).parentElement.querySelectorAll('option').forEach( (option) => {
option.removeAttribute('selected')
})
// Secondly, add selected attribute for the current one
document.querySelector(`#${value}`).setAttribute('selected', true)
}
}
}
},
theme: 'snow',
});
It is not necessary to put the ID attribute, but this is the simplest way you can tweak the drop-down list in quill. The value in this case is the value of the attribute of the <option> tag.
And that's all for now. I hope the article was useful for you. Also you can find all types of custom buttons on my quill editor web tool Bye(^∀^●)ノシ

Comments

(0)

captcha
Send
It's empty now. Be the first (o゚v゚)ノ

Other