Skip to content

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

:art: Improve editor font weight selection https://github.com/siyuan-note/siyuan/issues/19009

Daniel2996f765c012
diff --git a/app/src/config/tabs/appearanceTab.ts b/app/src/config/tabs/appearanceTab.ts
index d12e252731..619ec41f43 100644
--- a/app/src/config/tabs/appearanceTab.ts
+++ b/app/src/config/tabs/appearanceTab.ts
@@ -74,6 +74,20 @@ const getEditorFontDisplay = (fonts: IFontItem[]) =>
const isCodeFont = (font: Pick<IFontItem, "spacing">) =>
font.spacing === "monospace" || font.spacing === "dual" || font.spacing === "character-cell";
+const loadAvailableFonts = async () => {
+ const nativeMobile = isNativeMobileContainer();
+ const [systemResponse, customFonts] = await Promise.all([
+ fetchSyncPost("/api/system/getSysFonts"),
+ nativeMobile ? loadCustomFonts() : Promise.resolve([] as ICustomFont[])
+ ]);
+ const systemFonts = Array.isArray(systemResponse.data) ? systemResponse.data as IFontItem[] : [];
+ return {
+ nativeMobile,
+ customFonts,
+ fontItems: [...customFonts, ...systemFonts],
+ };
+};
+
const genFontConfigHtml = (configKey: FontFamiliesConfigKey, title: string, description: string) => {
const fonts = getEditorFonts(window.siyuan.config.editor, configKey);
return `<div class="fn__flex b3-label config-item config-wrap" data-font-config-key="${configKey}">
@@ -151,7 +165,8 @@ const genSelectedFontListHtml = (fonts: IFontItem[]) => fonts.map((font, index)
</div>`).join("");
const bindSelectedFontList = (element: HTMLElement, getFonts: () => IFontItem[],
- persist: (fonts: IFontItem[]) => void) => {
+ persist: (fonts: IFontItem[]) => void,
+ openWeightMenu: (chip: HTMLElement, index: number, event: MouseEvent) => void) => {
element.addEventListener("click", (event) => {
const action = (event.target as HTMLElement).closest<HTMLElement>('[data-type="font-remove"]');
const chipElement = action?.closest<HTMLElement>(".b3-chip");
@@ -163,6 +178,20 @@ const bindSelectedFontList = (element: HTMLElement, getFonts: () => IFontItem[],
fonts.splice(index, 1);
persist(fonts);
});
+ element.addEventListener("contextmenu", (event: MouseEvent) => {
+ const target = event.target as HTMLElement;
+ if (target.closest('[data-type="font-remove"]')) {
+ return;
+ }
+ const chipElement = target.closest<HTMLElement>(".b3-chip");
+ const index = parseInt(chipElement?.dataset.index, 10);
+ if (!chipElement || !element.contains(chipElement) || !Number.isInteger(index)) {
+ return;
+ }
+ event.preventDefault();
+ event.stopPropagation();
+ openWeightMenu(chipElement, index, event);
+ });
element.addEventListener("mousedown", (event: MouseEvent) => {
if (event.button !== 0 || (event.target as HTMLElement).closest('[data-type="font-remove"]')) {
return;
@@ -290,25 +319,20 @@ const mountAppearanceFontFamily = (root: HTMLElement, configKey: FontFamiliesCon
}
);
};
- bindSelectedFontList(selectedListElement, () => selectedFonts, persistEditorFonts);
+ bindSelectedFontList(selectedListElement, () => selectedFonts, persistEditorFonts, (chip, index, event) => {
+ openFontWeightMenu(chip, index, event);
+ });
mountedFontConfigUpdaters.set(fontConfigElement, updateFontInput);
updateFontInput(window.siyuan.config.editor);
fontFamiliesElement.addEventListener("click", async () => {
- const nativeMobile = isNativeMobileContainer();
- let systemResponse: IWebSocketData;
- let customFonts: ICustomFont[];
+ let availableFonts: Awaited<ReturnType<typeof loadAvailableFonts>>;
try {
- [systemResponse, customFonts] = await Promise.all([
- fetchSyncPost("/api/system/getSysFonts"),
- nativeMobile ? loadCustomFonts() : Promise.resolve([] as ICustomFont[])
- ]);
+ availableFonts = await loadAvailableFonts();
} catch (error) {
console.warn("load font list failed", error);
return;
}
- const systemFonts = Array.isArray(systemResponse.data) ? systemResponse.data as IFontItem[] : [];
-
- const fontItems = [...customFonts, ...systemFonts];
+ const {nativeMobile, customFonts, fontItems} = availableFonts;
selectedFonts = getEditorFonts(window.siyuan.config.editor, configKey).map((selectedFont) =>
fontItems.find((font) => font.family === selectedFont.family && font.weight === selectedFont.weight) ||
selectedFont);
@@ -514,6 +538,66 @@ const mountAppearanceFontFamily = (root: HTMLElement, configKey: FontFamiliesCon
fontMenu.element.querySelector<HTMLInputElement>('[data-type="font-search"]').focus();
});
+ async function openFontWeightMenu(chipElement: HTMLElement, index: number, event: MouseEvent) {
+ const selectedFont = selectedFonts[index];
+ if (!selectedFont || selectedFont.family !== chipElement.dataset.family) {
+ return;
+ }
+ let availableFonts: Awaited<ReturnType<typeof loadAvailableFonts>>;
+ try {
+ availableFonts = await loadAvailableFonts();
+ } catch (error) {
+ console.warn("load font list failed", error);
+ return;
+ }
+ const variantsByWeight = new Map<number, IFontItem>();
+ availableFonts.fontItems.forEach((font) => {
+ if (font.family === selectedFont.family) {
+ variantsByWeight.set(font.weight || 400, font);
+ }
+ });
+ if (!variantsByWeight.has(selectedFont.weight || 400)) {
+ variantsByWeight.set(selectedFont.weight || 400, selectedFont);
+ }
+ const variants = Array.from(variantsByWeight.values()).sort((fontA, fontB) =>
+ (fontA.weight || 400) - (fontB.weight || 400));
+ if (variants.length < 2) {
+ return;
+ }
+
+ const customFontsByID = new Map(availableFonts.customFonts.map((font) => [font.id, font]));
+ const weightMenu = new Menu();
+ variants.forEach((font) => {
+ weightMenu.addItem({
+ iconHTML: "",
+ label: escapeHtml(font.displayName || font.family),
+ checked: (font.weight || 400) === (selectedFont.weight || 400),
+ bind(element) {
+ const labelElement = element.querySelector<HTMLElement>(".b3-menu__label");
+ const customFont = font.id ? customFontsByID.get(font.id) : undefined;
+ if (customFont) {
+ registerCustomFont(customFont);
+ }
+ if (labelElement) {
+ labelElement.style.fontFamily = font.family;
+ labelElement.style.fontWeight = String(font.weight || 400);
+ }
+ },
+ click() {
+ const currentFont = selectedFonts[index];
+ if (!currentFont || currentFont.family !== selectedFont.family ||
+ currentFont.weight === font.weight) {
+ return;
+ }
+ const fonts = [...selectedFonts];
+ fonts[index] = font;
+ persistEditorFonts(fonts);
+ }
+ });
+ });
+ weightMenu.open({x: event.clientX, y: event.clientY, target: chipElement});
+ }
+
function updateFontInput(data: Config.IEditor) {
selectedFonts = getEditorFonts(data, configKey);
fontFamiliesElement.style.removeProperty("font-family");