7
7
} from '@ant-design/icons' ;
8
8
import { useIntl } from '@umijs/max' ;
9
9
import { Button , Slider , Tooltip } from 'antd' ;
10
+ import dayjs from 'dayjs' ;
10
11
import _ from 'lodash' ;
11
12
import React , { useCallback , useEffect , useRef , useState } from 'react' ;
12
13
import IconFont from '../icon-font' ;
@@ -25,6 +26,8 @@ type CanvasImageEditorProps = {
25
26
imageStatus : {
26
27
isOriginal : boolean ;
27
28
isResetNeeded : boolean ;
29
+ width : number ;
30
+ height : number ;
28
31
} ;
29
32
} ;
30
33
@@ -244,7 +247,7 @@ const CanvasImageEditor: React.FC<CanvasImageEditorProps> = ({
244
247
const mask = generateMask ( ) ;
245
248
246
249
const link = document . createElement ( 'a' ) ;
247
- link . download = 'mask .png' ;
250
+ link . download = `mask_ ${ dayjs ( ) . format ( 'YYYYMMDDHHmmss' ) } .png` ;
248
251
link . href = mask || '' ;
249
252
link . click ( ) ;
250
253
} , [ generateMask ] ) ;
@@ -494,15 +497,37 @@ const CanvasImageEditor: React.FC<CanvasImageEditorProps> = ({
494
497
redrawStrokes ( newStrokes ) ;
495
498
} ;
496
499
497
- const download = ( ) => {
500
+ const downloadOriginImage = ( ) => {
501
+ console . log ( 'Downloading original image' , imageStatus ) ;
498
502
const canvas = canvasRef . current ! ;
499
503
const link = document . createElement ( 'a' ) ;
500
- link . download = 'image .png' ;
504
+ link . download = `image_ ${ dayjs ( ) . format ( 'YYYYMMDDHHmmss' ) } .png` ;
501
505
link . href = canvas . toDataURL ( 'image/png' ) ;
502
506
link . click ( ) ;
503
507
link . remove ( ) ;
504
508
} ;
505
509
510
+ const downloadNewImage = ( ) => {
511
+ const url = imageSrc || '' ;
512
+ const filename = `${ imageStatus . width } x${ imageStatus . height } _${ dayjs ( ) . format ( 'YYYYMMDDHHmmss' ) } .png` ;
513
+
514
+ const link = document . createElement ( 'a' ) ;
515
+ link . href = url ;
516
+ link . download = filename ;
517
+ document . body . appendChild ( link ) ;
518
+ link . click ( ) ;
519
+ link . remove ( ) ;
520
+ } ;
521
+
522
+ const download = ( ) => {
523
+ console . log ( 'Downloading image:' , imageStatus ) ;
524
+ if ( imageStatus . isOriginal ) {
525
+ downloadOriginImage ( ) ;
526
+ } else {
527
+ downloadNewImage ( ) ;
528
+ }
529
+ } ;
530
+
506
531
const drawImage = useCallback ( async ( ) => {
507
532
if ( ! containerRef . current || ! canvasRef . current ) return ;
508
533
return new Promise < void > ( ( resolve ) => {
0 commit comments