From b05a1925ca7ff9529561a3afae8f543ae9f458a3 Mon Sep 17 00:00:00 2001 From: Innei Date: Sat, 27 Jul 2024 21:50:28 +0800 Subject: [PATCH] fix: font setting render Signed-off-by: Innei --- .../src/modules/settings/modules/fonts.tsx | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/renderer/src/modules/settings/modules/fonts.tsx b/src/renderer/src/modules/settings/modules/fonts.tsx index 9f1b7bced..099b24cea 100644 --- a/src/renderer/src/modules/settings/modules/fonts.tsx +++ b/src/renderer/src/modules/settings/modules/fonts.tsx @@ -14,8 +14,11 @@ import { SelectValue, } from "@renderer/components/ui/select" import { tipcClient } from "@renderer/lib/client" +import { nextFrame } from "@renderer/lib/dom" +import { getStorageNS } from "@renderer/lib/ns" import { useQuery } from "@tanstack/react-query" -import { useCallback, useMemo, useRef } from "react" +import { useCallback, useEffect, useMemo, useRef } from "react" +import * as React from "react" const FALLBACK_FONT = "Default" const DEFAULT_FONT = "SN Pro" @@ -101,7 +104,7 @@ export const UIFontSelector = () => { const setCustom = usePresentCustomFontDialog("uiFontFamily") const isCustomFont = useMemo( () => - + uiFont !== DEFAULT_FONT && data.find((d) => d.value === uiFont) === undefined, [data, uiFont], ) @@ -125,9 +128,7 @@ export const UIFontSelector = () => { - {isCustomFont && ( - {uiFont} - )} + {isCustomFont && {uiFont}} {DEFAULT_FONT} {data.map(({ label, value }) => ( @@ -143,34 +144,49 @@ export const UIFontSelector = () => { const usePresentCustomFontDialog = ( setKey: "uiFontFamily" | "readerFontFamily", ) => { + const HISTORY_KEY = getStorageNS("customFonts") const { present } = useModalStack() return useCallback(() => { present({ title: "Custom Font", - content: function Content({ dismiss }) { + clickOutsideToDismiss: true, + content: function Content({ dismiss, setClickOutSideToDismiss }) { const inputRef = useRef(null) + useEffect(() => { + nextFrame(() => inputRef.current?.focus()) + }, [inputRef]) + + const save: React.FormEventHandler = (e) => { + e.preventDefault() + const value = inputRef.current?.value + if (value) { + setUISetting(setKey, value) + localStorage.setItem(HISTORY_KEY, value) + dismiss() + } + } return ( -
- + +
+ { + setClickOutSideToDismiss(false) + }} + />
- { - const value = inputRef.current?.value - if (value) { - setUISetting(setKey, value) - dismiss() - } - }} - > + Save
-
+ + ) }, }) - }, [present, setKey]) + }, [HISTORY_KEY, present, setKey]) }