Skip to content

Commit

Permalink
feat: h2c模式添加跨域图片加载失败时的处理回调
Browse files Browse the repository at this point in the history
扩展h2cImgLoadErrCallback参数以支持此特性
跨域图片的加载失败时,添加相关逻辑,让其不影响截图的整体流程
  • Loading branch information
likai committed Oct 16, 2024
1 parent 0569d93 commit 64d301b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/lib/main-entrance/PlugInParameters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
customToolbarType,
mouseEventType,
screenShotType,
userToolbarType
} from "@/lib/type/ComponentType";

Expand Down Expand Up @@ -35,6 +36,7 @@ let useCustomImgSize = false;
let customImgSize = { w: 0, h: 0 };
// 调用者定义的工具栏数据
let userToolbar: Array<customToolbarType> = [];
let h2cCrossImgLoadErrFn: screenShotType["h2cImgLoadErrCallback"] | null = null;
let saveCallback: ((code: number, msg: string) => void) | null = null;
let saveImgTitle: string | null = null;
let canvasEvents: mouseEventType | null = null;
Expand All @@ -60,6 +62,7 @@ export default class PlugInParameters {
saveImgTitle = null;
destroyContainer = true;
userToolbar = [];
h2cCrossImgLoadErrFn = null;
}
}

Expand Down Expand Up @@ -223,13 +226,20 @@ export default class PlugInParameters {
toolbarData.push({ ...item, id: 100 + (i + 1) });
}
userToolbar = toolbarData;
console.log(userToolbar);
}

public getUserToolbar() {
return userToolbar;
}

public setH2cCrossImgLoadErrFn(fn: screenShotType["h2cImgLoadErrCallback"]) {
h2cCrossImgLoadErrFn = fn;
}

public getH2cCrossImgLoadErrFn() {
return h2cCrossImgLoadErrFn;
}

public setCanvasEvents(event: mouseEventType) {
canvasEvents = event;
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib/split-methods/SetPlugInParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export function setPlugInParameters(options: screenShotType) {
plugInParameters.setUserToolbar(options.userToolbar);
}

// h2c模式下,跨域图片加载失败时的回调函数
if (options?.h2cImgLoadErrCallback) {
plugInParameters.setH2cCrossImgLoadErrFn(options.h2cImgLoadErrCallback);
}

// 处理用户定义的画布事件
if (options?.canvasEvents) {
plugInParameters.setCanvasEvents(options.canvasEvents);
Expand Down
15 changes: 14 additions & 1 deletion src/lib/split-methods/drawCrossImg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import PlugInParameters from "@/lib/main-entrance/PlugInParameters";

export function drawCrossImg(html: Document) {
const promises: Promise<string>[] = [];
const imageNodes = html.querySelectorAll("img");
const plugInParameters = new PlugInParameters();
imageNodes.forEach(element => {
const href = element.getAttribute("src");
if (!href) return;
Expand All @@ -21,7 +24,17 @@ export function drawCrossImg(html: Document) {
element.setAttribute("src", base64);
resolve("转换成功");
};
img.onerror = reject;
img.onerror = function(err) {
const h2cCrossImgLoadErrFn = plugInParameters?.getH2cCrossImgLoadErrFn();
if (h2cCrossImgLoadErrFn && typeof err != "string") {
h2cCrossImgLoadErrFn({
...err,
imgUrl: href
});
}
// 跨域图片加载失败时,此处不做处理
resolve(true);
};
if (href !== null) {
img.src = href;
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/type/ComponentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export type screenShotType = {
cutInfo: positionInfoType;
}) => void; // 工具栏截图确认回调
closeCallback?: () => void; // 工具栏关闭回调
h2cImgLoadErrCallback?: (err: Event & { imgUrl: string }) => void; // html2canvas跨域图片加载失败回调
triggerCallback?: (res: {
code: number;
msg: string;
Expand Down

0 comments on commit 64d301b

Please sign in to comment.