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 22, 2024
1 parent 4ccb5e9 commit 22bb7d9
Show file tree
Hide file tree
Showing 31 changed files with 786 additions and 322 deletions.
17 changes: 17 additions & 0 deletions packages/common/infra/src/modules/doc/entities/record-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ export class DocRecordList extends Entity {
[]
);

public readonly trashDocs$ = LiveData.from<DocRecord[]>(
this.store.watchTrashDocIds().pipe(
map(ids =>
ids.map(id => {
const exists = this.pool.get(id);
if (exists) {
return exists;
}
const record = this.framework.createEntity(DocRecord, { id });
this.pool.set(id, record);
return record;
})
)
),
[]
);

public readonly isReady$ = LiveData.from(
this.store.watchDocListReady(),
false
Expand Down
2 changes: 2 additions & 0 deletions packages/common/infra/src/modules/doc/entities/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ export class DocRecord extends Entity<{ id: string }> {
}

title$ = this.meta$.map(meta => meta.title ?? '');

trash$ = this.meta$.map(meta => meta.trash ?? false);
}
24 changes: 23 additions & 1 deletion packages/common/infra/src/modules/doc/stores/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,29 @@ export class DocsStore extends Store {
return () => {
dispose();
};
}).pipe(distinctUntilChanged((p, c) => isEqual(p, c)));
});
}

watchTrashDocIds() {
return new Observable<string[]>(subscriber => {
const emit = () => {
subscriber.next(
this.workspaceService.workspace.docCollection.meta.docMetas
.map(v => (v.trash ? v.id : null))
.filter(Boolean) as string[]
);
};

emit();

const dispose =
this.workspaceService.workspace.docCollection.meta.docMetaUpdated.on(
emit
).dispose;
return () => {
dispose();
};
});
}

