FEATURE signature: show full pages - can select zone

This commit is contained in:
nobohan 2024-11-25 12:32:11 +01:00 committed by Julien Fastré
parent c2882b1079
commit da37a3db5f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -467,9 +467,24 @@ async function downloadAndOpen(): Promise<Blob> {
return raw; return raw;
} }
const addCanvasEvents = () => {
if (multiPage.value) {
Array.from(Array(pageCount.value).keys()).map((p) => {
const canvas = getCanvas(p + 1);
canvas.addEventListener(
"pointerup",
(e) => canvasClick(e, canvas),
false
);
});
} else {
const canvas = document.querySelectorAll("canvas")[0] as HTMLCanvasElement;
canvas.addEventListener("pointerup", (e) => canvasClick(e, canvas), false);
}
};
const initPdf = () => { const initPdf = () => {
const canvas = document.querySelectorAll("canvas")[0] as HTMLCanvasElement; addCanvasEvents();
canvas.addEventListener("pointerup", canvasClick, false);
setTimeout(drawAllZones, 800); setTimeout(drawAllZones, 800);
}; };
@ -477,9 +492,11 @@ async function toggleMultiPage() {
if (multiPage.value) { if (multiPage.value) {
await setAllPages(); await setAllPages();
setTimeout(drawAllZones, 200); setTimeout(drawAllZones, 200);
addCanvasEvents();
} else { } else {
await setPage(1); await setPage(page.value);
setTimeout(drawAllZones, 200); setTimeout(drawAllZones, 200);
addCanvasEvents();
} }
} }
@ -509,14 +526,22 @@ const selectZone = (z: SignatureZone, canvas: HTMLCanvasElement) => {
userSignatureZone.value = z; userSignatureZone.value = z;
const ctx = canvas.getContext("2d"); const ctx = canvas.getContext("2d");
if (ctx) { if (ctx) {
setPage(page.value); if (multiPage.value) {
setPage(parseInt(canvas.id.split("-")[1]));
} else {
setPage(page.value);
}
setTimeout(drawAllZones, 200); setTimeout(drawAllZones, 200);
} }
}; };
const selectZoneEvent = (e: PointerEvent, canvas: HTMLCanvasElement) => const selectZoneEvent = (e: PointerEvent, canvas: HTMLCanvasElement) =>
signature.zones signature.zones
.filter((z) => z.PDFPage.index + 1 === page.value) .filter(
(z) =>
multiPage.value ||
(z.PDFPage.index + 1 === page.value && !multiPage.value)
)
.map((z) => { .map((z) => {
if ( if (
hitSignature(z, [e.offsetX, e.offsetY], canvas.width, canvas.height) hitSignature(z, [e.offsetX, e.offsetY], canvas.width, canvas.height)
@ -531,12 +556,10 @@ const selectZoneEvent = (e: PointerEvent, canvas: HTMLCanvasElement) =>
} }
}); });
const canvasClick = (e: PointerEvent) => { const canvasClick = (e: PointerEvent, canvas: HTMLCanvasElement) =>
const canvas = document.querySelectorAll("canvas")[0] as HTMLCanvasElement; //TODO change canvas as a function of multiOption
canvasEvent.value === "select" canvasEvent.value === "select"
? selectZoneEvent(e, canvas) ? selectZoneEvent(e, canvas)
: addZoneEvent(e, canvas); : addZoneEvent(e, canvas);
};
const turnPage = async (upOrDown: number) => { const turnPage = async (upOrDown: number) => {
//userSignatureZone.value = null; // desactivate the reset of the zone when turning page //userSignatureZone.value = null; // desactivate the reset of the zone when turning page
@ -558,8 +581,7 @@ const turnSignature = async (upOrDown: number) => {
let currentZone = signature.zones[zoneIndex]; let currentZone = signature.zones[zoneIndex];
if (currentZone) { if (currentZone) {
page.value = currentZone.PDFPage.index + 1; page.value = currentZone.PDFPage.index + 1;
userSignatureZone.value = currentZone; const canvas = getCanvas(currentZone.PDFPage.index + 1);
const canvas = document.querySelectorAll("canvas")[0];
selectZone(currentZone, canvas); selectZone(currentZone, canvas);
} }
}; };
@ -614,7 +636,6 @@ const drawAllZones = () => {
(z.PDFPage.index + 1 === page.value && !multiPage.value) (z.PDFPage.index + 1 === page.value && !multiPage.value)
) )
.map((z) => { .map((z) => {
console.log('drawAllZones: z is ', z)
const canvas = getCanvas(z.PDFPage.index + 1); const canvas = getCanvas(z.PDFPage.index + 1);
const ctx = canvas.getContext("2d"); const ctx = canvas.getContext("2d");
if (ctx) { if (ctx) {
@ -767,14 +788,14 @@ init();
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
#canvas { canvas {
box-shadow: 0 20px 20px rgba(0, 0, 0, 0.2); box-shadow: 0 20px 20px rgba(0, 0, 0, 0.2);
} }
.onAddZone { .onAddZone {
cursor: not-allowed; cursor: not-allowed;
#canvas { canvas {
cursor: copy; cursor: copy;
} }
} }