Skip to content

Commit

Permalink
refactor(core): use new backlink indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Jun 19, 2024
1 parent c7d8b06 commit 392865e
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Tooltip,
} from '@affine/component';
import { useCurrentWorkspacePropertiesAdapter } from '@affine/core/hooks/use-affine-adapter';
import { useBlockSuitePageBacklinks } from '@affine/core/hooks/use-block-suite-page-backlinks';
import { DocLinksServices } from '@affine/core/modules/doc-link';
import type {
PageInfoCustomProperty,
PageInfoCustomPropertyMeta,
Expand Down Expand Up @@ -384,7 +384,7 @@ export const PagePropertiesSettingsPopup = ({
};

type PageBacklinksPopupProps = PropsWithChildren<{
backlinks: string[];
backlinks: { docId: string; blockId: string; title: string }[];
}>;

export const PageBacklinksPopup = ({
Expand All @@ -402,11 +402,11 @@ export const PageBacklinksPopup = ({
}}
items={
<div className={styles.backlinksList}>
{backlinks.map(pageId => (
{backlinks.map(link => (
<AffinePageReference
key={pageId}
key={link.docId + ':' + link.blockId}
wrapper={MenuItem}
pageId={pageId}
pageId={link.docId}
docCollection={manager.workspace.docCollection}
/>
))}
Expand Down Expand Up @@ -601,10 +601,13 @@ export const PagePropertiesTableHeader = ({
const manager = useContext(managerContext);

const t = useAFFiNEI18N();
const backlinks = useBlockSuitePageBacklinks(
manager.workspace.docCollection,
manager.pageId
);
const { docLinksServices } = useServices({ DocLinksServices });
const docBacklinks = docLinksServices.backlinks;
const backlinks = useLiveData(docBacklinks.backlinks$);

useEffect(() => {
docBacklinks.revalidate();
}, [docBacklinks]);

const { docService, workspaceService } = useServices({
DocService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ export const BlocksuiteDocEditor = forwardRef<
}}
></div>
) : null}
{docPage && !page.readonly ? (
{/* {docPage && !page.readonly ? (
<adapted.BiDirectionalLinkPanel doc={page} pageRoot={docPage} />
) : null}
) : null} */}
</div>
{portals}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { DocService } from '@toeverything/infra';
import {
effect,
Entity,
fromPromise,
LiveData,
mapInto,
} from '@toeverything/infra';
import { exhaustMap } from 'rxjs';

import type { DocsSearchService } from '../../docs-search';

interface Backlink {
docId: string;
blockId: string;
title: string;
}

export class DocBacklinks extends Entity {
constructor(
private readonly docsSearchService: DocsSearchService,
private readonly docService: DocService
) {
super();
}

backlinks$ = new LiveData<Backlink[]>([]);

revalidate = effect(
exhaustMap(() => {
return fromPromise(
this.docsSearchService.searchRefsTo(this.docService.doc.id)
).pipe(mapInto(this.backlinks$));
})
);
}
14 changes: 14 additions & 0 deletions packages/frontend/core/src/modules/doc-link/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DocScope, DocService, type Framework } from '@toeverything/infra';

import { DocsSearchService } from '../docs-search';
import { DocBacklinks } from './entities/doc-backlinks';
import { DocLinksServices } from './services/doc-links';

export { DocLinksServices } from './services/doc-links';

export function configureDocLinksModules(framework: Framework) {
framework
.scope(DocScope)
.service(DocLinksServices)
.entity(DocBacklinks, [DocsSearchService, DocService]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Service } from '@toeverything/infra';

import { DocBacklinks } from '../entities/doc-backlinks';

export class DocLinksServices extends Service {
backlinks = this.framework.createEntity(DocBacklinks);
}
16 changes: 11 additions & 5 deletions packages/frontend/core/src/modules/docs-search/schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { Schema } from '@toeverything/infra';
import { defineSchema } from '@toeverything/infra';

export const docIndexSchema = {
export const docIndexSchema = defineSchema({
title: 'FullText',
} satisfies Schema;
});

export const blockIndexSchema = {
export type DocIndexSchema = typeof docIndexSchema;

export const blockIndexSchema = defineSchema({
docId: 'String',
blockId: 'String',
content: 'FullText',
flavour: 'String',
} satisfies Schema;
ref: 'String',
blob: 'String',
});

export type BlockIndexSchema = typeof blockIndexSchema;
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,49 @@ export class DocsSearchService extends Service {
return result;
}

async searchRefsTo(docId: string): Promise<
{
docId: string;
blockId: string;
title: string;
}[]
> {
const { buckets } = await this.indexer.blockIndex.aggregate(
{
type: 'match',
field: 'ref',
match: docId,
},
'docId',
{
hits: {
fields: ['docId', 'blockId'],
pagination: {
limit: 1,
},
},
pagination: {
limit: 100,
},
}
);

const docData = await this.indexer.docIndex.getAll(
buckets.map(bucket => bucket.key)
);

return buckets.map(bucket => {
const title =
docData.find(doc => doc.id === bucket.key)?.get('title') ?? '';
const blockId = bucket.hits.nodes[0]?.fields.blockId ?? '';
return {
docId: bucket.key,
blockId: typeof blockId === 'string' ? blockId : blockId[0],
title: typeof title === 'string' ? title : title[0],
};
});
}

async getDocTitle(docId: string) {
const doc = await this.indexer.docIndex.get(docId);
const title = doc?.get('title');
Expand Down
2 changes: 2 additions & 0 deletions packages/frontend/core/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { configureInfraModules, type Framework } from '@toeverything/infra';

import { configureCloudModule } from './cloud';
import { configureCollectionModule } from './collection';
import { configureDocLinksModules } from './doc-link';
import { configureDocsSearchModule } from './docs-search';
import { configureFindInPageModule } from './find-in-page';
import { configureI18nModule } from './i18n';
Expand Down Expand Up @@ -35,6 +36,7 @@ export function configureCommonModules(framework: Framework) {
configurePeekViewModule(framework);
configureQuickSearchModule(framework);
configureDocsSearchModule(framework);
configureDocLinksModules(framework);
configureI18nModule(framework);
}

Expand Down

0 comments on commit 392865e

Please sign in to comment.