refactor: add docsService for blocksuite to change and observe doc mode (#7200)

This PR provide `DocModeService` to manage document mode, which is used by blocksuite.

### What Changed?
- Add document mode apis from `DocService` and `DocsService`
- Remove useless `pageService.getEditorMode` and `pageService.slots.editorModeSwitch` api

[Related PR in blocksuite](https://github.com/toeverything/blocksuite/pull/7288)

[Bump Blocksuite](https://github.com/toeverything/AFFiNE/pull/7209)
> ## Features
> * [feat(presets): support different tracker control for actions blocksuite#7285](https://github.com/toeverything/blocksuite/pull/7285) @donteatfriedrice
> * [feat(edgeless): add default color and editing border for edgeless text blocksuite#7278](https://github.com/toeverything/blocksuite/pull/7278) @Flrande
> * [feat(blocks): center peek entries blocksuite#7275](https://github.com/toeverything/blocksuite/pull/7275) @pengx17
>
> ## Bugfix
> * [fix(blocks): affine-embed-synced-doc-block peek on dblclick blocksuite#7284](https://github.com/toeverything/blocksuite/pull/7284) @pengx17
> * [fix(edgeless): edgeless text rect should automatically update when changing font blocksuite#7292](https://github.com/toeverything/blocksuite/pull/7292) @Flrande
> * [fix(edgeless): align panel not work blocksuite#7293](https://github.com/toeverything/blocksuite/pull/7293) @Flrande
> * [fix(blocks): filterable list unexpected hover style blocksuite#7291](https://github.com/toeverything/blocksuite/pull/7291) @golok727
> * [fix(blocks): slash menu unexpected hover style blocksuite#7290](https://github.com/toeverything/blocksuite/pull/7290) @golok727
> * [fix(blocks): bookmark block error on undo or delete while loading blocksuite#7276](https://github.com/toeverything/blocksuite/pull/7276) @golok727
> * [fix(presets): chat input max-height blocksuite#7283](https://github.com/toeverything/blocksuite/pull/7283) @fundon
> * [fix(presets): get image blob from selection blocksuite#7286](https://github.com/toeverything/blocksuite/pull/7286) @donteatfriedrice
> * [fix(blocks): connectors resize and rotate blocksuite#7256](https://github.com/toeverything/blocksuite/pull/7256) @fundon
>
> ## Refactor
> * [refactor: add DocModeService for doc mode get, set, toogle and observe change blocksuite#7288](https://github.com/toeverything/blocksuite/pull/7288) @akumatus
> * [refactor(edgeless): rewrite auto-connect component as a widget blocksuite#7287](https://github.com/toeverything/blocksuite/pull/7287) @doouding
>
> ## Misc
> * [test: fix flaky blocksuite#7295](https://github.com/toeverything/blocksuite/pull/7295) @L-Sun
> * [style(page): adjust heading font-weight and letter-spacing blocksuite#7277](https://github.com/toeverything/blocksuite/pull/7277) @CatsJuice
> * [chore: apply member ordering rule blocksuite#7279](https://github.com/toeverything/blocksuite/pull/7279) @Saul-Mirone

---
This commit is contained in:
akumatus
2024-06-13 08:45:31 +00:00
committed by Yue Wu
parent a3ca41fd6a
commit 24cb0d3757
16 changed files with 220 additions and 117 deletions

View File

@@ -75,12 +75,12 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@blocksuite/block-std": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/blocks": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/global": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/block-std": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/blocks": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/global": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/icons": "2.1.55",
"@blocksuite/presets": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/store": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/presets": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/store": "0.15.0-canary-202406121423-a4241b5",
"@storybook/addon-actions": "^7.6.17",
"@storybook/addon-essentials": "^7.6.17",
"@storybook/addon-interactions": "^7.6.17",

View File

@@ -18,13 +18,13 @@
"@affine/graphql": "workspace:*",
"@affine/i18n": "workspace:*",
"@affine/templates": "workspace:*",
"@blocksuite/block-std": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/blocks": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/global": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/block-std": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/blocks": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/global": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/icons": "2.1.55",
"@blocksuite/inline": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/presets": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/store": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/inline": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/presets": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/store": "0.15.0-canary-202406121423-a4241b5",
"@dnd-kit/core": "^6.1.0",
"@dnd-kit/modifiers": "^7.0.0",
"@dnd-kit/sortable": "^8.0.0",

View File

@@ -15,7 +15,13 @@ import {
PageEditor,
} from '@blocksuite/presets';
import type { Doc } from '@blocksuite/store';
import { type DocMode, useLiveData, useService } from '@toeverything/infra';
import {
type DocMode,
DocService,
DocsService,
useLiveData,
useService,
} from '@toeverything/infra';
import React, {
forwardRef,
Fragment,
@@ -30,6 +36,7 @@ import { PagePropertiesTable } from '../../affine/page-properties';
import { AffinePageReference } from '../../affine/reference-link';
import { BlocksuiteEditorJournalDocTitle } from './journal-doc-title';
import {
patchDocModeService,
patchForSharedPage,
patchNotificationService,
patchPeekViewService,
@@ -73,6 +80,8 @@ const usePatchSpecs = (page: Doc, shared: boolean, mode: DocMode) => {
const [reactToLit, portals] = useLitPortalFactory();
const peekViewService = useService(PeekViewService);
const quickSearchService = useService(QuickSearchService);
const docService = useService(DocService);
const docsService = useService(DocsService);
const referenceRenderer: ReferenceReactRenderer = useMemo(() => {
return function customReference(reference) {
const pageId = reference.delta.attributes?.reference?.pageId;
@@ -101,9 +110,14 @@ const usePatchSpecs = (page: Doc, shared: boolean, mode: DocMode) => {
if (shared) {
patched = patchForSharedPage(patched);
}
if (!page.readonly) {
patched = patchDocModeService(patched, docService, docsService);
}
return patched;
}, [
confirmModal,
docService,
docsService,
page.readonly,
peekViewService,
quickSearchService,

View File

@@ -25,6 +25,7 @@ import {
ReferenceNodeConfig,
type RootService,
} from '@blocksuite/blocks';
import type { DocMode, DocService, DocsService } from '@toeverything/infra';
import { html, LitElement, type TemplateResult } from 'lit';
import { customElement } from 'lit/decorators.js';
import { literal } from 'lit/static-html.js';
@@ -281,6 +282,57 @@ export function patchPeekViewService(
return specs;
}
export function patchDocModeService(
specs: BlockSpec[],
docService: DocService,
docsService: DocsService
) {
const rootSpec = specs.find(
spec => spec.schema.model.flavour === 'affine:page'
) as BlockSpec<string, RootService>;
if (!rootSpec) {
return specs;
}
patchSpecService(rootSpec, pageService => {
const DEFAULT_MODE = 'page';
pageService.docModeService = {
setMode: (mode: DocMode, id?: string) => {
if (id) {
docsService.list.setMode(id, mode);
} else {
docService.doc.setMode(mode);
}
},
getMode: (id?: string) => {
const mode = id
? docsService.list.getMode(id)
: docService.doc.getMode();
return mode || DEFAULT_MODE;
},
toggleMode: (id?: string) => {
const mode = id
? docsService.list.toggleMode(id)
: docService.doc.toggleMode();
return mode || DEFAULT_MODE;
},
onModeChange: (handler: (mode: DocMode) => void, id?: string) => {
// eslint-disable-next-line rxjs/finnish
const mode$ = id
? docsService.list.observeMode(id)
: docService.doc.observeMode();
const sub = mode$.subscribe(m => handler(m || DEFAULT_MODE));
return {
dispose: sub.unsubscribe,
};
},
};
});
return specs;
}
export function patchQuickSearchService(
specs: BlockSpec[],
service: QuickSearchService

View File

@@ -21,12 +21,17 @@ const EditorChatPanel = ({ editor }: SidebarTabProps) => {
if (!editor) return;
const pageService = editor.host.spec.getService('affine:page');
pageService.slots.docLinkClicked.on(() => {
(chatPanelRef.current as ChatPanel).doc = editor.doc;
});
pageService.slots.editorModeSwitch.on(() => {
(chatPanelRef.current as ChatPanel).host = editor.host;
});
const disposable = [
pageService.slots.docLinkClicked.on(() => {
(chatPanelRef.current as ChatPanel).doc = editor.doc;
}),
pageService.docModeService.onModeChange(() => {
if (!editor.host) return;
(chatPanelRef.current as ChatPanel).host = editor.host;
}),
];
return () => disposable.forEach(d => d.dispose());
}, [editor]);
if (!editor) {

View File

@@ -186,8 +186,6 @@ const DetailPageImpl = memo(function DetailPageImpl() {
editorHost.std.spec.getService<PageRootService>('affine:page');
const disposable = new DisposableGroup();
pageService.getEditorMode = (pageId: string) =>
docRecordList.doc$(pageId).value?.mode$.value ?? 'page';
pageService.getDocUpdatedAt = (pageId: string) => {
const linkedPage = docRecordList.doc$(pageId).value;
if (!linkedPage) return new Date();
@@ -210,11 +208,6 @@ const DetailPageImpl = memo(function DetailPageImpl() {
jumpToTag(workspace.id, tagId);
})
);
disposable.add(
pageService.slots.editorModeSwitch.on(mode => {
doc.setMode(mode);
})
);
setEditor(editor);

View File

@@ -29,10 +29,10 @@
"@affine/env": "workspace:*",
"@affine/i18n": "workspace:*",
"@affine/native": "workspace:*",
"@blocksuite/block-std": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/blocks": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/presets": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/store": "0.15.0-canary-202406111431-81a6bee",
"@blocksuite/block-std": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/blocks": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/presets": "0.15.0-canary-202406121423-a4241b5",
"@blocksuite/store": "0.15.0-canary-202406121423-a4241b5",
"@electron-forge/cli": "^7.3.0",
"@electron-forge/core": "^7.3.0",
"@electron-forge/core-utils": "^7.3.0",