Files
AFFiNE-Mirror/blocksuite/affine/blocks/embed/src/embed-loom-block/utils.ts
T
Saul-Mirone 388641bc89 refactor(editor): rename doc to store on block components (#12173)
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **Refactor**
  - Unified internal data access by replacing all references from `doc` to `store` across all components, blocks, widgets, and utilities. This affects how readonly state, block operations, and service retrieval are handled throughout the application.
- **Tests**
  - Updated all test utilities and test cases to use `store` instead of `doc` for document-related operations.
- **Chores**
  - Updated context providers and property names to reflect the change from `doc` to `store` for improved consistency and maintainability.

No user-facing features or behaviors have changed.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-08 01:01:05 +00:00

76 lines
1.9 KiB
TypeScript

import type {
EmbedLoomBlockUrlData,
EmbedLoomModel,
} from '@blocksuite/affine-model';
import { isAbortError } from '@blocksuite/affine-shared/utils';
import type { EmbedLoomBlockComponent } from './embed-loom-block.js';
const LoomOEmbedEndpoint = 'https://www.loom.com/v1/oembed';
export async function queryEmbedLoomData(
embedLoomModel: EmbedLoomModel,
signal?: AbortSignal
): Promise<Partial<EmbedLoomBlockUrlData>> {
const url = embedLoomModel.props.url;
const loomEmbedData: Partial<EmbedLoomBlockUrlData> =
await queryLoomOEmbedData(url, signal);
return loomEmbedData;
}
export async function queryLoomOEmbedData(
url: string,
signal?: AbortSignal
): Promise<Partial<EmbedLoomBlockUrlData>> {
let loomOEmbedData: Partial<EmbedLoomBlockUrlData> = {};
const oEmbedUrl = `${LoomOEmbedEndpoint}?url=${url}`;
const oEmbedResponse = await fetch(oEmbedUrl, { signal }).catch(() => null);
if (oEmbedResponse && oEmbedResponse.ok) {
const oEmbedJson = await oEmbedResponse.json();
const { title, description, thumbnail_url: image } = oEmbedJson;
loomOEmbedData = {
title,
description,
image,
};
}
return loomOEmbedData;
}
export async function refreshEmbedLoomUrlData(
embedLoomElement: EmbedLoomBlockComponent,
signal?: AbortSignal
): Promise<void> {
let title = null,
description = null,
image = null;
try {
embedLoomElement.loading = true;
const queryUrlData = embedLoomElement.service?.queryUrlData;
if (!queryUrlData) return;
const loomUrlData = await queryUrlData(embedLoomElement.model);
({ title = null, description = null, image = null } = loomUrlData);
if (signal?.aborted) return;
embedLoomElement.store.updateBlock(embedLoomElement.model, {
title,
description,
image,
});
} catch (error) {
if (signal?.aborted || isAbortError(error)) return;
} finally {
embedLoomElement.loading = false;
}
}