refactor(editor): extract image block (#9309)

This commit is contained in:
Saul-Mirone
2024-12-25 13:26:29 +00:00
parent ebd97752bf
commit 5274441e14
44 changed files with 350 additions and 204 deletions
@@ -0,0 +1,9 @@
import { getImageSelectionsCommand } from '@blocksuite/affine-shared/commands';
import type { BlockCommands } from '@blocksuite/block-std';
import { insertImagesCommand } from './insert-images.js';
export const commands: BlockCommands = {
getImageSelections: getImageSelectionsCommand,
insertImages: insertImagesCommand,
};
@@ -0,0 +1,44 @@
import { getImageFilesFromLocal } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/block-std';
import { addSiblingImageBlock } from '../utils.js';
export const insertImagesCommand: Command<
'selectedModels',
'insertedImageIds',
{ removeEmptyLine?: boolean; place?: 'after' | 'before' }
> = (ctx, next) => {
const { selectedModels, place, removeEmptyLine, std } = ctx;
if (!selectedModels) return;
return next({
insertedImageIds: getImageFilesFromLocal().then(imageFiles => {
if (imageFiles.length === 0) return [];
if (selectedModels.length === 0) return [];
const targetModel =
place === 'before'
? selectedModels[0]
: selectedModels[selectedModels.length - 1];
const imageService = std.getService('affine:image');
if (!imageService) return [];
const maxFileSize = imageService.maxFileSize;
const result = addSiblingImageBlock(
std.host,
imageFiles,
maxFileSize,
targetModel,
place
);
if (removeEmptyLine && targetModel.text?.length === 0) {
std.doc.deleteBlock(targetModel);
}
return result ?? [];
}),
});
};