mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 18:16:15 +08:00
feat(core): add blocksuite writer info service (#10754)
This commit is contained in:
@@ -33,7 +33,6 @@ import { type Framework } from '@toeverything/infra';
|
||||
|
||||
import { DocScope } from '../doc/scopes/doc';
|
||||
import { DocService } from '../doc/services/doc';
|
||||
import { EditorScope } from '../editor';
|
||||
import { GlobalCache, GlobalState } from '../storage/providers/global';
|
||||
import { GlobalStateService } from '../storage/services/global';
|
||||
import { UrlService } from '../url';
|
||||
@@ -55,10 +54,10 @@ import { ValidatorProvider } from './provider/validator';
|
||||
import { ServerScope } from './scopes/server';
|
||||
import { AcceptInviteService } from './services/accept-invite';
|
||||
import { AuthService } from './services/auth';
|
||||
import { BlocksuiteWriterInfoService } from './services/blocksuite-writer-info';
|
||||
import { CaptchaService } from './services/captcha';
|
||||
import { CloudDocMetaService } from './services/cloud-doc-meta';
|
||||
import { DefaultServerService } from './services/default-server';
|
||||
import { EditorUserCursorLabelService } from './services/editor-user-cursor-label';
|
||||
import { EventSourceService } from './services/eventsource';
|
||||
import { FetchService } from './services/fetch';
|
||||
import { GraphQLService } from './services/graphql';
|
||||
@@ -167,11 +166,6 @@ export function configureCloudModule(framework: Framework) {
|
||||
.service(WorkspaceInvoicesService)
|
||||
.entity(WorkspaceInvoices, [WorkspaceService, WorkspaceServerService])
|
||||
.service(SelfhostLicenseService, [SelfhostLicenseStore, WorkspaceService])
|
||||
.store(SelfhostLicenseStore, [WorkspaceServerService]);
|
||||
|
||||
framework
|
||||
.scope(WorkspaceScope)
|
||||
.scope(DocScope)
|
||||
.scope(EditorScope)
|
||||
.service(EditorUserCursorLabelService, [WorkspaceServerService]);
|
||||
.store(SelfhostLicenseStore, [WorkspaceServerService])
|
||||
.service(BlocksuiteWriterInfoService, [WorkspaceServerService]);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { WriterInfoServiceExtension } from '@blocksuite/affine/shared/services';
|
||||
import { OnEvent, Service } from '@toeverything/infra';
|
||||
|
||||
import { type Workspace, WorkspaceInitialized } from '../../workspace';
|
||||
import type { DocImpl } from '../../workspace/impls/doc';
|
||||
import type { WorkspaceServerService } from './workspace-server';
|
||||
|
||||
/**
|
||||
* This service is used to set the writer info for the blocksuite editor.
|
||||
*/
|
||||
@OnEvent(WorkspaceInitialized, i => i.onWorkspaceInitialized)
|
||||
export class BlocksuiteWriterInfoService extends Service {
|
||||
constructor(private readonly workspaceServerService: WorkspaceServerService) {
|
||||
super();
|
||||
}
|
||||
|
||||
onWorkspaceInitialized(workspace: Workspace) {
|
||||
const setWriterInfo = (doc: DocImpl) => {
|
||||
const account = this.workspaceServerService.server?.account$.value;
|
||||
doc.awarenessStore.awareness.setLocalStateField('user', {
|
||||
name: account?.label,
|
||||
});
|
||||
doc.storeExtensions.push(
|
||||
WriterInfoServiceExtension({
|
||||
getWriterInfo: () => {
|
||||
if (!account) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
id: account.id,
|
||||
name: account.label,
|
||||
avatar: account.avatar,
|
||||
};
|
||||
},
|
||||
})
|
||||
);
|
||||
};
|
||||
const subscription = workspace.docCollection.meta.docMetaAdded.subscribe(
|
||||
docId => {
|
||||
const doc = workspace.docCollection.docs.get(docId) as DocImpl;
|
||||
setWriterInfo(doc);
|
||||
}
|
||||
);
|
||||
this.disposables.push(() => subscription.unsubscribe.bind(subscription));
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
import { OnEvent, Service } from '@toeverything/infra';
|
||||
|
||||
import type { Editor } from '../../editor';
|
||||
import { EditorInitialized } from '../../editor/events';
|
||||
import type { WorkspaceServerService } from './workspace-server';
|
||||
|
||||
@OnEvent(EditorInitialized, i => i.onEditorInitialized)
|
||||
export class EditorUserCursorLabelService extends Service {
|
||||
constructor(private readonly workspaceServerService: WorkspaceServerService) {
|
||||
super();
|
||||
}
|
||||
|
||||
onEditorInitialized(editor: Editor) {
|
||||
if (this.workspaceServerService.server) {
|
||||
const subscription =
|
||||
this.workspaceServerService.server.account$.subscribe(account => {
|
||||
editor.doc.blockSuiteDoc.awarenessStore.awareness.setLocalStateField(
|
||||
'user',
|
||||
{
|
||||
name: account?.label,
|
||||
}
|
||||
);
|
||||
});
|
||||
this.disposables.push(() => subscription.unsubscribe());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { SpecProvider } from '@blocksuite/affine/shared/utils';
|
||||
import {
|
||||
AwarenessStore,
|
||||
type Doc,
|
||||
type ExtensionType,
|
||||
type GetBlocksOptions,
|
||||
type Query,
|
||||
Store,
|
||||
@@ -92,6 +93,8 @@ export class DocImpl implements Doc {
|
||||
*/
|
||||
protected readonly _ySpaceDoc: Y.Doc;
|
||||
|
||||
readonly storeExtensions: ExtensionType[] = [];
|
||||
|
||||
readonly awarenessStore: AwarenessStore;
|
||||
|
||||
readonly id: string;
|
||||
@@ -284,7 +287,9 @@ export class DocImpl implements Doc {
|
||||
|
||||
const storeExtensions = SpecProvider._.getSpec('store');
|
||||
const extensionSet = new Set(
|
||||
storeExtensions.value.concat(extensions ?? [])
|
||||
storeExtensions.value
|
||||
.concat(extensions ?? [])
|
||||
.concat(this.storeExtensions)
|
||||
);
|
||||
|
||||
const doc = new Store({
|
||||
|
||||
@@ -110,7 +110,7 @@ export class WorkspaceRepositoryService extends Service {
|
||||
|
||||
workspace.engine.start();
|
||||
|
||||
this.framework.emitEvent(WorkspaceInitialized, workspace);
|
||||
workspaceScope.emitEvent(WorkspaceInitialized, workspace);
|
||||
|
||||
flavourProvider?.onWorkspaceInitialized?.(workspace);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user