@@ -5,10 +5,7 @@ import type {
5
5
ImageBlockModel ,
6
6
ImageBlockProps ,
7
7
} from '@blocksuite/affine-model' ;
8
- import {
9
- FileSizeLimitService ,
10
- NativeClipboardProvider ,
11
- } from '@blocksuite/affine-shared/services' ;
8
+ import { NativeClipboardProvider } from '@blocksuite/affine-shared/services' ;
12
9
import {
13
10
downloadBlob ,
14
11
getBlockProps ,
@@ -312,6 +309,21 @@ export async function copyImageBlob(
312
309
}
313
310
}
314
311
312
+ function hasExceeded (
313
+ std : BlockStdScope ,
314
+ files : File [ ] ,
315
+ maxFileSize = std . store . blobSync . maxFileSize
316
+ ) {
317
+ const exceeded = files . some ( file => file . size > maxFileSize ) ;
318
+
319
+ if ( exceeded ) {
320
+ const size = humanFileSize ( maxFileSize , true , 0 ) ;
321
+ toast ( std . host , `You can only upload files less than ${ size } ` ) ;
322
+ }
323
+
324
+ return exceeded ;
325
+ }
326
+
315
327
export function shouldResizeImage ( node : Node , target : EventTarget | null ) {
316
328
return ! ! (
317
329
target &&
@@ -322,79 +334,51 @@ export function shouldResizeImage(node: Node, target: EventTarget | null) {
322
334
}
323
335
324
336
export function addSiblingImageBlock (
325
- editorHost : EditorHost ,
337
+ std : BlockStdScope ,
326
338
files : File [ ] ,
327
- maxFileSize : number ,
328
339
targetModel : BlockModel ,
329
340
place : 'after' | 'before' = 'after'
330
341
) {
331
- const imageFiles = files . filter ( file => file . type . startsWith ( 'image/' ) ) ;
332
- if ( ! imageFiles . length ) {
333
- return ;
334
- }
342
+ files = files . filter ( file => file . type . startsWith ( 'image/' ) ) ;
343
+ if ( ! files . length ) return ;
335
344
336
- const isSizeExceeded = imageFiles . some ( file => file . size > maxFileSize ) ;
337
- if ( isSizeExceeded ) {
338
- toast (
339
- editorHost ,
340
- `You can only upload files less than ${ humanFileSize (
341
- maxFileSize ,
342
- true ,
343
- 0
344
- ) } `
345
- ) ;
346
- return ;
347
- }
345
+ if ( hasExceeded ( std , files ) ) return ;
348
346
349
347
const imageBlockProps : Partial < ImageBlockProps > &
350
348
{
351
349
flavour : 'affine:image' ;
352
- } [ ] = imageFiles . map ( file => ( {
350
+ } [ ] = files . map ( file => ( {
353
351
flavour : 'affine:image' ,
354
352
size : file . size ,
355
353
} ) ) ;
356
354
357
- const doc = editorHost . doc ;
358
- const blockIds = doc . addSiblingBlocks ( targetModel , imageBlockProps , place ) ;
355
+ const blockIds = std . store . addSiblingBlocks (
356
+ targetModel ,
357
+ imageBlockProps ,
358
+ place
359
+ ) ;
359
360
blockIds . forEach (
360
- ( blockId , index ) =>
361
- void uploadBlobForImage ( editorHost , blockId , imageFiles [ index ] )
361
+ ( blockId , index ) => void uploadBlobForImage ( std . host , blockId , files [ index ] )
362
362
) ;
363
363
return blockIds ;
364
364
}
365
365
366
366
export function addImageBlocks (
367
- editorHost : EditorHost ,
367
+ std : BlockStdScope ,
368
368
files : File [ ] ,
369
- maxFileSize : number ,
370
369
parent ?: BlockModel | string | null ,
371
370
parentIndex ?: number
372
371
) {
373
- const imageFiles = files . filter ( file => file . type . startsWith ( 'image/' ) ) ;
374
- if ( ! imageFiles . length ) {
375
- return ;
376
- }
372
+ files = files . filter ( file => file . type . startsWith ( 'image/' ) ) ;
373
+ if ( ! files . length ) return ;
377
374
378
- const isSizeExceeded = imageFiles . some ( file => file . size > maxFileSize ) ;
379
- if ( isSizeExceeded ) {
380
- toast (
381
- editorHost ,
382
- `You can only upload files less than ${ humanFileSize (
383
- maxFileSize ,
384
- true ,
385
- 0
386
- ) } `
387
- ) ;
388
- return ;
389
- }
375
+ if ( hasExceeded ( std , files ) ) return ;
390
376
391
- const doc = editorHost . doc ;
392
- const blockIds = imageFiles . map ( file =>
393
- doc . addBlock ( 'affine:image' , { size : file . size } , parent , parentIndex )
377
+ const blockIds = files . map ( file =>
378
+ std . store . addBlock ( 'affine:image' , { size : file . size } , parent , parentIndex )
394
379
) ;
395
380
blockIds . forEach (
396
- ( blockId , index ) =>
397
- void uploadBlobForImage ( editorHost , blockId , imageFiles [ index ] )
381
+ ( blockId , index ) => void uploadBlobForImage ( std . host , blockId , files [ index ] )
398
382
) ;
399
383
return blockIds ;
400
384
}
@@ -445,25 +429,12 @@ export async function addImages(
445
429
transformPoint ?: boolean ; // determines whether we should use `toModelCoord` to convert the point
446
430
}
447
431
) : Promise < string [ ] > {
448
- const imageFiles = [ ...files ] . filter ( file => file . type . startsWith ( 'image/' ) ) ;
449
- if ( ! imageFiles . length ) return [ ] ;
432
+ files = [ ...files ] . filter ( file => file . type . startsWith ( 'image/' ) ) ;
433
+ if ( ! files . length ) return [ ] ;
450
434
451
- const gfx = std . get ( GfxControllerIdentifier ) ;
452
-
453
- const maxFileSize = std . store . get ( FileSizeLimitService ) . maxFileSize ;
454
- const isSizeExceeded = imageFiles . some ( file => file . size > maxFileSize ) ;
455
- if ( isSizeExceeded ) {
456
- toast (
457
- std . host ,
458
- `You can only upload files less than ${ humanFileSize (
459
- maxFileSize ,
460
- true ,
461
- 0
462
- ) } `
463
- ) ;
464
- return [ ] ;
465
- }
435
+ if ( hasExceeded ( std , files ) ) return [ ] ;
466
436
437
+ const gfx = std . get ( GfxControllerIdentifier ) ;
467
438
const { point, maxWidth, transformPoint = true } = options ;
468
439
let { x, y } = gfx . viewport . center ;
469
440
if ( point ) {
@@ -476,11 +447,11 @@ export async function addImages(
476
447
477
448
const dropInfos : { point : Point ; blockId : string } [ ] = [ ] ;
478
449
const IMAGE_STACK_GAP = 32 ;
479
- const isMultipleFiles = imageFiles . length > 1 ;
450
+ const isMultipleFiles = files . length > 1 ;
480
451
const inTopLeft = isMultipleFiles ? true : false ;
481
452
482
453
// create image cards without image data
483
- imageFiles . forEach ( ( file , index ) => {
454
+ files . forEach ( ( file , index ) => {
484
455
const point = new Point (
485
456
x + index * IMAGE_STACK_GAP ,
486
457
y + index * IMAGE_STACK_GAP
@@ -500,7 +471,7 @@ export async function addImages(
500
471
} ) ;
501
472
502
473
// upload image data and update the image model
503
- const uploadPromises = imageFiles . map ( async ( file , index ) => {
474
+ const uploadPromises = files . map ( async ( file , index ) => {
504
475
const { point, blockId } = dropInfos [ index ] ;
505
476
const block = std . store . getBlock ( blockId ) ;
506
477
const imageSize = await readImageSize ( file ) ;
0 commit comments