Files
AFFiNE-Mirror/blocksuite/affine/blocks/bookmark/src/utils.ts
donteatfriedrice 0d518adc5b refactor(editor): add cache extension for link preview service (#12196)
Closes: [BS-2578](https://linear.app/affine-design/issue/BS-2578/优化-footnote-预览的逻辑:支持缓存结果,避免重复-loading)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Introduced a link preview caching mechanism, enabling faster and more efficient reuse of link preview data across the app.
  - Added a feature flag for enabling or disabling link preview cache, configurable through workspace experimental settings.
  - Enhanced localization with new entries describing the link preview cache feature.

- **Improvements**
  - Updated link preview service architecture for better extensibility and maintainability.
  - Improved integration of feature flags throughout chat and rendering components.

- **Bug Fixes**
  - Fixed tooltip formatting for footnote URLs.

- **Chores**
  - Updated dependencies and localization completeness tracking.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-13 05:11:34 +00:00

48 lines
1.2 KiB
TypeScript

import { LinkPreviewServiceIdentifier } from '@blocksuite/affine-shared/services';
import { isAbortError } from '@blocksuite/affine-shared/utils';
import type { BookmarkBlockComponent } from './bookmark-block.js';
export async function refreshBookmarkUrlData(
bookmarkElement: BookmarkBlockComponent,
signal?: AbortSignal
) {
let title = null,
description = null,
icon = null,
image = null;
try {
bookmarkElement.loading = true;
const linkPreviewer = bookmarkElement.std.get(LinkPreviewServiceIdentifier);
const bookmarkUrlData = await linkPreviewer.query(
bookmarkElement.model.props.url,
signal
);
title = bookmarkUrlData.title ?? null;
description = bookmarkUrlData.description ?? null;
icon = bookmarkUrlData.icon ?? null;
image = bookmarkUrlData.image ?? null;
if (!title && !description && !icon && !image) {
bookmarkElement.error = true;
}
if (signal?.aborted) return;
bookmarkElement.store.updateBlock(bookmarkElement.model, {
title,
description,
icon,
image,
});
} catch (error) {
if (signal?.aborted || isAbortError(error)) return;
throw error;
} finally {
bookmarkElement.loading = false;
}
}