refactor(core): implement doc created/updated by service (#12150)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Documents now automatically track and display "created by" and "updated by" user information.
  - Document creation and update timestamps are now managed and shown more accurately.
  - Workspace and document metadata (name, avatar) updates are more responsive and reliable.
  - Document creation supports middleware for customizing properties and behavior.

- **Improvements**
  - Simplified and unified event handling for document list updates, reducing redundant event subscriptions.
  - Enhanced integration of editor and theme settings into the document creation process.
  - Explicit Yjs document initialization for improved workspace stability and reliability.
  - Consolidated journal-related metadata display in document icons and titles for clarity.

- **Bug Fixes**
  - Fixed inconsistencies in how workspace and document names are set and updated.
  - Improved accuracy of "last updated" indicators by handling timestamps automatically.

- **Refactor**
  - Removed deprecated event subjects and direct metadata manipulation in favor of more robust, reactive patterns.
  - Streamlined document creation logic across various features (quick search, journal, recording, etc.).
  - Simplified user avatar display components and removed cloud metadata dependencies.
  - Removed legacy editor setting and theme service dependencies from multiple modules.

- **Chores**
  - Updated internal APIs and interfaces to support new metadata and event handling mechanisms.
  - Cleaned up unused code and dependencies related to editor settings and theme services.
  - Skipped flaky end-to-end test to improve test suite stability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
EYHN
2025-05-08 07:53:33 +00:00
parent 93d74ff220
commit 2d1600fa00
56 changed files with 496 additions and 458 deletions

View File

@@ -4,7 +4,6 @@ import { WorkspaceServerService } from '../cloud';
import { WorkspaceDialogService } from '../dialogs';
import { DocScope, DocsService } from '../doc';
import { DocDisplayMetaService } from '../doc-display-meta';
import { EditorSettingService } from '../editor-setting';
import { JournalService } from '../journal';
import { GuardService, MemberSearchService } from '../permissions';
import { DocGrantedUsersService } from '../permissions/services/doc-granted-users';
@@ -20,7 +19,6 @@ export function configAtMenuConfigModule(framework: Framework) {
JournalService,
DocDisplayMetaService,
WorkspaceDialogService,
EditorSettingService,
DocsService,
SearchMenuService,
WorkspaceServerService,

View File

@@ -16,7 +16,6 @@ import {
type EditorHost,
} from '@blocksuite/affine/std';
import type { DocMeta } from '@blocksuite/affine/store';
import { Text } from '@blocksuite/affine/store';
import {
type LinkedMenuGroup,
type LinkedMenuItem,
@@ -43,7 +42,6 @@ import { AuthService, type WorkspaceServerService } from '../../cloud';
import type { WorkspaceDialogService } from '../../dialogs';
import type { DocsService } from '../../doc';
import type { DocDisplayMetaService } from '../../doc-display-meta';
import type { EditorSettingService } from '../../editor-setting';
import { type JournalService, suggestJournalDate } from '../../journal';
import { NotificationService } from '../../notification';
import type { GuardService, MemberSearchService } from '../../permissions';
@@ -65,7 +63,6 @@ export class AtMenuConfigService extends Service {
private readonly journalService: JournalService,
private readonly docDisplayMetaService: DocDisplayMetaService,
private readonly dialogService: WorkspaceDialogService,
private readonly editorSettingService: EditorSettingService,
private readonly docsService: DocsService,
private readonly searchMenuService: SearchMenuService,
private readonly workspaceServerService: WorkspaceServerService,
@@ -141,10 +138,7 @@ export class AtMenuConfigService extends Service {
const createPage = (mode: DocMode) => {
const page = this.docsService.createDoc({
docProps: {
note: this.editorSettingService.editorSetting.get('affine:note'),
page: { title: new Text(query) },
},
title: query,
primaryMode: mode,
});

View File

@@ -95,6 +95,8 @@ import { UserCopilotQuotaStore } from './stores/user-copilot-quota';
import { UserFeatureStore } from './stores/user-feature';
import { UserQuotaStore } from './stores/user-quota';
import { UserSettingsStore } from './stores/user-settings';
import { DocCreatedByService } from './services/doc-created-by';
import { DocUpdatedByService } from './services/doc-updated-by';
export function configureCloudModule(framework: Framework) {
configureDefaultAuthProvider(framework);
@@ -164,7 +166,9 @@ export function configureCloudModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(WorkspaceServerService)
.service(DocCreatedByService, [WorkspaceServerService])
.scope(DocScope)
.service(DocUpdatedByService, [WorkspaceServerService])
.service(CloudDocMetaService)
.entity(CloudDocMeta, [CloudDocMetaStore, DocService, GlobalCache])
.store(CloudDocMetaStore, [WorkspaceServerService]);

View File

@@ -0,0 +1,19 @@
import { OnEvent, Service } from '@toeverything/infra';
import { DocCreated, type DocRecord } from '../../doc';
import type { DocCreateOptions } from '../../doc/types';
import type { WorkspaceServerService } from './workspace-server';
@OnEvent(DocCreated, t => t.onDocCreated)
export class DocCreatedByService extends Service {
constructor(private readonly workspaceServerService: WorkspaceServerService) {
super();
}
onDocCreated(event: { doc: DocRecord; docCreateOptions: DocCreateOptions }) {
const account = this.workspaceServerService.server?.account$.value;
if (account) {
event.doc.setCreatedBy(account.id);
}
}
}

View File

@@ -0,0 +1,37 @@
import { OnEvent, Service } from '@toeverything/infra';
import { throttle } from 'lodash-es';
import type { Transaction } from 'yjs';
import type { Doc } from '../../doc';
import { DocInitialized } from '../../doc/events';
import type { WorkspaceServerService } from './workspace-server';
@OnEvent(DocInitialized, t => t.onDocInitialized)
export class DocUpdatedByService extends Service {
constructor(private readonly workspaceServerService: WorkspaceServerService) {
super();
}
onDocInitialized(doc: Doc) {
const handleTransactionThrottled = throttle(
(trx: Transaction) => {
if (trx.local) {
const account = this.workspaceServerService.server?.account$.value;
if (account) {
doc.setUpdatedBy(account.id);
}
}
},
1000,
{
leading: true,
trailing: true,
}
);
doc.yDoc.on('afterTransaction', handleTransactionThrottled);
this.disposables.push(() => {
doc.yDoc.off('afterTransaction', handleTransactionThrottled);
handleTransactionThrottled.cancel();
});
}
}

View File

@@ -25,6 +25,8 @@ export const AFFiNE_WORKSPACE_DB_SCHEMA = {
pageWidth: f.string().optional(),
isTemplate: f.boolean().optional(),
integrationType: integrationType.optional(),
createdBy: f.string().optional(),
updatedBy: f.string().optional(),
}),
docCustomPropertyInfo: {
id: f.string().primaryKey().optional().default(nanoid),

View File

@@ -135,7 +135,15 @@ export class DocDisplayMetaService extends Service {
const referenced = !!options?.reference;
const titleAlias = referenced ? options?.title : undefined;
const originalTitle = doc ? get(doc.title$) : '';
const title = titleAlias ?? originalTitle;
// link to journal doc
const journalDateString = get(this.journalService.journalDate$(docId));
const journalIcon = journalDateString
? this.getJournalIcon(journalDateString, options)
: undefined;
const journalTitle = journalDateString
? i18nTime(journalDateString, { absolute: { accuracy: 'day' } })
: undefined;
const title = titleAlias ?? journalTitle ?? originalTitle;
const mode = doc ? get(doc.primaryMode$) : undefined;
const finalMode = options?.mode ?? mode ?? 'page';
const referenceToNode = !!(referenced && options.referenceToNode);
@@ -149,17 +157,11 @@ export class DocDisplayMetaService extends Service {
// title alias
if (titleAlias) return iconSet.AliasIcon;
if (journalIcon) return journalIcon;
// link to specified block
if (referenceToNode) return iconSet.BlockLinkIcon;
// link to journal doc
const journalDate = this._toDayjs(
get(this.journalService.journalDate$(docId))
);
if (journalDate) {
return this.getJournalIcon(journalDate, options);
}
// link to regular doc (reference)
if (options?.reference) {
return finalMode === 'edgeless'
@@ -177,12 +179,18 @@ export class DocDisplayMetaService extends Service {
const enableEmojiIcon =
get(this.featureFlagService.flags.enable_emoji_doc_icon.$) &&
options?.enableEmojiIcon !== false;
const lng = get(this.i18nService.i18n.currentLanguageKey$);
const doc = get(this.docsService.list.doc$(docId));
const referenced = !!options?.reference;
const titleAlias = referenced ? options?.title : undefined;
const originalTitle = doc ? get(doc.title$) : '';
const title = titleAlias ?? originalTitle;
// journal title
const journalDateString = get(this.journalService.journalDate$(docId));
const journalTitle = journalDateString
? i18nTime(journalDateString, { absolute: { accuracy: 'day' } })
: undefined;
const title = titleAlias ?? journalTitle ?? originalTitle;
// emoji title
if (enableEmojiIcon && title) {
@@ -200,6 +208,8 @@ export class DocDisplayMetaService extends Service {
// title alias
if (titleAlias) return titleAlias;
if (journalTitle) return journalTitle;
// doc not found
if (!doc) {
return this.i18nService.i18n.i18next.t(
@@ -208,12 +218,6 @@ export class DocDisplayMetaService extends Service {
);
}
// journal title
const journalDateString = get(this.journalService.journalDate$(docId));
if (journalDateString) {
return i18nTime(journalDateString, { absolute: { accuracy: 'day' } });
}
// original title
if (originalTitle) return originalTitle;
@@ -229,15 +233,4 @@ export class DocDisplayMetaService extends Service {
updatedDate: docRecord.meta$.value.updatedDate,
};
}
private _isJournalString(j?: string | false) {
return j ? !!j?.match(/^\d{4}-\d{2}-\d{2}$/) : false;
}
private _toDayjs(j?: string | false) {
if (!j || !this._isJournalString(j)) return null;
const day = dayjs(j);
if (!day.isValid()) return null;
return day;
}
}

View File

@@ -1,5 +1,7 @@
import type { DocMode, RootBlockModel } from '@blocksuite/affine/model';
import { Entity } from '@toeverything/infra';
import { throttle } from 'lodash-es';
import type { Transaction } from 'yjs';
import type { DocProperties } from '../../db';
import type { WorkspaceService } from '../../workspace';
@@ -13,6 +15,25 @@ export class Doc extends Entity {
private readonly workspaceService: WorkspaceService
) {
super();
const handleTransactionThrottled = throttle(
(trx: Transaction) => {
if (trx.local) {
this.setUpdatedAt(Date.now());
}
},
1000,
{
leading: true,
trailing: true,
}
);
this.yDoc.on('afterTransaction', handleTransactionThrottled);
this.disposables.push(() => {
this.yDoc.off('afterTransaction', handleTransactionThrottled);
handleTransactionThrottled.cancel();
});
}
/**
@@ -26,6 +47,7 @@ export class Doc extends Entity {
return this.scope.props.docId;
}
public readonly yDoc = this.scope.props.blockSuiteDoc.spaceDoc;
public readonly blockSuiteDoc = this.scope.props.blockSuiteDoc;
public readonly record = this.scope.props.record;
@@ -34,6 +56,26 @@ export class Doc extends Entity {
readonly primaryMode$ = this.record.primaryMode$;
readonly title$ = this.record.title$;
readonly trash$ = this.record.trash$;
readonly createdAt$ = this.record.createdAt$;
readonly updatedAt$ = this.record.updatedAt$;
readonly createdBy$ = this.record.createdBy$;
readonly updatedBy$ = this.record.updatedBy$;
setCreatedAt(createdAt: number) {
this.record.setMeta({ createDate: createdAt });
}
setUpdatedAt(updatedAt: number) {
this.record.setMeta({ updatedDate: updatedAt });
}
setCreatedBy(createdBy: string) {
this.setProperty('createdBy', createdBy);
}
setUpdatedBy(updatedBy: string) {
this.setProperty('updatedBy', updatedBy);
}
customProperty$(propertyId: string) {
return this.record.customProperty$(propertyId);

View File

@@ -30,6 +30,12 @@ export class DocRecord extends Entity<{ id: string }> {
{ id: this.id }
);
property$(propertyId: string) {
return this.properties$.selector(p => p[propertyId]) as LiveData<
string | undefined | null
>;
}
customProperty$(propertyId: string) {
return this.properties$.selector(
p => p['custom:' + propertyId]
@@ -87,4 +93,28 @@ export class DocRecord extends Entity<{ id: string }> {
title$ = this.meta$.map(meta => meta.title ?? '');
trash$ = this.meta$.map(meta => meta.trash ?? false);
createdAt$ = this.meta$.map(meta => meta.createDate);
updatedAt$ = this.meta$.map(meta => meta.updatedDate);
createdBy$ = this.property$('createdBy');
updatedBy$ = this.property$('updatedBy');
setCreatedAt(createdAt: number) {
this.setMeta({ createDate: createdAt });
}
setUpdatedAt(updatedAt: number) {
this.setMeta({ updatedDate: updatedAt });
}
setCreatedBy(createdBy: string) {
this.setProperty('createdBy', createdBy);
}
setUpdatedBy(updatedBy: string) {
this.setProperty('updatedBy', updatedBy);
}
}

View File

@@ -2,7 +2,11 @@ import { createEvent } from '@toeverything/infra';
import type { Doc } from '../entities/doc';
import type { DocRecord } from '../entities/record';
import type { DocCreateOptions } from '../types';
export const DocCreated = createEvent<DocRecord>('DocCreated');
export const DocCreated = createEvent<{
doc: DocRecord;
docCreateOptions: DocCreateOptions;
}>('DocCreated');
export const DocInitialized = createEvent<Doc>('DocInitialized');

View File

@@ -14,16 +14,23 @@ import { Doc } from './entities/doc';
import { DocPropertyList } from './entities/property-list';
import { DocRecord } from './entities/record';
import { DocRecordList } from './entities/record-list';
import { DocCreateMiddleware } from './providers/doc-create-middleware';
import { DocScope } from './scopes/doc';
import { DocService } from './services/doc';
import { DocsService } from './services/docs';
import { DocPropertiesStore } from './stores/doc-properties';
import { DocsStore } from './stores/docs';
export { DocCreateMiddleware } from './providers/doc-create-middleware';
export function configureDocModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(DocsService, [DocsStore, DocPropertiesStore])
.service(DocsService, [
DocsStore,
DocPropertiesStore,
[DocCreateMiddleware],
])
.store(DocPropertiesStore, [WorkspaceService, WorkspaceDBService])
.store(DocsStore, [WorkspaceService, DocPropertiesStore])
.entity(DocRecord, [DocsStore, DocPropertiesStore])

View File

@@ -0,0 +1,13 @@
import { createIdentifier } from '@toeverything/infra';
import type { DocRecord } from '../entities/record';
import type { DocCreateOptions } from '../types';
export interface DocCreateMiddleware {
beforeCreate?: (docCreateOptions: DocCreateOptions) => DocCreateOptions;
afterCreate?: (doc: DocRecord, docCreateOptions: DocCreateOptions) => void;
}
export const DocCreateMiddleware = createIdentifier<DocCreateMiddleware>(
'DocCreateMiddleware'
);

View File

@@ -1,6 +1,5 @@
import { DebugLogger } from '@affine/debug';
import { Unreachable } from '@affine/env/constant';
import type { DocMode } from '@blocksuite/affine/model';
import { replaceIdMiddleware } from '@blocksuite/affine/shared/adapters';
import type { AffineTextAttributes } from '@blocksuite/affine/shared/types';
import type { DeltaInsert } from '@blocksuite/affine/store';
@@ -9,19 +8,18 @@ import { LiveData, ObjectPool, Service } from '@toeverything/infra';
import { omitBy } from 'lodash-es';
import { combineLatest, map } from 'rxjs';
import {
type DocProps,
initDocFromProps,
} from '../../../blocksuite/initialization';
import { initDocFromProps } from '../../../blocksuite/initialization';
import type { DocProperties } from '../../db';
import { getAFFiNEWorkspaceSchema } from '../../workspace';
import type { Doc } from '../entities/doc';
import { DocPropertyList } from '../entities/property-list';
import { DocRecordList } from '../entities/record-list';
import { DocCreated, DocInitialized } from '../events';
import type { DocCreateMiddleware } from '../providers/doc-create-middleware';
import { DocScope } from '../scopes/doc';
import type { DocPropertiesStore } from '../stores/doc-properties';
import type { DocsStore } from '../stores/docs';
import type { DocCreateOptions } from '../types';
import { DocService } from './doc';
const logger = new DebugLogger('DocsService');
@@ -58,7 +56,8 @@ export class DocsService extends Service {
constructor(
private readonly store: DocsStore,
private readonly docPropertiesStore: DocPropertiesStore
private readonly docPropertiesStore: DocPropertiesStore,
private readonly docCreateMiddlewares: DocCreateMiddleware[]
) {
super();
}
@@ -110,16 +109,21 @@ export class DocsService extends Service {
return { doc: obj, release };
}
createDoc(
options: {
primaryMode?: DocMode;
docProps?: DocProps;
isTemplate?: boolean;
} = {}
) {
const doc = this.store.createBlockSuiteDoc();
initDocFromProps(doc, options.docProps);
const docRecord = this.list.doc$(doc.id).value;
createDoc(options: DocCreateOptions = {}) {
for (const middleware of this.docCreateMiddlewares) {
options = middleware.beforeCreate
? middleware.beforeCreate(options)
: options;
}
const id = this.store.createDoc(options.id);
const docStore = this.store.getBlockSuiteDoc(id);
if (!docStore) {
throw new Error('Failed to create doc');
}
if (options.skipInit !== true) {
initDocFromProps(docStore, options.docProps, options);
}
const docRecord = this.list.doc$(id).value;
if (!docRecord) {
throw new Unreachable();
}
@@ -129,7 +133,14 @@ export class DocsService extends Service {
if (options.isTemplate) {
docRecord.setProperty('isTemplate', true);
}
this.eventBus.emit(DocCreated, docRecord);
for (const middleware of this.docCreateMiddlewares) {
middleware.afterCreate?.(docRecord, options);
}
docRecord.setCreatedAt(Date.now());
this.eventBus.emit(DocCreated, {
doc: docRecord,
docCreateOptions: options,
});
return docRecord;
}
@@ -200,7 +211,14 @@ export class DocsService extends Service {
schema: getAFFiNEWorkspaceSchema(),
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc(id).getStore({ id }),
create: (id: string) => {
this.createDoc({ id });
const store = collection.getDoc(id)?.getStore({ id });
if (!store) {
throw new Error('Failed to create doc');
}
return store;
},
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},
@@ -293,7 +311,14 @@ export class DocsService extends Service {
schema: getAFFiNEWorkspaceSchema(),
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc(id).getStore({ id }),
create: (id: string) => {
this.createDoc({ id });
const store = collection.getDoc(id)?.getStore({ id });
if (!store) {
throw new Error('Failed to create doc');
}
return store;
},
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},

View File

@@ -6,8 +6,9 @@ import {
yjsObserveByPath,
yjsObserveDeep,
} from '@toeverything/infra';
import { nanoid } from 'nanoid';
import { distinctUntilChanged, map, switchMap } from 'rxjs';
import { Array as YArray, Map as YMap } from 'yjs';
import { Array as YArray, Map as YMap, transact } from 'yjs';
import type { WorkspaceService } from '../../workspace';
import type { DocPropertiesStore } from './doc-properties';
@@ -32,9 +33,33 @@ export class DocsStore extends Store {
return this.workspaceService.workspace.docCollection;
}
createBlockSuiteDoc() {
const doc = this.workspaceService.workspace.docCollection.createDoc();
return doc.getStore({ id: doc.id });
createDoc(docId?: string) {
const id = docId ?? nanoid();
transact(
this.workspaceService.workspace.rootYDoc,
() => {
const docs = this.workspaceService.workspace.rootYDoc
.getMap('meta')
.get('pages');
if (!docs || !(docs instanceof YArray)) {
return;
}
docs.push([
new YMap([
['id', id],
['title', ''],
['createDate', Date.now()],
['tags', new YArray()],
]),
]);
},
{ force: true }
);
return id;
}
watchDocIds() {

View File

@@ -0,0 +1,11 @@
import type { DocProps } from '@affine/core/blocksuite/initialization';
import type { DocMode } from '@blocksuite/affine/model';
export interface DocCreateOptions {
id?: string;
title?: string;
primaryMode?: DocMode;
skipInit?: boolean;
docProps?: DocProps;
isTemplate?: boolean;
}

View File

@@ -0,0 +1,63 @@
import { Service } from '@toeverything/infra';
import type { DocCreateMiddleware, DocRecord } from '../../doc';
import type { DocCreateOptions } from '../../doc/types';
import type { AppThemeService } from '../../theme';
import type { EdgelessDefaultTheme } from '../schema';
import type { EditorSettingService } from '../services/editor-setting';
const getValueByDefaultTheme = (
defaultTheme: EdgelessDefaultTheme,
currentAppTheme: string
) => {
switch (defaultTheme) {
case 'dark':
return 'dark';
case 'light':
return 'light';
case 'specified':
return currentAppTheme === 'dark' ? 'dark' : 'light';
case 'auto':
return 'system';
default:
return 'system';
}
};
export class EditorSettingDocCreateMiddleware
extends Service
implements DocCreateMiddleware
{
constructor(
private readonly editorSettingService: EditorSettingService,
private readonly appThemeService: AppThemeService
) {
super();
}
beforeCreate(docCreateOptions: DocCreateOptions): DocCreateOptions {
// clone the docCreateOptions to avoid mutating the original object
docCreateOptions = {
...docCreateOptions,
};
const preferMode =
this.editorSettingService.editorSetting.settings$.value.newDocDefaultMode;
const mode = preferMode === 'ask' ? 'page' : preferMode;
docCreateOptions.primaryMode ??= mode;
docCreateOptions.docProps = {
...docCreateOptions.docProps,
note: this.editorSettingService.editorSetting.get('affine:note'),
};
return docCreateOptions;
}
afterCreate(doc: DocRecord, _docCreateOptions: DocCreateOptions) {
const edgelessDefaultTheme = getValueByDefaultTheme(
this.editorSettingService.editorSetting.get('edgelessDefaultTheme'),
this.appThemeService.appTheme.theme$.value ?? 'light'
);
doc.setProperty('edgelessColorTheme', edgelessDefaultTheme);
}
}

View File

@@ -2,9 +2,13 @@ import { type Framework } from '@toeverything/infra';
import { ServersService } from '../cloud';
import { DesktopApiService } from '../desktop-api';
import { DocCreateMiddleware } from '../doc';
import { I18n } from '../i18n';
import { GlobalState, GlobalStateService } from '../storage';
import { AppThemeService } from '../theme';
import { WorkspaceScope } from '../workspace';
import { EditorSetting } from './entities/editor-setting';
import { EditorSettingDocCreateMiddleware } from './impls/doc-create-middleware';
import { CurrentUserDBEditorSettingProvider } from './impls/user-db';
import { EditorSettingProvider } from './provider/editor-setting-provider';
import { EditorSettingService } from './services/editor-setting';
@@ -21,6 +25,11 @@ export function configureEditorSettingModule(framework: Framework) {
.impl(EditorSettingProvider, CurrentUserDBEditorSettingProvider, [
ServersService,
GlobalState,
])
.scope(WorkspaceScope)
.impl(DocCreateMiddleware, EditorSettingDocCreateMiddleware, [
EditorSettingService,
AppThemeService,
]);
}

View File

@@ -1,28 +1,12 @@
import { OnEvent, Service } from '@toeverything/infra';
import { Service } from '@toeverything/infra';
import { DocsService } from '../../doc';
import type { Workspace } from '../../workspace';
import { WorkspaceInitialized } from '../../workspace';
import {
EditorSetting,
type EditorSettingExt,
} from '../entities/editor-setting';
@OnEvent(WorkspaceInitialized, e => e.onWorkspaceInitialized)
export class EditorSettingService extends Service {
editorSetting = this.framework.createEntity(
EditorSetting
) as EditorSettingExt;
onWorkspaceInitialized(workspace: Workspace) {
// set default mode for new doc
workspace.docCollection.slots.docCreated.subscribe(docId => {
const preferMode = this.editorSetting.settings$.value.newDocDefaultMode;
const docsService = workspace.scope.get(DocsService);
const mode = preferMode === 'ask' ? 'page' : preferMode;
docsService.list.setPrimaryMode(docId, mode);
});
// never dispose, because this service always live longer than workspace
}
}

View File

@@ -64,7 +64,7 @@ export class ImportClipperService extends Service {
flavour,
async docCollection => {
docCollection.meta.initialize();
docCollection.meta.setName(workspaceName);
docCollection.doc.getMap('meta').set('name', workspaceName);
docId = await MarkdownTransformer.importMarkdownToDoc({
collection: docCollection,
schema: getAFFiNEWorkspaceSchema(),
@@ -73,6 +73,7 @@ export class ImportClipperService extends Service {
});
}
);
if (!docId) {
throw new Error('Failed to import doc');
}

View File

@@ -54,7 +54,7 @@ export class ImportTemplateService extends Service {
flavour,
async (docCollection, _, docStorage) => {
docCollection.meta.initialize();
docCollection.meta.setName(workspaceName);
docCollection.doc.getMap('meta').set('name', workspaceName);
const doc = docCollection.createDoc();
docId = doc.id;
await docStorage.pushDocUpdate({

View File

@@ -72,10 +72,6 @@ export class IntegrationWriter extends Entity {
const doc = collection.getDoc(docId)?.getStore();
if (!doc) throw new Error('Doc not found');
doc.workspace.meta.setDocMeta(docId, {
updatedDate: Date.now(),
});
if (updateStrategy === 'override') {
const pageBlock = doc.getBlocksByFlavour('affine:page')[0];
// remove all children of the page block

View File

@@ -1,7 +1,6 @@
import { type Framework } from '@toeverything/infra';
import { DocScope, DocService, DocsService } from '../doc';
import { EditorSettingService } from '../editor-setting';
import { TemplateDocService } from '../template-doc';
import { WorkspaceScope } from '../workspace';
import { JournalService } from './services/journal';
@@ -19,12 +18,7 @@ export { suggestJournalDate } from './suggest-journal-date';
export function configureJournalModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(JournalService, [
JournalStore,
DocsService,
EditorSettingService,
TemplateDocService,
])
.service(JournalService, [JournalStore, DocsService, TemplateDocService])
.store(JournalStore, [DocsService])
.scope(DocScope)
.service(JournalDocService, [DocService, JournalService]);

View File

@@ -1,13 +1,7 @@
import { Text } from '@blocksuite/affine/store';
import { LiveData, Service } from '@toeverything/infra';
import dayjs from 'dayjs';
import {
type DocProps,
initDocFromProps,
} from '../../../blocksuite/initialization';
import type { DocsService } from '../../doc';
import type { EditorSettingService } from '../../editor-setting';
import type { TemplateDocService } from '../../template-doc';
import type { JournalStore } from '../store/journal';
@@ -19,7 +13,6 @@ export class JournalService extends Service {
constructor(
private readonly store: JournalStore,
private readonly docsService: DocsService,
private readonly editorSettingService: EditorSettingService,
private readonly templateDocService: TemplateDocService
) {
super();
@@ -53,7 +46,9 @@ export class JournalService extends Service {
private createJournal(maybeDate: MaybeDate) {
const day = dayjs(maybeDate);
const title = day.format(JOURNAL_DATE_FORMAT);
const docRecord = this.docsService.createDoc();
const docRecord = this.docsService.createDoc({
title,
});
// set created date to match the journal date
docRecord.setMeta({
createDate: dayjs()
@@ -81,15 +76,6 @@ export class JournalService extends Service {
this.docsService
.duplicateFromTemplate(pageTemplateDocId, docRecord.id)
.catch(console.error);
} else {
const { doc, release } = this.docsService.open(docRecord.id);
this.docsService.list.setPrimaryMode(docRecord.id, 'page');
const docProps: DocProps = {
page: { title: new Text(title) },
note: this.editorSettingService.editorSetting.get('affine:note'),
};
initDocFromProps(doc.blockSuiteDoc, docProps);
release();
}
this.setJournalDate(docRecord.id, title);
return docRecord;

View File

@@ -1,10 +1,7 @@
import { track } from '@affine/track';
import { Text } from '@blocksuite/affine/store';
import { Service } from '@toeverything/infra';
import type { DocProps } from '../../../blocksuite/initialization';
import type { DocsService } from '../../doc';
import { EditorSettingService } from '../../editor-setting';
import type { WorkbenchService } from '../../workbench';
import { CollectionsQuickSearchSession } from '../impls/collections';
import { CommandsQuickSearchSession } from '../impls/commands';
@@ -95,23 +92,17 @@ export class CMDKQuickSearchService extends Service {
}
if (result.source === 'creation') {
const editorSettingService =
this.framework.get(EditorSettingService);
const docProps: DocProps = {
page: { title: new Text(result.payload.title) },
note: editorSettingService.editorSetting.get('affine:note'),
};
if (result.id === 'creation:create-page') {
const newDoc = this.docsService.createDoc({
primaryMode: 'page',
docProps,
title: result.payload.title,
});
this.workbenchService.workbench.openDoc(newDoc.id);
} else if (result.id === 'creation:create-edgeless') {
const newDoc = this.docsService.createDoc({
primaryMode: 'edgeless',
docProps,
title: result.payload.title,
});
this.workbenchService.workbench.openDoc(newDoc.id);
}

View File

@@ -2,18 +2,11 @@ export { AppThemeService } from './services/theme';
import { type Framework } from '@toeverything/infra';
import { EditorSettingService } from '../editor-setting';
import { WorkspaceScope } from '../workspace';
import { AppTheme } from './entities/theme';
import { EdgelessThemeService } from './services/edgeless-theme';
import { AppThemeService } from './services/theme';
export function configureAppThemeModule(framework: Framework) {
framework
.service(AppThemeService)
.entity(AppTheme)
.scope(WorkspaceScope)
.service(EdgelessThemeService, [AppThemeService, EditorSettingService]);
framework.service(AppThemeService).entity(AppTheme);
}
export function configureEssentialThemeModule(framework: Framework) {

View File

@@ -1,43 +0,0 @@
import { OnEvent, Service } from '@toeverything/infra';
import type { DocRecord } from '../../doc';
import { DocCreated } from '../../doc';
import type { EditorSettingService } from '../../editor-setting';
import type { EdgelessDefaultTheme } from '../../editor-setting/schema';
import type { AppThemeService } from './theme';
const getValueByDefaultTheme = (
defaultTheme: EdgelessDefaultTheme,
currentAppTheme: string
) => {
switch (defaultTheme) {
case 'dark':
return 'dark';
case 'light':
return 'light';
case 'specified':
return currentAppTheme === 'dark' ? 'dark' : 'light';
case 'auto':
return 'system';
default:
return 'system';
}
};
@OnEvent(DocCreated, i => i.onDocCreated)
export class EdgelessThemeService extends Service {
constructor(
private readonly appThemeService: AppThemeService,
private readonly editorSettingService: EditorSettingService
) {
super();
}
onDocCreated(docRecord: DocRecord) {
const value = getValueByDefaultTheme(
this.editorSettingService.editorSetting.get('edgelessDefaultTheme'),
this.appThemeService.appTheme.theme$.value ?? 'light'
);
docRecord.setProperty('edgelessColorTheme', value);
}
}

View File

@@ -46,7 +46,7 @@ import {
} from '@toeverything/infra';
import { isEqual } from 'lodash-es';
import { map, Observable, switchMap, tap } from 'rxjs';
import { type Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import { Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import type { Server, ServersService } from '../../cloud';
import {
@@ -169,6 +169,7 @@ class CloudWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
const docCollection = new WorkspaceImpl({
id: workspaceId,
rootDoc: new YDoc({ guid: workspaceId }),
blobSource: {
get: async key => {
const record = await blobStorage.get(key);

View File

@@ -31,7 +31,7 @@ import { LiveData, Service } from '@toeverything/infra';
import { isEqual } from 'lodash-es';
import { nanoid } from 'nanoid';
import { Observable } from 'rxjs';
import { type Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import { Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import { DesktopApiService } from '../../desktop-api';
import type {
@@ -150,6 +150,7 @@ class LocalWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
const docCollection = new WorkspaceImpl({
id: id,
rootDoc: new YDoc({ guid: id }),
blobSource: {
get: async key => {
const record = await blobStorage.get(key);

View File

@@ -1,7 +1,9 @@
import type { Workspace as WorkspaceInterface } from '@blocksuite/affine/store';
import { Entity, LiveData } from '@toeverything/infra';
import { Observable } from 'rxjs';
import { Entity, LiveData, yjsObserveByPath } from '@toeverything/infra';
import type { Observable } from 'rxjs';
import { Doc as YDoc, transact } from 'yjs';
import { DocsService } from '../../doc';
import { WorkspaceImpl } from '../impls/workspace';
import type { WorkspaceScope } from '../scopes/workspace';
import { WorkspaceEngineService } from '../services/engine';
@@ -19,12 +21,15 @@ export class Workspace extends Entity {
readonly flavour = this.meta.flavour;
readonly rootYDoc = new YDoc({ guid: this.openOptions.metadata.id });
_docCollection: WorkspaceInterface | null = null;
get docCollection() {
if (!this._docCollection) {
this._docCollection = new WorkspaceImpl({
id: this.openOptions.metadata.id,
rootDoc: this.rootYDoc,
blobSource: {
get: async key => {
const record = await this.engine.blob.get(key);
@@ -54,13 +59,15 @@ export class Workspace extends Entity {
onLoadDoc: doc => this.engine.doc.connectDoc(doc),
onLoadAwareness: awareness =>
this.engine.awareness.connectAwareness(awareness),
onCreateDoc: docId =>
this.docs.createDoc({ id: docId, skipInit: true }).id,
});
}
return this._docCollection;
}
get rootYDoc() {
return this.docCollection.doc;
get docs() {
return this.scope.get(DocsService);
}
get canGracefulStop() {
@@ -73,29 +80,39 @@ export class Workspace extends Entity {
}
name$ = LiveData.from<string | undefined>(
new Observable(subscriber => {
subscriber.next(this.docCollection.meta.name);
const subscription =
this.docCollection.meta.commonFieldsUpdated.subscribe(() => {
subscriber.next(this.docCollection.meta.name);
});
return subscription.unsubscribe.bind(subscription);
}),
yjsObserveByPath(this.rootYDoc.getMap('meta'), 'name') as Observable<
string | undefined
>,
undefined
);
avatar$ = LiveData.from<string | undefined>(
new Observable(subscriber => {
subscriber.next(this.docCollection.meta.avatar);
const subscription =
this.docCollection.meta.commonFieldsUpdated.subscribe(() => {
subscriber.next(this.docCollection.meta.avatar);
});
return subscription.unsubscribe.bind(subscription);
}),
avatar$ = LiveData.from(
yjsObserveByPath(this.rootYDoc.getMap('meta'), 'avatar') as Observable<
string | undefined
>,
undefined
);
setAvatar(avatar: string) {
transact(
this.rootYDoc,
() => {
this.rootYDoc.getMap('meta').set('avatar', avatar);
},
{ force: true }
);
}
setName(name: string) {
transact(
this.rootYDoc,
() => {
this.rootYDoc.getMap('meta').set('name', name);
},
{ force: true }
);
}
override dispose(): void {
this.docCollection.dispose();
}

View File

@@ -17,16 +17,18 @@ import {
} from '@blocksuite/affine/sync';
import { Subject } from 'rxjs';
import type { Awareness } from 'y-protocols/awareness.js';
import * as Y from 'yjs';
import type { Doc as YDoc } from 'yjs';
import { DocImpl } from './doc';
import { WorkspaceMetaImpl } from './meta';
type WorkspaceOptions = {
id?: string;
rootDoc: YDoc;
blobSource?: BlobSource;
onLoadDoc?: (doc: Y.Doc) => void;
onLoadDoc?: (doc: YDoc) => void;
onLoadAwareness?: (awareness: Awareness) => void;
onCreateDoc?: (docId?: string) => string;
};
export class WorkspaceImpl implements Workspace {
@@ -34,7 +36,7 @@ export class WorkspaceImpl implements Workspace {
readonly blockCollections = new Map<string, Doc>();
readonly doc: Y.Doc;
readonly doc: YDoc;
readonly id: string;
@@ -45,8 +47,6 @@ export class WorkspaceImpl implements Workspace {
slots = {
/* eslint-disable rxjs/finnish */
docListUpdated: new Subject<void>(),
docRemoved: new Subject<string>(),
docCreated: new Subject<string>(),
/* eslint-enable rxjs/finnish */
};
@@ -54,20 +54,24 @@ export class WorkspaceImpl implements Workspace {
return this.blockCollections;
}
readonly onLoadDoc?: (doc: Y.Doc) => void;
readonly onLoadDoc?: (doc: YDoc) => void;
readonly onLoadAwareness?: (awareness: Awareness) => void;
readonly onCreateDoc?: (docId?: string) => string;
constructor({
id,
rootDoc,
blobSource,
onLoadDoc,
onLoadAwareness,
}: WorkspaceOptions = {}) {
onCreateDoc,
}: WorkspaceOptions) {
this.id = id || '';
this.doc = new Y.Doc({ guid: id });
this.doc = rootDoc;
this.onLoadDoc = onLoadDoc;
this.onLoadDoc?.(this.doc);
this.onLoadAwareness = onLoadAwareness;
this.onCreateDoc = onCreateDoc;
blobSource = blobSource ?? new MemoryBlobSource();
const logger = new NoopLogger();
@@ -97,7 +101,6 @@ export class WorkspaceImpl implements Workspace {
if (!doc) return;
this.blockCollections.delete(id);
doc.remove();
this.slots.docRemoved.next(id);
});
}
@@ -111,6 +114,17 @@ export class WorkspaceImpl implements Workspace {
* will be created in the doc simultaneously.
*/
createDoc(docId?: string): Doc {
if (this.onCreateDoc) {
const id = this.onCreateDoc(docId);
const doc = this.getDoc(id);
if (!doc) {
throw new BlockSuiteError(
ErrorCode.DocCollectionError,
'create doc failed'
);
}
return doc;
}
const id = docId ?? this.idGenerator();
if (this._hasDoc(id)) {
throw new BlockSuiteError(
@@ -125,7 +139,6 @@ export class WorkspaceImpl implements Workspace {
createDate: Date.now(),
tags: [],
});
this.slots.docCreated.next(id);
return this.getDoc(id) as Doc;
}