-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyboard.ts
57 lines (55 loc) · 1.52 KB
/
keyboard.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
export const bindings = {
tab: {
key: 9,
offset: 0,
handler: function(range, context) {
this.quill.format('indent', '+1', 'user');
}
},
threeDashesDivider: {
key: 189,
offset: 2,
handler: function (range, context) {
if (context.prefix !== '--') {
return true;
}
this.quill.insertText(range.index, '\n', 'user');
this.quill.insertEmbed(range.index + 1, 'divider', true, 'user');
this.quill.setSelection(range.index + 2, 'silent');
this.quill.deleteText(range.index - 2, 2);
}
},
spaceToLink: {
collapsed: true,
key: ' ',
prefix: /https?:\/\/[^\s]+/,
handler: (() => {
let prevOffset = 0;
return function (range, context) {
let url;
const regex = /https?:\/\/[^\s]+/g;
const text = this.quill.getText(prevOffset, range.index);
const match = text.match(regex);
if (match === null) {
prevOffset = range.index;
return true;
}
if (match.length > 1) {
url = match[match.length - 1];
} else {
url = match[0];
}
const ops = [];
// only retain if non-zero. link is in the beginning
if (range.index - url.length) {
ops.push({ retain: range.index - url.length });
}
ops.push({ delete: url.length });
ops.push({ insert: url, attributes: { link: url } });
this.quill.updateContents({ ops });
prevOffset = range.index;
return true;
}
})()
}
};