forked from contentco/quill-emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-emoji-blot.js
45 lines (37 loc) · 1.17 KB
/
format-emoji-blot.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
import Parchment from 'parchment';
import emojiMap from "./emoji-map";
class EmojiBlot extends Parchment.Embed {
static create(value) {
let node = super.create();
if (typeof value === 'object') {
EmojiBlot.buildSpan(value, node);
} else if (typeof value === "string") {
const valueObj = emojiMap[value];
if (valueObj) {
EmojiBlot.buildSpan(valueObj, node);
}
}
return node;
}
static value(node) {
return node.dataset.name;
}
static buildSpan(value, node) {
node.setAttribute('data-name', value.name);
let emojiSpan = document.createElement('span');
emojiSpan.classList.add(this.emojiClass);
emojiSpan.classList.add(this.emojiPrefix + value.name);
// unicode can be '1f1f5-1f1ea',see emoji-list.js.
emojiSpan.innerText = String.fromCodePoint(...EmojiBlot.parseUnicode(value.unicode));
node.appendChild(emojiSpan);
}
static parseUnicode(string) {
return string.split('-').map(str => parseInt(str, 16));
}
}
EmojiBlot.blotName = 'emoji';
EmojiBlot.className = 'ql-emojiblot';
EmojiBlot.tagName = 'span';
EmojiBlot.emojiClass = 'ap';
EmojiBlot.emojiPrefix = 'ap-';
export default EmojiBlot;