forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.ts
130 lines (121 loc) · 3.8 KB
/
bubble.ts
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
import merge from 'lodash.merge';
import Emitter from '../core/emitter';
import BaseTheme, { BaseTooltip } from './base';
import { Range } from '../core/selection';
import icons from '../ui/icons';
const TOOLBAR_CONFIG = [
['bold', 'italic', 'link'],
[{ header: 1 }, { header: 2 }, 'blockquote'],
];
class BubbleTooltip extends BaseTooltip {
static TEMPLATE = [
'<span class="ql-tooltip-arrow"></span>',
'<div class="ql-tooltip-editor">',
'<input type="text" data-formula="e=mc^2" data-link="https://quilljs.com" data-video="Embed URL">',
'<a class="ql-close"></a>',
'</div>',
].join('');
constructor(quill, bounds) {
super(quill, bounds);
this.quill.on(
Emitter.events.EDITOR_CHANGE,
(type, range, oldRange, source) => {
if (type !== Emitter.events.SELECTION_CHANGE) return;
if (
range != null &&
range.length > 0 &&
source === Emitter.sources.USER
) {
this.show();
// Lock our width so we will expand beyond our offsetParent boundaries
this.root.style.left = '0px';
this.root.style.width = '';
this.root.style.width = `${this.root.offsetWidth}px`;
const lines = this.quill.getLines(range.index, range.length);
if (lines.length === 1) {
this.position(this.quill.getBounds(range));
} else {
const lastLine = lines[lines.length - 1];
const index = this.quill.getIndex(lastLine);
const length = Math.min(
lastLine.length() - 1,
range.index + range.length - index,
);
const indexBounds = this.quill.getBounds(new Range(index, length));
this.position(indexBounds);
}
} else if (
document.activeElement !== this.textbox &&
this.quill.hasFocus()
) {
this.hide();
}
},
);
}
listen() {
super.listen();
this.root.querySelector('.ql-close').addEventListener('click', () => {
this.root.classList.remove('ql-editing');
});
this.quill.on(Emitter.events.SCROLL_OPTIMIZE, () => {
// Let selection be restored by toolbar handlers before repositioning
setTimeout(() => {
if (this.root.classList.contains('ql-hidden')) return;
const range = this.quill.getSelection();
if (range != null) {
this.position(this.quill.getBounds(range));
}
}, 1);
});
}
cancel() {
this.show();
}
position(reference) {
const shift = super.position(reference);
const arrow = this.root.querySelector('.ql-tooltip-arrow');
// @ts-expect-error
arrow.style.marginLeft = '';
if (shift !== 0) {
// @ts-expect-error
arrow.style.marginLeft = `${-1 * shift - arrow.offsetWidth / 2}px`;
}
return shift;
}
}
class BubbleTheme extends BaseTheme {
constructor(quill, options) {
if (
options.modules.toolbar != null &&
options.modules.toolbar.container == null
) {
options.modules.toolbar.container = TOOLBAR_CONFIG;
}
super(quill, options);
this.quill.container.classList.add('ql-bubble');
}
extendToolbar(toolbar) {
// @ts-expect-error
this.tooltip = new BubbleTooltip(this.quill, this.options.bounds);
this.tooltip.root.appendChild(toolbar.container);
this.buildButtons(toolbar.container.querySelectorAll('button'), icons);
this.buildPickers(toolbar.container.querySelectorAll('select'), icons);
}
}
BubbleTheme.DEFAULTS = merge({}, BaseTheme.DEFAULTS, {
modules: {
toolbar: {
handlers: {
link(value) {
if (!value) {
this.quill.format('link', false);
} else {
this.quill.theme.tooltip.edit();
}
},
},
},
},
});
export { BubbleTooltip, BubbleTheme as default };