├── screenshot.png
├── docs
├── fonts
│ ├── OpenSans-Bold-webfont.eot
│ ├── OpenSans-Bold-webfont.woff
│ ├── OpenSans-Light-webfont.eot
│ ├── OpenSans-Italic-webfont.eot
│ ├── OpenSans-Italic-webfont.woff
│ ├── OpenSans-Light-webfont.woff
│ ├── OpenSans-Regular-webfont.eot
│ ├── OpenSans-Regular-webfont.woff
│ ├── OpenSans-Semibold-webfont.eot
│ ├── OpenSans-Semibold-webfont.ttf
│ ├── OpenSans-BoldItalic-webfont.eot
│ ├── OpenSans-Semibold-webfont.woff
│ ├── OpenSans-BoldItalic-webfont.woff
│ ├── OpenSans-LightItalic-webfont.eot
│ ├── OpenSans-LightItalic-webfont.woff
│ ├── OpenSans-SemiboldItalic-webfont.eot
│ ├── OpenSans-SemiboldItalic-webfont.ttf
│ └── OpenSans-SemiboldItalic-webfont.woff
├── scripts
│ ├── linenumber.js
│ └── prettify
│ │ ├── lang-css.js
│ │ ├── Apache-License-2.0.txt
│ │ └── prettify.js
├── styles
│ ├── prettify-jsdoc.css
│ ├── prettify-tomorrow.css
│ └── jsdoc-default.css
├── index.html
├── CustomQuillTools.js.html
├── QuillToolbarItem.html
├── QuillToolbarButton.html
└── QuillToolbarDropDown.html
├── LICENSE
├── README.md
├── example.html
└── DynamicQuillTools.js
/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/screenshot.png
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Bold-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Bold-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Bold-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Bold-webfont.woff
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Light-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Light-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Italic-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Italic-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Italic-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Italic-webfont.woff
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Light-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Light-webfont.woff
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Regular-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Regular-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Regular-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Regular-webfont.woff
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Semibold-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Semibold-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Semibold-webfont.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Semibold-webfont.ttf
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-BoldItalic-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-BoldItalic-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-Semibold-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-Semibold-webfont.woff
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-BoldItalic-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-BoldItalic-webfont.woff
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-LightItalic-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-LightItalic-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-LightItalic-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-LightItalic-webfont.woff
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-SemiboldItalic-webfont.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-SemiboldItalic-webfont.eot
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-SemiboldItalic-webfont.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-SemiboldItalic-webfont.ttf
--------------------------------------------------------------------------------
/docs/fonts/OpenSans-SemiboldItalic-webfont.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/T-vK/DynamicQuillTools/HEAD/docs/fonts/OpenSans-SemiboldItalic-webfont.woff
--------------------------------------------------------------------------------
/docs/scripts/linenumber.js:
--------------------------------------------------------------------------------
1 | /*global document */
2 | (function() {
3 | var source = document.getElementsByClassName('prettyprint source linenums');
4 | var i = 0;
5 | var lineNumber = 0;
6 | var lineId;
7 | var lines;
8 | var totalLines;
9 | var anchorHash;
10 |
11 | if (source && source[0]) {
12 | anchorHash = document.location.hash.substring(1);
13 | lines = source[0].getElementsByTagName('li');
14 | totalLines = lines.length;
15 |
16 | for (; i < totalLines; i++) {
17 | lineNumber++;
18 | lineId = 'line' + lineNumber;
19 | lines[i].id = lineId;
20 | if (lineId === anchorHash) {
21 | lines[i].className += ' selected';
22 | }
23 | }
24 | }
25 | })();
26 |
--------------------------------------------------------------------------------
/docs/scripts/prettify/lang-css.js:
--------------------------------------------------------------------------------
1 | PR.registerLangHandler(PR.createSimpleLexer([["pln",/^[\t\n\f\r ]+/,null," \t\r\n"]],[["str",/^"(?:[^\n\f\r"\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*"/,null],["str",/^'(?:[^\n\f\r'\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*'/,null],["lang-css-str",/^url\(([^"')]*)\)/i],["kwd",/^(?:url|rgb|!important|@import|@page|@media|@charset|inherit)(?=[^\w-]|$)/i,null],["lang-css-kw",/^(-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*)\s*:/i],["com",/^\/\*[^*]*\*+(?:[^*/][^*]*\*+)*\//],["com",
2 | /^(?:<\!--|--\>)/],["lit",/^(?:\d+|\d*\.\d+)(?:%|[a-z]+)?/i],["lit",/^#[\da-f]{3,6}/i],["pln",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i],["pun",/^[^\s\w"']+/]]),["css"]);PR.registerLangHandler(PR.createSimpleLexer([],[["kwd",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i]]),["css-kw"]);PR.registerLangHandler(PR.createSimpleLexer([],[["str",/^[^"')]+/]]),["css-str"]);
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2019, T-vK
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions
6 | are met:
7 |
8 | 1. Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 |
11 | 2. Redistributions in binary form must reproduce the above copyright
12 | notice, this list of conditions and the following disclaimer in the
13 | documentation and/or other materials provided with the distribution.
14 |
15 | 3. Neither the name of the copyright holder nor the names of its
16 | contributors may be used to endorse or promote products derived from
17 | this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/docs/styles/prettify-jsdoc.css:
--------------------------------------------------------------------------------
1 | /* JSDoc prettify.js theme */
2 |
3 | /* plain text */
4 | .pln {
5 | color: #000000;
6 | font-weight: normal;
7 | font-style: normal;
8 | }
9 |
10 | /* string content */
11 | .str {
12 | color: hsl(104, 100%, 24%);
13 | font-weight: normal;
14 | font-style: normal;
15 | }
16 |
17 | /* a keyword */
18 | .kwd {
19 | color: #000000;
20 | font-weight: bold;
21 | font-style: normal;
22 | }
23 |
24 | /* a comment */
25 | .com {
26 | font-weight: normal;
27 | font-style: italic;
28 | }
29 |
30 | /* a type name */
31 | .typ {
32 | color: #000000;
33 | font-weight: normal;
34 | font-style: normal;
35 | }
36 |
37 | /* a literal value */
38 | .lit {
39 | color: #006400;
40 | font-weight: normal;
41 | font-style: normal;
42 | }
43 |
44 | /* punctuation */
45 | .pun {
46 | color: #000000;
47 | font-weight: bold;
48 | font-style: normal;
49 | }
50 |
51 | /* lisp open bracket */
52 | .opn {
53 | color: #000000;
54 | font-weight: bold;
55 | font-style: normal;
56 | }
57 |
58 | /* lisp close bracket */
59 | .clo {
60 | color: #000000;
61 | font-weight: bold;
62 | font-style: normal;
63 | }
64 |
65 | /* a markup tag name */
66 | .tag {
67 | color: #006400;
68 | font-weight: normal;
69 | font-style: normal;
70 | }
71 |
72 | /* a markup attribute name */
73 | .atn {
74 | color: #006400;
75 | font-weight: normal;
76 | font-style: normal;
77 | }
78 |
79 | /* a markup attribute value */
80 | .atv {
81 | color: #006400;
82 | font-weight: normal;
83 | font-style: normal;
84 | }
85 |
86 | /* a declaration */
87 | .dec {
88 | color: #000000;
89 | font-weight: bold;
90 | font-style: normal;
91 | }
92 |
93 | /* a variable name */
94 | .var {
95 | color: #000000;
96 | font-weight: normal;
97 | font-style: normal;
98 | }
99 |
100 | /* a function name */
101 | .fun {
102 | color: #000000;
103 | font-weight: bold;
104 | font-style: normal;
105 | }
106 |
107 | /* Specify class=linenums on a pre to get line numbering */
108 | ol.linenums {
109 | margin-top: 0;
110 | margin-bottom: 0;
111 | }
112 |
--------------------------------------------------------------------------------
/docs/styles/prettify-tomorrow.css:
--------------------------------------------------------------------------------
1 | /* Tomorrow Theme */
2 | /* Original theme - https://github.com/chriskempson/tomorrow-theme */
3 | /* Pretty printing styles. Used with prettify.js. */
4 | /* SPAN elements with the classes below are added by prettyprint. */
5 | /* plain text */
6 | .pln {
7 | color: #4d4d4c; }
8 |
9 | @media screen {
10 | /* string content */
11 | .str {
12 | color: hsl(104, 100%, 24%); }
13 |
14 | /* a keyword */
15 | .kwd {
16 | color: hsl(240, 100%, 50%); }
17 |
18 | /* a comment */
19 | .com {
20 | color: hsl(0, 0%, 60%); }
21 |
22 | /* a type name */
23 | .typ {
24 | color: hsl(240, 100%, 32%); }
25 |
26 | /* a literal value */
27 | .lit {
28 | color: hsl(240, 100%, 40%); }
29 |
30 | /* punctuation */
31 | .pun {
32 | color: #000000; }
33 |
34 | /* lisp open bracket */
35 | .opn {
36 | color: #000000; }
37 |
38 | /* lisp close bracket */
39 | .clo {
40 | color: #000000; }
41 |
42 | /* a markup tag name */
43 | .tag {
44 | color: #c82829; }
45 |
46 | /* a markup attribute name */
47 | .atn {
48 | color: #f5871f; }
49 |
50 | /* a markup attribute value */
51 | .atv {
52 | color: #3e999f; }
53 |
54 | /* a declaration */
55 | .dec {
56 | color: #f5871f; }
57 |
58 | /* a variable name */
59 | .var {
60 | color: #c82829; }
61 |
62 | /* a function name */
63 | .fun {
64 | color: #4271ae; } }
65 | /* Use higher contrast and text-weight for printable form. */
66 | @media print, projection {
67 | .str {
68 | color: #060; }
69 |
70 | .kwd {
71 | color: #006;
72 | font-weight: bold; }
73 |
74 | .com {
75 | color: #600;
76 | font-style: italic; }
77 |
78 | .typ {
79 | color: #404;
80 | font-weight: bold; }
81 |
82 | .lit {
83 | color: #044; }
84 |
85 | .pun, .opn, .clo {
86 | color: #440; }
87 |
88 | .tag {
89 | color: #006;
90 | font-weight: bold; }
91 |
92 | .atn {
93 | color: #404; }
94 |
95 | .atv {
96 | color: #060; } }
97 | /* Style */
98 | /*
99 | pre.prettyprint {
100 | background: white;
101 | font-family: Consolas, Monaco, 'Andale Mono', monospace;
102 | font-size: 12px;
103 | line-height: 1.5;
104 | border: 1px solid #ccc;
105 | padding: 10px; }
106 | */
107 |
108 | /* Specify class=linenums on a pre to get line numbering */
109 | ol.linenums {
110 | margin-top: 0;
111 | margin-bottom: 0; }
112 |
113 | /* IE indents via margin-left */
114 | li.L0,
115 | li.L1,
116 | li.L2,
117 | li.L3,
118 | li.L4,
119 | li.L5,
120 | li.L6,
121 | li.L7,
122 | li.L8,
123 | li.L9 {
124 | /* */ }
125 |
126 | /* Alternate shading for lines */
127 | li.L1,
128 | li.L3,
129 | li.L5,
130 | li.L7,
131 | li.L9 {
132 | /* */ }
133 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DynamicQuillTools
2 |
3 | DynamicQuillTools is a library that allows you to dynamically add or remove new custom elements to/from a Quill Editor's toolbar. For instance a button or a drop down menu.
4 |
5 | ## Screenshot
6 |
7 | In this screenshot you can see a custom drop down menu and a custom button.
8 |
9 | 
10 |
11 | ## Live Demo
12 | https://t-vk.github.io/DynamicQuillTools/example.html
13 |
14 | ## CDN
15 | If you prefer CDNs you can include the library on your website like this:
16 | ``` HTML
17 |
18 | ```
19 |
20 | ## Examples
21 | For a full example take a look at [example.html](./example.html).
22 |
23 | ### Very Basic Example
24 |
25 | ``` JavaScript
26 | const myButton = new QuillToolbarButton({
27 | icon: ``
28 | })
29 | myButton.onClick = function(quill) {
30 | alert('You just clicked the button!')
31 | }
32 | myButton.attach(quill) // Add the custom button to the quill editor
33 | ```
34 |
35 | ### Adding a Custom Button
36 |
37 | ``` JavaScript
38 | const myButton = new QuillToolbarButton({
39 | icon: ``
40 | })
41 | myButton.onClick = function(quill) {
42 | // Do whatever you want here. You could use this.getValue() or this.setValue() if you wanted.
43 |
44 | // For example, get the selected text and convert it to uppercase:
45 | const { index, length } = quill.selection.savedRange
46 | const selectedText = quill.getText(index, length)
47 | const newText = selectedText.toUpperCase()
48 | quill.deleteText(index, length)
49 | quill.insertText(index, newText)
50 | quill.setSelection(index, newText.length)
51 | }
52 | myButton.attach(quill)
53 | ```
54 |
55 | ### Adding a Custom Drop Down Menu
56 |
57 | ``` JavaScript
58 | const dropDownItems = {
59 | 'Mike Smith': 'mike.smith@gmail.com',
60 | 'Jonathan Dyke': 'jonathan.dyke@yahoo.com',
61 | 'Max Anderson': 'max.anderson@gmail.com'
62 | }
63 |
64 | const myDropDown = new QuillToolbarDropDown({
65 | label: "Email Addresses",
66 | rememberSelection: false
67 | })
68 |
69 | myDropDown.setItems(dropDownItems)
70 |
71 | myDropDown.onSelect = function(label, value, quill) {
72 | // Do whatever you want with the new dropdown selection here
73 |
74 | // For example, insert the value of the dropdown selection:
75 | const { index, length } = quill.selection.savedRange
76 | quill.deleteText(index, length)
77 | quill.insertText(index, value)
78 | quill.setSelection(index + value.length)
79 | }
80 |
81 | myDropDown.attach(quill)
82 | ```
83 |
84 |
85 | ## API Documentation
86 | https://t-vk.github.io/DynamicQuillTools/docs
87 |
--------------------------------------------------------------------------------
/example.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
88 |
89 |
90 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Home - Documentation
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
23 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/DynamicQuillTools.js:
--------------------------------------------------------------------------------
1 | /** @class Abstract class representing a tool for a Quill Editor toolbar. */
2 | class QuillToolbarItem {
3 | constructor(options) {
4 | const me = this
5 | me.options = options
6 |
7 | me.qlFormatsEl = document.createElement("span")
8 | me.qlFormatsEl.className = "ql-formats"
9 | }
10 | /**
11 | * Attaches this tool to the given Quill Editor instance.
12 | *
13 | * @param {Quill} quill - The Quill Editor instance that this tool should get added to.
14 | */
15 | attach(quill) {
16 | const me = this
17 | me.quill = quill
18 | me.toolbar = quill.getModule('toolbar')
19 | me.toolbarEl = me.toolbar.container
20 | me.toolbarEl.appendChild(me.qlFormatsEl)
21 | }
22 | /**
23 | * Detaches this tool from the given Quill Editor instance.
24 | *
25 | * @param {Quill} quill - The Quill Editor instance that this tool should get added to.
26 | */
27 | detach(quill) {
28 | const me = this
29 | me.toolbarEl.removeChild(me.qlFormatsEl)
30 | }
31 | /**
32 | * Calculate the width of text.
33 | *
34 | * @param {string} text - The text of which the length should be calculated.
35 | * @param {string} [font="500 14px 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"] - The font css that shuold be applied to the text before calculating the width.
36 | */
37 | _getTextWidth(text, font="500 14px 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif") {
38 | const canvas = this._getTextWidth.canvas || (this._getTextWidth.canvas = document.createElement("canvas"))
39 | const context = canvas.getContext("2d")
40 | context.font = font
41 | const metrics = context.measureText(text)
42 | return metrics.width
43 | }
44 | /**
45 | * Add a global css rule to the document.
46 | *
47 | * @param {string} cssRule - CSS rules
48 | */
49 | _addCssRule(cssRule) {
50 | const style = document.createElement("style");
51 | document.head.appendChild(style);
52 | style.sheet.insertRule(cssRule, 0)
53 | }
54 | /**
55 | * Generate a random ID.
56 | *
57 | * @returns {string} random 10 digit ID
58 | */
59 | _generateId() {
60 | return Math.random().toString().substr(2, 10)
61 | }
62 | }
63 |
64 |
65 | /** @class Class representing a dropdown tool for a Quill Editor toolbar. */
66 | class QuillToolbarDropDown extends QuillToolbarItem {
67 | /**
68 | * Creates an instance of QuillToolbarDropDown.
69 | *
70 | * @constructor
71 | * @param {object} [options] - The options/settings for this QuillToolbarDropDown.
72 | * @param {string} [options.id=`dropdown-${random10digitNumber}`] - The id of the quill tool.
73 | * @param {string} [options.label=""] - The default label that is being displayed before making a selection.
74 | * @param {boolean} [options.rememberSelection=true] - Automatically change the label to the current selection.
75 | * @param {object} [options.items={}] - The default items this dropdown will have. Needs to be a key-value-object (key=visible label; value=actual value).
76 | */
77 | constructor(options) {
78 | super(options)
79 | const me = this
80 |
81 | me.id = me.options.id || `dropdown-${me._generateId()}`
82 |
83 | const qlPicker = document.createElement("span")
84 | qlPicker.className = `ql-${me.id} ql-picker`
85 | me.qlFormatsEl.appendChild(qlPicker)
86 |
87 | const qlPickerLabel = document.createElement("span")
88 | qlPickerLabel.className = "ql-picker-label"
89 | qlPicker.appendChild(qlPickerLabel)
90 | qlPickerLabel.addEventListener('click', function(e) {
91 | qlPicker.classList.toggle('ql-expanded')
92 |
93 | })
94 | window.addEventListener('click', function(e){
95 | if (!qlPicker.contains(e.target)){
96 | qlPicker.classList.remove('ql-expanded')
97 | }
98 | })
99 |
100 | const qlPickerOptions = document.createElement("span")
101 | qlPickerOptions.className = "ql-picker-options"
102 | qlPicker.appendChild(qlPickerOptions)
103 |
104 | me.dropDownEl = qlPicker
105 | me.dropDownPickerEl = me.dropDownEl.querySelector('.ql-picker-options')
106 | me.dropDownPickerLabelEl = me.dropDownEl.querySelector('.ql-picker-label')
107 | me.dropDownPickerLabelEl.innerHTML = ``
108 |
109 | me.setLabel(me.options.label || "")
110 | me.setItems(me.options.items || {})
111 |
112 | me._addCssRule(`
113 | .ql-snow .ql-picker.ql-${me.id} .ql-picker-label::before, .ql-${me.id} .ql-picker.ql-size .ql-picker-item::before {
114 | content: attr(data-label);
115 | }
116 | `)
117 | }
118 | /**
119 | * Set the items for this dropdown tool.
120 | *
121 | * @param {object} items - Needs to be a key-value-object (key=visible label; value=actual value).
122 | */
123 | setItems(items) {
124 | const me = this
125 | for (const [label,value] of Object.entries(items)) {
126 | const newItemEl = document.createElement("span")
127 | newItemEl.className = "ql-picker-item"
128 | newItemEl.innerHTML = label
129 | newItemEl.setAttribute('data-value', value)
130 | newItemEl.onclick = function(e) {
131 | me.dropDownEl.classList.remove('ql-expanded')
132 | if (me.options.rememberSelection)
133 | me.setLabel(label)
134 | if (me.onSelect)
135 | me.onSelect(label, value, me.quill)
136 | }
137 | me.dropDownPickerEl.appendChild(newItemEl)
138 | }
139 | }
140 | /**
141 | * Set the label for this dropdown tool and automatically adjust the width to fit the label.
142 | *
143 | * @param {String} newLabel - The new label that should be set.
144 | */
145 | setLabel(newLabel) {
146 | const me = this
147 | const requiredWidth = `${me._getTextWidth(newLabel)+30}px`
148 | me.dropDownPickerLabelEl.style.width = requiredWidth
149 | me.dropDownPickerLabelEl.setAttribute('data-label', newLabel)
150 | }
151 | /**
152 | * A callback that gets called automatically when the dropdown selection changes. This callback is expected to be overwritten.
153 | *
154 | * @param {string} label - The label of the newly selected item.
155 | * @param {string} value - The value of the newly selected item.
156 | * @param {Quill} quill - The quill instance the dropdown tool is attached to.
157 | */
158 | onSelect(label, value, quill) {
159 |
160 | }
161 | }
162 |
163 |
164 | /** @class Class representing a button tool for a Quill Editor toolbar. */
165 | class QuillToolbarButton extends QuillToolbarItem {
166 | /**
167 | * Creates an instance of QuillToolbarButton.
168 | *
169 | * @constructor
170 | * @param {object} [options] - The options/settings for this QuillToolbarButton.
171 | * @param {string} [options.id=`button-${random10digitNumber}`] - The id of the quill tool.
172 | * @param {string} [options.value] - The default hidden value of the button.
173 | * @param {string} options.icon - The default icon this button tool will have.
174 | */
175 | constructor(options) {
176 | super(options)
177 | const me = this
178 |
179 | me.id = me.options.id || `button-${me._generateId()}`
180 |
181 | me.qlButton = document.createElement("button")
182 | me.qlButton.className = `ql-${me.id}`
183 | me.setValue(me.options.value)
184 | me.setIcon(me.options.icon)
185 | me.qlButton.onclick = function() {
186 | me.onClick(me.quill)
187 | }
188 | me.qlFormatsEl.appendChild(me.qlButton)
189 | }
190 | /**
191 | * Set the icon for this button tool.
192 | *
193 | * @param {string} newLabel - The