mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-24 13:58:50 +08:00
b3f0f38b41
Closes: [BS-3438](https://linear.app/affine-design/issue/BS-3438/attachment-embed-view-中的-status-组件) Closes: [BS-3447](https://linear.app/affine-design/issue/BS-3447/触发-litportal-re-render) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a visual status indicator for embedded attachments with reload capability. - Added a new resource status component to display error messages and reload actions. - **Improvements** - Enhanced attachment rendering flow with reactive state and unified embed handling. - Simplified resource state and blob URL lifecycle management. - Added status visibility flags for PDF and video embeds. - **Bug Fixes** - Improved error handling and refresh support for embedded content including PDFs, videos, and audio. - **Style** - Added styles for the attachment embed status indicator positioning. - **Refactor** - Streamlined attachment and resource controller implementations for better maintainability. - **Tests** - Added end-to-end test verifying PDF viewer reload and re-rendering in embed mode. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
201 lines
4.5 KiB
TypeScript
201 lines
4.5 KiB
TypeScript
import type { Disposable } from '@blocksuite/global/disposable';
|
|
import type { BlobEngine, BlobState } from '@blocksuite/sync';
|
|
import {
|
|
computed,
|
|
effect,
|
|
type ReadonlySignal,
|
|
signal,
|
|
} from '@preact/signals-core';
|
|
import type { TemplateResult } from 'lit-html';
|
|
|
|
export type ResourceKind = 'Blob' | 'File' | 'Image';
|
|
|
|
export type StateKind =
|
|
| 'loading'
|
|
| 'uploading'
|
|
| 'error'
|
|
| 'error:oversize'
|
|
| 'none';
|
|
|
|
export type StateInfo = {
|
|
icon: TemplateResult;
|
|
title?: string;
|
|
description?: string;
|
|
};
|
|
|
|
export type ResolvedStateInfoPart = {
|
|
loading: boolean;
|
|
error: boolean;
|
|
state: StateKind;
|
|
url: string | null;
|
|
};
|
|
|
|
export type ResolvedStateInfo = StateInfo & ResolvedStateInfoPart;
|
|
|
|
export class ResourceController implements Disposable {
|
|
readonly blobUrl$ = signal<string | null>(null);
|
|
|
|
readonly state$ = signal<Partial<BlobState>>({});
|
|
|
|
readonly resolvedState$ = computed<ResolvedStateInfoPart>(() => {
|
|
const url = this.blobUrl$.value;
|
|
const {
|
|
uploading = false,
|
|
downloading = false,
|
|
overSize = false,
|
|
errorMessage,
|
|
} = this.state$.value;
|
|
const hasExceeded = overSize;
|
|
const hasError = hasExceeded || Boolean(errorMessage);
|
|
const state = this.determineState(
|
|
hasExceeded,
|
|
hasError,
|
|
uploading,
|
|
downloading
|
|
);
|
|
|
|
const loading = state === 'uploading' || state === 'loading';
|
|
|
|
return { error: hasError, loading, state, url };
|
|
});
|
|
|
|
private engine?: BlobEngine;
|
|
|
|
constructor(
|
|
readonly blobId$: ReadonlySignal<string | undefined>,
|
|
readonly kind: ResourceKind = 'File'
|
|
) {}
|
|
|
|
// This is a tradeoff, initializing `Blob Sync Engine`.
|
|
setEngine(engine: BlobEngine) {
|
|
this.engine = engine;
|
|
return this;
|
|
}
|
|
|
|
determineState(
|
|
hasExceeded: boolean,
|
|
hasError: boolean,
|
|
uploading: boolean,
|
|
downloading: boolean
|
|
): StateKind {
|
|
if (hasExceeded) return 'error:oversize';
|
|
if (hasError) return 'error';
|
|
if (uploading) return 'uploading';
|
|
if (downloading) return 'loading';
|
|
return 'none';
|
|
}
|
|
|
|
resolveStateWith(
|
|
info: {
|
|
loadingIcon: TemplateResult;
|
|
errorIcon?: TemplateResult;
|
|
} & StateInfo
|
|
): ResolvedStateInfo {
|
|
const { error, loading, state, url } = this.resolvedState$.value;
|
|
|
|
const { icon, title, description, loadingIcon, errorIcon } = info;
|
|
|
|
const result = {
|
|
error,
|
|
loading,
|
|
state,
|
|
icon,
|
|
title,
|
|
description,
|
|
url,
|
|
};
|
|
|
|
if (loading) {
|
|
result.icon = loadingIcon ?? icon;
|
|
result.title = state === 'uploading' ? 'Uploading...' : 'Loading...';
|
|
} else if (error) {
|
|
result.icon = errorIcon ?? icon;
|
|
result.description = this.state$.value.errorMessage ?? description;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
updateState(state: Partial<BlobState>) {
|
|
this.state$.value = { ...this.state$.value, ...state };
|
|
}
|
|
|
|
subscribe() {
|
|
return effect(() => {
|
|
const blobId = this.blobId$.value;
|
|
if (!blobId) return;
|
|
|
|
const blobState$ = this.engine?.blobState$(blobId);
|
|
if (!blobState$) return;
|
|
|
|
const subscription = blobState$.subscribe(state => {
|
|
let { uploading, downloading } = state;
|
|
if (state.overSize || state.errorMessage) {
|
|
uploading = false;
|
|
downloading = false;
|
|
}
|
|
|
|
this.updateState({ ...state, uploading, downloading });
|
|
});
|
|
|
|
return () => subscription.unsubscribe();
|
|
});
|
|
}
|
|
|
|
async blob() {
|
|
const blobId = this.blobId$.peek();
|
|
if (!blobId) return null;
|
|
|
|
let blob: Blob | null = null;
|
|
let errorMessage: string | null = null;
|
|
|
|
try {
|
|
if (!this.engine) {
|
|
throw new Error('Blob engine is not initialized');
|
|
}
|
|
|
|
blob = (await this.engine.get(blobId)) ?? null;
|
|
|
|
if (!blob) errorMessage = `${this.kind} not found`;
|
|
} catch (err) {
|
|
console.error(err);
|
|
errorMessage = `Failed to retrieve ${this.kind}`;
|
|
}
|
|
|
|
if (errorMessage) this.updateState({ errorMessage });
|
|
|
|
return blob;
|
|
}
|
|
|
|
async createUrlWith(type?: string) {
|
|
let blob = await this.blob();
|
|
if (!blob) return null;
|
|
|
|
if (type) blob = new Blob([blob], { type });
|
|
|
|
return URL.createObjectURL(blob);
|
|
}
|
|
|
|
async refreshUrlWith(type?: string) {
|
|
const url = await this.createUrlWith(type);
|
|
if (!url) return;
|
|
|
|
const prevUrl = this.blobUrl$.peek();
|
|
|
|
this.blobUrl$.value = url;
|
|
|
|
if (!prevUrl) return;
|
|
|
|
// Releases the previous url.
|
|
URL.revokeObjectURL(prevUrl);
|
|
}
|
|
|
|
dispose() {
|
|
const url = this.blobUrl$.peek();
|
|
if (!url) return;
|
|
|
|
// Releases the current url.
|
|
URL.revokeObjectURL(url);
|
|
}
|
|
}
|