mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-11 05:58:56 +08:00
refactor(editor): unify directories naming (#11516)
**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`
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
export * from './insert-embed-iframe-with-url';
|
||||
export * from './insert-empty-embed-iframe';
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
import {
|
||||
EdgelessCRUDIdentifier,
|
||||
SurfaceBlockComponent,
|
||||
} from '@blocksuite/affine-block-surface';
|
||||
import { EmbedIframeService } from '@blocksuite/affine-shared/services';
|
||||
import { Bound, Vec } from '@blocksuite/global/gfx';
|
||||
import {
|
||||
BlockSelection,
|
||||
type Command,
|
||||
SurfaceSelection,
|
||||
TextSelection,
|
||||
} from '@blocksuite/std';
|
||||
import { GfxControllerIdentifier } from '@blocksuite/std/gfx';
|
||||
|
||||
import {
|
||||
EMBED_IFRAME_DEFAULT_HEIGHT_IN_SURFACE,
|
||||
EMBED_IFRAME_DEFAULT_WIDTH_IN_SURFACE,
|
||||
} from '../consts';
|
||||
|
||||
export const insertEmbedIframeWithUrlCommand: Command<
|
||||
{ url: string },
|
||||
{ blockId: string; flavour: string }
|
||||
> = (ctx, next) => {
|
||||
const { url, std } = ctx;
|
||||
const embedIframeService = std.get(EmbedIframeService);
|
||||
if (!embedIframeService || !embedIframeService.canEmbed(url)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const config = embedIframeService.getConfig(url);
|
||||
if (!config) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { host } = std;
|
||||
const selectionManager = host.selection;
|
||||
|
||||
let selectedBlockId: string | undefined;
|
||||
const textSelection = selectionManager.find(TextSelection);
|
||||
const blockSelection = selectionManager.find(BlockSelection);
|
||||
const surfaceSelection = selectionManager.find(SurfaceSelection);
|
||||
if (textSelection) {
|
||||
selectedBlockId = textSelection.blockId;
|
||||
} else if (blockSelection) {
|
||||
selectedBlockId = blockSelection.blockId;
|
||||
} else if (surfaceSelection && surfaceSelection.editing) {
|
||||
selectedBlockId = surfaceSelection.blockId;
|
||||
}
|
||||
|
||||
const flavour = 'affine:embed-iframe';
|
||||
const props: Record<string, unknown> = { url };
|
||||
// When there is a selected block, it means that the selection is in note or edgeless text
|
||||
// we should insert the embed iframe block after the selected block and only need the url prop
|
||||
let newBlockId: string | undefined;
|
||||
if (selectedBlockId) {
|
||||
const block = host.view.getBlock(selectedBlockId);
|
||||
if (!block) return;
|
||||
const parent = host.doc.getParent(block.model);
|
||||
if (!parent) return;
|
||||
const index = parent.children.indexOf(block.model);
|
||||
newBlockId = host.doc.addBlock(flavour, props, parent, index + 1);
|
||||
} else {
|
||||
// When there is no selected block and in edgeless mode
|
||||
// We should insert the embed iframe block to surface
|
||||
// It means that not only the url prop but also the xywh prop is needed
|
||||
const rootId = std.store.root?.id;
|
||||
if (!rootId) return;
|
||||
const edgelessRoot = std.view.getBlock(rootId);
|
||||
if (!edgelessRoot) return;
|
||||
|
||||
const gfx = std.get(GfxControllerIdentifier);
|
||||
const crud = std.get(EdgelessCRUDIdentifier);
|
||||
|
||||
gfx.viewport.smoothZoom(1);
|
||||
const surfaceBlock = gfx.surfaceComponent;
|
||||
if (!(surfaceBlock instanceof SurfaceBlockComponent)) return;
|
||||
|
||||
const options = config.options;
|
||||
const { widthInSurface, heightInSurface } = options ?? {};
|
||||
const width = widthInSurface ?? EMBED_IFRAME_DEFAULT_WIDTH_IN_SURFACE;
|
||||
const height = heightInSurface ?? EMBED_IFRAME_DEFAULT_HEIGHT_IN_SURFACE;
|
||||
const center = Vec.toVec(surfaceBlock.renderer.viewport.center);
|
||||
const xywh = Bound.fromCenter(center, width, height).serialize();
|
||||
newBlockId = crud.addBlock(
|
||||
flavour,
|
||||
{
|
||||
...props,
|
||||
xywh,
|
||||
},
|
||||
surfaceBlock.model
|
||||
);
|
||||
|
||||
gfx.tool.setTool(
|
||||
// @ts-expect-error FIXME: resolve after gfx tool refactor
|
||||
'default'
|
||||
);
|
||||
|
||||
gfx.selection.set({
|
||||
elements: [newBlockId],
|
||||
editing: false,
|
||||
});
|
||||
}
|
||||
|
||||
if (!newBlockId) {
|
||||
return;
|
||||
}
|
||||
|
||||
next({ blockId: newBlockId, flavour });
|
||||
};
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
import type { EmbedIframeBlockProps } from '@blocksuite/affine-model';
|
||||
import type { Command } from '@blocksuite/std';
|
||||
import type { BlockModel } from '@blocksuite/store';
|
||||
|
||||
import type { EmbedLinkInputPopupOptions } from '../components/embed-iframe-link-input-popup';
|
||||
import { EmbedIframeBlockComponent } from '../embed-iframe-block';
|
||||
|
||||
export const insertEmptyEmbedIframeCommand: Command<
|
||||
{
|
||||
place?: 'after' | 'before';
|
||||
removeEmptyLine?: boolean;
|
||||
selectedModels?: BlockModel[];
|
||||
linkInputPopupOptions?: EmbedLinkInputPopupOptions;
|
||||
},
|
||||
{
|
||||
insertedEmbedIframeBlockId: Promise<string>;
|
||||
}
|
||||
> = (ctx, next) => {
|
||||
const { selectedModels, place, removeEmptyLine, std, linkInputPopupOptions } =
|
||||
ctx;
|
||||
if (!selectedModels?.length) return;
|
||||
|
||||
const targetModel =
|
||||
place === 'before'
|
||||
? selectedModels[0]
|
||||
: selectedModels[selectedModels.length - 1];
|
||||
|
||||
const embedIframeBlockProps: Partial<EmbedIframeBlockProps> & {
|
||||
flavour: 'affine:embed-iframe';
|
||||
} = {
|
||||
flavour: 'affine:embed-iframe',
|
||||
};
|
||||
|
||||
const result = std.store.addSiblingBlocks(
|
||||
targetModel,
|
||||
[embedIframeBlockProps],
|
||||
place
|
||||
);
|
||||
if (result.length === 0) return;
|
||||
|
||||
if (removeEmptyLine && targetModel.text?.length === 0) {
|
||||
std.store.deleteBlock(targetModel);
|
||||
}
|
||||
|
||||
next({
|
||||
insertedEmbedIframeBlockId: std.host.updateComplete.then(async () => {
|
||||
const blockComponent = std.view.getBlock(result[0]);
|
||||
if (blockComponent instanceof EmbedIframeBlockComponent) {
|
||||
await blockComponent.updateComplete;
|
||||
blockComponent.toggleLinkInputPopup(linkInputPopupOptions);
|
||||
}
|
||||
return result[0];
|
||||
}),
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user