Skip to content

Commit

Permalink
refactor: pdf viewer using op and service
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Nov 12, 2024
1 parent 1f69988 commit 266724b
Show file tree
Hide file tree
Showing 16 changed files with 573 additions and 572 deletions.
2 changes: 1 addition & 1 deletion packages/common/infra/src/op/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface PendingCall extends PromiseWithResolvers<any> {
timeout: number | NodeJS.Timeout;
}

interface OpClientOptions {
export interface OpClientOptions {
timeout?: number;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/common/infra/src/op/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type MessageCommunicapable = Pick<
> & {
start?(): void;
close?(): void;
terminate?(): void; // For Worker
};

export function ignoreUnknownEvent(handler: (data: Messages) => void) {
Expand Down Expand Up @@ -150,6 +151,7 @@ export abstract class AutoMessageHandler {

close() {
this.port.close?.();
this.port.terminate?.(); // For Worker
this.port.removeEventListener('message', this.handleMessage);
}
}
17 changes: 12 additions & 5 deletions packages/frontend/core/src/components/attachment-viewer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { RenderOut } from '@affine/core/modules/pdf/workers/types';
import type { AttachmentBlockModel } from '@blocksuite/affine/blocks';
import { filesize } from 'filesize';

Expand Down Expand Up @@ -29,19 +30,18 @@ export async function download(model: AttachmentBlockModel) {
export function renderItem(
scroller: HTMLElement | null,
className: string,
id: number,
width: number,
height: number,
buffer: Uint8ClampedArray
data: RenderOut
) {
if (!scroller) return;

const item = scroller.querySelector(
`[data-index="${id}"] > div.${className}`
`[data-index="${data.index}"] > div.${className}`
);
if (!item) return;
if (item.firstElementChild) return;

const { width, height, buffer } = data;

const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) return;
Expand All @@ -67,6 +67,13 @@ export function buildAttachmentProps(model: AttachmentBlockModel) {
return { model, name, ext, size, isPDF };
}

/**
* Generates a set of sequences.
*
* 1. when `start` is `0`, returns `[0, .., 5]`
* 2. when `end` is `total - 1`, returns `[total - 1, .., total - 5]`
* 2. when `start > 0` and `end < total - 1`, returns `[18, 17, 19, 16, 20, 15, 21]`
*/
export function genSeq(start: number, end: number, total: number) {
start = Math.max(start, 0);
end = Math.min(end, Math.max(total - 1, 0));
Expand Down
Loading

0 comments on commit 266724b

Please sign in to comment.