Skip to content

Commit

Permalink
feat: center peek view
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed May 30, 2024
1 parent 40697f5 commit 604ff28
Show file tree
Hide file tree
Showing 20 changed files with 440 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/common/infra/src/livedata/livedata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export class LiveData<T = unknown>
distinctUntilChanged(comparator?: (previous: T, current: T) => boolean) {
return LiveData.from(
this.pipe(distinctUntilChanged(comparator)),
null as any
null as T
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useLitPortalFactory,
} from '@affine/component';
import { useJournalInfoHelper } from '@affine/core/hooks/use-journal';
import { PeekViewService } from '@affine/core/modules/peek-view';
import { WorkbenchService } from '@affine/core/modules/workbench';
import type { BlockSpec } from '@blocksuite/block-std';
import {
Expand All @@ -30,6 +31,7 @@ import { AffinePageReference } from '../../affine/reference-link';
import { BlocksuiteEditorJournalDocTitle } from './journal-doc-title';
import {
patchNotificationService,
patchPeekViewService,
patchReferenceRenderer,
type ReferenceReactRenderer,
} from './specs/custom/spec-patchers';
Expand Down Expand Up @@ -66,6 +68,7 @@ interface BlocksuiteEditorProps {

const usePatchSpecs = (page: Doc, specs: BlockSpec[]) => {
const [reactToLit, portals] = useLitPortalFactory();
const peekViewService = useService(PeekViewService);
const referenceRenderer: ReferenceReactRenderer = useMemo(() => {
return function customReference(reference) {
const pageId = reference.delta.attributes?.reference?.pageId;
Expand All @@ -83,8 +86,9 @@ const usePatchSpecs = (page: Doc, specs: BlockSpec[]) => {
patchReferenceRenderer(patched, reactToLit, referenceRenderer),
confirmModal
);
patched = patchPeekViewService(patched, peekViewService);
return patched;
}, [confirmModal, reactToLit, referenceRenderer, specs]);
}, [confirmModal, peekViewService, reactToLit, referenceRenderer, specs]);

return [
patchedSpecs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
type ToastOptions,
type useConfirmModal,
} from '@affine/component';
import type { PeekViewService } from '@affine/core/modules/peek-view';
import type { ActivePeekView } from '@affine/core/modules/peek-view/entities/peek-view';
import { DebugLogger } from '@affine/debug';
import type { BlockSpec } from '@blocksuite/block-std';
import type {
AffineReference,
Expand All @@ -15,6 +18,8 @@ import type {
import { LitElement, type TemplateResult } from 'lit';
import React, { createElement, type ReactNode } from 'react';

const logger = new DebugLogger('affine::spec-patchers');

export type ReferenceReactRenderer = (
reference: AffineReference
) => React.ReactElement;
Expand Down Expand Up @@ -190,3 +195,27 @@ export function patchNotificationService(
});
return specs;
}

export function patchPeekViewService(
specs: BlockSpec[],
service: PeekViewService
) {
const rootSpec = specs.find(
spec => spec.schema.model.flavour === 'affine:page'
) as BlockSpec<string, RootService>;

if (!rootSpec) {
return specs;
}

patchSpecService(rootSpec, pageService => {
pageService.peekViewService = {
peek: (target: ActivePeekView['target']) => {
logger.debug('center peek', target);
service.peekView.open(target);
},
};
});

return specs;
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const CollectionSidebarNavItem = ({
<span>{collection.name}</span>
</SidebarMenuLinkItem>
<Collapsible.Content className={styles.collapsibleContent}>
<div style={{ marginLeft: 20, marginTop: -4 }}>
<div className={styles.docsListContainer}>
{pagesToRender.map(page => {
return (
<Doc
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useBlockSuitePageReferences } from '@affine/core/hooks/use-block-suite-page-references';
import { WorkbenchService } from '@affine/core/modules/workbench';
import { WorkbenchLink, WorkbenchService } from '@affine/core/modules/workbench';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { EdgelessIcon, PageIcon } from '@blocksuite/icons';
import type { DocCollection, DocMeta } from '@blocksuite/store';
Expand All @@ -13,7 +13,7 @@ import {
getDNDId,
} from '../../../../hooks/affine/use-global-dnd-helper';
import { useNavigateHelper } from '../../../../hooks/use-navigate-helper';
import { MenuItem as CollectionItem } from '../../../app-sidebar';
import { MenuLinkItem } from '../../../app-sidebar';
import { DragMenuItemOverlay } from '../components/drag-menu-item-overlay';
import { PostfixItem } from '../components/postfix-item';
import { ReferencePage } from '../components/reference-page';
Expand Down Expand Up @@ -78,11 +78,12 @@ export const Doc = ({
data-draggable={true}
data-dragging={isDragging}
>
<CollectionItem
<MenuLinkItem
data-testid="collection-page"
data-type="collection-list-item"
icon={icon}
onClick={clickDoc}
to={`/${docId}`}
linkComponent={WorkbenchLink}
className={styles.title}
active={active}
collapsed={referencesToRender.length > 0 ? collapsed : undefined}
Expand All @@ -101,7 +102,7 @@ export const Doc = ({
{...listeners}
>
{doc.title || t['Untitled']()}
</CollectionItem>
</MenuLinkItem>
<Collapsible.Content className={styles.collapsibleContent}>
{referencesToRender.map(id => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,9 @@ export const emptyCollectionNewButton = style({
height: '28px',
fontSize: cssVar('fontXs'),
});
export const docsListContainer = style({
marginLeft: 20,
display: 'flex',
flexDirection: 'column',
gap: 4,
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useBlockSuitePageReferences } from '@affine/core/hooks/use-block-suite-page-references';
import { WorkbenchService } from '@affine/core/modules/workbench';
import {
WorkbenchLink,
WorkbenchService,
} from '@affine/core/modules/workbench';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { EdgelessIcon, PageIcon } from '@blocksuite/icons';
import type { DocCollection, DocMeta } from '@blocksuite/store';
Expand Down Expand Up @@ -60,10 +63,11 @@ export const ReferencePage = ({
data-type="reference-page"
data-testid={`reference-page-${pageId}`}
active={active}
to={`/workspace/${docCollection.id}/${pageId}`}
to={`/${pageId}`}
icon={icon}
collapsed={collapsible ? collapsed : undefined}
onCollapsedChange={setCollapsed}
linkComponent={WorkbenchLink}
postfix={
<PostfixItem
docCollection={docCollection}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
parseDNDId,
} from '@affine/core/hooks/affine/use-global-dnd-helper';
import { useBlockSuitePageReferences } from '@affine/core/hooks/use-block-suite-page-references';
import { WorkbenchService } from '@affine/core/modules/workbench';
import { WorkbenchLink, WorkbenchService } from '@affine/core/modules/workbench';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { EdgelessIcon, PageIcon } from '@blocksuite/icons';
import { type AnimateLayoutChanges, useSortable } from '@dnd-kit/sortable';
Expand Down Expand Up @@ -102,7 +102,8 @@ export const FavouriteDocSidebarNavItem = ({
data-dragging={isDragging}
className={draggableMenuItemStyles.draggableMenuItem}
active={linkActive}
to={`/workspace/${workspace.id}/${pageId}`}
to={`/${pageId}`}
linkComponent={WorkbenchLink}
collapsed={collapsible ? collapsed : undefined}
onCollapsedChange={setCollapsed}
postfix={
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 @@ -5,6 +5,7 @@ import { configureCloudModule } from './cloud';
import { configureCollectionModule } from './collection';
import { configureFindInPageModule } from './find-in-page';
import { configureNavigationModule } from './navigation';
import { configurePeekViewModule } from './peek-view';
import { configurePermissionsModule } from './permissions';
import { configureWorkspacePropertiesModule } from './properties';
import { configureRightSidebarModule } from './right-sidebar';
Expand All @@ -28,6 +29,7 @@ export function configureCommonModules(framework: Framework) {
configureShareDocsModule(framework);
configureTelemetryModule(framework);
configureFindInPageModule(framework);
configurePeekViewModule(framework);
}

export function configureImpls(framework: Framework) {
Expand Down
27 changes: 27 additions & 0 deletions packages/frontend/core/src/modules/peek-view/entities/peek-view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { BlockElement } from '@blocksuite/block-std';
import type { AffineReference } from '@blocksuite/blocks';
import { Entity, LiveData } from '@toeverything/infra';

export type ActivePeekView = {
target: BlockElement | AffineReference | HTMLAnchorElement;
};

export class PeekViewEntity extends Entity {
constructor() {
super();
}

private readonly _active$ = new LiveData<ActivePeekView | null>(null);

active$ = this._active$.distinctUntilChanged();
show$ = this.active$.map(active => active !== null).distinctUntilChanged();

open(target: ActivePeekView['target']) {
this._active$.next({ target });
}

close() {
if (this._active$.value === null) return;
this._active$.next(null);
}
}
10 changes: 10 additions & 0 deletions packages/frontend/core/src/modules/peek-view/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Framework } from '@toeverything/infra';

import { PeekViewEntity } from './entities/peek-view';
import { PeekViewService } from './services/peek-view';

export function configurePeekViewModule(framework: Framework) {
framework.service(PeekViewService).entity(PeekViewEntity);
}

export { PeekViewEntity, PeekViewService };
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Service } from '@toeverything/infra';

import { PeekViewEntity } from '../entities/peek-view';

export class PeekViewService extends Service {
public readonly peekView = this.framework.createEntity(PeekViewEntity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { style } from '@vanilla-extract/css';

export const editor = style({
vars: {
'--affine-editor-width': '100%',
'--affine-editor-side-padding': '160px',
},
minHeight: '100%',
});
Loading

0 comments on commit 604ff28

Please sign in to comment.