refactor(core): add CustomEditorWrapper

This commit is contained in:
Jimmfly 2024-12-24 12:59:37 +08:00
parent 4173272228
commit ae889e26f1
No known key found for this signature in database
7 changed files with 88 additions and 84 deletions

View File

@ -34,6 +34,7 @@
"@radix-ui/react-dialog": "^1.1.3", "@radix-ui/react-dialog": "^1.1.3",
"@radix-ui/react-popover": "^1.1.3", "@radix-ui/react-popover": "^1.1.3",
"@radix-ui/react-scroll-area": "^1.2.2", "@radix-ui/react-scroll-area": "^1.2.2",
"@radix-ui/react-slot": "^1.1.1",
"@radix-ui/react-toolbar": "^1.1.1", "@radix-ui/react-toolbar": "^1.1.1",
"@sentry/react": "^8.44.0", "@sentry/react": "^8.44.0",
"@toeverything/pdf-viewer": "^0.1.1", "@toeverything/pdf-viewer": "^0.1.1",

View File

@ -33,7 +33,10 @@ import {
import { encodeStateAsUpdate } from 'yjs'; import { encodeStateAsUpdate } from 'yjs';
import { pageHistoryModalAtom } from '../../atoms/page-history'; import { pageHistoryModalAtom } from '../../atoms/page-history';
import { BlockSuiteEditor } from '../../blocksuite/block-suite-editor'; import {
BlockSuiteEditor,
CustomEditorWrapper,
} from '../../blocksuite/block-suite-editor';
import { PureEditorModeSwitch } from '../../blocksuite/block-suite-mode-switch'; import { PureEditorModeSwitch } from '../../blocksuite/block-suite-mode-switch';
import { AffineErrorBoundary } from '../affine-error-boundary'; import { AffineErrorBoundary } from '../affine-error-boundary';
import { import {
@ -131,11 +134,13 @@ const HistoryEditorPreview = ({
<AffineErrorBoundary> <AffineErrorBoundary>
<Scrollable.Root> <Scrollable.Root>
<Scrollable.Viewport className="affine-page-viewport"> <Scrollable.Viewport className="affine-page-viewport">
<BlockSuiteEditor <CustomEditorWrapper>
className={styles.editor} <BlockSuiteEditor
mode={mode} className={styles.editor}
page={snapshotPage} mode={mode}
/> page={snapshotPage}
/>
</CustomEditorWrapper>
</Scrollable.Viewport> </Scrollable.Viewport>
<Scrollable.Scrollbar /> <Scrollable.Scrollbar />
</Scrollable.Root> </Scrollable.Root>

View File

@ -0,0 +1,43 @@
import {
EditorSettingService,
fontStyleOptions,
} from '@affine/core/modules/editor-setting';
import { Slot } from '@radix-ui/react-slot';
import { useLiveData, useService } from '@toeverything/infra';
import { cssVar } from '@toeverything/theme';
import { type CSSProperties, type ReactNode, useMemo } from 'react';
export const CustomEditorWrapper = ({ children }: { children: ReactNode }) => {
const editorSetting = useService(EditorSettingService).editorSetting;
const settings = useLiveData(
editorSetting.settings$.selector(s => ({
fontFamily: s.fontFamily,
customFontFamily: s.customFontFamily,
fullWidthLayout: s.fullWidthLayout,
}))
);
const value = useMemo(() => {
const fontStyle = fontStyleOptions.find(
option => option.key === settings.fontFamily
);
if (!fontStyle) {
return cssVar('fontSansFamily');
}
const customFontFamily = settings.customFontFamily;
return customFontFamily && fontStyle.key === 'Custom'
? `${customFontFamily}, ${fontStyle.value}`
: fontStyle.value;
}, [settings.customFontFamily, settings.fontFamily]);
return (
<Slot
style={
{
'--affine-font-family': value,
} as CSSProperties
}
>
{children}
</Slot>
);
};

View File

@ -10,3 +10,4 @@ edgelessEffects();
registerBlocksuitePresetsCustomComponents(); registerBlocksuitePresetsCustomComponents();
export * from './blocksuite-editor'; export * from './blocksuite-editor';
export * from './custom-editor-wrapper';

View File

@ -2,18 +2,15 @@ import './page-detail-editor.css';
import type { AffineEditorContainer } from '@blocksuite/affine/presets'; import type { AffineEditorContainer } from '@blocksuite/affine/presets';
import { useLiveData, useService } from '@toeverything/infra'; import { useLiveData, useService } from '@toeverything/infra';
import { cssVar } from '@toeverything/theme';
import clsx from 'clsx'; import clsx from 'clsx';
import type { CSSProperties } from 'react';
import { useMemo } from 'react';
import { DocService } from '../modules/doc'; import { DocService } from '../modules/doc';
import { EditorService } from '../modules/editor'; import { EditorService } from '../modules/editor';
import { EditorSettingService } from '../modules/editor-setting';
import { import {
EditorSettingService, BlockSuiteEditor as Editor,
fontStyleOptions, CustomEditorWrapper,
} from '../modules/editor-setting'; } from './blocksuite/block-suite-editor';
import { BlockSuiteEditor as Editor } from './blocksuite/block-suite-editor';
import * as styles from './page-detail-editor.css'; import * as styles from './page-detail-editor.css';
declare global { declare global {
@ -50,36 +47,19 @@ export const PageDetailEditor = ({ onLoad }: PageDetailEditorProps) => {
? pageWidth === 'fullWidth' ? pageWidth === 'fullWidth'
: settings.fullWidthLayout; : settings.fullWidthLayout;
const value = useMemo(() => {
const fontStyle = fontStyleOptions.find(
option => option.key === settings.fontFamily
);
if (!fontStyle) {
return cssVar('fontSansFamily');
}
const customFontFamily = settings.customFontFamily;
return customFontFamily && fontStyle.key === 'Custom'
? `${customFontFamily}, ${fontStyle.value}`
: fontStyle.value;
}, [settings.customFontFamily, settings.fontFamily]);
return ( return (
<Editor <CustomEditorWrapper>
className={clsx(styles.editor, { <Editor
'full-screen': !isSharedMode && fullWidthLayout, className={clsx(styles.editor, {
'is-public': isSharedMode, 'full-screen': !isSharedMode && fullWidthLayout,
})} 'is-public': isSharedMode,
style={ })}
{ mode={mode}
'--affine-font-family': value, defaultOpenProperty={defaultOpenProperty}
} as CSSProperties page={editor.doc.blockSuiteDoc}
} shared={isSharedMode}
mode={mode} onEditorReady={onLoad}
defaultOpenProperty={defaultOpenProperty} />
page={editor.doc.blockSuiteDoc} </CustomEditorWrapper>
shared={isSharedMode}
onEditorReady={onLoad}
/>
); );
}; };

View File

@ -2,14 +2,13 @@ import { Scrollable } from '@affine/component';
import { PageDetailSkeleton } from '@affine/component/page-detail-skeleton'; import { PageDetailSkeleton } from '@affine/component/page-detail-skeleton';
import { AIProvider } from '@affine/core/blocksuite/presets/ai'; import { AIProvider } from '@affine/core/blocksuite/presets/ai';
import { AffineErrorBoundary } from '@affine/core/components/affine/affine-error-boundary'; import { AffineErrorBoundary } from '@affine/core/components/affine/affine-error-boundary';
import { BlockSuiteEditor } from '@affine/core/components/blocksuite/block-suite-editor'; import {
BlockSuiteEditor,
CustomEditorWrapper,
} from '@affine/core/components/blocksuite/block-suite-editor';
import { EditorOutlineViewer } from '@affine/core/components/blocksuite/outline-viewer'; import { EditorOutlineViewer } from '@affine/core/components/blocksuite/outline-viewer';
import { PageNotFound } from '@affine/core/desktop/pages/404'; import { PageNotFound } from '@affine/core/desktop/pages/404';
import { EditorService } from '@affine/core/modules/editor'; import { EditorService } from '@affine/core/modules/editor';
import {
EditorSettingService,
fontStyleOptions,
} from '@affine/core/modules/editor-setting';
import { DebugLogger } from '@affine/debug'; import { DebugLogger } from '@affine/debug';
import { import {
type EdgelessRootService, type EdgelessRootService,
@ -23,9 +22,8 @@ import {
useService, useService,
useServices, useServices,
} from '@toeverything/infra'; } from '@toeverything/infra';
import { cssVar } from '@toeverything/theme';
import clsx from 'clsx'; import clsx from 'clsx';
import { type CSSProperties, useCallback, useEffect, useMemo } from 'react'; import { useCallback, useEffect } from 'react';
import { WorkbenchService } from '../../../workbench'; import { WorkbenchService } from '../../../workbench';
import type { DocReferenceInfo } from '../../entities/peek-view'; import type { DocReferenceInfo } from '../../entities/peek-view';
@ -90,14 +88,6 @@ function DocPeekPreviewEditor({
const workbench = useService(WorkbenchService).workbench; const workbench = useService(WorkbenchService).workbench;
const peekView = useService(PeekViewService).peekView; const peekView = useService(PeekViewService).peekView;
const editorElement = useLiveData(editor.editorContainer$); const editorElement = useLiveData(editor.editorContainer$);
const editorSetting = useService(EditorSettingService).editorSetting;
const settings = useLiveData(
editorSetting.settings$.selector(s => ({
fontFamily: s.fontFamily,
customFontFamily: s.customFontFamily,
fullWidthLayout: s.fullWidthLayout,
}))
);
const handleOnEditorReady = useCallback( const handleOnEditorReady = useCallback(
(editorContainer: AffineEditorContainer) => { (editorContainer: AffineEditorContainer) => {
@ -156,38 +146,21 @@ function DocPeekPreviewEditor({
peekView.close(); peekView.close();
}, [doc, peekView, workbench]); }, [doc, peekView, workbench]);
const value = useMemo(() => {
const fontStyle = fontStyleOptions.find(
option => option.key === settings.fontFamily
);
if (!fontStyle) {
return cssVar('fontSansFamily');
}
const customFontFamily = settings.customFontFamily;
return customFontFamily && fontStyle.key === 'Custom'
? `${customFontFamily}, ${fontStyle.value}`
: fontStyle.value;
}, [settings.customFontFamily, settings.fontFamily]);
return ( return (
<AffineErrorBoundary> <AffineErrorBoundary>
<Scrollable.Root> <Scrollable.Root>
<Scrollable.Viewport <Scrollable.Viewport
className={clsx('affine-page-viewport', styles.affineDocViewport)} className={clsx('affine-page-viewport', styles.affineDocViewport)}
> >
<BlockSuiteEditor <CustomEditorWrapper>
className={styles.editor} <BlockSuiteEditor
mode={mode} className={styles.editor}
page={doc.blockSuiteDoc} mode={mode}
onEditorReady={handleOnEditorReady} page={doc.blockSuiteDoc}
defaultOpenProperty={defaultOpenProperty} onEditorReady={handleOnEditorReady}
style={ defaultOpenProperty={defaultOpenProperty}
{ />
'--affine-font-family': value, </CustomEditorWrapper>
} as CSSProperties
}
/>
</Scrollable.Viewport> </Scrollable.Viewport>
<Scrollable.Scrollbar /> <Scrollable.Scrollbar />
</Scrollable.Root> </Scrollable.Root>

View File

@ -370,6 +370,7 @@ __metadata:
"@radix-ui/react-dialog": "npm:^1.1.3" "@radix-ui/react-dialog": "npm:^1.1.3"
"@radix-ui/react-popover": "npm:^1.1.3" "@radix-ui/react-popover": "npm:^1.1.3"
"@radix-ui/react-scroll-area": "npm:^1.2.2" "@radix-ui/react-scroll-area": "npm:^1.2.2"
"@radix-ui/react-slot": "npm:^1.1.1"
"@radix-ui/react-toolbar": "npm:^1.1.1" "@radix-ui/react-toolbar": "npm:^1.1.1"
"@sentry/react": "npm:^8.44.0" "@sentry/react": "npm:^8.44.0"
"@swc/core": "npm:^1.10.1" "@swc/core": "npm:^1.10.1"