fix: font style setting only control editor's font (#3117)

Co-authored-by: Alex Yang <himself65@outlook.com>
This commit is contained in:
Qi
2023-07-10 19:58:53 +08:00
committed by GitHub
parent 127c63601e
commit cfa18d1bc3
4 changed files with 43 additions and 35 deletions
+26 -23
View File
@@ -1,6 +1,5 @@
import { useAtom } from 'jotai'; import { atom, useAtom } from 'jotai';
import { atomWithStorage } from 'jotai/utils'; import { atomWithStorage } from 'jotai/utils';
import { useCallback } from 'react';
export type DateFormats = export type DateFormats =
| 'MM/dd/YYYY' | 'MM/dd/YYYY'
@@ -15,7 +14,7 @@ export type AppSetting = {
clientBorder: boolean; clientBorder: boolean;
fullWidthLayout: boolean; fullWidthLayout: boolean;
windowFrameStyle: 'frameless' | 'NativeTitleBar'; windowFrameStyle: 'frameless' | 'NativeTitleBar';
fontStyle: 'Sans' | 'Serif' | 'Mono'; fontStyle: FontFamily;
dateFormat: DateFormats; dateFormat: DateFormats;
startWeekOnMonday: boolean; startWeekOnMonday: boolean;
enableBlurBackground: boolean; enableBlurBackground: boolean;
@@ -38,16 +37,18 @@ export const dateFormatOptions: DateFormats[] = [
'dd MMMM YYYY', 'dd MMMM YYYY',
]; ];
export const fontStyleOptions: { export type FontFamily = 'Sans' | 'Serif' | 'Mono';
key: AppSetting['fontStyle'];
value: string; export const fontStyleOptions = [
}[] = [
{ key: 'Sans', value: 'var(--affine-font-sans-family)' }, { key: 'Sans', value: 'var(--affine-font-sans-family)' },
{ key: 'Serif', value: 'var(--affine-font-serif-family)' }, { key: 'Serif', value: 'var(--affine-font-serif-family)' },
{ key: 'Mono', value: 'var(--affine-font-mono-family)' }, { key: 'Mono', value: 'var(--affine-font-mono-family)' },
]; ] satisfies {
key: FontFamily;
value: string;
}[];
export const AppSettingAtom = atomWithStorage<AppSetting>('AFFiNE settings', { const appSettingBaseAtom = atomWithStorage<AppSetting>('affine-settings', {
clientBorder: false, clientBorder: false,
fullWidthLayout: false, fullWidthLayout: false,
windowFrameStyle: 'frameless', windowFrameStyle: 'frameless',
@@ -60,19 +61,21 @@ export const AppSettingAtom = atomWithStorage<AppSetting>('AFFiNE settings', {
autoDownloadUpdate: true, autoDownloadUpdate: true,
}); });
export const useAppSetting = () => { type SetStateAction<Value> = Value | ((prev: Value) => Value);
const [settings, setSettings] = useAtom(AppSettingAtom);
return [ const appSettingAtom = atom<
settings, AppSetting,
useCallback( [SetStateAction<Partial<AppSetting>>],
(patch: Partial<AppSetting>) => { void
setSettings((prev: AppSetting) => ({ >(
...prev, get => get(appSettingBaseAtom),
...patch, (get, set, apply) => {
})); const prev = get(appSettingBaseAtom);
}, const next = typeof apply === 'function' ? apply(prev) : apply;
[setSettings] set(appSettingBaseAtom, { ...prev, ...next });
), }
] as const; );
export const useAppSetting = () => {
return useAtom(appSettingAtom);
}; };
@@ -58,14 +58,7 @@ const FontFamilySettings = () => {
defaultValue={appSettings.fontStyle} defaultValue={appSettings.fontStyle}
onValueChange={useCallback( onValueChange={useCallback(
(key: AppSetting['fontStyle']) => { (key: AppSetting['fontStyle']) => {
const value = fontStyleOptions.find(option => option.key === key)
?.value;
setAppSettings({ fontStyle: key }); setAppSettings({ fontStyle: key });
document
.querySelector('html')
?.style.setProperty('--affine-font-family', value || null);
}, },
[setAppSettings] [setAppSettings]
)} )}
+16 -4
View File
@@ -21,13 +21,13 @@ import type { PluginBlockSuiteAdapter } from '@toeverything/plugin-infra/type';
import clsx from 'clsx'; import clsx from 'clsx';
import { useAtomValue, useSetAtom } from 'jotai'; import { useAtomValue, useSetAtom } from 'jotai';
import Head from 'next/head'; import Head from 'next/head';
import type { FC, ReactElement } from 'react'; import type { CSSProperties, FC, ReactElement } from 'react';
import React, { memo, Suspense, useCallback, useMemo } from 'react'; import { memo, Suspense, useCallback, useMemo } from 'react';
import { Panel, PanelGroup, PanelResizeHandle } from 'react-resizable-panels'; import { Panel, PanelGroup, PanelResizeHandle } from 'react-resizable-panels';
import { pageSettingFamily } from '../atoms'; import { pageSettingFamily } from '../atoms';
import { contentLayoutAtom } from '../atoms/layout'; import { contentLayoutAtom } from '../atoms/layout';
import { useAppSetting } from '../atoms/settings'; import { fontStyleOptions, useAppSetting } from '../atoms/settings';
import { BlockSuiteEditor as Editor } from './blocksuite/block-suite-editor'; import { BlockSuiteEditor as Editor } from './blocksuite/block-suite-editor';
import { editor } from './page-detail-editor.css'; import { editor } from './page-detail-editor.css';
import { pluginContainer } from './page-detail-editor.css'; import { pluginContainer } from './page-detail-editor.css';
@@ -69,12 +69,24 @@ const EditorWrapper = memo(function EditorWrapper({
const [appSettings] = useAppSetting(); const [appSettings] = useAppSetting();
assertExists(meta); assertExists(meta);
const value = useMemo(() => {
const fontStyle = fontStyleOptions.find(
option => option.key === appSettings.fontStyle
);
assertExists(fontStyle);
return fontStyle.value;
}, [appSettings.fontStyle]);
return ( return (
<Editor <Editor
className={clsx(editor, { className={clsx(editor, {
'full-screen': appSettings?.fullWidthLayout, 'full-screen': appSettings.fullWidthLayout,
})} })}
style={
{
'--affine-font-family': value,
} as CSSProperties
}
key={`${workspace.id}-${pageId}`} key={`${workspace.id}-${pageId}`}
mode={isPublic ? 'page' : currentMode} mode={isPublic ? 'page' : currentMode}
page={page} page={page}
@@ -55,7 +55,7 @@ export const WorkspaceFallback = (): ReactElement => {
return ( return (
<AppContainer> <AppContainer>
<AppSidebarFallback /> <AppSidebarFallback />
<MainContainer></MainContainer> <MainContainer />
</AppContainer> </AppContainer>
); );
}; };