Skip to content

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

:bug: Preserve built-in fonts for dynamic date icons https://github.com/siyuan-note/siyuan/issues/19941

Danielce28b6427c55
diff --git a/app/src/emoji/dynamicIcon.test.ts b/app/src/emoji/dynamicIcon.test.ts
index ae0829c510..e5669578a7 100644
--- a/app/src/emoji/dynamicIcon.test.ts
+++ b/app/src/emoji/dynamicIcon.test.ts
@@ -2,13 +2,16 @@ import * as assert from "node:assert/strict";
import {test} from "node:test";
import {getDynamicIconValue, refreshDynamicIcons} from "./dynamicIcon";
-test("font refresh reloads only local dynamic icons and preserves saved values", () => {
+test("font refresh reloads only local text icons and preserves date icons and saved values", () => {
assert.equal(getDynamicIconValue("api/icon/getDynamicIcon?_fontRefresh=old"), "api/icon/getDynamicIcon");
const sources = [
"api/icon/getDynamicIcon?type=8&content=%E6%97%A5&id=target",
- "/api/icon/getDynamicIcon?type=1&date=&_fontRefresh=old",
- "http://localhost/api/icon/getDynamicIcon?type=2",
- "https://example.com/api/icon/getDynamicIcon?type=1",
+ "/api/icon/getDynamicIcon?type=8&content=Text&_fontRefresh=old",
+ "http://localhost/api/icon/getDynamicIcon?type=8&content=Text",
+ ...["1", "2", "3", "4", "5", "6", "7", "", "unknown"].map(type =>
+ `api/icon/getDynamicIcon?type=${type}&date=&_fontRefresh=old`),
+ "api/icon/getDynamicIcon",
+ "https://example.com/api/icon/getDynamicIcon?type=8",
"/emojis/custom.svg",
"http://[invalid",
];
diff --git a/app/src/emoji/dynamicIcon.ts b/app/src/emoji/dynamicIcon.ts
index 85712c3de1..a077dc6f47 100644
--- a/app/src/emoji/dynamicIcon.ts
+++ b/app/src/emoji/dynamicIcon.ts
@@ -21,7 +21,8 @@ export const refreshDynamicIcons = () => {
} catch {
return;
}
- if (url.origin !== window.location.origin || !url.pathname.endsWith("/api/icon/getDynamicIcon")) {
+ if (url.origin !== window.location.origin || !url.pathname.endsWith("/api/icon/getDynamicIcon") ||
+ url.searchParams.get("type") !== "8") {
return;
}
// 刷新标记仅用于重新请求图片,选择图标时移除,不写入图标属性。
diff --git a/app/src/types/config.d.ts b/app/src/types/config.d.ts
index 1d2763ecc1..355d9d89ea 100644
--- a/app/src/types/config.d.ts
+++ b/app/src/types/config.d.ts
@@ -304,7 +304,7 @@ declare namespace Config {
light: {color: string; opacity: number};
dark: {color: string; opacity: number};
};
- /** 全局默认字体,按优先级从高到低排列;动态图标也使用此列表和首选字重,字体由客户端解析 */
+ /** 全局默认字体,按优先级从高到低排列;动态文字图标使用此列表和首选字重,字体由客户端解析,日期类图标保留内置字体 */
globalFontFamilies: IEditor["fontFamilies"];
/**
* Close button behavior
diff --git a/kernel/api/icon.go b/kernel/api/icon.go
index 5e0dee0d89..ca07aea6f8 100644
--- a/kernel/api/icon.go
+++ b/kernel/api/icon.go
@@ -181,7 +181,7 @@ var getDynamicIcon = contractHandler(apicontract.GetDynamicIcon, func(c *gin.Con
}
var fonts []*conf.EditorFont
- if nil != model.Conf.Appearance {
+ if "8" == iconType && nil != model.Conf.Appearance {
fonts = model.Conf.Appearance.GlobalFontFamilies
}
svg = applyDynamicIconFont(svg, fonts)
diff --git a/kernel/api/icon_test.go b/kernel/api/icon_test.go
index c41f2c04cc..eeeea49d5e 100644
--- a/kernel/api/icon_test.go
+++ b/kernel/api/icon_test.go
@@ -40,13 +40,13 @@ func TestAPIContractDynamicIconGlobalFonts(t *testing.T) {
{"escaped", []*conf.EditorFont{{Family: "A\"\\\n<&';font-weight:1", Weight: 500}}, `"A\22 \5c \a <&';font-weight:1", ` + fallback, "500"},
} {
for _, allowScript := range []bool{false, true} {
- for iconType := 1; iconType <= 8; iconType++ {
- t.Run(fmt.Sprintf("%s/type%d/script%t", test.name, iconType, allowScript), func(t *testing.T) {
+ for _, iconType := range []string{"1", "2", "3", "4", "5", "6", "7", "8", "", "unknown"} {
+ t.Run(fmt.Sprintf("%s/type%s/script%t", test.name, iconType, allowScript), func(t *testing.T) {
model.Conf.Appearance.GlobalFontFamilies = test.fonts
model.Conf.Editor.AllowSVGScript = allowScript
recorder := httptest.NewRecorder()
context, _ := gin.CreateTestContext(recorder)
- context.Request = httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/icon/getDynamicIcon?type=%d&content=Text&date=2026-09-29&lang=en", iconType), nil)
+ context.Request = httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/icon/getDynamicIcon?type=%s&content=Text&date=2026-09-29&lang=en", iconType), nil)
getDynamicIcon(context)
requireAPIContract(t, http.MethodGet, "/api/icon/getDynamicIcon", recorder)
var svg struct {
@@ -60,7 +60,11 @@ func TestAPIContractDynamicIconGlobalFonts(t *testing.T) {
if err := xml.Unmarshal(recorder.Body.Bytes(), &svg); err != nil {
t.Fatal(err)
}
- if svg.Family != test.family || svg.Weight != test.weight {
+ family, weight := fallback, "400"
+ if "8" == iconType {
+ family, weight = test.family, test.weight
+ }
+ if svg.Family != family || svg.Weight != weight {
t.Fatalf("unexpected font: %q / %q", svg.Family, svg.Weight)
}
if len(svg.Texts) == 0 {
diff --git a/kernel/apicontract/icon.go b/kernel/apicontract/icon.go
index 3e22e59182..136236a26e 100644
--- a/kernel/apicontract/icon.go
+++ b/kernel/apicontract/icon.go
@@ -1,6 +1,6 @@
package apicontract
-// DynamicIconRequest 生成动态图标 SVG,字体按全局默认字体的优先顺序回退,并使用首选字体的字重。
+// DynamicIconRequest 生成动态图标 SVG。文字图标(type=8)跟随全局默认字体列表和首选字重,日期类图标使用内置字体和常规字重。
// 未配置全局字体时使用内置字体列表;字体由客户端解析,不嵌入字体文件,缺失字体时继续回退。
type DynamicIconRequest struct {
Type string `json:"type" api:"optional"`