This repository was archived by the owner on Sep 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathemoji.ts
More file actions
91 lines (74 loc) · 2.51 KB
/
emoji.ts
File metadata and controls
91 lines (74 loc) · 2.51 KB
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
import { TinyEmitter as Emitter } from 'tiny-emitter';
import twemoji from 'twemoji';
import { EMOJI, HIDE_PREVIEW, SHOW_PREVIEW } from './events';
import { smile } from './icons';
import { save } from './recent';
import { createElement } from './util';
import { CLASS_EMOJI, CLASS_CUSTOM_EMOJI } from './classes';
import { EmojiButtonOptions, EmojiRecord } from './types';
export class Emoji {
private emojiButton: HTMLElement;
constructor(
private emoji: EmojiRecord,
private showVariants: boolean,
private showPreview: boolean,
private events: Emitter,
private options: EmojiButtonOptions,
private lazy = true
) {}
render(): HTMLElement {
this.emojiButton = createElement('button', CLASS_EMOJI);
let content = this.emoji.emoji;
if (this.emoji.custom) {
content = this.lazy
? smile
: `<img class="${CLASS_CUSTOM_EMOJI}" src="${this.emoji.emoji}">`;
} else if (this.options.style === 'twemoji') {
content = this.lazy
? smile
: twemoji.parse(this.emoji.emoji, this.options.twemojiOptions);
}
this.emojiButton.innerHTML = content;
this.emojiButton.tabIndex = -1;
this.emojiButton.dataset.emoji = this.emoji.emoji;
if (this.emoji.custom) {
this.emojiButton.dataset.custom = 'true';
}
this.emojiButton.title = this.emoji.name;
this.emojiButton.addEventListener('focus', () => this.onEmojiHover());
this.emojiButton.addEventListener('blur', () => this.onEmojiLeave());
this.emojiButton.addEventListener('click', () => this.onEmojiClick());
this.emojiButton.addEventListener('mouseover', () => this.onEmojiHover());
this.emojiButton.addEventListener('mouseout', () => this.onEmojiLeave());
if (this.options.style === 'twemoji' && this.lazy) {
this.emojiButton.style.opacity = '0.25';
}
return this.emojiButton;
}
onEmojiClick(): void {
// TODO move this side effect out of Emoji, make the recent module listen for event
if (
(!(this.emoji as EmojiRecord).variations ||
!this.showVariants ||
!this.options.showVariants) &&
this.options.showRecents
) {
save(this.emoji, this.options);
}
this.events.emit(EMOJI, {
emoji: this.emoji,
showVariants: this.showVariants,
button: this.emojiButton
});
}
onEmojiHover(): void {
if (this.showPreview) {
this.events.emit(SHOW_PREVIEW, this.emoji);
}
}
onEmojiLeave(): void {
if (this.showPreview) {
this.events.emit(HIDE_PREVIEW);
}
}
}