/
snow.js
132 lines (125 loc) · 3.94 KB
/
snow.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
import extend from 'extend';
import Emitter from '../core/emitter';
import BaseTheme, { BaseTooltip } from './base';
import LinkBlot from '../formats/link';
import { Range } from '../core/selection';
import icons from '../ui/icons';
const TOOLBAR_CONFIG = [
[{ header: ['1', '2', '3', false] }],
['bold', 'italic', 'underline', 'link'],
[{ list: 'ordered' }, { list: 'bullet' }],
['clean'],
];
class SnowTooltip extends BaseTooltip {
constructor(quill, bounds) {
super(quill, bounds);
this.preview = this.root.querySelector('a.ql-preview');
}
listen() {
super.listen();
this.root.querySelector('a.ql-action').addEventListener('click', event => {
if (this.root.classList.contains('ql-editing')) {
this.save();
} else {
this.edit('link', this.preview.textContent);
}
event.preventDefault();
});
this.root.querySelector('a.ql-remove').addEventListener('click', event => {
if (this.linkRange != null) {
const range = this.linkRange;
this.restoreFocus();
this.quill.formatText(range, 'link', false, Emitter.sources.USER);
delete this.linkRange;
}
event.preventDefault();
this.hide();
});
this.quill.on(
Emitter.events.SELECTION_CHANGE,
(range, oldRange, source) => {
if (range == null) return;
if (range.length === 0 && source === Emitter.sources.USER) {
const [link, offset] = this.quill.scroll.descendant(
LinkBlot,
range.index,
);
if (link != null) {
this.linkRange = new Range(range.index - offset, link.length());
const preview = LinkBlot.formats(link.domNode);
this.preview.textContent = preview;
this.preview.setAttribute('href', preview);
this.show();
this.position(this.quill.getBounds(this.linkRange));
return;
}
} else {
delete this.linkRange;
}
this.hide();
},
);
}
show() {
super.show();
this.root.removeAttribute('data-mode');
}
}
SnowTooltip.TEMPLATE = [
'<a class="ql-preview" rel="noopener noreferrer" target="_blank" href="about:blank"></a>',
'<input type="text" data-formula="e=mc^2" data-link="https://quilljs.com" data-video="Embed URL">',
'<a class="ql-action"></a>',
'<a class="ql-remove"></a>',
].join('');
class SnowTheme 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-snow');
}
extendToolbar(toolbar) {
toolbar.container.classList.add('ql-snow');
this.buildButtons(toolbar.container.querySelectorAll('button'), icons);
this.buildPickers(toolbar.container.querySelectorAll('select'), icons);
this.tooltip = new SnowTooltip(this.quill, this.options.bounds);
if (toolbar.container.querySelector('.ql-link')) {
this.quill.keyboard.addBinding(
{ key: 'k', shortKey: true },
(range, context) => {
toolbar.handlers.link.call(toolbar, !context.format.link);
},
);
}
}
}
SnowTheme.DEFAULTS = extend(true, {}, BaseTheme.DEFAULTS, {
modules: {
toolbar: {
handlers: {
link(value) {
if (value) {
const range = this.quill.getSelection();
if (range == null || range.length === 0) return;
let preview = this.quill.getText(range);
if (
/^\S+@\S+\.\S+$/.test(preview) &&
preview.indexOf('mailto:') !== 0
) {
preview = `mailto:${preview}`;
}
const { tooltip } = this.quill.theme;
tooltip.edit('link', preview);
} else {
this.quill.format('link', false);
}
},
},
},
},
});
export default SnowTheme;