Skip to content

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

:bug: Preserve Windows fallback emoji icon alignment https://github.com/siyuan-note/siyuan/issues/20731

Danielc485dca12be4
diff --git a/app/src/assets/scss/component/_list.scss b/app/src/assets/scss/component/_list.scss
index b316cefb2e..ec16e87032 100644
--- a/app/src/assets/scss/component/_list.scss
+++ b/app/src/assets/scss/component/_list.scss
@@ -311,12 +311,20 @@
align-items: center;
justify-content: center;
+ body.body--windows & > .b3-list-item__emoji {
+ transform: translateY(var(--b3-file-tree-emoji-offset, 0px));
+ }
+
&:hover {
color: var(--b3-theme-on-background);
background-color: var(--b3-list-icon-hover);
}
}
+ &__emoji {
+ pointer-events: none;
+ }
+
&__hinttext {
overflow: hidden;
white-space: nowrap;
diff --git a/app/src/emoji/fileTreeIcon.test.ts b/app/src/emoji/fileTreeIcon.test.ts
index ef1440188b..b6904b0c98 100644
--- a/app/src/emoji/fileTreeIcon.test.ts
+++ b/app/src/emoji/fileTreeIcon.test.ts
@@ -74,16 +74,16 @@ describe("file tree icon", () => {
});
it("keeps the Emoji defaults when SVG icons are disabled", () => {
- assert.equal(getFileTreeIconHTML("", "notebook", "", false, false), "🗃");
- assert.equal(getFileTreeIconHTML("", "folder", "", false, false), "📑");
- assert.equal(getFileTreeIconHTML("", "file", "", false, false), "📄");
- assert.equal(getFileTreeIconHTML("", "lock", "", false, false), "🔒️");
+ assert.equal(getFileTreeIconHTML("", "notebook", "", false, false), '<span class="b3-list-item__emoji">🗃</span>');
+ assert.equal(getFileTreeIconHTML("", "folder", "", false, false), '<span class="b3-list-item__emoji">📑</span>');
+ assert.equal(getFileTreeIconHTML("", "file", "", false, false), '<span class="b3-list-item__emoji">📄</span>');
+ assert.equal(getFileTreeIconHTML("", "lock", "", false, false), '<span class="b3-list-item__emoji">🔒️</span>');
assert.equal(getDocumentIconHTML("", "mobile-tabs__item-icon", false),
'<span class="mobile-tabs__item-icon">📄</span>');
});
it("treats an explicitly configured icon as custom", () => {
- assert.equal(getFileTreeIconHTML("1f4c4", "folder", "", false, true), "📄");
+ assert.equal(getFileTreeIconHTML("1f4c4", "folder", "", false, true), '<span class="b3-list-item__emoji">📄</span>');
assert.equal(getDocumentIconHTML("1f4c4", "mobile-tabs__item-icon", true),
'<span class="mobile-tabs__item-icon">📄</span>');
assert.equal(getFileTreeDefaultIconAttr("1f4c4", "folder"), "");
@@ -111,13 +111,13 @@ describe("file tree icon", () => {
updateFileTreeItemIcon(liElement, "1f4c4");
assert.equal("defaultIcon" in dataset, false);
- assert.equal(iconElement.innerHTML, "📄");
+ assert.equal(iconElement.innerHTML, '<span class="b3-list-item__emoji">📄</span>');
updateFileTreeItemIcon(liElement, "", "file");
window.siyuan.config.fileTree.useSVGDefaultIcon = false;
refreshDefaultFileTreeIcons({querySelectorAll: () => [liElement]} as unknown as ParentNode);
assert.equal(dataset.defaultIcon, "file");
- assert.equal(iconElement.innerHTML, "📄");
+ assert.equal(iconElement.innerHTML, '<span class="b3-list-item__emoji">📄</span>');
});
it("refreshes locked notebook icons with the SVG setting", () => {
@@ -131,6 +131,24 @@ describe("file tree icon", () => {
assert.match(iconElement.innerHTML, /#iconLock/);
window.siyuan.config.fileTree.useSVGDefaultIcon = false;
refreshDefaultFileTreeIcons({querySelectorAll: () => [liElement]} as unknown as ParentNode);
- assert.equal(iconElement.innerHTML, "🔒️");
+ assert.equal(iconElement.innerHTML, '<span class="b3-list-item__emoji">🔒️</span>');
+ });
+
+ it("keeps the compensation wrapper limited to emoji when switching icon kinds", () => {
+ const iconElement = {classList: {contains: (value: string) => value === "b3-list-item__icon"}, innerHTML: ""};
+ const liElement = {
+ children: [iconElement],
+ dataset: {} as Record<string, string>,
+ } as unknown as HTMLElement;
+ updateFileTreeItemIcon(liElement, "1f4c4", "file");
+ assert.equal(iconElement.innerHTML, '<span class="b3-list-item__emoji">📄</span>');
+ updateFileTreeItemIcon(liElement, "custom.png", "file");
+ assert.equal(iconElement.innerHTML, '<img class="" src="/emojis/custom.png"/>');
+ window.siyuan.config.fileTree.useSVGDefaultIcon = true;
+ updateFileTreeItemIcon(liElement, "", "file");
+ assert.equal(iconElement.innerHTML, '<svg><use xlink:href="#iconFile"></use></svg>');
+ window.siyuan.config.fileTree.useSVGDefaultIcon = false;
+ refreshDefaultFileTreeIcons({querySelectorAll: () => [liElement]} as unknown as ParentNode);
+ assert.equal(iconElement.innerHTML, '<span class="b3-list-item__emoji">📄</span>');
});
});
diff --git a/app/src/emoji/fileTreeIcon.ts b/app/src/emoji/fileTreeIcon.ts
index b5897c253b..6ab6d69bf3 100644
--- a/app/src/emoji/fileTreeIcon.ts
+++ b/app/src/emoji/fileTreeIcon.ts
@@ -28,6 +28,11 @@ const isDefaultIcon = (value: string | undefined): value is TFileTreeDefaultIcon
const getDirectIconElement = (liElement: HTMLElement) => Array.from(liElement.children).find((item) =>
item.classList.contains("b3-list-item__icon") || item.classList.contains("b3-list-item__graphic")) as HTMLElement | undefined;
+const renderFileTreeEmoji = (icon: string, className: string, needSpan: boolean) => {
+ const html = unicode2Emoji(icon, className, needSpan);
+ return html && !html.startsWith("<") ? `<span class="b3-list-item__emoji">${html}</span>` : html;
+};
+
export const getFileTreeDefaultIconAttr = (icon: string, defaultIcon: TFileTreeDefaultIcon, disabled = false) =>
!icon && !disabled ? ` data-default-icon="${defaultIcon}"` : "";
@@ -35,11 +40,11 @@ export const getFileTreeIconHTML = (icon: string, defaultIcon: TFileTreeDefaultI
needSpan = false, useSVGDefaultIcon =
window.siyuan.config.fileTree.useSVGDefaultIcon === true) => {
if (icon) {
- return unicode2Emoji(icon, className, needSpan);
+ return renderFileTreeEmoji(icon, className, needSpan);
}
if (!useSVGDefaultIcon) {
- return unicode2Emoji(getDefaultEmoji(defaultIcon), className, needSpan);
+ return renderFileTreeEmoji(getDefaultEmoji(defaultIcon), className, needSpan);
}
const classAttr = needSpan ? ` class="${escapeAttr(className)}"` : "";
@@ -75,7 +80,7 @@ export const updateFileTreeItemIcon = (liElement: HTMLElement, icon: string,
}
if (icon) {
delete liElement.dataset.defaultIcon;
- iconElement.innerHTML = unicode2Emoji(icon);
+ iconElement.innerHTML = getFileTreeIconHTML(icon, defaultIcon);
return;
}
liElement.dataset.defaultIcon = defaultIcon;
diff --git a/app/src/util/emojiFont.test.ts b/app/src/util/emojiFont.test.ts
index d3446b802d..a3b45d1341 100644
--- a/app/src/util/emojiFont.test.ts
+++ b/app/src/util/emojiFont.test.ts
@@ -56,6 +56,13 @@ describe("Unicode 17 emoji fonts", () => {
}
});
+ it("compensates file tree emoji only when using the bundled Windows fallback", () => {
+ for (const platform of ["apple", "windows11", "other"] as const) {
+ assert.ok(getEmojiFontStyle(platform, "").includes(
+ `--b3-file-tree-emoji-offset: ${platform === "other" ? "-1px" : "0px"}`));
+ }
+ });
+
it("resolves the same bundled font from app, PDF and offline HTML paths", () => {
const font = readFileSync(path.join(appPath, NOTO_EMOJI_FONT_PATH));
assert.equal(font.subarray(0, 4).toString(), "wOF2");
diff --git a/app/src/util/emojiFont.ts b/app/src/util/emojiFont.ts
index f3889014a3..42fda722cc 100644
--- a/app/src/util/emojiFont.ts
+++ b/app/src/util/emojiFont.ts
@@ -97,5 +97,6 @@ export const getEmojiFontStyle = (platform: "apple" | "windows11" | "other", ser
size-adjust: 92%;
}`;
}
- return style;
+ // 内置字体分支供 Windows 文档树保留字形补偿,原生字体分支由字体度量完成对齐。
+ return `${style}\n:root { --b3-file-tree-emoji-offset: ${platform === "other" ? "-1px" : "0px"}; }`;
};
diff --git a/app/tests/emojiIconAlignment.test.js b/app/tests/emojiIconAlignment.test.js
index 09da848a3e..c03d8c1811 100644
--- a/app/tests/emojiIconAlignment.test.js
+++ b/app/tests/emojiIconAlignment.test.js
@@ -18,7 +18,7 @@ const UNCALIBRATED_TOLERANCE = 1.5;
// 独立格子里没有文字可参照,度量按文字对齐标定后会略微上移,这里只限制在可见范围内
const STANDALONE_TOLERANCE = 2;
-const icon = () => `<span class="b3-list-item__icon" id="icon">${GLYPH}</span>`;
+const icon = () => `<span class="b3-list-item__icon" id="icon"><span class="b3-list-item__emoji">${GLYPH}</span></span>`;
const text = (className) => `<span class="${className}" id="text">${TEXT}</span>`;
// 每个图标位使用最贴近真实标记的结构;reference 指明该位置的对齐参照物
@@ -77,16 +77,16 @@ const getFileTreeIconHTML = (iconValue, defaultIcon, className = "", needSpan =
// 文字必须使用与产品相同的字体栈,否则测到的文字墨迹中心没有参考价值
const themeVars = () => fs.readFileSync(path.join(root, "appearance/themes/daylight/theme.css"), "utf8")
- .match(/--b3-font-family[-a-z]*: [^;]+;/g).join(" ");
+ .match(/[^{}]+\{[^{}]*\}/g).map(rule => {
+ const declarations = rule.match(/--b3-font-family[-a-z]*: [^;]+;/g);
+ return declarations ? `${rule.slice(0, rule.indexOf("{"))} { ${declarations.join(" ")} }` : "";
+ }).join("\n");
-const buildPage = ({css, fontStyle, servePath, body, rowClass = ""}) => {
- // Emojis Additional 只补充 Unicode 17 新增码位,不覆盖图标字符。它在 file:// 离屏页面里会被错误地
- // 分配给图标文本且不绘制(产品由 HTTP 提供字体,不存在此情况),因此测试页排除该 face。
- const fontCSS = fontStyle("windows11", servePath)
- .replace(/@font-face \{[^}]*font-family: "Emojis Additional";[^}]*\}/, "");
- return `<!doctype html><meta charset="utf-8">
+const buildPage = ({css, fontStyle, body, rowClass = "", bodyClass = "body--windows", lang = "ja"}) => {
+ const fontCSS = fontStyle();
+ return `<!doctype html><html lang="${lang}"><meta charset="utf-8">
<style>${fontCSS}</style>
-<style>:root { ${themeVars()} }</style>
+<style>${themeVars()}</style>
<style>${css}</style>
<style>
body { margin: 0; padding: 20px; background: #808080; font-size: 14px; }
@@ -94,7 +94,7 @@ body { margin: 0; padding: 20px; background: #808080; font-size: 14px; }
.measure-list { width: 220px; }
${rowClass}
</style>
-<body class="body--windows">${body}</body>`;
+<body class="${bodyClass}">${body}</body></html>`;
};
const inkIn = (bitmap, size, fromX, toX, fromY, toY) => {
@@ -153,16 +153,25 @@ const runElectron = async () => {
{logger: {warn() {}, debug() {}}}).css;
const mobileCSS = require("sass").compile(path.join(root, "src/assets/scss/mobile.scss"),
{logger: {warn() {}, debug() {}}}).css;
- // 内置字体需与产品一样可加载:主题里 Emojis Additional 排在最前,加载失败会让整段图标不渲染
+ // 通过 HTTP 加载完整字体栈,覆盖补充字体与原生字体混用的情况。
const notoSource = path.join(root, "appearance/fonts/Noto-COLRv1-2.051/Noto-COLRv1.woff2");
- const notoTarget = path.join(process.argv[2], "appearance/fonts/Noto-COLRv1-2.051/Noto-COLRv1.woff2");
- fs.mkdirSync(path.dirname(notoTarget), {recursive: true});
- fs.copyFileSync(notoSource, notoTarget);
- const servePath = "file:///" + process.argv[2].replace(/\\/g, "/") + "/";
+ let pageHTML = "";
+ const server = require("node:http").createServer((request, response) => {
+ if (request.url === "/") {
+ response.setHeader("Content-Type", "text/html; charset=utf-8");
+ response.end(pageHTML);
+ } else if (request.url === "/appearance/fonts/Noto-COLRv1-2.051/Noto-COLRv1.woff2") {
+ response.setHeader("Content-Type", "font/woff2");
+ response.end(fs.readFileSync(notoSource));
+ } else {
+ response.writeHead(404).end();
+ }
+ });
+ await new Promise(resolve => server.listen(0, "127.0.0.1", resolve));
+ const servePath = `http://127.0.0.1:${server.address().port}/`;
const load = async page => {
- const htmlPath = path.join(process.argv[2], "page.html");
- fs.writeFileSync(htmlPath, page);
- await win.loadFile(htmlPath);
+ pageHTML = page;
+ await win.loadURL(servePath);
await win.webContents.executeJavaScript(`document.fonts.ready.then(() => Promise.all([
document.fonts.load('12px "Emojis Icon"'), document.fonts.load('14px "Emojis Icon"'),
document.fonts.load('16px "Emojis Icon"'), document.fonts.load('19px "Emojis Icon"'),
@@ -172,8 +181,13 @@ const runElectron = async () => {
};
// 文档树图标:逐字形核对与文字的对齐,并确认图片/SVG 图标不受影响
- for (const [platform, tolerance] of [["windows11", TEXT_TOLERANCE], ["other", UNCALIBRATED_TOLERANCE]]) {
- for (const fontSize of [14, 16]) {
+ for (const [platform, bodyClass, tolerance] of [
+ ["windows11", "body--windows", TEXT_TOLERANCE],
+ ["other", "body--windows", UNCALIBRATED_TOLERANCE],
+ ["other", "body--linux", UNCALIBRATED_TOLERANCE],
+ ["other", "body--android", UNCALIBRATED_TOLERANCE],
+ ]) {
+ for (const [fontSize, lang] of [[14, "ja"], [16, "ja"], [16, "en"], [16, "zh-CN"]]) {
const rows = Object.entries(TREE_ICONS).map(([name, value]) =>
`<li class="b3-list-item" id="row-${name}">
<span class="b3-list-item__icon" id="icon-${name}">${
@@ -187,7 +201,7 @@ const runElectron = async () => {
getFileTreeIconHTML("", "file", "", false, true)}</span>
<span class="b3-list-item__text" id="text-svg">${TEXT}</span></li>`);
await load(buildPage({css: baseCSS, fontStyle: () => fontModule.getEmojiFontStyle(platform, servePath),
- servePath, body: `<div class="file-tree sy__file"><ul class="b3-list measure-list">${
+ bodyClass, lang, body: `<div class="file-tree sy__file"><ul class="b3-list measure-list">${
rows.join("")}</ul></div>`,
rowClass: `.measure-list .b3-list-item__icon { font-size: ${fontSize}px; }`}));
const image = await win.webContents.capturePage();
@@ -210,13 +224,29 @@ const runElectron = async () => {
`${name} graphic must stay centered`);
continue;
}
- // emoji 字形不应被包裹,也不应依赖 transform 做几何补偿
- assert.equal(iconBox.childTag, null, `${name}: emoji glyph must not be wrapped`);
+ assert.equal(iconBox.childTag, "SPAN", `${name}: emoji glyph has a separate click-neutral wrapper`);
+ const shift = await win.webContents.executeJavaScript(`(() => {
+ const icon = document.querySelector("#icon-${name}");
+ const emoji = icon.firstElementChild;
+ const before = emoji.getBoundingClientRect();
+ const pointerTarget = document.elementFromPoint(before.x + before.width / 2,
+ before.y + before.height / 2);
+ emoji.style.transform = "none";
+ const original = emoji.getBoundingClientRect();
+ emoji.style.transform = "";
+ return {delta: before.y - original.y, hitsIcon: pointerTarget === icon,
+ parentY: icon.getBoundingClientRect().y, parentHeight: icon.getBoundingClientRect().height};
+ })()`);
+ assert.equal(shift.delta, platform === "other" && bodyClass === "body--windows" ? -1 : 0,
+ `${platform}/${bodyClass}: only the Windows fallback shifts emoji ink`);
+ assert.equal(shift.hitsIcon, true, `${name}: glyph preserves the icon click target`);
+ assert.equal(shift.parentY, iconBox.y);
+ assert.equal(shift.parentHeight, iconBox.height);
const textBox = await measure(win, `#text-${name}`);
const iconInk = inkOf(bitmap, size, iconBox);
const textInk = inkOf(bitmap, size, textBox);
const delta = iconInk.center - textInk.center;
- console.log(`${platform}/${fontSize}px ${name}: ink ${iconInk.top.toFixed(1)}..` +
+ console.log(`${platform}/${bodyClass}/${lang}/${fontSize}px ${name}: ink ${iconInk.top.toFixed(1)}..` +
`${iconInk.bottom.toFixed(1)} text center ${textInk.center.toFixed(2)} delta ${delta.toFixed(2)}px`);
assert.ok(Math.abs(delta) <= tolerance,
`${platform}/${fontSize}px ${name}: emoji ink center is ${delta.toFixed(2)}px ` +
@@ -229,14 +259,15 @@ const runElectron = async () => {
for (const site of SITES) {
await load(buildPage({css: site.entry === "mobile" ? mobileCSS : baseCSS,
fontStyle: () => fontModule.getEmojiFontStyle("windows11", servePath),
- servePath, body: site.markup}));
+ body: site.markup}));
const image = await win.webContents.capturePage();
const size = image.getSize();
const bitmap = image.getBitmap();
const iconBox = await measure(win, "#icon");
assert.ok(iconBox.fontFamily.includes('"Emojis Icon"'),
`${site.name}: must use the icon emoji font, got ${iconBox.fontFamily}`);
- assert.equal(iconBox.childTag, null, `${site.name}: emoji glyph must not be wrapped`);
+ assert.equal(iconBox.childTag, site.name === "document tree icon" ? "SPAN" : null,
+ `${site.name}: only file tree emoji use the compensation wrapper`);
const iconInk = inkOf(bitmap, size, iconBox);
if (site.reference === "text") {
const textInk = inkOf(bitmap, size, await measure(win, "#text"));