/
toolbar.js
135 lines (124 loc) · 4.15 KB
/
toolbar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import extend from 'extend';
import Emitter from '../core/emitter';
import Parchment from 'parchment';
import Module from '../core/module';
import logger from '../core/logger';
let debug = logger('quill:toolbar');
class Toolbar extends Module {
constructor(quill, options) {
if (Array.isArray(options)) {
let container = document.createElement('div');
addControls(container, options);
quill.container.parentNode.insertBefore(container, quill.container);
options = { container: container };
}
super(quill, options);
if (typeof this.options.container === 'string') {
this.options.container = document.querySelector(this.options.container);
}
if (this.options.container == null) {
return debug.error('Container required for toolbar', this.options);
}
this.container = this.options.container;
this.container.classList.add('ql-toolbar');
this.container.classList.toggle('ios', /iPhone|iPad/i.test(navigator.userAgent));
this.controls = [];
[].forEach.call(this.container.querySelectorAll('a, button, input[type=button], select'), (input) => {
this.attach(input);
});
this.quill.on(Quill.events.SELECTION_CHANGE, this.update, this)
.on(Quill.events.TEXT_CHANGE, this.update, this);
}
attach(input) {
let format = [].find.call(input.classList, (className) => {
return className.indexOf('ql-') === 0;
});
if (!format) return;
format = format.slice('ql-'.length);
// if (this.quill.options.formats.indexOf(format) < 0) return; // TODO enable
let eventName = input.tagName === 'SELECT' ? 'change' : 'click';
input.addEventListener(eventName, () => {
this.quill.focus();
let value;
if (input.tagName === 'SELECT') {
value = input.options[input.selectedIndex].value || false;
} else {
value = input.classList.contains('ql-active') ? false : input.getAttribute('data-value') || true;
}
if (this.quill.controls[format]) {
this.quill.controls[format](format, value);
} else {
this.quill.format(format, value, Emitter.sources.USER);
}
});
// TODO use weakmap
this.controls.push([format, input]);
}
update() {
let range = this.quill.getSelection();
if (range == null) return;
let formats = this.quill.getFormat(range);
this.controls.forEach(function(pair) {
let [format, input] = pair;
if (input.tagName === 'SELECT') {
if (formats[format] == null) {
input.querySelector('option[selected]').selected = true;
} else {
// TODO never reports array
input.value = Array.isArray(formats[format]) ? '' : formats[format];
}
} else {
input.classList.toggle('ql-active', formats[format]);
}
});
}
}
Toolbar.DEFAULTS = {
container: null
};
function addButton(container, format, value) {
let input = document.createElement('button');
input.classList.add('ql-' + format);
if (value != null) {
input.setAttribute('data-value', value);
}
container.appendChild(input);
}
function addControls(container, groups) {
if (!Array.isArray(groups[0])) {
groups = [groups];
}
groups.forEach(function(controls) {
let group = document.createElement('span');
group.classList.add('ql-formats');
controls.forEach(function(control) {
if (typeof control === 'string') {
addButton(group, control);
} else {
let format = Object.keys(control)[0];
let value = control[format];
if (typeof value === 'string') {
addButton(group, format, value);
} else {
addSelect(group, format, value);
}
}
});
container.appendChild(group);
});
}
function addSelect(container, format, values) {
let input = document.createElement('select');
input.classList.add('ql-' + format);
values.forEach(function(value) {
let option = document.createElement('option');
if (value !== false) {
option.setAttribute('value', value);
} else {
option.setAttribute('selected', 'selected');
}
input.appendChild(option);
});
container.appendChild(input);
}
export { Toolbar as default, addControls };