Files
AFFiNE-Mirror/blocksuite/affine/block-image/src/commands/insert-images.ts
T
2025-01-09 04:16:28 +00:00

45 lines
1.2 KiB
TypeScript

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.store.deleteBlock(targetModel);
}
return result ?? [];
}),
});
};