File tree Expand file tree Collapse file tree 4 files changed +31
-2
lines changed Expand file tree Collapse file tree 4 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 1
1
import {
2
2
customToolbarType ,
3
3
mouseEventType ,
4
+ screenShotType ,
4
5
userToolbarType
5
6
} from "@/lib/type/ComponentType" ;
6
7
@@ -35,6 +36,7 @@ let useCustomImgSize = false;
35
36
let customImgSize = { w : 0 , h : 0 } ;
36
37
// 调用者定义的工具栏数据
37
38
let userToolbar : Array < customToolbarType > = [ ] ;
39
+ let h2cCrossImgLoadErrFn : screenShotType [ "h2cImgLoadErrCallback" ] | null = null ;
38
40
let saveCallback : ( ( code : number , msg : string ) => void ) | null = null ;
39
41
let saveImgTitle : string | null = null ;
40
42
let canvasEvents : mouseEventType | null = null ;
@@ -60,6 +62,7 @@ export default class PlugInParameters {
60
62
saveImgTitle = null ;
61
63
destroyContainer = true ;
62
64
userToolbar = [ ] ;
65
+ h2cCrossImgLoadErrFn = null ;
63
66
}
64
67
}
65
68
@@ -223,13 +226,20 @@ export default class PlugInParameters {
223
226
toolbarData . push ( { ...item , id : 100 + ( i + 1 ) } ) ;
224
227
}
225
228
userToolbar = toolbarData ;
226
- console . log ( userToolbar ) ;
227
229
}
228
230
229
231
public getUserToolbar ( ) {
230
232
return userToolbar ;
231
233
}
232
234
235
+ public setH2cCrossImgLoadErrFn ( fn : screenShotType [ "h2cImgLoadErrCallback" ] ) {
236
+ h2cCrossImgLoadErrFn = fn ;
237
+ }
238
+
239
+ public getH2cCrossImgLoadErrFn ( ) {
240
+ return h2cCrossImgLoadErrFn ;
241
+ }
242
+
233
243
public setCanvasEvents ( event : mouseEventType ) {
234
244
canvasEvents = event ;
235
245
}
Original file line number Diff line number Diff line change @@ -87,6 +87,11 @@ export function setPlugInParameters(options: screenShotType) {
87
87
plugInParameters . setUserToolbar ( options . userToolbar ) ;
88
88
}
89
89
90
+ // h2c模式下,跨域图片加载失败时的回调函数
91
+ if ( options ?. h2cImgLoadErrCallback ) {
92
+ plugInParameters . setH2cCrossImgLoadErrFn ( options . h2cImgLoadErrCallback ) ;
93
+ }
94
+
90
95
// 处理用户定义的画布事件
91
96
if ( options ?. canvasEvents ) {
92
97
plugInParameters . setCanvasEvents ( options . canvasEvents ) ;
Original file line number Diff line number Diff line change
1
+ import PlugInParameters from "@/lib/main-entrance/PlugInParameters" ;
2
+
1
3
export function drawCrossImg ( html : Document ) {
2
4
const promises : Promise < string > [ ] = [ ] ;
3
5
const imageNodes = html . querySelectorAll ( "img" ) ;
6
+ const plugInParameters = new PlugInParameters ( ) ;
4
7
imageNodes . forEach ( element => {
5
8
const href = element . getAttribute ( "src" ) ;
6
9
if ( ! href ) return ;
@@ -21,7 +24,17 @@ export function drawCrossImg(html: Document) {
21
24
element . setAttribute ( "src" , base64 ) ;
22
25
resolve ( "转换成功" ) ;
23
26
} ;
24
- img . onerror = reject ;
27
+ img . onerror = function ( err ) {
28
+ const h2cCrossImgLoadErrFn = plugInParameters ?. getH2cCrossImgLoadErrFn ( ) ;
29
+ if ( h2cCrossImgLoadErrFn && typeof err != "string" ) {
30
+ h2cCrossImgLoadErrFn ( {
31
+ ...err ,
32
+ imgUrl : href
33
+ } ) ;
34
+ }
35
+ // 跨域图片加载失败时,此处不做处理
36
+ resolve ( true ) ;
37
+ } ;
25
38
if ( href !== null ) {
26
39
img . src = href ;
27
40
}
Original file line number Diff line number Diff line change @@ -113,6 +113,7 @@ export type screenShotType = {
113
113
cutInfo : positionInfoType ;
114
114
} ) => void ; // 工具栏截图确认回调
115
115
closeCallback ?: ( ) => void ; // 工具栏关闭回调
116
+ h2cImgLoadErrCallback ?: ( err : Event & { imgUrl : string } ) => void ; // html2canvas跨域图片加载失败回调
116
117
triggerCallback ?: ( res : {
117
118
code : number ;
118
119
msg : string ;
You can’t perform that action at this time.
0 commit comments