mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-14 16:46:22 +08:00
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:
@@ -6,6 +6,7 @@ import type { Store } from '@blocksuite/affine/store';
|
||||
import { css, html, LitElement, nothing } from 'lit';
|
||||
import { property, query } from 'lit/decorators.js';
|
||||
import { createRef, type Ref, ref } from 'lit/directives/ref.js';
|
||||
import { Doc as YDoc } from 'yjs';
|
||||
|
||||
import { PPTBuilder } from '../slides/index';
|
||||
import { getAIPanelWidget } from '../utils/ai-widgets';
|
||||
@@ -223,6 +224,7 @@ export class AISlidesRenderer extends WithDisposable(LitElement) {
|
||||
|
||||
const collection = new WorkspaceImpl({
|
||||
id: 'SLIDES_PREVIEW',
|
||||
rootDoc: new YDoc({ guid: 'SLIDES_PREVIEW' }),
|
||||
});
|
||||
collection.meta.initialize();
|
||||
const doc = collection.createDoc().getStore();
|
||||
|
||||
@@ -20,6 +20,7 @@ import { property, query } from 'lit/decorators.js';
|
||||
import { repeat } from 'lit/directives/repeat.js';
|
||||
import { styleMap } from 'lit/directives/style-map.js';
|
||||
import type { Root } from 'mdast';
|
||||
import { Doc as YDoc } from 'yjs';
|
||||
|
||||
import { MiniMindmapSchema, MiniMindmapSpecs } from './spec.js';
|
||||
|
||||
@@ -101,6 +102,7 @@ export class MiniMindmapPreview extends WithDisposable(LitElement) {
|
||||
|
||||
const collection = new WorkspaceImpl({
|
||||
id: 'MINI_MINDMAP_TEMPORARY',
|
||||
rootDoc: new YDoc({ guid: 'MINI_MINDMAP_TEMPORARY' }),
|
||||
});
|
||||
collection.meta.initialize();
|
||||
const doc = collection.createDoc('doc:home').getStore();
|
||||
|
||||
@@ -174,18 +174,6 @@ const BlockSuiteEditorImpl = ({
|
||||
std.command.exec(appendParagraphCommand);
|
||||
}, [affineEditorContainerProxy.host?.std, page, readonly, shared]);
|
||||
|
||||
useEffect(() => {
|
||||
const disposable = page.slots.blockUpdated.subscribe(() => {
|
||||
disposable.unsubscribe();
|
||||
page.workspace.meta.setDocMeta(page.id, {
|
||||
updatedDate: Date.now(),
|
||||
});
|
||||
});
|
||||
return () => {
|
||||
disposable.unsubscribe();
|
||||
};
|
||||
}, [page]);
|
||||
|
||||
useEffect(() => {
|
||||
const editorContainer = rootRef.current;
|
||||
if (editorContainer) {
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
import { toast } from '@affine/component';
|
||||
import type { DocProps } from '@affine/core/blocksuite/initialization';
|
||||
import { AppSidebarService } from '@affine/core/modules/app-sidebar';
|
||||
import { DocsService } from '@affine/core/modules/doc';
|
||||
import { EditorSettingService } from '@affine/core/modules/editor-setting';
|
||||
import { WorkbenchService } from '@affine/core/modules/workbench';
|
||||
import { getAFFiNEWorkspaceSchema } from '@affine/core/modules/workspace';
|
||||
import {
|
||||
DEFAULT_PAGE_BLOCK_HEIGHT,
|
||||
DEFAULT_PAGE_BLOCK_WIDTH,
|
||||
type DocMode,
|
||||
} from '@blocksuite/affine/model';
|
||||
import { type DocMode } from '@blocksuite/affine/model';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import { useServices } from '@toeverything/infra';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
@@ -17,15 +11,9 @@ import { useCallback, useMemo } from 'react';
|
||||
import { getStoreManager } from '../manager/migrating-store';
|
||||
|
||||
export const usePageHelper = (docCollection: Workspace) => {
|
||||
const {
|
||||
docsService,
|
||||
workbenchService,
|
||||
editorSettingService,
|
||||
appSidebarService,
|
||||
} = useServices({
|
||||
const { docsService, workbenchService, appSidebarService } = useServices({
|
||||
DocsService,
|
||||
WorkbenchService,
|
||||
EditorSettingService,
|
||||
AppSidebarService,
|
||||
});
|
||||
const workbench = workbenchService.workbench;
|
||||
@@ -44,13 +32,7 @@ export const usePageHelper = (docCollection: Workspace) => {
|
||||
}
|
||||
) => {
|
||||
appSidebar.setHovering(false);
|
||||
const docProps: DocProps = {
|
||||
note: {
|
||||
...editorSettingService.editorSetting.get('affine:note'),
|
||||
xywh: `[0, 0, ${DEFAULT_PAGE_BLOCK_WIDTH}, ${DEFAULT_PAGE_BLOCK_HEIGHT}]`,
|
||||
},
|
||||
};
|
||||
const page = docsService.createDoc({ docProps });
|
||||
const page = docsService.createDoc();
|
||||
|
||||
if (mode) {
|
||||
docRecordList.doc$(page.id).value?.setPrimaryMode(mode);
|
||||
@@ -64,13 +46,7 @@ export const usePageHelper = (docCollection: Workspace) => {
|
||||
}
|
||||
return page;
|
||||
},
|
||||
[
|
||||
appSidebar,
|
||||
docRecordList,
|
||||
docsService,
|
||||
editorSettingService.editorSetting,
|
||||
workbench,
|
||||
]
|
||||
[appSidebar, docRecordList, docsService, workbench]
|
||||
);
|
||||
|
||||
const createEdgelessAndOpen = useCallback(
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { DocsService } from '@affine/core/modules/doc';
|
||||
import { EditorSettingService } from '@affine/core/modules/editor-setting';
|
||||
import {
|
||||
CreationQuickSearchSession,
|
||||
DocsQuickSearchSession,
|
||||
@@ -20,7 +19,7 @@ import {
|
||||
QuickSearchExtension,
|
||||
type QuickSearchResult,
|
||||
} from '@blocksuite/affine/shared/services';
|
||||
import { type ExtensionType, Text } from '@blocksuite/affine/store';
|
||||
import { type ExtensionType } from '@blocksuite/affine/store';
|
||||
import type {
|
||||
SlashMenuConfig,
|
||||
SlashMenuItem,
|
||||
@@ -28,8 +27,6 @@ import type {
|
||||
import type { FrameworkProvider } from '@toeverything/infra';
|
||||
import { pick } from 'lodash-es';
|
||||
|
||||
import type { DocProps } from '../initialization';
|
||||
|
||||
export function patchQuickSearchService(framework: FrameworkProvider) {
|
||||
const QuickSearch = QuickSearchExtension({
|
||||
async openQuickSearch() {
|
||||
@@ -96,16 +93,11 @@ export function patchQuickSearchService(framework: FrameworkProvider) {
|
||||
|
||||
if (result.source === 'creation') {
|
||||
const docsService = framework.get(DocsService);
|
||||
const editorSettingService = framework.get(EditorSettingService);
|
||||
const mode =
|
||||
result.id === 'creation:create-edgeless' ? 'edgeless' : 'page';
|
||||
const docProps: DocProps = {
|
||||
page: { title: new Text(result.payload.title) },
|
||||
note: editorSettingService.editorSetting.get('affine:note'),
|
||||
};
|
||||
const newDoc = docsService.createDoc({
|
||||
primaryMode: mode,
|
||||
docProps,
|
||||
title: result.payload.title,
|
||||
});
|
||||
|
||||
resolve({ docId: newDoc.id });
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { DocCreateOptions } from '@affine/core/modules/doc/types';
|
||||
import {
|
||||
NoteDisplayMode,
|
||||
type NoteProps,
|
||||
@@ -22,11 +23,15 @@ export interface DocProps {
|
||||
) => void;
|
||||
}
|
||||
|
||||
export function initDocFromProps(doc: Store, props?: DocProps) {
|
||||
export function initDocFromProps(
|
||||
doc: Store,
|
||||
props?: DocProps,
|
||||
options: DocCreateOptions = {}
|
||||
) {
|
||||
doc.load(() => {
|
||||
const pageBlockId = doc.addBlock(
|
||||
'affine:page',
|
||||
props?.page || { title: new Text('') }
|
||||
props?.page || { title: new Text(options.title || '') }
|
||||
);
|
||||
const surfaceId = doc.addBlock(
|
||||
'affine:surface' as never,
|
||||
|
||||
@@ -25,7 +25,7 @@ import type {
|
||||
TransformerMiddleware,
|
||||
} from '@blocksuite/affine/store';
|
||||
import { toDraftModel, Transformer } from '@blocksuite/affine/store';
|
||||
|
||||
import { Doc as YDoc } from 'yjs';
|
||||
const updateSnapshotText = (
|
||||
point: TextRangePoint,
|
||||
snapshot: BlockSnapshot,
|
||||
@@ -164,7 +164,9 @@ export async function markDownToDoc(
|
||||
middlewares?: TransformerMiddleware[]
|
||||
) {
|
||||
// Should not create a new doc in the original collection
|
||||
const collection = new WorkspaceImpl();
|
||||
const collection = new WorkspaceImpl({
|
||||
rootDoc: new YDoc({ guid: 'markdownToDoc' }),
|
||||
});
|
||||
collection.meta.initialize();
|
||||
const transformer = new Transformer({
|
||||
schema,
|
||||
|
||||
@@ -113,6 +113,7 @@ const getOrCreateShellWorkspace = (
|
||||
if (!docCollection) {
|
||||
docCollection = new WorkspaceImpl({
|
||||
id: workspaceId,
|
||||
rootDoc: new YDoc({ guid: workspaceId }),
|
||||
blobSource: {
|
||||
name: 'cloud',
|
||||
readonly: true,
|
||||
|
||||
@@ -1,56 +1,26 @@
|
||||
import { Avatar, PropertyValue } from '@affine/component';
|
||||
import { CloudDocMetaService } from '@affine/core/modules/cloud/services/cloud-doc-meta';
|
||||
import { PropertyValue } from '@affine/component';
|
||||
import { PublicUserLabel } from '@affine/core/modules/cloud/views/public-user';
|
||||
import { DocService } from '@affine/core/modules/doc';
|
||||
import { WorkspaceService } from '@affine/core/modules/workspace';
|
||||
import { useI18n } from '@affine/i18n';
|
||||
import { useLiveData, useService } from '@toeverything/infra';
|
||||
import { useEffect, useMemo } from 'react';
|
||||
|
||||
import { userWrapper } from './created-updated-by.css';
|
||||
|
||||
const CloudUserAvatar = (props: { type: 'CreatedBy' | 'UpdatedBy' }) => {
|
||||
const cloudDocMetaService = useService(CloudDocMetaService);
|
||||
const cloudDocMeta = useLiveData(cloudDocMetaService.cloudDocMeta.meta$);
|
||||
const isRevalidating = useLiveData(
|
||||
cloudDocMetaService.cloudDocMeta.isRevalidating$
|
||||
const CreatedByUpdatedByAvatar = (props: {
|
||||
type: 'CreatedBy' | 'UpdatedBy';
|
||||
}) => {
|
||||
const docService = useService(DocService);
|
||||
const userId = useLiveData(
|
||||
props.type === 'CreatedBy'
|
||||
? docService.doc.createdBy$
|
||||
: docService.doc.updatedBy$
|
||||
);
|
||||
const error = useLiveData(cloudDocMetaService.cloudDocMeta.error$);
|
||||
|
||||
useEffect(() => {
|
||||
cloudDocMetaService.cloudDocMeta.revalidate();
|
||||
}, [cloudDocMetaService]);
|
||||
|
||||
const user = useMemo(() => {
|
||||
if (!cloudDocMeta) return null;
|
||||
if (props.type === 'CreatedBy' && cloudDocMeta.createdBy) {
|
||||
return {
|
||||
name: cloudDocMeta.createdBy.name,
|
||||
avatarUrl: cloudDocMeta.createdBy.avatarUrl,
|
||||
};
|
||||
} else if (props.type === 'UpdatedBy' && cloudDocMeta.updatedBy) {
|
||||
return {
|
||||
name: cloudDocMeta.updatedBy.name,
|
||||
avatarUrl: cloudDocMeta.updatedBy.avatarUrl,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}, [cloudDocMeta, props.type]);
|
||||
|
||||
if (!cloudDocMeta) {
|
||||
if (isRevalidating) {
|
||||
// TODO: loading ui
|
||||
return null;
|
||||
}
|
||||
if (error) {
|
||||
// error ui
|
||||
return;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (user) {
|
||||
if (userId) {
|
||||
return (
|
||||
<div className={userWrapper}>
|
||||
<Avatar url={user.avatarUrl || ''} name={user.name} size={22} />
|
||||
<span>{user.name}</span>
|
||||
<PublicUserLabel id={userId} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -85,7 +55,7 @@ export const CreatedByValue = () => {
|
||||
|
||||
return (
|
||||
<PropertyValue readonly>
|
||||
<CloudUserAvatar type="CreatedBy" />
|
||||
<CreatedByUpdatedByAvatar type="CreatedBy" />
|
||||
</PropertyValue>
|
||||
);
|
||||
};
|
||||
@@ -104,7 +74,7 @@ export const UpdatedByValue = () => {
|
||||
|
||||
return (
|
||||
<PropertyValue readonly>
|
||||
<CloudUserAvatar type="UpdatedBy" />
|
||||
<CreatedByUpdatedByAvatar type="UpdatedBy" />
|
||||
</PropertyValue>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -16,16 +16,11 @@ export function useDocCollectionPage(
|
||||
useEffect(() => {
|
||||
const group = new DisposableGroup();
|
||||
group.add(
|
||||
docCollection.slots.docCreated.subscribe(id => {
|
||||
if (pageId === id) {
|
||||
setPage(docCollection.getDoc(id)?.getStore() ?? null);
|
||||
}
|
||||
})
|
||||
);
|
||||
group.add(
|
||||
docCollection.slots.docRemoved.subscribe(id => {
|
||||
if (pageId === id) {
|
||||
docCollection.slots.docListUpdated.subscribe(() => {
|
||||
if (!pageId) {
|
||||
setPage(null);
|
||||
} else {
|
||||
setPage(docCollection.getDoc(pageId)?.getStore() ?? null);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
@@ -16,16 +16,11 @@ export function useDocCollectionPage(
|
||||
useEffect(() => {
|
||||
const group = new DisposableGroup();
|
||||
group.add(
|
||||
docCollection.slots.docCreated.subscribe(id => {
|
||||
if (pageId === id) {
|
||||
setPage(docCollection.getDoc(id)?.getStore() ?? null);
|
||||
}
|
||||
})
|
||||
);
|
||||
group.add(
|
||||
docCollection.slots.docRemoved.subscribe(id => {
|
||||
if (pageId === id) {
|
||||
docCollection.slots.docListUpdated.subscribe(() => {
|
||||
if (!pageId) {
|
||||
setPage(null);
|
||||
} else {
|
||||
setPage(docCollection.getDoc(pageId)?.getStore() ?? null);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
+5
-2
@@ -2,14 +2,17 @@ import { getAFFiNEWorkspaceSchema } from '@affine/core/modules/workspace';
|
||||
import { WorkspaceImpl } from '@affine/core/modules/workspace/impls/workspace';
|
||||
import type { DocSnapshot, Store } from '@blocksuite/affine/store';
|
||||
import { Transformer } from '@blocksuite/affine/store';
|
||||
|
||||
import { Doc as YDoc } from 'yjs';
|
||||
const getCollection = (() => {
|
||||
let collection: WorkspaceImpl | null = null;
|
||||
return async function () {
|
||||
if (collection) {
|
||||
return collection;
|
||||
}
|
||||
collection = new WorkspaceImpl({});
|
||||
collection = new WorkspaceImpl({
|
||||
id: 'edgeless-settings',
|
||||
rootDoc: new YDoc({ guid: 'edgeless-settings' }),
|
||||
});
|
||||
collection.meta.initialize();
|
||||
return collection;
|
||||
};
|
||||
|
||||
+6
-17
@@ -38,22 +38,11 @@ export const ProfilePanel = () => {
|
||||
}, [workspace])
|
||||
);
|
||||
const [name, setName] = useState('');
|
||||
const currentName = useLiveData(workspace.name$);
|
||||
|
||||
useEffect(() => {
|
||||
if (workspace?.docCollection) {
|
||||
setName(workspace.docCollection.meta.name ?? UNTITLED_WORKSPACE_NAME);
|
||||
const dispose =
|
||||
workspace.docCollection.meta.commonFieldsUpdated.subscribe(() => {
|
||||
setName(workspace.docCollection.meta.name ?? UNTITLED_WORKSPACE_NAME);
|
||||
});
|
||||
return () => {
|
||||
dispose.unsubscribe();
|
||||
};
|
||||
} else {
|
||||
setName(UNTITLED_WORKSPACE_NAME);
|
||||
}
|
||||
return;
|
||||
}, [workspace]);
|
||||
setName(currentName ?? UNTITLED_WORKSPACE_NAME);
|
||||
}, [currentName]);
|
||||
|
||||
const setWorkspaceAvatar = useCallback(
|
||||
async (file: File | null) => {
|
||||
@@ -61,14 +50,14 @@ export const ProfilePanel = () => {
|
||||
return;
|
||||
}
|
||||
if (!file) {
|
||||
workspace.docCollection.meta.setAvatar('');
|
||||
workspace.setAvatar('');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const reducedFile = await validateAndReduceImage(file);
|
||||
const blobs = workspace.docCollection.blobSync;
|
||||
const blobId = await blobs.set(reducedFile);
|
||||
workspace.docCollection.meta.setAvatar(blobId);
|
||||
workspace.setAvatar(blobId);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
@@ -82,7 +71,7 @@ export const ProfilePanel = () => {
|
||||
if (!workspace) {
|
||||
return;
|
||||
}
|
||||
workspace.docCollection.meta.setName(name);
|
||||
workspace.setName(name);
|
||||
},
|
||||
[workspace]
|
||||
);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
|
||||
@@ -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]);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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'
|
||||
);
|
||||
@@ -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),
|
||||
},
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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]);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ export async function buildShowcaseWorkspace(
|
||||
) {
|
||||
const meta = await workspacesService.create(flavour, async docCollection => {
|
||||
docCollection.meta.initialize();
|
||||
docCollection.meta.setName(workspaceName);
|
||||
docCollection.doc.getMap('meta').set('name', workspaceName);
|
||||
const blob = await (await fetch(onboardingUrl)).blob();
|
||||
|
||||
await ZipTransformer.importDocs(
|
||||
|
||||
Reference in New Issue
Block a user