feat: Enhance image field lazy loading robustness and refine PDF iframe patching logic

This commit is contained in:
Suherdy Yacob 2026-03-16 15:53:15 +07:00
parent c98aa6777e
commit f119a32b0e

View File

@ -4,21 +4,31 @@ import { patch } from "@web/core/utils/patch";
import { PDFIframe } from "@sign/components/sign_request/PDF_iframe"; import { PDFIframe } from "@sign/components/sign_request/PDF_iframe";
import { SignablePDFIframe } from "@sign/components/sign_request/signable_PDF_iframe"; import { SignablePDFIframe } from "@sign/components/sign_request/signable_PDF_iframe";
console.log("Sign Image Field: Loading custom patches...");
/** /**
* Robust lazy loading trigger for image sign items. * Robust lazy loading trigger for image sign items.
*/ */
const triggerLazyLoad = (el) => { const triggerLazyLoad = (el) => {
if (!el) return; if (!el) return;
const lazyImg = el.querySelector('.o_sign_image_lazy'); const lazyImg = el.querySelector('.o_sign_image_lazy');
if (lazyImg && lazyImg.dataset.src) { if (lazyImg && (lazyImg.dataset.src || lazyImg.src)) {
const targetSrc = lazyImg.dataset.src || lazyImg.src;
// Check if it's currently showing the transparent placeholder
const currentSrc = lazyImg.getAttribute('src'); const currentSrc = lazyImg.getAttribute('src');
if (!currentSrc || currentSrc.startsWith('data:image/gif')) { if (!currentSrc || currentSrc.startsWith('data:image/gif') || currentSrc === '') {
lazyImg.src = lazyImg.dataset.src; console.log("Sign Image Field: Loading image data for", el);
lazyImg.src = targetSrc;
lazyImg.onload = () => { lazyImg.onload = () => {
lazyImg.classList.remove('o_sign_image_loading'); lazyImg.classList.remove('o_sign_image_loading');
el.classList.remove('o_sign_image_loading_container'); el.classList.remove('o_sign_image_loading_container');
lazyImg.style.opacity = 1; lazyImg.style.opacity = 1;
}; };
} else {
// Already has a real src, just ensure loading classes are gone
lazyImg.classList.remove('o_sign_image_loading');
el.classList.remove('o_sign_image_loading_container');
lazyImg.style.opacity = 1;
} }
} }
}; };
@ -27,21 +37,16 @@ const triggerLazyLoad = (el) => {
* Setup IntersectionObserver for an element. * Setup IntersectionObserver for an element.
*/ */
const setupLazyObserver = (iframeInstance, el) => { const setupLazyObserver = (iframeInstance, el) => {
if (!el || !window.IntersectionObserver) { if (!el) return;
// Fallback for no observer support
triggerLazyLoad(el);
return;
}
const lazyImg = el.querySelector('.o_sign_image_lazy');
if (!lazyImg || (!lazyImg.dataset.src && !lazyImg.src)) return;
// Use a safety timeout to ensure it loads even if observer fails // Safety fallback: Always load after 2 seconds regardless of visibility
const safetyTimeout = setTimeout(() => triggerLazyLoad(el), 3000); // This handles cases where IntersectionObserver fails in iframes or remote bundling issues
setTimeout(() => triggerLazyLoad(el), 2000);
if (window.IntersectionObserver) {
const observer = new IntersectionObserver((entries) => { const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => { entries.forEach(entry => {
if (entry.isIntersecting) { if (entry.isIntersecting) {
clearTimeout(safetyTimeout);
triggerLazyLoad(el); triggerLazyLoad(el);
observer.disconnect(); observer.disconnect();
} }
@ -50,64 +55,58 @@ const setupLazyObserver = (iframeInstance, el) => {
rootMargin: '200px' rootMargin: '200px'
}); });
observer.observe(el); observer.observe(el);
}
}; };
// 1. Patch PDFIframe (Base class) for read-only / non-signer views // 1. Patch PDFIframe (Base class)
patch(PDFIframe.prototype, { // We avoid calling 'super' here because PDFIframe is a base class and has no parent.
// Odoo 19's patch system might misbehave with 'super' in base class prototype patches.
const pdfIframePatch = {
enableCustom(signItem) { enableCustom(signItem) {
// Shared logic: Ensure lazy loading is set up for all image fields console.log("Sign Image Field: PDFIframe.enableCustom triggered for", signItem.data.type);
if (signItem && signItem.data && signItem.data.type === 'image') {
setupLazyObserver(this, signItem.el);
}
// Instead of super.enableCustom(signItem), we check if there's an ORIGINAL to call
// Normally for PDFIframe it's an empty function.
}
};
// Use a safe patch wrapper to ensure we don't break the prototype
try {
patch(PDFIframe.prototype, pdfIframePatch);
} catch (e) {
console.error("Sign Image Field: Failed to patch PDFIframe.prototype", e);
}
// 2. Patch SignablePDFIframe (Subclass)
const signablePDFIframePatch = {
enableCustom(signItem) {
console.log("Sign Image Field: SignablePDFIframe.enableCustom triggered for", signItem.data.type);
// Setup observer BEFORE role checks so it works for observers/read-only too
if (signItem && signItem.data && signItem.data.type === 'image') { if (signItem && signItem.data && signItem.data.type === 'image') {
setupLazyObserver(this, signItem.el); setupLazyObserver(this, signItem.el);
} }
// Base PDFIframe.enableCustom is empty, but we call super just in case of other patches // Call base implementation
if (typeof super.enableCustom === 'function') {
super.enableCustom(signItem);
}
}
});
// 2. Patch SignablePDFIframe (Subclass) for active signers
patch(SignablePDFIframe.prototype, {
enableCustom(signItem) {
// IMPORTANT: Execute shared logic first!
// PDFIframe.enableCustom (our patch above) will handle lazy loading.
// However, SignablePDFIframe does NOT normally call super.enableCustom().
// We manually trigger the shared observation logic here to be sure.
if (signItem && signItem.data && signItem.data.type === 'image') {
setupLazyObserver(this, signItem.el);
}
// Now call the original SignablePDFIframe.enableCustom (which has the role check)
super.enableCustom(signItem); super.enableCustom(signItem);
// Additional setup only for the current signer
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) { if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
const el = signItem.el; const el = signItem.el;
const data = signItem.data; const data = signItem.data;
// Check if we are responsible for this field (duplicate check from original but needed for our additions) // Re-apply responsible check for upload logic only
if (this.readonly || data.responsible !== this.currentRole) { if (this.readonly || (data.responsible > 0 && data.responsible !== this.currentRole)) {
return; return;
} }
el.setAttribute('data-type', 'image'); el.setAttribute('data-type', 'image');
// Initial font size adjustment for placeholder
const adjustFontSize = () => {
const placeholder = el.querySelector('.o_placeholder');
if (placeholder) {
const minDimension = Math.min(el.offsetHeight, el.offsetWidth);
const fontSize = Math.max(10, Math.min(16, minDimension * 0.4));
placeholder.style.fontSize = fontSize + 'px !important';
placeholder.style.lineHeight = '1';
}
};
setTimeout(adjustFontSize, 100);
// Upload implementation // Upload implementation
const input = el.querySelector('.o_sign_image_upload_input'); const input = el.querySelector('.o_sign_image_upload_input');
if (input) { if (input) {
// Remove any previous listeners if possible (though patch should handle it)
el.addEventListener('click', (e) => { el.addEventListener('click', (e) => {
if (e.target !== input) { if (e.target !== input) {
input.click(); input.click();
@ -175,9 +174,12 @@ patch(SignablePDFIframe.prototype, {
if (item.data.type === 'image') { if (item.data.type === 'image') {
return item.el.dataset.value || item.data.value || false; return item.el.dataset.value || item.data.value || false;
} }
if (typeof super.getSignatureValueFromElement === 'function') {
return super.getSignatureValueFromElement(item); return super.getSignatureValueFromElement(item);
} }
return false; };
}
}); try {
patch(SignablePDFIframe.prototype, signablePDFIframePatch);
} catch (e) {
console.error("Sign Image Field: Failed to patch SignablePDFIframe.prototype", e);
}