feat(editor): support bookmark block convert to embed iframe block (#10907)

This commit is contained in:
donteatfriedrice
2025-03-17 05:00:55 +00:00
parent 8bd4125c96
commit 6eb34d70c1

View File

@@ -1,8 +1,10 @@
import { EmbedIframeService } from '@blocksuite/affine-block-embed';
import { toast } from '@blocksuite/affine-components/toast'; import { toast } from '@blocksuite/affine-components/toast';
import { BookmarkBlockModel } from '@blocksuite/affine-model'; import { BookmarkBlockModel } from '@blocksuite/affine-model';
import { import {
ActionPlacement, ActionPlacement,
EmbedOptionProvider, EmbedOptionProvider,
FeatureFlagService,
type ToolbarAction, type ToolbarAction,
type ToolbarActionGroup, type ToolbarActionGroup,
type ToolbarModuleConfig, type ToolbarModuleConfig,
@@ -102,11 +104,21 @@ export const builtinToolbarConfig = {
); );
if (!model) return true; if (!model) return true;
const url = model.props.url;
// check if the url can be embedded as iframe block
const featureFlag = ctx.std.get(FeatureFlagService);
const embedIframeService = ctx.std.get(EmbedIframeService);
const isEmbedIframeEnabled = featureFlag.getFlag(
'enable_embed_iframe_block'
);
const canEmbedAsIframe =
isEmbedIframeEnabled && embedIframeService.canEmbed(url);
const options = ctx.std const options = ctx.std
.get(EmbedOptionProvider) .get(EmbedOptionProvider)
.getEmbedBlockOptions(model.props.url); .getEmbedBlockOptions(url);
return options?.viewType !== 'embed'; return !canEmbedAsIframe && options?.viewType !== 'embed';
}, },
run(ctx) { run(ctx) {
const model = ctx.getCurrentModelByType( const model = ctx.getCurrentModelByType(
@@ -118,7 +130,23 @@ export const builtinToolbarConfig = {
const { caption, url, style } = model.props; const { caption, url, style } = model.props;
const { parent } = model; const { parent } = model;
const index = parent?.children.indexOf(model); const index = parent?.children.indexOf(model);
if (!parent) return;
let blockId: string | undefined;
// first try to embed as iframe block
const featureFlag = ctx.std.get(FeatureFlagService);
const isEmbedIframeEnabled = featureFlag.getFlag(
'enable_embed_iframe_block'
);
const embedIframeService = ctx.std.get(EmbedIframeService);
if (isEmbedIframeEnabled && embedIframeService.canEmbed(url)) {
blockId = embedIframeService.addEmbedIframeBlock(
{ url, caption },
parent.id,
index
);
} else {
const options = ctx.std const options = ctx.std
.get(EmbedOptionProvider) .get(EmbedOptionProvider)
.getEmbedBlockOptions(url); .getEmbedBlockOptions(url);
@@ -131,7 +159,7 @@ export const builtinToolbarConfig = {
? style ? style
: styles.find(s => s !== 'vertical' && s !== 'cube'); : styles.find(s => s !== 'vertical' && s !== 'cube');
const blockId = ctx.store.addBlock( blockId = ctx.store.addBlock(
flavour, flavour,
{ {
url, url,
@@ -141,6 +169,9 @@ export const builtinToolbarConfig = {
parent, parent,
index index
); );
}
if (!blockId) return;
ctx.store.deleteBlock(model); ctx.store.deleteBlock(model);