You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.4 KiB
54 lines
1.4 KiB
import { TinyEmitter as Emitter } from 'tiny-emitter';
|
|
|
|
import twemoji from 'twemoji';
|
|
|
|
import { SHOW_PREVIEW, HIDE_PREVIEW } from './events';
|
|
import { createElement } from './util';
|
|
import { EmojiRecord, EmojiButtonOptions } from './types';
|
|
|
|
const CLASS_PREVIEW = 'emoji-picker__preview';
|
|
const CLASS_PREVIEW_EMOJI = 'emoji-picker__preview-emoji';
|
|
const CLASS_PREVIEW_NAME = 'emoji-picker__preview-name';
|
|
|
|
// Options for twemoji.parse(emoji, twemojiOptions)
|
|
const twemojiOptions = {
|
|
ext: '.svg',
|
|
folder: 'svg'
|
|
};
|
|
|
|
export class EmojiPreview {
|
|
private emoji: HTMLElement;
|
|
private name: HTMLElement;
|
|
|
|
constructor(private events: Emitter, private options: EmojiButtonOptions) {}
|
|
|
|
render(): HTMLElement {
|
|
const preview = createElement('div', CLASS_PREVIEW);
|
|
|
|
this.emoji = createElement('div', CLASS_PREVIEW_EMOJI);
|
|
preview.appendChild(this.emoji);
|
|
|
|
this.name = createElement('div', CLASS_PREVIEW_NAME);
|
|
preview.appendChild(this.name);
|
|
|
|
this.events.on(SHOW_PREVIEW, (emoji: EmojiRecord) =>
|
|
this.showPreview(emoji)
|
|
);
|
|
this.events.on(HIDE_PREVIEW, () => this.hidePreview());
|
|
|
|
return preview;
|
|
}
|
|
|
|
showPreview(emoji: EmojiRecord): void {
|
|
this.emoji.innerHTML =
|
|
this.options.style === 'native'
|
|
? emoji.emoji
|
|
: twemoji.parse(emoji.emoji, twemojiOptions);
|
|
this.name.innerHTML = emoji.name;
|
|
}
|
|
|
|
hidePreview(): void {
|
|
this.emoji.innerHTML = '';
|
|
this.name.innerHTML = '';
|
|
}
|
|
}
|
|
|