Skip to content

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

:art: Automatically adjust editor font weight based on font file https://github.com/siyuan-note/siyuan/issues/10313

Danieladdaa3bdac31
diff --git a/app/src/config/editor.ts b/app/src/config/editor.ts
index fe11f0e27c..98b4826460 100644
--- a/app/src/config/editor.ts
+++ b/app/src/config/editor.ts
@@ -437,6 +437,7 @@ export const editor = {
/// #endif
const fontFamilyElement = editor.element.querySelector("#fontFamily") as HTMLSelectElement;
+ fontFamilyElement.dataset.weight = String(window.siyuan.config.editor.fontWeight || 0);
fontFamilyElement.addEventListener("click", () => {
fetchPost("/api/system/getSysFonts", {}, (response) => {
const fontMenu = new Menu();
@@ -450,20 +451,22 @@ export const editor = {
}
fontFamilyElement.value = "";
fontFamilyElement.style.fontFamily = "";
+ fontFamilyElement.dataset.weight = "0";
setEditor();
}
});
- response.data.forEach((item: string) => {
+ response.data.forEach((item: {family: string; weight: number; displayName: string}) => {
fontMenu.addItem({
iconHTML: "",
- checked: window.siyuan.config.editor.fontFamily === item,
- label: `<div style='font-family:"${item}",var(--b3-font-family);'>${item}</div>`,
+ checked: window.siyuan.config.editor.fontFamily === item.family && window.siyuan.config.editor.fontWeight === item.weight,
+ label: `<div style='font-family:"${item.family}",var(--b3-font-family);'>${item.displayName}</div>`,
click: () => {
- if (item === window.siyuan.config.editor.fontFamily) {
+ if (item.family === window.siyuan.config.editor.fontFamily && item.weight === window.siyuan.config.editor.fontWeight) {
return;
}
- fontFamilyElement.value = item;
- fontFamilyElement.style.fontFamily = item + ",var(--b3-font-family)";
+ fontFamilyElement.value = item.family;
+ fontFamilyElement.style.fontFamily = item.family + ",var(--b3-font-family)";
+ fontFamilyElement.dataset.weight = String(item.weight);
setEditor();
}
});
@@ -556,6 +559,7 @@ export const editor = {
generateHistoryInterval: parseInt((editor.element.querySelector("#generateHistoryInterval") as HTMLInputElement).value),
historyRetentionDays: parseInt((editor.element.querySelector("#historyRetentionDays") as HTMLInputElement).value),
fontFamily: fontFamilyElement.value,
+ fontWeight: parseInt(fontFamilyElement.dataset.weight || "0"),
emoji: window.siyuan.config.editor.emoji
}, response => {
editor._onSetEditor(response.data);
diff --git a/app/src/types/config.d.ts b/app/src/types/config.d.ts
index 9e56ab18ca..72bf51903f 100644
--- a/app/src/types/config.d.ts
+++ b/app/src/types/config.d.ts
@@ -455,6 +455,10 @@ declare namespace Config {
* The font used in the editor
*/
fontFamily: string;
+ /**
+ * The font weight used in the editor, 0 means not set
+ */
+ fontWeight: number;
/**
* The font size used in the editor
*/
diff --git a/app/src/util/assets.ts b/app/src/util/assets.ts
index a4b96b579d..4dfc670284 100644
--- a/app/src/util/assets.ts
+++ b/app/src/util/assets.ts
@@ -298,7 +298,7 @@ export const setInlineStyle = async (set = true, servePath = "../../../") => {
}`;
}
if (window.siyuan.config.editor.fontFamily) {
- style += `\n.b3-typography:not(.b3-typography--default), .protyle-wysiwyg, .protyle-title {font-family: "Emojis Additional", "Emojis Reset", "${window.siyuan.config.editor.fontFamily}", var(--b3-font-family)}`;
+ style += `\n.b3-typography:not(.b3-typography--default), .protyle-wysiwyg, .protyle-title {${window.siyuan.config.editor.fontWeight ? `font-weight: ${window.siyuan.config.editor.fontWeight};` : ""}font-family: "Emojis Additional", "Emojis Reset", "${window.siyuan.config.editor.fontFamily}", var(--b3-font-family)}`;
}
// pad 端菜单移除显示,如工作空间
if ("ontouchend" in document) {
@@ -394,13 +394,13 @@ export const setBodyHighlight = () => {
// 预定义颜色:赤橙黄绿青蓝紫(提高饱和度和亮度)
const colors = [
- { h: 0, s: 85, l: 50 }, // 赤 - 鲜艳红
- { h: 30, s: 90, l: 52 }, // 橙 - 亮橙色
- { h: 50, s: 88, l: 50 }, // 黄 - 金黄色
- { h: 140, s: 80, l: 48 }, // 绿 - 翠绿色
- { h: 185, s: 85, l: 50 }, // 青 - 亮青色
- { h: 230, s: 82, l: 52 }, // 蓝 - 宝蓝色
- { h: 280, s: 85, l: 50 }, // 紫 - 亮紫色
+ {h: 0, s: 85, l: 50}, // 赤 - 鲜艳红
+ {h: 30, s: 90, l: 52}, // 橙 - 亮橙色
+ {h: 50, s: 88, l: 50}, // 黄 - 金黄色
+ {h: 140, s: 80, l: 48}, // 绿 - 翠绿色
+ {h: 185, s: 85, l: 50}, // 青 - 亮青色
+ {h: 230, s: 82, l: 52}, // 蓝 - 宝蓝色
+ {h: 280, s: 85, l: 50}, // 紫 - 亮紫色
];
let hue, saturation, lightness;
diff --git a/kernel/api/system.go b/kernel/api/system.go
index 97b5c184d7..104ebe8738 100644
--- a/kernel/api/system.go
+++ b/kernel/api/system.go
@@ -675,15 +675,9 @@ func setFollowSystemLockScreen(c *gin.Context) {
func getSysFonts(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
- fonts := util.LoadSysFonts()
- // TODO: 字重 https://github.com/siyuan-note/siyuan/issues/10313
- var families []string
- for _, font := range fonts {
- families = append(families, font.Family)
- }
- families = gulu.Str.RemoveDuplicatedElem(families)
- ret.Data = families
+ fonts := util.LoadSysFonts()
+ ret.Data = fonts
}
func version(c *gin.Context) {
diff --git a/kernel/conf/editor.go b/kernel/conf/editor.go
index 4e18df111b..0c32a78e65 100644
--- a/kernel/conf/editor.go
+++ b/kernel/conf/editor.go
@@ -24,6 +24,7 @@ type Editor struct {
FontSize int `json:"fontSize"` // 字体大小
FontSizeScrollZoom bool `json:"fontSizeScrollZoom"` // 字体大小是否支持滚轮缩放
FontFamily string `json:"fontFamily"` // 字体
+ FontWeight int `json:"fontWeight"` // 字重
CodeSyntaxHighlightLineNum bool `json:"codeSyntaxHighlightLineNum"` // 代码块是否显示行号
CodeTabSpaces int `json:"codeTabSpaces"` // 代码块中 Tab 转换空格数,配置为 0 则表示不转换
CodeLineWrap bool `json:"codeLineWrap"` // 代码块是否自动折行
diff --git a/kernel/util/font.go b/kernel/util/font.go
index de208fd643..465c5ac6d9 100644
--- a/kernel/util/font.go
+++ b/kernel/util/font.go
@@ -193,6 +193,17 @@ func parseFont(font *sfnt.Font) *Font {
}
}
+ // 自动匹配 W1-W9(部分字体使用不带前导零的缩写)
+ if weight == 400 {
+ for i := 1; i <= 9; i++ {
+ wStr := "w" + strconv.Itoa(i)
+ if strings.Contains(s, wStr) {
+ weight = i * 100
+ break
+ }
+ }
+ }
+
// 自动匹配标准关键词
if weight == 400 { // 如果 W 系列没匹配到
switch {