@sex-editor/emoji 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ import { LexicalEditor } from 'lexical';
2
+
3
+ declare function registerEmojiPlugin(editor: LexicalEditor): () => void;
4
+
5
+ type EmojiMatch = Readonly<{
6
+ position: number;
7
+ shortcode: string;
8
+ unifiedID: string;
9
+ }>;
10
+ declare function findEmoji(text: string): EmojiMatch | null;
11
+
12
+ export { type EmojiMatch, findEmoji, registerEmojiPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ // src/plugin/registerEmojiPlugin.ts
2
+ import { TextNode } from "lexical";
3
+ import { $createEmojiNode } from "@sex-editor/core";
4
+
5
+ // src/utils/findEmoji.ts
6
+ import emojis from "emoji-datasource-facebook/emoji.json";
7
+ var emojiReplacementMap = emojis.reduce((acc, row) => {
8
+ if (!row.has_img_facebook) {
9
+ return acc;
10
+ }
11
+ acc.set(`:${row.short_name}:`, row.unified);
12
+ if (row.text != null) {
13
+ acc.set(row.text, row.unified);
14
+ }
15
+ if (row.texts != null) {
16
+ row.texts.forEach((text) => acc.set(text, row.unified));
17
+ }
18
+ return acc;
19
+ }, /* @__PURE__ */ new Map());
20
+ function findEmoji(text) {
21
+ const skippedText = [];
22
+ for (const word of text.split(" ")) {
23
+ if (!emojiReplacementMap.has(word)) {
24
+ skippedText.push(word);
25
+ continue;
26
+ }
27
+ if (skippedText.length > 0) {
28
+ skippedText.push("");
29
+ }
30
+ return {
31
+ position: skippedText.join(" ").length,
32
+ shortcode: word,
33
+ unifiedID: emojiReplacementMap.get(word)
34
+ };
35
+ }
36
+ return null;
37
+ }
38
+
39
+ // src/plugin/registerEmojiPlugin.ts
40
+ function $textNodeTransform(node) {
41
+ if (!node.isSimpleText() || node.hasFormat("code")) {
42
+ return;
43
+ }
44
+ const text = node.getTextContent();
45
+ const emojiMatch = findEmoji(text);
46
+ if (emojiMatch === null) {
47
+ return;
48
+ }
49
+ let targetNode;
50
+ if (emojiMatch.position === 0) {
51
+ [targetNode] = node.splitText(emojiMatch.position + emojiMatch.shortcode.length);
52
+ } else {
53
+ [, targetNode] = node.splitText(
54
+ emojiMatch.position,
55
+ emojiMatch.position + emojiMatch.shortcode.length
56
+ );
57
+ }
58
+ const emojiNode = $createEmojiNode(emojiMatch.unifiedID);
59
+ targetNode.replace(emojiNode);
60
+ }
61
+ function registerEmojiPlugin(editor) {
62
+ return editor.registerNodeTransform(TextNode, $textNodeTransform);
63
+ }
64
+ export {
65
+ findEmoji,
66
+ registerEmojiPlugin
67
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@sex-editor/emoji",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "devDependencies": {
16
+ "tsup": "8.3.5",
17
+ "@sex-editor/core": "0.0.2"
18
+ },
19
+ "dependencies": {
20
+ "emoji-datasource-facebook": "^16.0.0"
21
+ },
22
+ "peerDependencies": {
23
+ "@sex-editor/core": ">=0.0.2 <0.0.3",
24
+ "lexical": "^0.39.0"
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "sideEffects": false,
30
+ "keywords": [],
31
+ "author": "",
32
+ "license": "ISC",
33
+ "scripts": {
34
+ "dev": "tsup --watch",
35
+ "build": "tsup"
36
+ }
37
+ }