refactor(core): adapter to new journal and property (#8508)

remove old WorkspacePropertyAdapter
add JournalService
This commit is contained in:
EYHN
2024-10-16 08:46:38 +00:00
parent 72e1489c62
commit 140ac723e6
39 changed files with 269 additions and 255 deletions
@@ -5,7 +5,7 @@ import {
WorkspaceScope,
} from '@toeverything/infra';
import { WorkspacePropertiesAdapter } from '../properties';
import { JournalService } from '../journal';
import { DocDisplayMetaService } from './services/doc-display-meta';
export { DocDisplayMetaService };
@@ -14,7 +14,7 @@ export function configureDocDisplayMetaModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(DocDisplayMetaService, [
WorkspacePropertiesAdapter,
JournalService,
DocsService,
FeatureFlagService,
]);
@@ -29,7 +29,7 @@ import { LiveData, Service } from '@toeverything/infra';
import type { Dayjs } from 'dayjs';
import dayjs from 'dayjs';
import type { WorkspacePropertiesAdapter } from '../../properties';
import type { JournalService } from '../../journal';
type IconType = 'rc' | 'lit';
interface DocDisplayIconOptions<T extends IconType> {
@@ -83,7 +83,7 @@ const icons = { rc: rcIcons, lit: litIcons } as {
export class DocDisplayMetaService extends Service {
constructor(
private readonly propertiesAdapter: WorkspacePropertiesAdapter,
private readonly journalService: JournalService,
private readonly docsService: DocsService,
private readonly featureFlagService: FeatureFlagService
) {
@@ -110,7 +110,7 @@ export class DocDisplayMetaService extends Service {
// journal icon
const journalDate = this._toDayjs(
this.propertiesAdapter.getJournalPageDateString(docId)
this.journalService.journalDate$(docId).value
);
if (journalDate) {
if (!options?.compareDate) return iconSet.TodayIcon;
@@ -148,8 +148,7 @@ export class DocDisplayMetaService extends Service {
const doc = get(this.docsService.list.doc$(docId));
const docTitle = doc ? get(doc.title$) : undefined;
const journalDateString =
this.propertiesAdapter.getJournalPageDateString(docId);
const journalDateString = get(this.journalService.journalDate$(docId));
// journal
if (journalDateString) {
@@ -10,7 +10,7 @@ import {
useEditCollection,
} from '@affine/core/components/page-list';
import { CollectionService } from '@affine/core/modules/collection';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/properties';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/favorite';
import { ShareDocsListService } from '@affine/core/modules/share-doc';
import type { AffineDNDData } from '@affine/core/types/dnd';
import type { Collection } from '@affine/env/filter';
@@ -8,7 +8,7 @@ import { usePageHelper } from '@affine/core/components/blocksuite/block-suite-pa
import { useDeleteCollectionInfo } from '@affine/core/components/hooks/affine/use-delete-collection-info';
import { IsFavoriteIcon } from '@affine/core/components/pure/icons';
import { CollectionService } from '@affine/core/modules/collection';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/properties';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/favorite';
import { WorkbenchService } from '@affine/core/modules/workbench';
import { useI18n } from '@affine/i18n';
import { track } from '@affine/track';
@@ -56,7 +56,6 @@ export const ExplorerDocNode = ({
DocDisplayMetaService,
FeatureFlagService,
});
// const pageInfoAdapter = useCurrentWorkspacePropertiesAdapter();
const active =
useLiveData(globalContextService.globalContext.docId.$) === docId;
@@ -9,7 +9,7 @@ import { usePageHelper } from '@affine/core/components/blocksuite/block-suite-pa
import { useBlockSuiteMetaHelper } from '@affine/core/components/hooks/affine/use-block-suite-meta-helper';
import { useAsyncCallback } from '@affine/core/components/hooks/affine-async-hooks';
import { IsFavoriteIcon } from '@affine/core/components/pure/icons';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/properties';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/favorite';
import { WorkbenchService } from '@affine/core/modules/workbench';
import { useI18n } from '@affine/i18n';
import { track } from '@affine/track';
@@ -15,11 +15,11 @@ import {
useSelectDoc,
useSelectTag,
} from '@affine/core/components/page-list/selector';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/favorite';
import {
type FolderNode,
OrganizeService,
} from '@affine/core/modules/organize';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/properties';
import { WorkbenchService } from '@affine/core/modules/workbench';
import type { AffineDNDData } from '@affine/core/types/dnd';
import { Unreachable } from '@affine/env/constant';
@@ -1,6 +1,6 @@
import { MenuItem } from '@affine/component';
import { IsFavoriteIcon } from '@affine/core/components/pure/icons';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/properties';
import { CompatibleFavoriteItemsAdapter } from '@affine/core/modules/favorite';
import { useI18n } from '@affine/i18n';
import { useLiveData, useService } from '@toeverything/infra';
import { useMemo } from 'react';
@@ -1,6 +1,6 @@
import { IconButton, useConfirmModal } from '@affine/component';
import { ExplorerTreeRoot } from '@affine/core/modules/explorer/views/tree';
import { MigrationFavoriteItemsAdapter } from '@affine/core/modules/properties';
import { MigrationFavoriteItemsAdapter } from '@affine/core/modules/favorite';
import { Trans, useI18n } from '@affine/i18n';
import { track } from '@affine/track';
import { BroomIcon, HelpIcon } from '@blocksuite/icons/rc';
@@ -8,16 +8,26 @@ import {
import { AuthService } from '../cloud';
import { FavoriteList } from './entities/favorite-list';
import { FavoriteService } from './services/favorite';
import {
CompatibleFavoriteItemsAdapter,
MigrationFavoriteItemsAdapter,
} from './services/old/adapter';
import { FavoriteStore } from './stores/favorite';
export { FavoriteSupportType, isFavoriteSupportType } from './constant';
export type { FavoriteList } from './entities/favorite-list';
export { FavoriteService } from './services/favorite';
export {
CompatibleFavoriteItemsAdapter,
MigrationFavoriteItemsAdapter,
} from './services/old/adapter';
export function configureFavoriteModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(FavoriteService)
.entity(FavoriteList, [FavoriteStore])
.store(FavoriteStore, [AuthService, WorkspaceDBService, WorkspaceService]);
.store(FavoriteStore, [AuthService, WorkspaceDBService, WorkspaceService])
.service(MigrationFavoriteItemsAdapter, [WorkspaceService])
.service(CompatibleFavoriteItemsAdapter, [FavoriteService]);
}
@@ -6,7 +6,8 @@ import { LiveData, Service } from '@toeverything/infra';
import { defaultsDeep } from 'lodash-es';
import { Observable } from 'rxjs';
import type { FavoriteService, FavoriteSupportType } from '../../favorite';
import type { FavoriteSupportType } from '../../constant';
import type { FavoriteService } from '../favorite';
import {
PagePropertyType,
PageSystemPropertyId,
@@ -23,8 +24,10 @@ const AFFINE_PROPERTIES_ID = 'affine:workspace-properties';
* May abstract the adapter for each property type, e.g. PagePropertiesAdapter, SchemaAdapter, etc.
* So that the adapter could be more focused and easier to maintain (like assigning default values)
* However the properties for an abstraction may not be limited to a single yjs map.
*
* @deprecated use docService.doc.properties$
*/
export class WorkspacePropertiesAdapter extends Service {
class WorkspacePropertiesAdapter {
// provides a easy-to-use interface for workspace properties
public readonly proxy: WorkspaceAffineProperties;
public readonly properties: Y.Map<any>;
@@ -38,7 +41,6 @@ export class WorkspacePropertiesAdapter extends Service {
}
constructor(public readonly workspaceService: WorkspaceService) {
super();
// check if properties exists, if not, create one
const rootDoc = workspaceService.workspace.docCollection.doc;
this.properties = rootDoc.getMap(AFFINE_PROPERTIES_ID);
@@ -165,7 +167,9 @@ export class WorkspacePropertiesAdapter extends Service {
}
export class MigrationFavoriteItemsAdapter extends Service {
constructor(private readonly adapter: WorkspacePropertiesAdapter) {
adapter = new WorkspacePropertiesAdapter(this.workspaceService);
constructor(public readonly workspaceService: WorkspaceService) {
super();
}
+2 -2
View File
@@ -16,11 +16,11 @@ import { configureFavoriteModule } from './favorite';
import { configureFindInPageModule } from './find-in-page';
import { configureI18nModule } from './i18n';
import { configureImportTemplateModule } from './import-template';
import { configureJournalModule } from './journal';
import { configureNavigationModule } from './navigation';
import { configureOrganizeModule } from './organize';
import { configurePeekViewModule } from './peek-view';
import { configurePermissionsModule } from './permissions';
import { configureWorkspacePropertiesModule } from './properties';
import { configureQuickSearchModule } from './quicksearch';
import { configureShareDocsModule } from './share-doc';
import { configureShareSettingModule } from './share-setting';
@@ -36,7 +36,6 @@ export function configureCommonModules(framework: Framework) {
configureCollectionModule(framework);
configureNavigationModule(framework);
configureTagModule(framework);
configureWorkspacePropertiesModule(framework);
configureCloudModule(framework);
configureQuotaModule(framework);
configurePermissionsModule(framework);
@@ -61,4 +60,5 @@ export function configureCommonModules(framework: Framework) {
configureUserspaceModule(framework);
configureDocInfoModule(framework);
configureAppSidebarModule(framework);
configureJournalModule(framework);
}
@@ -0,0 +1,23 @@
import {
DocScope,
DocService,
DocsService,
type Framework,
WorkspaceScope,
} from '@toeverything/infra';
import { JournalService } from './services/journal';
import { JournalDocService } from './services/journal-doc';
import { JournalStore } from './store/journal';
export { JournalService } from './services/journal';
export { JournalDocService } from './services/journal-doc';
export function configureJournalModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(JournalService, [JournalStore])
.store(JournalStore, [DocsService])
.scope(DocScope)
.service(JournalDocService, [DocService, JournalService]);
}
@@ -0,0 +1,20 @@
import { type DocService, Service } from '@toeverything/infra';
import type { JournalService } from './journal';
export class JournalDocService extends Service {
constructor(
private readonly docService: DocService,
private readonly journalService: JournalService
) {
super();
}
readonly journalDate$ = this.journalService.journalDate$(
this.docService.doc.id
);
setJournalDate(date: string) {
this.journalService.setJournalDate(this.docService.doc.id, date);
}
}
@@ -0,0 +1,21 @@
import { LiveData, Service } from '@toeverything/infra';
import type { JournalStore } from '../store/journal';
export class JournalService extends Service {
constructor(private readonly store: JournalStore) {
super();
}
journalDate$(docId: string) {
return LiveData.from(this.store.watchDocJournalDate(docId), undefined);
}
setJournalDate(docId: string, date: string) {
this.store.setDocJournalDate(docId, date);
}
getJournalsByDate(date: string) {
return this.store.getDocsByJournalDate(date);
}
}
@@ -0,0 +1,43 @@
import type { DocsService } from '@toeverything/infra';
import { LiveData, Store } from '@toeverything/infra';
import type { Observable } from 'rxjs';
function isJournalString(j?: string | false) {
return j ? !!j?.match(/^\d{4}-\d{2}-\d{2}$/) : false;
}
export class JournalStore extends Store {
constructor(private readonly docsService: DocsService) {
super();
}
watchDocJournalDate(docId: string): Observable<string | undefined> {
return LiveData.computed(get => {
const doc = get(this.docsService.list.doc$(docId));
if (!doc) {
// if doc not exists
return undefined;
}
const journal = get(doc.properties$.selector(p => p.journal));
if (journal && !isJournalString(journal)) {
return undefined;
}
return journal ?? undefined;
});
}
setDocJournalDate(docId: string, date: string) {
const doc = this.docsService.list.doc$(docId).value;
if (!doc) {
// doc not exists, do nothing
return;
}
doc.setProperty('journal', date);
}
getDocsByJournalDate(date: string) {
return this.docsService.list.docs$.value.filter(
doc => doc.properties$.value.journal === date
);
}
}
@@ -1,29 +0,0 @@
export {
CompatibleFavoriteItemsAdapter,
MigrationFavoriteItemsAdapter,
WorkspacePropertiesAdapter,
} from './services/adapter';
export { WorkspaceLegacyProperties } from './services/legacy-properties';
import {
type Framework,
WorkspaceScope,
WorkspaceService,
} from '@toeverything/infra';
import { FavoriteService } from '../favorite';
import {
CompatibleFavoriteItemsAdapter,
MigrationFavoriteItemsAdapter,
WorkspacePropertiesAdapter,
} from './services/adapter';
import { WorkspaceLegacyProperties } from './services/legacy-properties';
export function configureWorkspacePropertiesModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(WorkspaceLegacyProperties, [WorkspaceService])
.service(WorkspacePropertiesAdapter, [WorkspaceService])
.service(MigrationFavoriteItemsAdapter, [WorkspacePropertiesAdapter])
.service(CompatibleFavoriteItemsAdapter, [FavoriteService]);
}
@@ -1,86 +0,0 @@
import type { Tag } from '@affine/env/filter';
import type { DocsPropertiesMeta } from '@blocksuite/affine/store';
import type { WorkspaceService } from '@toeverything/infra';
import { LiveData, Service } from '@toeverything/infra';
import { Observable } from 'rxjs';
/**
* @deprecated use WorkspacePropertiesAdapter instead (later)
*/
export class WorkspaceLegacyProperties extends Service {
constructor(private readonly workspaceService: WorkspaceService) {
super();
}
get workspaceId() {
return this.workspaceService.workspace.id;
}
get properties() {
return this.workspaceService.workspace.docCollection.meta.properties;
}
get tagOptions() {
return this.properties.tags?.options ?? [];
}
updateProperties = (properties: DocsPropertiesMeta) => {
this.workspaceService.workspace.docCollection.meta.setProperties(
properties
);
};
subscribe(cb: () => void) {
const disposable =
this.workspaceService.workspace.docCollection.meta.docMetaUpdated.on(cb);
return disposable.dispose;
}
properties$ = LiveData.from(
new Observable<DocsPropertiesMeta>(sub => {
return this.subscribe(() => sub.next(this.properties));
}),
this.properties
);
tagOptions$ = LiveData.from(
new Observable<Tag[]>(sub => {
return this.subscribe(() => sub.next(this.tagOptions));
}),
this.tagOptions
);
updateTagOptions = (options: Tag[]) => {
this.updateProperties({
...this.properties,
tags: {
options,
},
});
};
updateTagOption = (id: string, option: Tag) => {
this.updateTagOptions(this.tagOptions.map(o => (o.id === id ? option : o)));
};
removeTagOption = (id: string) => {
this.workspaceService.workspace.docCollection.doc.transact(() => {
this.updateTagOptions(this.tagOptions.filter(o => o.id !== id));
// need to remove tag from all pages
this.workspaceService.workspace.docCollection.docs.forEach(doc => {
const tags = doc.meta?.tags ?? [];
if (tags.includes(id)) {
this.updatePageTags(
doc.id,
tags.filter(t => t !== id)
);
}
});
});
};
updatePageTags = (pageId: string, tags: string[]) => {
this.workspaceService.workspace.docCollection.setDocMeta(pageId, {
tags,
});
};
}
@@ -7,9 +7,9 @@ import {
DocsService,
type Framework,
WorkspaceScope,
WorkspaceService,
} from '@toeverything/infra';
import { WorkspaceLegacyProperties } from '../properties';
import { Tag } from './entities/tag';
import { TagList } from './entities/tag-list';
import { TagService } from './service/tag';
@@ -19,7 +19,7 @@ export function configureTagModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(TagService)
.store(TagStore, [WorkspaceLegacyProperties])
.store(TagStore, [WorkspaceService])
.entity(TagList, [TagStore, DocsService])
.entity(Tag, [TagStore, DocsService]);
}
@@ -1,24 +1,43 @@
import type { Tag as TagSchema } from '@affine/env/filter';
import { Store } from '@toeverything/infra';
import type { Tag, Tag as TagSchema } from '@affine/env/filter';
import type { DocsPropertiesMeta } from '@blocksuite/affine/store';
import type { WorkspaceService } from '@toeverything/infra';
import { LiveData, Store } from '@toeverything/infra';
import { nanoid } from 'nanoid';
import type { WorkspaceLegacyProperties } from '../../properties';
import { Observable } from 'rxjs';
export class TagStore extends Store {
constructor(private readonly properties: WorkspaceLegacyProperties) {
get properties() {
return this.workspaceService.workspace.docCollection.meta.properties;
}
get tagOptions() {
return this.properties.tags?.options ?? [];
}
tagOptions$ = LiveData.from(
new Observable<Tag[]>(sub => {
return this.subscribe(() => sub.next(this.tagOptions));
}),
this.tagOptions
);
subscribe(cb: () => void) {
const disposable =
this.workspaceService.workspace.docCollection.meta.docMetaUpdated.on(cb);
return disposable.dispose;
}
constructor(private readonly workspaceService: WorkspaceService) {
super();
}
watchTagIds() {
return this.properties.tagOptions$
.map(tags => tags.map(tag => tag.id))
.asObservable();
return this.tagOptions$.map(tags => tags.map(tag => tag.id)).asObservable();
}
createNewTag(value: string, color: string) {
const newId = nanoid();
this.properties.updateTagOptions([
...this.properties.tagOptions$.value,
this.updateTagOptions([
...this.tagOptions$.value,
{
id: newId,
value,
@@ -30,24 +49,65 @@ export class TagStore extends Store {
return newId;
}
updateProperties = (properties: DocsPropertiesMeta) => {
this.workspaceService.workspace.docCollection.meta.setProperties(
properties
);
};
updateTagOptions = (options: Tag[]) => {
this.updateProperties({
...this.properties,
tags: {
options,
},
});
};
updateTagOption = (id: string, option: Tag) => {
this.updateTagOptions(this.tagOptions.map(o => (o.id === id ? option : o)));
};
removeTagOption = (id: string) => {
this.workspaceService.workspace.docCollection.doc.transact(() => {
this.updateTagOptions(this.tagOptions.filter(o => o.id !== id));
// need to remove tag from all pages
this.workspaceService.workspace.docCollection.docs.forEach(doc => {
const tags = doc.meta?.tags ?? [];
if (tags.includes(id)) {
this.updatePageTags(
doc.id,
tags.filter(t => t !== id)
);
}
});
});
};
updatePageTags = (pageId: string, tags: string[]) => {
this.workspaceService.workspace.docCollection.setDocMeta(pageId, {
tags,
});
};
deleteTag(id: string) {
this.properties.removeTagOption(id);
this.removeTagOption(id);
}
watchTagInfo(id: string) {
return this.properties.tagOptions$.map(
return this.tagOptions$.map(
tags => tags.find(tag => tag.id === id) as TagSchema | undefined
);
}
updateTagInfo(id: string, tagInfo: Partial<TagSchema>) {
const tag = this.properties.tagOptions$.value.find(tag => tag.id === id) as
const tag = this.tagOptions$.value.find(tag => tag.id === id) as
| TagSchema
| undefined;
if (!tag) {
return;
}
this.properties.updateTagOption(id, {
this.updateTagOption(id, {
id: id,
value: tag.value,
color: tag.color,