mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-25 02:13:00 +08:00
feat(editor): add resource controller (#12121)
Closes: [BS-3398](https://linear.app/affine-design/issue/BS-3398/实现资源控制器) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Introduced a ResourceController for centralized and reactive management of attachment resource states, improving error handling, loading indicators, and UI state resolution for attachments. - Added public access to resource management utilities via new export paths. - **Refactor** - Streamlined attachment state management by replacing manual state tracking with the new ResourceController, simplifying code and enhancing maintainability. - Updated rendering logic for attachments to use unified state objects for clearer UI feedback. - Centralized blob URL creation and download state management within the ResourceController. - **Chores** - Updated dependencies and internal references to reflect the new resource management approach, ensuring consistency across packages. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -41,9 +41,9 @@ export async function getAttachmentBlob(model: AttachmentBlockModel) {
|
||||
* the download process may take a long time!
|
||||
*/
|
||||
export function downloadAttachmentBlob(block: AttachmentBlockComponent) {
|
||||
const { host, model, blobUrl, blobState$ } = block;
|
||||
const { host, model, blobUrl, resourceController } = block;
|
||||
|
||||
if (blobState$.peek().downloading) {
|
||||
if (resourceController.state$.peek().downloading) {
|
||||
toast(host, 'Download in progress...');
|
||||
return;
|
||||
}
|
||||
@@ -56,7 +56,7 @@ export function downloadAttachmentBlob(block: AttachmentBlockComponent) {
|
||||
return;
|
||||
}
|
||||
|
||||
block.updateBlobState({ downloading: true });
|
||||
resourceController.updateState({ downloading: true });
|
||||
|
||||
toast(host, `Downloading ${shortName}`);
|
||||
|
||||
@@ -67,34 +67,24 @@ export function downloadAttachmentBlob(block: AttachmentBlockComponent) {
|
||||
tmpLink.dispatchEvent(event);
|
||||
tmpLink.remove();
|
||||
|
||||
block.updateBlobState({ downloading: false });
|
||||
resourceController.updateState({ downloading: false });
|
||||
}
|
||||
|
||||
export async function refreshData(
|
||||
std: BlockStdScope,
|
||||
block: AttachmentBlockComponent
|
||||
) {
|
||||
export async function refreshData(block: AttachmentBlockComponent) {
|
||||
const model = block.model;
|
||||
const sourceId = model.props.sourceId$.peek();
|
||||
if (!sourceId) return;
|
||||
|
||||
const blobUrl = block.blobUrl;
|
||||
if (blobUrl) {
|
||||
URL.revokeObjectURL(blobUrl);
|
||||
block.blobUrl = null;
|
||||
}
|
||||
|
||||
let blob = await std.store.blobSync.get(sourceId);
|
||||
if (!blob) {
|
||||
block.updateBlobState({ errorMessage: 'File not found' });
|
||||
return;
|
||||
}
|
||||
|
||||
const type = model.props.type$.peek();
|
||||
|
||||
blob = new Blob([blob], { type });
|
||||
const url = await block.resourceController.createBlobUrlWith(type);
|
||||
if (!url) return;
|
||||
|
||||
block.blobUrl = URL.createObjectURL(blob);
|
||||
// Releases the previous url.
|
||||
const prevUrl = block.blobUrl;
|
||||
if (prevUrl) URL.revokeObjectURL(prevUrl);
|
||||
|
||||
block.blobUrl = url;
|
||||
}
|
||||
|
||||
export async function getFileType(file: File) {
|
||||
|
||||
Reference in New Issue
Block a user