5858 <n-select
5959 v-else-if =" settingStore.fontSettingStyle === 'multi'"
6060 :value =" fontFamilyToArray(settingStore.globalFont)"
61- @update:value ="
62- (value: string[]) => (settingStore.globalFont = fontArrayToFamily(value))
63- "
61+ @update:value =" (val: string[]) => (settingStore.globalFont = fontArrayToFamily(val))"
6462 :options =" getOptions('globalFont')"
6563 class =" set"
6664 filterable
6967 />
7068 <n-select
7169 v-else-if =" settingStore.fontSettingStyle === 'single'"
72- v-model:value =" settingStore.globalFont"
70+ :value =" fontFamilyToDisplay(settingStore.globalFont)"
71+ @update:value =" (val) => (settingStore.globalFont = fontDisplayToFamily(val))"
7372 :options =" getOptions('globalFont')"
7473 class =" set"
7574 filterable
127126 multiple
128127 tag
129128 @update:value ="
130- (value : string[]) => (desktopLyricConfig.fontFamily = fontArrayToFamily(value ))
129+ (val : string[]) => (desktopLyricConfig.fontFamily = fontArrayToFamily(val ))
131130 "
132131 />
133132 <n-select
134133 v-else-if =" settingStore.fontSettingStyle === 'single'"
135- :value =" desktopLyricConfig.fontFamily"
134+ :value =" fontFamilyToDisplay( desktopLyricConfig.fontFamily) "
136135 :options =" getOptions('desktop')"
137136 class =" set"
138137 filterable
139138 @update:value ="
140139 (val) => {
141- desktopLyricConfig.fontFamily = val;
140+ desktopLyricConfig.fontFamily = fontDisplayToFamily( val) ;
142141 saveDesktopLyricConfig();
143142 }
144143 "
185184 v-else-if =" settingStore.fontSettingStyle === 'multi'"
186185 :value =" fontFamilyToArray(settingStore[font.keySetting])"
187186 @update:value ="
188- (value : string[]) => (settingStore[font.keySetting] = fontArrayToFamily(value ))
187+ (val : string[]) => (settingStore[font.keySetting] = fontArrayToFamily(val ))
189188 "
190189 :options =" getOptions(font.keySetting)"
191190 class =" set"
195194 />
196195 <n-select
197196 v-else-if =" settingStore.fontSettingStyle === 'single'"
198- v-model:value =" settingStore[font.keySetting]"
197+ :value =" fontFamilyToDisplay(settingStore[font.keySetting])"
198+ @update:value =" (val) => (settingStore[font.keySetting] = fontDisplayToFamily(val))"
199199 :options =" getOptions(font.keySetting)"
200200 class =" set"
201201 filterable
@@ -273,6 +273,51 @@ const getAllSystemFonts = async () => {
273273 }
274274};
275275
276+ /**
277+ * 字符串是否拥有在开头和结尾的成对引号
278+ * @note 不移除首尾空格,因为在下面的所有调用场景,都会先移除首尾空格
279+ * @param s 字符串
280+ * @returns 是否拥有成对引号
281+ */
282+ const hasPairedQuotes = (s : string ): boolean => {
283+ const l = s .length ;
284+ if (l < 2 ) return false ;
285+ if (s .startsWith (' "' )) {
286+ if (s .indexOf (' "' , 1 ) === l - 1 ) return true ;
287+ } else if (s .startsWith (" '" )) {
288+ if (s .indexOf (" '" , 1 ) === l - 1 ) return true ;
289+ }
290+ return false ;
291+ };
292+
293+ /**
294+ * 将 Font Family 字符串转换为用户可见字符串
295+ * @param fontFamily Font Family 字符串
296+ * @return 用户可见字符串
297+ */
298+ const fontFamilyToDisplay = (fontFamily : string ): string => {
299+ // 移除首尾空格
300+ fontFamily = fontFamily .trim ();
301+ // 移除引号
302+ if (hasPairedQuotes (fontFamily )) {
303+ fontFamily = fontFamily .substring (1 , fontFamily .length - 1 );
304+ }
305+ return fontFamily .trim ();
306+ };
307+
308+ /**
309+ * 用户可见字符串转换为 Font Family 字符串
310+ * @param display 用户可见字符串(单一字体)
311+ * @return Font Family 字符串
312+ */
313+ const fontDisplayToFamily = (display : string ): string => {
314+ display = display .trim ();
315+ if ((display .includes (" ," ) || display .includes (" " )) && ! hasPairedQuotes (display )) {
316+ return ` "${display }" ` ;
317+ }
318+ return display ;
319+ };
320+
276321/**
277322 * 字体字符串转数组
278323 * @param fontFamily 字体字符串
@@ -284,17 +329,7 @@ const fontFamilyToArray = (fontFamily: string): string[] => {
284329 const matches = fontFamily .match (regex );
285330 if (! matches ) return [];
286331
287- return matches
288- .map ((s ) => {
289- // 移除首尾空格
290- s = s .trim ();
291- // 移除引号
292- if (s .match (/ ^ "| ^ '/ )) {
293- s = s .substring (1 , s .length - 1 );
294- }
295- return s .trim ();
296- })
297- .filter (Boolean );
332+ return matches .map (fontFamilyToDisplay ).filter (Boolean );
298333};
299334
300335/**
@@ -303,15 +338,7 @@ const fontFamilyToArray = (fontFamily: string): string[] => {
303338 * @returns 字体字符串
304339 */
305340const fontArrayToFamily = (fontArray : string []): string => {
306- return fontArray
307- .map ((font ) => {
308- font = font .trim ();
309- if ((font .includes (" ," ) || font .includes (" " )) && ! / ^ ["'] . * ["'] $ / .test (font )) {
310- return ` "${font }" ` ;
311- }
312- return font ;
313- })
314- .join (" , " );
341+ return fontArray .map (fontDisplayToFamily ).join (" , " );
315342};
316343
317344// 获取桌面歌词配置
0 commit comments