@usememos/editor 26.10.13 → 26.10.14
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.
- package/README.md +57 -12
- package/dist/base.css +54 -2
- package/dist/emoji-data-WTFGY3MU.js +6 -0
- package/dist/emoji-data-WTFGY3MU.js.map +7 -0
- package/dist/emoji.js +74 -0
- package/dist/emoji.js.map +7 -0
- package/dist/index.js +942 -92
- package/dist/index.js.map +4 -4
- package/dist/react.js.map +2 -2
- package/dist/style.css +147 -4
- package/dist/theme.css +26 -2
- package/dist/types/commands.d.ts +48 -3
- package/dist/types/editor.d.ts +59 -3
- package/dist/types/emoji/data.d.ts +1 -0
- package/dist/types/emoji/index.d.ts +21 -0
- package/dist/types/emoji/search.d.ts +17 -0
- package/dist/types/icons.d.ts +39 -0
- package/dist/types/labels.d.ts +20 -0
- package/dist/types/markdown/blocks.d.ts +2 -0
- package/dist/types/markdown/htmlSection.d.ts +10 -1
- package/dist/types/markdown/inline.d.ts +2 -0
- package/dist/types/markdown/options.d.ts +14 -0
- package/dist/types/markdown/serialize.d.ts +8 -0
- package/dist/types/model.d.ts +42 -5
- package/dist/types/react/index.d.ts +1 -1
- package/dist/types/theme.d.ts +8 -0
- package/dist/types/ui/linkCard.d.ts +46 -0
- package/dist/types/view/dom.d.ts +7 -1
- package/package.json +5 -1
package/dist/emoji.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/emoji/index.ts
|
|
2
|
+
import { containerContent, inlineText, suggestion } from "@usememos/editor";
|
|
3
|
+
|
|
4
|
+
// src/emoji/search.ts
|
|
5
|
+
function parseEmoji(data) {
|
|
6
|
+
return data.split("\n").map((line) => {
|
|
7
|
+
const [emoji2 = "", names = "", tags = ""] = line.split(" ");
|
|
8
|
+
return { emoji: emoji2, names: names.split(" ").filter(Boolean), tags: tags.split(" ").filter(Boolean) };
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function searchEmoji(emoji2, query, limit = Infinity) {
|
|
12
|
+
const q = query.toLowerCase();
|
|
13
|
+
if (!q) return emoji2.slice(0, limit);
|
|
14
|
+
const ranked = [[], [], [], [], []];
|
|
15
|
+
for (const e of emoji2) {
|
|
16
|
+
const rank = rankOf(e, q);
|
|
17
|
+
if (rank !== null) ranked[rank].push(e);
|
|
18
|
+
}
|
|
19
|
+
return ranked.flat().slice(0, limit);
|
|
20
|
+
}
|
|
21
|
+
function rankOf(e, q) {
|
|
22
|
+
let best = null;
|
|
23
|
+
const take = (rank) => {
|
|
24
|
+
if (best === null || rank < best) best = rank;
|
|
25
|
+
};
|
|
26
|
+
for (const name of e.names) {
|
|
27
|
+
if (name === q) return 0;
|
|
28
|
+
if (name.startsWith(q)) take(1);
|
|
29
|
+
else if (name.includes(`_${q}`) || name.includes(`-${q}`)) take(2);
|
|
30
|
+
else if (name.includes(q)) take(4);
|
|
31
|
+
}
|
|
32
|
+
if (best === null || best > 3) {
|
|
33
|
+
for (const tag of e.tags) if (tag.startsWith(q)) take(3);
|
|
34
|
+
}
|
|
35
|
+
return best;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/emoji/index.ts
|
|
39
|
+
var loading = null;
|
|
40
|
+
function loadEmoji() {
|
|
41
|
+
loading ??= import("./emoji-data-WTFGY3MU.js").then((m) => parseEmoji(m.EMOJI_DATA));
|
|
42
|
+
return loading;
|
|
43
|
+
}
|
|
44
|
+
var NAME_CHAR = /[a-z0-9_+-]/i;
|
|
45
|
+
var NO_SPACE_BEFORE = /[\s.,;:!?)\]}>'"’”»)」』】、。,.:;!?]/;
|
|
46
|
+
function emoji(options = {}) {
|
|
47
|
+
const { name = "emoji", limit = 8, minLength = 2 } = options;
|
|
48
|
+
return suggestion({
|
|
49
|
+
name,
|
|
50
|
+
trigger: [":", "\uFF1A"],
|
|
51
|
+
label: options.label ?? "Emoji",
|
|
52
|
+
className: "mbe-emoji-menu",
|
|
53
|
+
allow: ({ excluded, explicit, query }) => !excluded && (explicit || query.length >= minLength) && [...query].every((c) => NAME_CHAR.test(c)),
|
|
54
|
+
items: async ({ query }) => searchEmoji(await loadEmoji(), query, limit).map((e) => ({ id: e.names[0], label: `:${e.names[0]}:`, icon: e.emoji, value: e.emoji })),
|
|
55
|
+
command: ({ editor, range, item, query }) => {
|
|
56
|
+
const block = editor.state.doc.blocks.find((b) => b.id === range.from.blockId);
|
|
57
|
+
const content = block && containerContent(block, range.from.cell);
|
|
58
|
+
const text = content ? inlineText(content) : "";
|
|
59
|
+
let end = range.from.offset + 1 + query.length;
|
|
60
|
+
while (end < range.to.offset && NAME_CHAR.test(text[end])) end++;
|
|
61
|
+
const to = { ...range.from, offset: end };
|
|
62
|
+
const next = text[end];
|
|
63
|
+
const space = next !== void 0 && NO_SPACE_BEFORE.test(next) ? "" : " ";
|
|
64
|
+
editor.commands.insertText(item.value + space, { range: { from: range.from, to } });
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export {
|
|
69
|
+
emoji,
|
|
70
|
+
loadEmoji,
|
|
71
|
+
parseEmoji,
|
|
72
|
+
searchEmoji
|
|
73
|
+
};
|
|
74
|
+
//# sourceMappingURL=emoji.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/emoji/index.ts", "../src/emoji/search.ts"],
|
|
4
|
+
"sourcesContent": ["// @usememos/editor/emoji: `:name` autocomplete that writes the emoji itself,\n// as GitHub, Notion and Linear offer. The names are GitHub's (gemoji). The\n// list of 1,870 emoji loads the first time the popup opens, so an app pays\n// for it only when someone types an emoji.\n\nimport { type Extension, type SuggestionItem, containerContent, inlineText, suggestion } from '@usememos/editor';\nimport { type Emoji, parseEmoji, searchEmoji } from './search';\n\nexport { type Emoji, parseEmoji, searchEmoji } from './search';\n\nexport interface EmojiOptions {\n /** The suggestion's name, for `editor.suggestion.open(name)`. Default `emoji`. */\n readonly name?: string;\n /** Most emoji shown. Default 8. */\n readonly limit?: number;\n /** Characters typed after `:` before the popup opens, so `:)` and `a :b` stay quiet. Default 2. Ctrl+Space opens it sooner. */\n readonly minLength?: number;\n /** Accessible name of the popup. Default `Emoji`. */\n readonly label?: string;\n}\n\ninterface EmojiItem extends SuggestionItem {\n readonly value: string;\n}\n\nlet loading: Promise<readonly Emoji[]> | null = null;\n\n/** Every emoji the extension offers, loaded once on first use. */\nexport function loadEmoji(): Promise<readonly Emoji[]> {\n loading ??= import('./data').then((m) => parseEmoji(m.EMOJI_DATA));\n return loading;\n}\n\n/** A character of an emoji name. */\nconst NAME_CHAR = /[a-z0-9_+-]/i;\n\n/** Characters before which a pick adds no space, as for the editor's other suggestions. */\nconst NO_SPACE_BEFORE = /[\\s.,;:!?)\\]}>'\"\u2019\u201D\u00BB\uFF09\u300D\u300F\u3011\u3001\u3002\uFF0C\uFF0E\uFF1A\uFF1B\uFF01\uFF1F]/;\n\n/**\n * Emoji autocomplete: typing `:` and two letters at the start of a word lists\n * emoji by GitHub's names and tags (`:tada`, `:party`). A pick replaces the\n * typed name with the emoji, so the Markdown holds the character itself.\n */\nexport function emoji(options: EmojiOptions = {}): Extension {\n const { name = 'emoji', limit = 8, minLength = 2 } = options;\n return suggestion<EmojiItem>({\n name,\n trigger: [':', '\uFF1A'],\n label: options.label ?? 'Emoji',\n className: 'mbe-emoji-menu',\n allow: ({ excluded, explicit, query }) => !excluded && (explicit || query.length >= minLength) && [...query].every((c) => NAME_CHAR.test(c)),\n items: async ({ query }) =>\n searchEmoji(await loadEmoji(), query, limit).map((e) => ({ id: e.names[0], label: `:${e.names[0]}:`, icon: e.emoji, value: e.emoji })),\n command: ({ editor, range, item, query }) => {\n // Replace the trigger and the name, also the part after the caret, but not what follows the name, such as \"!\".\n const block = editor.state.doc.blocks.find((b) => b.id === range.from.blockId);\n const content = block && containerContent(block, range.from.cell);\n const text = content ? inlineText(content) : '';\n let end = range.from.offset + 1 + query.length;\n while (end < range.to.offset && NAME_CHAR.test(text[end]!)) end++;\n const to = { ...range.from, offset: end };\n const next = text[end];\n const space = next !== undefined && NO_SPACE_BEFORE.test(next) ? '' : ' ';\n editor.commands.insertText(item.value + space, { range: { from: range.from, to } });\n },\n });\n}\n", "// Finding emoji by name for the `emoji()` extension.\n\nexport interface Emoji {\n /** The character, such as \"\uD83C\uDF89\". */\n readonly emoji: string;\n /** Its names, the first the main one, such as [\"tada\"]. Typed as `:tada:` on GitHub. */\n readonly names: readonly string[];\n /** Other words it is found by, such as [\"party\"]. */\n readonly tags: readonly string[];\n}\n\n/** Reads the lines of `data.ts`: the emoji, its names and its tags, separated by tabs. */\nexport function parseEmoji(data: string): Emoji[] {\n return data.split('\\n').map((line) => {\n const [emoji = '', names = '', tags = ''] = line.split('\\t');\n return { emoji, names: names.split(' ').filter(Boolean), tags: tags.split(' ').filter(Boolean) };\n });\n}\n\n/**\n * Emoji whose names or tags match `query`, best first: a name equal to it,\n * a name starting with it, a word of a name starting with it (\"smile\" finds\n * \"sweat_smile\"), a tag starting with it, then a name containing it. Within\n * a rank the data's order is kept, which puts common emoji first.\n */\nexport function searchEmoji(emoji: readonly Emoji[], query: string, limit = Infinity): Emoji[] {\n const q = query.toLowerCase();\n if (!q) return emoji.slice(0, limit);\n const ranked: Emoji[][] = [[], [], [], [], []];\n for (const e of emoji) {\n const rank = rankOf(e, q);\n if (rank !== null) ranked[rank]!.push(e);\n }\n return ranked.flat().slice(0, limit);\n}\n\nfunction rankOf(e: Emoji, q: string): number | null {\n let best: number | null = null;\n const take = (rank: number) => {\n if (best === null || rank < best) best = rank;\n };\n for (const name of e.names) {\n if (name === q) return 0;\n if (name.startsWith(q)) take(1);\n else if (name.includes(`_${q}`) || name.includes(`-${q}`)) take(2);\n else if (name.includes(q)) take(4);\n }\n if (best === null || best > 3) for (const tag of e.tags) if (tag.startsWith(q)) take(3);\n return best;\n}\n"],
|
|
5
|
+
"mappings": ";AAKA,SAA8C,kBAAkB,YAAY,kBAAkB;;;ACOvF,SAAS,WAAW,MAAuB;A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