Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: emojis autcomplete in markdown (#615)

Myles Scolnick2c79cb2124bc
diff --git a/frontend/src/core/codemirror/language/markdown.ts b/frontend/src/core/codemirror/language/markdown.ts
index 6877ae3b8e..d00331ad8a 100644
--- a/frontend/src/core/codemirror/language/markdown.ts
+++ b/frontend/src/core/codemirror/language/markdown.ts
@@ -7,6 +7,12 @@ import { parseMixed } from "@lezer/common";
import { python, pythonLanguage } from "@codemirror/lang-python";
import dedent from "dedent";
import { logNever } from "@/utils/assertNever";
+import {
+ Completion,
+ CompletionSource,
+ autocompletion,
+} from "@codemirror/autocomplete";
+import { once } from "lodash-es";
const prefixKinds = ["", "f", "r", "fr", "rf"] as const;
type PrefixKind = (typeof prefixKinds)[number];
@@ -128,6 +134,10 @@ export class MarkdownLanguageAdapter implements LanguageAdapter {
},
],
}),
+ autocompletion({
+ activateOnTyping: true,
+ override: [emojiCompletionSource],
+ }),
python().support,
];
}
@@ -178,3 +188,37 @@ function indentOneTab(code: string): string {
.map((line) => (line.trim() === "" ? line : ` ${line}`))
.join("\n");
}
+
+const emojiCompletionSource: CompletionSource = async (context) => {
+ // Check if the cursor is at a position where an emoji can be inserted
+ if (!context.explicit && !context.matchBefore(/:\w*$/)) {
+ return null;
+ }
+
+ const emojiList = await getEmojiList();
+ const filter = context.matchBefore(/:\w*$/)?.text.slice(1) ?? "";
+
+ return {
+ from: context.pos - filter.length - 1,
+ options: emojiList,
+ validFor: /^[\w:]*$/,
+ };
+};
+
+// This loads emojis from a CDN
+// This only happens for searching for emojis, so when you are not connected to the internet,
+// everything works fine, except for autocompletion of emojis
+const getEmojiList = once(async (): Promise<Completion[]> => {
+ const emojiList = await fetch(
+ "https://unpkg.com/[email protected]/dist/emoji-en-US.json"
+ ).then((res) => res.json() as unknown as Record<string, string[]>);
+
+ return Object.entries(emojiList).map(([emoji, names]) => ({
+ shortcode: names[0],
+ label: names.map((d) => `:${d}`).join(" "),
+ emoji,
+ displayLabel: `${emoji} ${names[0].replaceAll("_", " ")}`,
+ apply: emoji,
+ type: "emoji",
+ }));
+});
diff --git a/frontend/src/css/codemirror.css b/frontend/src/css/codemirror.css
index 256d3e95b0..80cddfa10b 100644
--- a/frontend/src/css/codemirror.css
+++ b/frontend/src/css/codemirror.css
@@ -111,6 +111,10 @@
max-width: 200px;
}
+#App .cm-tooltip.cm-tooltip-autocomplete > ul {
+ min-width: unset;
+}
+
#App .cm-tooltip-autocomplete ul li[aria-selected] {
background: #1177ccb0;
max-width: 200px;