Improve signature restoration and event handling
Added `RestoreDrawSignatureIfAvailableAsync` in `ReceiverPage.razor` to restore previously captured signatures on the "Draw" tab using `JSRuntime`. Updated `OnPopupShownAsync` and `SetSignatureTabAsync` to invoke this method. In `receiver-signature.js`, fixed comment formatting and added `_detachPad` to cleanly remove event listeners from signature pads. Updated `initialize` to handle reinitialization of canvases by detaching old event listeners and reattaching new ones, preventing redundant listeners and ensuring proper cleanup. These changes enhance signature restoration functionality and improve event listener management to prevent memory leaks and ensure robust behavior.
This commit is contained in:
@@ -701,6 +701,7 @@
|
||||
async Task OnPopupShownAsync()
|
||||
{
|
||||
await InitializeActiveSignatureTabAsync();
|
||||
await RestoreDrawSignatureIfAvailableAsync();
|
||||
}
|
||||
|
||||
async Task SetSignatureTabAsync(string tab)
|
||||
@@ -710,6 +711,15 @@
|
||||
await InvokeAsync(StateHasChanged);
|
||||
await Task.Delay(50);
|
||||
await InitializeActiveSignatureTabAsync();
|
||||
await RestoreDrawSignatureIfAvailableAsync();
|
||||
}
|
||||
|
||||
async Task RestoreDrawSignatureIfAvailableAsync()
|
||||
{
|
||||
if (_activeSignatureTab != SignatureTabDraw || _capturedSignature is null)
|
||||
return;
|
||||
|
||||
await JSRuntime.InvokeVoidAsync("receiverSignature.loadExistingSignature", DrawCanvasId, _capturedSignature.DataUrl);
|
||||
}
|
||||
|
||||
async Task InitializeActiveSignatureTabAsync()
|
||||
|
||||
@@ -22,7 +22,7 @@ window.receiverSignature = (() => {
|
||||
|
||||
// ?? Annotation Checkboxes ????????????????????????????????????????????????
|
||||
|
||||
// Active install context — holds everything needed to reposition on resize/scroll
|
||||
// Active install context - holds everything needed to reposition on resize/scroll
|
||||
let _installCtx = null;
|
||||
|
||||
function installAnnotationCheckboxes(annotations, checkedIds, dotNetRef) {
|
||||
@@ -92,16 +92,16 @@ window.receiverSignature = (() => {
|
||||
// Initial render
|
||||
_renderAllCheckboxes(ctx);
|
||||
|
||||
// ResizeObserver — watches every page image for size changes (zoom in/out)
|
||||
// ResizeObserver - watches every page image for size changes (zoom in/out)
|
||||
const ro = new ResizeObserver(() => _repositionAll(ctx));
|
||||
pageEls.forEach(el => ro.observe(el));
|
||||
ctx.resizeObs = ro;
|
||||
|
||||
// Scroll — reposition when user scrolls the viewer
|
||||
// Scroll - reposition when user scrolls the viewer
|
||||
ctx.onScroll = () => _repositionAll(ctx);
|
||||
scrollEl.addEventListener('scroll', ctx.onScroll, { passive: true });
|
||||
|
||||
// Window resize — debounced 60 ms
|
||||
// Window resize - debounced 60 ms
|
||||
ctx.onResize = () => {
|
||||
if (ctx.resizeTimer) clearTimeout(ctx.resizeTimer);
|
||||
ctx.resizeTimer = setTimeout(() => _repositionAll(ctx), 60);
|
||||
@@ -226,16 +226,37 @@ window.receiverSignature = (() => {
|
||||
|
||||
function _clear(canvas) { canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height); }
|
||||
|
||||
function _detachPad(state) {
|
||||
if (!state || !state.canvas) return;
|
||||
|
||||
state.canvas.removeEventListener('mousedown', state.start);
|
||||
state.canvas.removeEventListener('mousemove', state.move);
|
||||
state.canvas.removeEventListener('touchstart', state.start);
|
||||
state.canvas.removeEventListener('touchmove', state.move);
|
||||
state.canvas.removeEventListener('touchend', state.end);
|
||||
window.removeEventListener('mouseup', state.end);
|
||||
}
|
||||
|
||||
function initialize(canvasId) {
|
||||
const canvas = document.getElementById(canvasId);
|
||||
if (!canvas || pads.has(canvasId)) return;
|
||||
if (!canvas) return;
|
||||
|
||||
const existing = pads.get(canvasId);
|
||||
if (existing && existing.canvas === canvas) return;
|
||||
if (existing) _detachPad(existing);
|
||||
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.lineWidth = 2.5; ctx.lineCap = 'round'; ctx.lineJoin = 'round'; ctx.strokeStyle = '#111';
|
||||
const state = { drawing: false, hasSignature: false };
|
||||
pads.set(canvasId, state);
|
||||
const state = { canvas: canvas, drawing: false, hasSignature: false, start: null, move: null, end: null };
|
||||
const start = e => { e.preventDefault(); const p = _pos(canvas, e); state.drawing = true; ctx.beginPath(); ctx.moveTo(p.x, p.y); };
|
||||
const move = e => { if (!state.drawing) return; e.preventDefault(); const p = _pos(canvas, e); ctx.lineTo(p.x, p.y); ctx.stroke(); state.hasSignature = true; };
|
||||
const end = e => { if (!state.drawing) return; e.preventDefault(); state.drawing = false; };
|
||||
state.start = start;
|
||||
state.move = move;
|
||||
state.end = end;
|
||||
|
||||
pads.set(canvasId, state);
|
||||
|
||||
canvas.addEventListener('mousedown', start);
|
||||
canvas.addEventListener('mousemove', move);
|
||||
window.addEventListener('mouseup', end);
|
||||
|
||||
Reference in New Issue
Block a user