Files
AFFiNE-Mirror/blocksuite/affine/blocks/image/src/commands/insert-images.ts
T
fundon 85e40e4026 refactor(editor): simplify attachment and image upload handling (#11987)
Closes: [BS-3303](https://linear.app/affine-design/issue/BS-3303/改進-pack-attachment-props-流程)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Enhanced attachment and image uploads with improved file size validation and clearer notifications.
  - Upload telemetry tracking added for attachments to monitor upload success or failure.

- **Refactor**
  - Streamlined and unified the process of adding attachments and images, making uploads more reliable and efficient.
  - Parameter names updated for clarity across attachment and image insertion features.

- **Documentation**
  - Updated API documentation to reflect parameter name changes for consistency.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-04-28 07:03:30 +00:00

41 lines
1.0 KiB
TypeScript

import { getImageFilesFromLocal } from '@blocksuite/affine-shared/utils';
import type { Command } from '@blocksuite/std';
import type { BlockModel } from '@blocksuite/store';
import { addSiblingImageBlocks } from '../utils';
export const insertImagesCommand: Command<
{
selectedModels?: BlockModel[];
removeEmptyLine?: boolean;
placement?: 'after' | 'before';
},
{
insertedImageIds: Promise<string[]>;
}
> = (ctx, next) => {
const { selectedModels, placement, removeEmptyLine, std } = ctx;
if (!selectedModels?.length) return;
const targetModel =
placement === 'before'
? selectedModels[0]
: selectedModels[selectedModels.length - 1];
return next({
insertedImageIds: getImageFilesFromLocal()
.then(files => addSiblingImageBlocks(std, files, targetModel, placement))
.then(result => {
if (
result.length &&
removeEmptyLine &&
targetModel.text?.length === 0
) {
std.store.deleteBlock(targetModel);
}
return result;
}),
});
};