Skip to content

Commit

Permalink
feat(blocks): allow peek view to be closed by the caller
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 authored and fundon committed Oct 22, 2024
1 parent ff95f12 commit f8f786b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { EditorService } from '@affine/core/modules/editor';
import { EditorSettingService } from '@affine/core/modules/editor-settting';
import { resolveLinkToDoc } from '@affine/core/modules/navigation';
import type { PeekViewService } from '@affine/core/modules/peek-view';
import type { ActivePeekView } from '@affine/core/modules/peek-view/entities/peek-view';
import {
CreationQuickSearchSession,
DocsQuickSearchSession,
Expand All @@ -35,6 +34,8 @@ import type {
AffineReference,
DocMode,
DocModeProvider,
PeekOptions,

Check failure on line 37 in packages/frontend/core/src/components/blocksuite/block-suite-editor/specs/custom/spec-patchers.tsx

View workflow job for this annotation

GitHub Actions / Lint

Module '"@blocksuite/affine/blocks"' has no exported member 'PeekOptions'.
PeekViewService as BSPeekViewService,
QuickSearchResult,
RootService,
} from '@blocksuite/affine/blocks';
Expand Down Expand Up @@ -243,11 +244,20 @@ export function patchEmbedLinkedDocBlockConfig(framework: FrameworkProvider) {

export function patchPeekViewService(service: PeekViewService) {
return PeekViewExtension({
peek: (target: ActivePeekView['target'], template?: TemplateResult) => {
logger.debug('center peek', target, template);
return service.peekView.open(target, template);
peek: (

Check failure on line 247 in packages/frontend/core/src/components/blocksuite/block-suite-editor/specs/custom/spec-patchers.tsx

View workflow job for this annotation

GitHub Actions / Lint

Type '(element: { target: HTMLElement; docId: string; blockIds?: string[]; template?: TemplateResult; }, options?: any) => Promise<void>' is not assignable to type '{ (pageRef: { docId: string; blockId?: string | undefined; }): Promise<void>; (target: HTMLElement, template?: TemplateResult | undefined): Promise<...>; <Element extends BlockComponent>(target: Element, template?: TemplateResult | undefined): Promise<...>; }'.
element: {
target: HTMLElement;
docId: string;
blockIds?: string[];
template?: TemplateResult;
},
options?: PeekOptions
) => {
logger.debug('center peek', element);
const { template, ...target } = element;
return service.peekView.open(target, template, options?.abortSignal);
},
});
} satisfies BSPeekViewService);
}

export function patchDocModeService(
Expand Down
27 changes: 25 additions & 2 deletions packages/frontend/core/src/modules/peek-view/entities/peek-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ export class PeekViewEntity extends Entity {
// return true if the peek view will be handled
open = async (
target: ActivePeekView['target'],
template?: TemplateResult
template?: TemplateResult,
abortSignal?: AbortSignal
) => {
const resolvedInfo = resolvePeekInfoFromPeekTarget(target, template);
if (!resolvedInfo) {
Expand All @@ -220,7 +221,11 @@ export class PeekViewEntity extends Entity {
// if there is an active peek view and it is a doc peek view, we will navigate it first
if (active?.info.type === 'doc' && this.show$.value?.value) {
// TODO(@pengx17): scroll to the viewing position?
this.workbenchService.workbench.openDoc(active.info.docId);
this.workbenchService.workbench.openDoc({
docId: active.info.docId,
blockIds: active.info.blockIds,
elementIds: active.info.elementIds,
});
}

this._active$.next({ target, info: resolvedInfo });
Expand All @@ -231,6 +236,24 @@ export class PeekViewEntity extends Entity {
? 'zoom'
: 'fade',
});

if (abortSignal) {
const abortListener = () => {
if (this.active$.value?.target === target) {
this.close();
}
};

abortSignal.addEventListener('abort', abortListener);

const showSubscription = this.show$.subscribe(v => {
if (!v && !abortSignal.aborted) {
abortSignal.removeEventListener('abort', abortListener);
showSubscription.unsubscribe();
}
});
}

return firstValueFrom(race(this._active$, this.show$).pipe(map(() => {})));
};

Expand Down

0 comments on commit f8f786b

Please sign in to comment.