22import Data from '../BootstrapBlazor/modules/data.js'
33import { addLink } from '../BootstrapBlazor/modules/utility.js'
44
5- export async function init ( id , options ) {
5+ export async function init ( id , invoke , options ) {
66 await addLink ( "./_content/BootstrapBlazor.ImageCropper/cropper.bundle.css" ) ;
77
88 const el = document . getElementById ( id ) ;
@@ -11,9 +11,9 @@ export async function init(id, options) {
1111 }
1212
1313 const image = el . querySelector ( ".bb-cropper-image" ) ;
14- const cropper = new Cropper ( image , getOptions ( options ) ) ;
14+ const cropper = new Cropper ( image , getOptions ( options . options ) ) ;
1515
16- Data . set ( id , cropper ) ;
16+ Data . set ( id , { el , invoke , options , cropper } ) ;
1717}
1818
1919const getOptions = op => {
@@ -27,86 +27,110 @@ const getOptions = op => {
2727}
2828
2929export function dispose ( id ) {
30- const cropper = Data . get ( id ) ;
30+ const ic = Data . get ( id ) ;
3131 Data . remove ( id ) ;
3232
33- if ( cropper != null ) {
34- cropper . destroy ( ) ;
33+ if ( ic != null ) {
34+ const { cropper } = ic ;
35+ if ( cropper ) {
36+ cropper . destroy ( ) ;
37+ }
3538 }
3639}
3740
3841export function crop ( id ) {
3942 let ret = null ;
40- const cropper = Data . get ( id ) ;
41- if ( cropper != null ) {
42- const { isRound } = cropper . options ;
43- cropper . crop ( ) ;
44- let resultData = cropper . getCroppedCanvas ( ) ;
45- if ( isRound ) {
46- resultData = getRoundCanvas ( resultData ) ;
43+ const ic = Data . get ( id ) ;
44+ if ( ic != null ) {
45+ const { cropper, options } = ic ;
46+ if ( cropper !== null ) {
47+ cropper . crop ( ) ;
48+ let resultData = cropper . getCroppedCanvas ( ) ;
49+
50+ const { isRound } = options . options ;
51+ if ( isRound ) {
52+ resultData = getRoundCanvas ( resultData ) ;
53+ }
54+ ret = resultData . toDataURL ( ) ;
55+ resultData = null ;
4756 }
48- ret = resultData . toDataURL ( ) ;
49- resultData = null ;
5057 }
5158 return ret ;
5259}
5360
5461export function replace ( id , url ) {
55- const cropper = Data . get ( id ) ;
56- if ( cropper != null ) {
57- cropper . replace ( url ) ;
62+ const ic = Data . get ( id ) ;
63+ if ( ic != null ) {
64+ const { cropper } = ic ;
65+ if ( cropper ) {
66+ cropper . replace ( url ) ;
67+ }
5868 }
5969}
6070
6171export function reset ( id ) {
62- const cropper = Data . get ( id ) ;
63- if ( cropper != null ) {
64- cropper . reset ( ) ;
72+ const ic = Data . get ( id ) ;
73+ if ( ic != null ) {
74+ const { cropper } = ic ;
75+ if ( cropper ) {
76+ cropper . reset ( ) ;
77+ }
6578 }
6679}
6780
6881export function setDragMode ( id , mode ) {
69- const cropper = Data . get ( id ) ;
70- if ( cropper != null ) {
71- cropper . setDragMode ( mode ) ;
82+ const ic = Data . get ( id ) ;
83+ if ( ic != null ) {
84+ const { cropper } = ic ;
85+ if ( cropper ) {
86+ cropper . setDragMode ( mode ) ;
87+ }
7288 }
7389}
7490
7591export function rotate ( id , angle ) {
76- const cropper = Data . get ( id ) ;
77- if ( cropper != null ) {
78- cropper . rotate ( angle ) ;
92+ const ic = Data . get ( id ) ;
93+ if ( ic != null ) {
94+ const { cropper } = ic ;
95+ if ( cropper ) {
96+ cropper . rotate ( angle ) ;
97+ }
7998 }
8099}
81100
82101export function clear ( id ) {
83- const cropper = Data . get ( id ) ;
84- if ( cropper != null ) {
85- cropper . clear ( ) ;
102+ const ic = Data . get ( id ) ;
103+ if ( ic != null ) {
104+ const { cropper } = ic ;
105+ if ( cropper ) {
106+ cropper . clear ( ) ;
107+ }
86108 }
87109}
88110
89111export async function enable ( id ) {
90- const cropper = Data . get ( id ) ;
91- if ( cropper != null ) {
92- cropper . enable ( ) ;
93- }
94-
95- const el = document . getElementById ( id ) ;
96- if ( el ) {
97- el . classList . remove ( "disabled" ) ;
112+ const ic = Data . get ( id ) ;
113+ if ( ic != null ) {
114+ const { el, cropper } = ic ;
115+ if ( cropper ) {
116+ cropper . enable ( ) ;
117+ }
118+ if ( el ) {
119+ el . classList . remove ( "disabled" ) ;
120+ }
98121 }
99122}
100123
101124export async function disable ( id ) {
102- const cropper = Data . get ( id ) ;
103- if ( cropper != null ) {
104- cropper . disable ( ) ;
105- }
106-
107- const el = document . getElementById ( id ) ;
108- if ( el ) {
109- el . classList . add ( "disabled" ) ;
125+ const ic = Data . get ( id ) ;
126+ if ( ic != null ) {
127+ const { el, cropper } = ic ;
128+ if ( cropper ) {
129+ cropper . disable ( ) ;
130+ }
131+ if ( el ) {
132+ el . classList . add ( "disabled" ) ;
133+ }
110134 }
111135}
112136
0 commit comments