fix: report preview is always an A4 sheet, scaled to fit
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b267b98f18
commit
f27d8441a9
2 files changed
+31
-7
No files matched your search
@@ -8,6 +8,7 @@
|
||||
|
||||
var ICON = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7 3h7l5 5v13H7z"/><path d="M14 3v5h5M10 13h6M10 17h6"/></svg>';
|
||||
var LANG_KEY = 'urutau.report.lang';
|
||||
var A4_PX = 794; // A4 width at 96 dpi: the preview is always an A4 sheet, scaled to fit
|
||||
|
||||
function boot() {
|
||||
var U = window.Urutau;
|
||||
@@ -37,7 +38,21 @@
|
||||
}
|
||||
});
|
||||
var t = U.t, el = U.el;
|
||||
var ctx0 = null, root = null, frame = null, status = null, lang = '';
|
||||
var ctx0 = null, root = null, frame = null, status = null, wrap = null, lang = '';
|
||||
var docH = 1123;
|
||||
|
||||
// fit: the iframe keeps the A4 width (so the report never reflows to the
|
||||
// screen) and is scaled down to the space available.
|
||||
function fit() {
|
||||
if (!frame || !wrap) return;
|
||||
var avail = wrap.clientWidth;
|
||||
var scale = Math.min(1, avail / A4_PX);
|
||||
frame.style.width = A4_PX + 'px';
|
||||
frame.style.height = docH + 'px';
|
||||
frame.style.transform = 'scale(' + scale + ')';
|
||||
wrap.style.height = Math.ceil(docH * scale) + 'px';
|
||||
}
|
||||
function onResize() { fit(); }
|
||||
|
||||
try { lang = localStorage.getItem(LANG_KEY) || ''; } catch (e) { lang = ''; }
|
||||
|
||||
@@ -106,20 +121,27 @@
|
||||
frame = el('iframe', { class: 'rpt-frame', title: t('rpt.preview') });
|
||||
frame.addEventListener('load', function () {
|
||||
var ok = false;
|
||||
try { ok = !!(frame.contentDocument && frame.contentDocument.querySelector('h1')); } catch (e) { ok = false; }
|
||||
try {
|
||||
var d = frame.contentDocument;
|
||||
ok = !!(d && d.querySelector('h1'));
|
||||
if (ok) docH = Math.max(d.documentElement.scrollHeight, d.body ? d.body.scrollHeight : 0, 1123);
|
||||
} catch (e) { ok = false; }
|
||||
fit();
|
||||
showStatus(ok ? '' : 'rpt.err', !ok);
|
||||
});
|
||||
root.appendChild(bar);
|
||||
root.appendChild(status);
|
||||
root.appendChild(el('div', { class: 'rpt-sheetwrap' }, frame));
|
||||
wrap = el('div', { class: 'rpt-fit' }, frame);
|
||||
root.appendChild(el('div', { class: 'rpt-sheetwrap' }, wrap));
|
||||
window.addEventListener('resize', onResize);
|
||||
return root;
|
||||
}
|
||||
|
||||
U.registerPage({
|
||||
id: 'reports', group: 'reports', icon: ICON, titleKey: 'rpt.title',
|
||||
mount: function (host, c) { host.appendChild(build(c)); refresh(); },
|
||||
mount: function (host, c) { host.appendChild(build(c)); fit(); refresh(); },
|
||||
update: function (c) { ctx0 = c; refresh(); },
|
||||
unmount: function () { if (root && root.parentNode) root.parentNode.removeChild(root); root = frame = status = null; }
|
||||
unmount: function () { window.removeEventListener('resize', onResize); if (root && root.parentNode) root.parentNode.removeChild(root); root = frame = status = wrap = null; }
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user