Skip to content

Commit 64d301b

Browse files
author
likai
committed
feat: h2c模式添加跨域图片加载失败时的处理回调
扩展h2cImgLoadErrCallback参数以支持此特性 跨域图片的加载失败时,添加相关逻辑,让其不影响截图的整体流程
1 parent 0569d93 commit 64d301b

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/lib/main-entrance/PlugInParameters.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
customToolbarType,
33
mouseEventType,
4+
screenShotType,
45
userToolbarType
56
} from "@/lib/type/ComponentType";
67

@@ -35,6 +36,7 @@ let useCustomImgSize = false;
3536
let customImgSize = { w: 0, h: 0 };
3637
// 调用者定义的工具栏数据
3738
let userToolbar: Array<customToolbarType> = [];
39+
let h2cCrossImgLoadErrFn: screenShotType["h2cImgLoadErrCallback"] | null = null;
3840
let saveCallback: ((code: number, msg: string) => void) | null = null;
3941
let saveImgTitle: string | null = null;
4042
let canvasEvents: mouseEventType | null = null;
@@ -60,6 +62,7 @@ export default class PlugInParameters {
6062
saveImgTitle = null;
6163
destroyContainer = true;
6264
userToolbar = [];
65+
h2cCrossImgLoadErrFn = null;
6366
}
6467
}
6568

@@ -223,13 +226,20 @@ export default class PlugInParameters {
223226
toolbarData.push({ ...item, id: 100 + (i + 1) });
224227
}
225228
userToolbar = toolbarData;
226-
console.log(userToolbar);
227229
}
228230

229231
public getUserToolbar() {
230232
return userToolbar;
231233
}
232234

235+
public setH2cCrossImgLoadErrFn(fn: screenShotType["h2cImgLoadErrCallback"]) {
236+
h2cCrossImgLoadErrFn = fn;
237+
}
238+
239+
public getH2cCrossImgLoadErrFn() {
240+
return h2cCrossImgLoadErrFn;
241+
}
242+
233243
public setCanvasEvents(event: mouseEventType) {
234244
canvasEvents = event;
235245
}

src/lib/split-methods/SetPlugInParameters.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export function setPlugInParameters(options: screenShotType) {
8787
plugInParameters.setUserToolbar(options.userToolbar);
8888
}
8989

90+
// h2c模式下,跨域图片加载失败时的回调函数
91+
if (options?.h2cImgLoadErrCallback) {
92+
plugInParameters.setH2cCrossImgLoadErrFn(options.h2cImgLoadErrCallback);
93+
}
94+
9095
// 处理用户定义的画布事件
9196
if (options?.canvasEvents) {
9297
plugInParameters.setCanvasEvents(options.canvasEvents);

src/lib/split-methods/drawCrossImg.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import PlugInParameters from "@/lib/main-entrance/PlugInParameters";
2+
13
export function drawCrossImg(html: Document) {
24
const promises: Promise<string>[] = [];
35
const imageNodes = html.querySelectorAll("img");
6+
const plugInParameters = new PlugInParameters();
47
imageNodes.forEach(element => {
58
const href = element.getAttribute("src");
69
if (!href) return;
@@ -21,7 +24,17 @@ export function drawCrossImg(html: Document) {
2124
element.setAttribute("src", base64);
2225
resolve("转换成功");
2326
};
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+
};
2538
if (href !== null) {
2639
img.src = href;
2740
}

src/lib/type/ComponentType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export type screenShotType = {
113113
cutInfo: positionInfoType;
114114
}) => void; // 工具栏截图确认回调
115115
closeCallback?: () => void; // 工具栏关闭回调
116+
h2cImgLoadErrCallback?: (err: Event & { imgUrl: string }) => void; // html2canvas跨域图片加载失败回调
116117
triggerCallback?: (res: {
117118
code: number;
118119
msg: string;

0 commit comments

Comments
 (0)