mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
Related to: [BS-3143](https://linear.app/affine-design/issue/BS-3143/更新-loading-和错误样式) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a unified resource controller for managing image and attachment resources, providing improved loading, error, and state handling. - Added a visual loading indicator overlay to image blocks for better feedback during image loading. - **Improvements** - Simplified and centralized image and attachment state management, reducing redundant properties and manual state tracking. - Updated fallback UI for image blocks with clearer titles, descriptions, and improved layout. - Enhanced batch image block creation and download handling for improved efficiency. - Refined image block accessibility with improved alt text and streamlined rendering logic. - Centralized target model selection for image insertion in AI actions. - Reordered CSS declarations without affecting styling. - Improved reactive state tracking for blob upload/download operations in mock server. - **Bug Fixes** - Improved cleanup of object URLs to prevent resource leaks. - Adjusted toolbar logic to more accurately reflect available actions based on image state. - **Tests** - Updated end-to-end tests to match new UI text and behaviors for image loading and error states. - **Chores** - Refactored internal logic and updated comments for clarity and maintainability. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
180 lines
4.1 KiB
TypeScript
180 lines
4.1 KiB
TypeScript
import type { Disposable } from '@blocksuite/global/disposable';
|
|
import type { BlobEngine, BlobState } from '@blocksuite/sync';
|
|
import { 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 ResolvedStateInfo = StateInfo & {
|
|
loading: boolean;
|
|
error: boolean;
|
|
state: StateKind;
|
|
};
|
|
|
|
export class ResourceController implements Disposable {
|
|
readonly blobUrl$ = signal<string | null>(null);
|
|
|
|
readonly state$ = signal<Partial<BlobState>>({});
|
|
|
|
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 {
|
|
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';
|
|
|
|
const { icon, title, description, loadingIcon, errorIcon } = info;
|
|
|
|
const result = {
|
|
error: hasError,
|
|
loading,
|
|
state,
|
|
icon,
|
|
title,
|
|
description,
|
|
};
|
|
|
|
if (loading) {
|
|
result.icon = loadingIcon ?? icon;
|
|
result.title = state === 'uploading' ? 'Uploading...' : 'Loading...';
|
|
} else if (hasError) {
|
|
result.icon = errorIcon ?? icon;
|
|
result.description = 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 {
|
|
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);
|
|
}
|
|
}
|