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