mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-24 05:48:59 +08:00
refactor(editor): extract image block (#9309)
This commit is contained in:
@@ -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 ?? [];
|
||||
}),
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user