mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-29 16:19:43 +08:00
1f45cc5dec
**Directory Structure Changes** - Renamed multiple block-related directories by removing the "block-" prefix: - `block-attachment` → `attachment` - `block-bookmark` → `bookmark` - `block-callout` → `callout` - `block-code` → `code` - `block-data-view` → `data-view` - `block-database` → `database` - `block-divider` → `divider` - `block-edgeless-text` → `edgeless-text` - `block-embed` → `embed`
115 lines
2.9 KiB
TypeScript
115 lines
2.9 KiB
TypeScript
import type {
|
|
EmbedYoutubeBlockUrlData,
|
|
EmbedYoutubeModel,
|
|
} from '@blocksuite/affine-model';
|
|
import type { LinkPreviewerService } from '@blocksuite/affine-shared/services';
|
|
import { isAbortError } from '@blocksuite/affine-shared/utils';
|
|
|
|
import type { EmbedYoutubeBlockComponent } from './embed-youtube-block.js';
|
|
|
|
export async function queryEmbedYoutubeData(
|
|
embedYoutubeModel: EmbedYoutubeModel,
|
|
linkPreviewer: LinkPreviewerService,
|
|
signal?: AbortSignal
|
|
): Promise<Partial<EmbedYoutubeBlockUrlData>> {
|
|
const url = embedYoutubeModel.props.url;
|
|
|
|
const [videoOpenGraphData, videoOEmbedData] = await Promise.all([
|
|
linkPreviewer.query(url, signal),
|
|
queryYoutubeOEmbedData(url, signal),
|
|
]);
|
|
|
|
const youtubeEmbedData: Partial<EmbedYoutubeBlockUrlData> = {
|
|
...videoOpenGraphData,
|
|
...videoOEmbedData,
|
|
};
|
|
|
|
if (youtubeEmbedData.creatorUrl) {
|
|
const creatorOpenGraphData = await linkPreviewer.query(
|
|
youtubeEmbedData.creatorUrl,
|
|
signal
|
|
);
|
|
youtubeEmbedData.creatorImage = creatorOpenGraphData.image;
|
|
}
|
|
|
|
return youtubeEmbedData;
|
|
}
|
|
|
|
export async function queryYoutubeOEmbedData(
|
|
url: string,
|
|
signal?: AbortSignal
|
|
): Promise<Partial<EmbedYoutubeBlockUrlData>> {
|
|
let youtubeOEmbedData: Partial<EmbedYoutubeBlockUrlData> = {};
|
|
|
|
const oEmbedUrl = `https://youtube.com/oembed?url=${url}&format=json`;
|
|
|
|
const oEmbedResponse = await fetch(oEmbedUrl, { signal }).catch(() => null);
|
|
if (oEmbedResponse && oEmbedResponse.ok) {
|
|
const oEmbedJson = await oEmbedResponse.json();
|
|
const { title, author_name, author_url } = oEmbedJson;
|
|
|
|
youtubeOEmbedData = {
|
|
title,
|
|
creator: author_name,
|
|
creatorUrl: author_url,
|
|
};
|
|
}
|
|
|
|
return youtubeOEmbedData;
|
|
}
|
|
|
|
export async function refreshEmbedYoutubeUrlData(
|
|
embedYoutubeElement: EmbedYoutubeBlockComponent,
|
|
signal?: AbortSignal
|
|
): Promise<void> {
|
|
let image = null,
|
|
title = null,
|
|
description = null,
|
|
creator = null,
|
|
creatorUrl = null,
|
|
creatorImage = null;
|
|
|
|
try {
|
|
embedYoutubeElement.loading = true;
|
|
|
|
// TODO(@mirone): remove service
|
|
const queryUrlData = embedYoutubeElement.service?.queryUrlData;
|
|
if (!queryUrlData) {
|
|
console.error(
|
|
`Trying to refresh youtube url data, but the queryUrlData is not found.`
|
|
);
|
|
return;
|
|
}
|
|
|
|
const youtubeUrlData = await queryUrlData(
|
|
embedYoutubeElement.model,
|
|
signal
|
|
);
|
|
|
|
({
|
|
image = null,
|
|
title = null,
|
|
description = null,
|
|
creator = null,
|
|
creatorUrl = null,
|
|
creatorImage = null,
|
|
} = youtubeUrlData);
|
|
|
|
if (signal?.aborted) return;
|
|
|
|
embedYoutubeElement.doc.updateBlock(embedYoutubeElement.model, {
|
|
image,
|
|
title,
|
|
description,
|
|
creator,
|
|
creatorUrl,
|
|
creatorImage,
|
|
});
|
|
} catch (error) {
|
|
if (signal?.aborted || isAbortError(error)) return;
|
|
throw error;
|
|
} finally {
|
|
embedYoutubeElement.loading = false;
|
|
}
|
|
}
|