refactor: Improve statelessness of iframe bridge restoration and CSS injection, and prevent re-binding of image field events.

This commit is contained in:
Suherdy Yacob 2026-03-16 17:21:34 +07:00
parent bf06e5be06
commit bff3b53fcb

View File

@ -4,41 +4,39 @@ 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: Initializing CSS-forced visibility patches..."); console.log("Sign Image Field: Initializing stateless CSS-forced visibility patches...");
/** /**
* Diagnostic Bridge Restoration & Universal Force-Show * Stateless Bridge Restoration & Universal Force-Show
* We re-connect the bridge but ALSO forcefully inject CSS to ensure elements are never hidden. * Odoo re-uses and hides iframes when switching between multiple documents in one session.
* We must forcefully reconnect without relying on state markers.
*/ */
const restoreBridgeAndForceShow = (instance, caller) => { const restoreBridgeAndForceShow = (instance) => {
if (!instance || !instance.root || !instance.root.defaultView) return; if (!instance || !instance.root || !instance.root.defaultView) return;
const iwin = instance.root.defaultView; const iwin = instance.root.defaultView;
const iframe = iwin.frameElement; const iframe = iwin.frameElement;
const isDocReady = iwin.document && iwin.document.head; const isDocReady = iwin.document && iwin.document.head;
if (iframe && !iframe.odoo_iframe_instance) { // Connect to Element
if (!iframe._odoo_bridge_logged) { if (iframe) {
console.log(`[Diagnostic] Restoring bridge on iframe element from ${caller}`);
iframe._odoo_bridge_logged = true;
}
iframe.odoo_iframe_instance = instance; iframe.odoo_iframe_instance = instance;
} }
if (!iwin.odoo_iframe_instance) { // Connect to Window
iwin.odoo_iframe_instance = instance; iwin.odoo_iframe_instance = instance;
}
if (iwin.PDFViewerApplication && !iwin.PDFViewerApplication.odoo_iframe_instance) { // Connect to Viewer App
if (iwin.PDFViewerApplication) {
iwin.PDFViewerApplication.odoo_iframe_instance = instance; iwin.PDFViewerApplication.odoo_iframe_instance = instance;
} }
// ULTIMATE FALLBACK: Inject CSS to override Odoo's broken d-none toggler // ULTIMATE FALLBACK: Inject CSS to override Odoo's broken d-none toggler
if (isDocReady && !iwin.document._odoo_force_show_injected) { // We check for the explicit element ID we inject to ensure we don't spam DOM
console.log(`[Diagnostic] Injecting permanent override CSS from ${caller}`); if (isDocReady && !iwin.document.getElementById('odoo-force-show-styles')) {
const style = iwin.document.createElement('style'); const style = iwin.document.createElement('style');
style.id = 'odoo-force-show-styles';
style.type = 'text/css'; style.type = 'text/css';
// Force display block and override any inline or class-based display:none
style.innerHTML = ` style.innerHTML = `
.o_sign_sign_item.d-none { .o_sign_sign_item.d-none {
display: block !important; display: block !important;
@ -48,7 +46,6 @@ const restoreBridgeAndForceShow = (instance, caller) => {
} }
`; `;
iwin.document.head.appendChild(style); iwin.document.head.appendChild(style);
iwin.document._odoo_force_show_injected = true;
} }
}; };
@ -82,6 +79,7 @@ const triggerLazyLoad = (el) => {
const setupLazyObserver = (el) => { const setupLazyObserver = (el) => {
if (!el) return; if (!el) return;
// Safety timeout
setTimeout(() => { setTimeout(() => {
if (el.classList.contains('o_sign_image_loading_container')) { if (el.classList.contains('o_sign_image_loading_container')) {
triggerLazyLoad(el); triggerLazyLoad(el);
@ -103,13 +101,12 @@ const setupLazyObserver = (el) => {
} }
}; };
const processedItems = new WeakSet(); // We use refreshSignItemsForPage as the hook because Odoo calls this whenever
// a page is rendered (initial load AND during document navigation).
// Patching Strategy: Use refreshSignItemsForPage as the hook point because it's
// called every time page renders, giving us a reliable hook to inject CSS
patch(PDFIframe.prototype, { patch(PDFIframe.prototype, {
refreshSignItemsForPage(page) { refreshSignItemsForPage(page) {
restoreBridgeAndForceShow(this, 'PDFIframe.refreshSignItemsForPage'); // Guarantee connection before evaluating
restoreBridgeAndForceShow(this);
try { try {
const result = super.refreshSignItemsForPage(...arguments); const result = super.refreshSignItemsForPage(...arguments);
@ -118,13 +115,17 @@ patch(PDFIframe.prototype, {
if (this.signItems && this.signItems[page]) { if (this.signItems && this.signItems[page]) {
for (const id in this.signItems[page]) { for (const id in this.signItems[page]) {
const item = this.signItems[page][id]; const item = this.signItems[page][id];
// Also forcefully remove d-none via JS just in case
// Native safety net
if (item && item.el) { if (item && item.el) {
item.el.classList.remove('d-none'); item.el.classList.remove('d-none');
} }
if (item && item.data && item.data.type === 'image' && item.el && !processedItems.has(item.el)) { if (item && item.data && item.data.type === 'image' && item.el) {
processedItems.add(item.el);
// Prevent multi-binding during rapid page flips
if (!item.el._odoo_lazy_configured) {
item.el._odoo_lazy_configured = true;
setupLazyObserver(item.el); setupLazyObserver(item.el);
// Add upload logic for active signer // Add upload logic for active signer
@ -198,10 +199,10 @@ patch(PDFIframe.prototype, {
} }
} }
} }
}
return result; return result;
} catch (error) { } catch (error) {
console.error("[Diagnostic] Error inside refreshSignItemsForPage:", error); console.error("[Odoo PDF Fix] Error during refreshSignItemsForPage:", error);
// Even if super fails, don't crash our script
} }
} }
}); });