Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Scope random emoji selection to the current page https://github.com/siyuan-note/siyuan/issues/13563

Daniel4e3541ce55ed
diff --git a/app/src/emoji/index.ts b/app/src/emoji/index.ts
index ed83d94240..490afcb5bf 100644
--- a/app/src/emoji/index.ts
+++ b/app/src/emoji/index.ts
@@ -28,15 +28,18 @@ import {
getActiveEmojiCategory,
getEmojiItemMap,
getEmojiVirtualChunks,
+ getRandomEmojiCategories,
groupCustomEmojiItems,
+ type TRandomEmojiScope,
} from "./panel";
-export const getRandomEmoji = () => {
- const emojis = window.siyuan.emojis[getRandom(0, window.siyuan.emojis.length - 1)];
- if (typeof emojis.items[getRandom(0, emojis.items.length - 1)] === "undefined") {
- return "1f600";
+export const getRandomEmoji = (scope: TRandomEmojiScope = "all") => {
+ const categories = getRandomEmojiCategories(window.siyuan.emojis, scope);
+ if (categories.length === 0) {
+ return scope === "all" ? "1f600" : "";
}
- return emojis.items[getRandom(0, emojis.items.length - 1)].unicode;
+ const category = categories[getRandom(0, categories.length - 1)];
+ return category.items[getRandom(0, category.items.length - 1)].unicode;
};
export const unicode2Emoji = (unicode: string, className = "", needSpan = false, lazy = false) => {
@@ -1414,7 +1417,10 @@ export const openEmojiPanel = (
dialog.destroy();
} else {
// 随机
- unicode = getRandomEmoji();
+ unicode = getRandomEmoji(customEmojiPage ? "custom" : "builtIn");
+ if (!unicode) {
+ break;
+ }
}
if (type === "notebook") {
fetchPost("/api/notebook/setNotebookIcon", {
diff --git a/app/src/emoji/panel.test.ts b/app/src/emoji/panel.test.ts
index 03dc7b347e..f8ea6cdcb1 100644
--- a/app/src/emoji/panel.test.ts
+++ b/app/src/emoji/panel.test.ts
@@ -5,6 +5,7 @@ import {
getActiveEmojiCategory,
getEmojiItemMap,
getEmojiVirtualChunks,
+ getRandomEmojiCategories,
groupCustomEmojiItems,
} from "./panel";
@@ -96,3 +97,31 @@ describe("emoji filtering", () => {
assert.equal(getEmojiItemMap(categories, true), getEmojiItemMap(categories, true));
});
});
+
+describe("getRandomEmojiCategories", () => {
+ const categories: IEmoji[] = [{
+ id: "custom",
+ title: "Custom",
+ title_zh_cn: "自定义",
+ title_ja_jp: "カスタム",
+ items: [createEmoji("custom.png")],
+ }, {
+ id: "people",
+ title: "People",
+ title_zh_cn: "人物",
+ title_ja_jp: "人々",
+ items: [createEmoji("1f600")],
+ }, {
+ id: "empty",
+ title: "Empty",
+ title_zh_cn: "空",
+ title_ja_jp: "空",
+ items: [],
+ }];
+
+ it("keeps random choices within the selected emoji page", () => {
+ assert.deepEqual(getRandomEmojiCategories(categories, "builtIn").map((item) => item.id), ["people"]);
+ assert.deepEqual(getRandomEmojiCategories(categories, "custom").map((item) => item.id), ["custom"]);
+ assert.deepEqual(getRandomEmojiCategories(categories, "all").map((item) => item.id), ["custom", "people"]);
+ });
+});
diff --git a/app/src/emoji/panel.ts b/app/src/emoji/panel.ts
index 226aa3ae69..24363526a9 100644
--- a/app/src/emoji/panel.ts
+++ b/app/src/emoji/panel.ts
@@ -5,6 +5,13 @@ export type TCustomEmojiGroup = {
items: IEmojiItem[];
};
+export type TRandomEmojiScope = "all" | "builtIn" | "custom";
+
+export const getRandomEmojiCategories = (categories: IEmoji[], scope: TRandomEmojiScope) => {
+ return categories.filter((category) => category.items.length > 0 &&
+ (scope === "all" || (category.id === "custom") === (scope === "custom")));
+};
+
const emojiItemMapCache = new WeakMap<IEmoji[], {
all: Map<string, IEmojiItem>;
builtIn: Map<string, IEmojiItem>;