This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
src.js
92 lines (85 loc) · 2.55 KB
/
src.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
/**
* Exports emoji-mart as a custom HTML element.
*
* @public
* @module emoji-mart-embed
*/
import { Picker, store, emojiIndex, getEmojiDataFromNative } from 'emoji-mart/dist-modern/index.js'
import allEmojiData from 'emoji-mart/data/all.json'
import React from 'react'
import { define } from 'remount'
/**
* Create the HTML custom element for emoji-mart emoji picker.
*
* @public
* @param {string} [customElementName="emoji-picker"] the name of the custom element
* @param {Object} [properties] custom properties for emoji-mart
* @param {string[]} [customAttributes=["set", "native", "title", "showPreview"]]
* @returns {void}
*/
async function definePicker(
customElementName = "emoji-picker",
properties = undefined,
customAttributes = ["set", "native", "title", "showPreview"]
) {
const PickerPrepared = (interactiveProperties) => {
// filter null values
for (const element in interactiveProperties) {
if (interactiveProperties[element] === null) {
delete interactiveProperties[element];
}
}
return React.createElement(Picker, {
// defaults
native: true,
onSelect: emoji => console.log('emoji selected', emoji),
title: 'Emoji',
// passed to function
...properties,
// manually set overwrite everything finally
...interactiveProperties
});
}
define({ [customElementName]: {
component: PickerPrepared,
attributes: customAttributes
}});
}
/**
* Sets the data store for emoji-mart.
*
* @public
* @param {Object} [dataStore]
* @param {function} [dataStore.getter] a syncronous(!) function to get data
* from
* @param {function} [dataStore.getter] a asyncronous function to save data
* to
* @see https://github.com/missive/emoji-mart#storage
* @returns {void}
*/
function setDataStore(dataStore) {
return store.setHandlers(dataStore);
}
/**
* Override real getEmojiDataFromNative function.
*
* It is only needed to be done, because the data object is not available
* outside of emoji-mart.
*
* @public
* @param {string} emoji
* @param {string} design
* @param {Object} [data]
* @see https://github.com/missive/emoji-mart#get-emoji-data-from-native
* @returns {Object}
*/
function getEmojiDataFromNativeOverwrite(emoji, design, data = allEmojiData) {
return getEmojiDataFromNative(emoji, design, data);
}
// export as global
window.emojiMart = {
definePicker,
setDataStore,
getEmojiDataFromNative: getEmojiDataFromNativeOverwrite,
emojiIndex
};