watchDocMeta(id: string) {
Expand Down
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 { DocLinksService } from '@affine/core/modules/doc-link';
import type {
PageInfoCustomProperty,
PageInfoCustomPropertyMeta,
Expand Down Expand Up @@ -379,7 +379,7 @@ export const PagePropertiesSettingsPopup = ({
};

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

export const PageBacklinksPopup = ({
Expand All @@ -397,11 +397,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 @@ -596,10 +596,11 @@ export const PagePropertiesTableHeader = ({
const manager = useContext(managerContext);

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

const { docService, workspaceService } = useServices({
DocService,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { cssVar } from '@toeverything/theme';
import { style } from '@vanilla-extract/css';

export const container = style({
width: '100%',
maxWidth: cssVar('--affine-editor-width'),
marginLeft: 'auto',
marginRight: 'auto',
paddingLeft: cssVar('--affine-editor-side-padding', '24'),
paddingRight: cssVar('--affine-editor-side-padding', '24'),
fontSize: cssVar('--affine-font-base'),
});

export const dividerContainer = style({
height: '16px',
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
});

export const divider = style({
background: cssVar('--affine-border-color'),
height: '0.5px',
width: '100%',
});

export const titleLine = style({
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
});

export const title = style({
fontWeight: 500,
fontSize: '15px',
lineHeight: '24px',
color: cssVar('--affine-text-primary-color'),
});

export const showButton = style({
width: '56px',
height: '28px',
borderRadius: '8px',
border: '1px solid ' + cssVar('--affine-border-color'),
backgroundColor: cssVar('--affine-white'),
textAlign: 'center',
fontSize: '12px',
lineHeight: '28px',
fontWeight: '500',
color: cssVar('--affine-text-primary-color'),
cursor: 'pointer',
});

export const linksContainer = style({
marginBottom: '16px',
});

export const linksTitles = style({
color: cssVar('--affine-text-secondary-color'),
height: '32px',
lineHeight: '32px',
});

export const link = style({
width: '100%',
height: '32px',
display: 'flex',
alignItems: 'center',
gap: '4px',
whiteSpace: 'nowrap',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { DocLinksService } from '@affine/core/modules/doc-link';
import {
useLiveData,
useServices,
WorkspaceService,
} from '@toeverything/infra';
import { useCallback, useState } from 'react';

import { AffinePageReference } from '../../affine/reference-link';
import * as styles from './bi-directional-link-panel.css';

export const BiDirectionalLinkPanel = () => {
const [show, setShow] = useState(false);
const { docLinksService, workspaceService } = useServices({
DocLinksService,
WorkspaceService,
});

const links = useLiveData(docLinksService.links.links$);
const backlinks = useLiveData(docLinksService.backlinks.backlinks$);

const handleClickShow = useCallback(() => {
setShow(!show);
}, [show]);

return (
<div className={styles.container}>
{!show && (
<div className={styles.dividerContainer}>
<div className={styles.divider}></div>
</div>
)}

<div className={styles.titleLine}>
<div className={styles.title}>Bi-Directional Links</div>
<div className={styles.showButton} onClick={handleClickShow}>
{show ? 'Hide' : 'Show'}
</div>
</div>

{show && (
<>
<div className={styles.dividerContainer}>
<div className={styles.divider}></div>
</div>
<div className={styles.linksContainer}>
<div className={styles.linksTitles}>
Backlinks · {backlinks.length}
</div>
{backlinks.map(link => (
<AffinePageReference
key={link.docId}
pageId={link.docId}
docCollection={workspaceService.workspace.docCollection}
/>
))}
</div>
<div className={styles.linksContainer}>
<div className={styles.linksTitles}>
Outgoing links · {links.length}
</div>
{links.map(link => (
<div key={link.docId} className={styles.link}>
<AffinePageReference
pageId={link.docId}
docCollection={workspaceService.workspace.docCollection}
/>
</div>
))}
</div>
</>
)}
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useJournalInfoHelper } from '@affine/core/hooks/use-journal';
import { PeekViewService } from '@affine/core/modules/peek-view';
import { WorkbenchService } from '@affine/core/modules/workbench';
import {
BiDirectionalLinkPanel,
DocMetaTags,
DocTitle,
EdgelessEditor,
Expand All @@ -34,6 +33,7 @@ import React, {

import { PagePropertiesTable } from '../../affine/page-properties';
import { AffinePageReference } from '../../affine/reference-link';
import { BiDirectionalLinkPanel } from './bi-directional-link-panel';
import { BlocksuiteEditorJournalDocTitle } from './journal-doc-title';
import {
patchDocModeService,
Expand Down Expand Up @@ -65,10 +65,6 @@ const adapted = {
react: React,
elementClass: EdgelessEditor,
}),
BiDirectionalLinkPanel: createReactComponentFromLit({
react: React,
elementClass: BiDirectionalLinkPanel,
}),
};

interface BlocksuiteEditorProps {
Expand Down Expand Up @@ -211,9 +207,7 @@ export const BlocksuiteDocEditor = forwardRef<
}}
></div>
) : null}
{docPage && !page.readonly ? (
<adapted.BiDirectionalLinkPanel doc={page} pageRoot={docPage} />
) : null}
{!page.readonly ? <BiDirectionalLinkPanel /> : null}
</div>
{portals}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ export const ItemGroup = <T extends ListItem>({
) : null}
</div>
) : null}
<Collapsible.Content className={styles.collapsibleContent}>
<Collapsible.Content
className={styles.collapsibleContent}
data-state={!collapsed ? 'open' : 'closed'}
>
<div className={styles.collapsibleContentInner}>
{items.map(item => (
<PageListItemRenderer key={item.id} {...item} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ export const CollectionSidebarNavItem = ({
inAllowList={allowList.has(page.id)}
removeFromAllowList={removeFromAllowList}
allPageMeta={allPagesMeta}
doc={page}
docId={page.id}
key={page.id}
docCollection={docCollection}
/>
);
})}
Expand Down
Loading

0 comments on commit 22bb7d9

Please sign in to comment.