Skip to content

Commit

Permalink
fix: fix report-item layout calc script [PT-186921649]
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanik committed Feb 1, 2024
1 parent 2f77863 commit 7201bf8
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions packages/tecrock-table/src/components/report-item/report-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,41 @@ function runInReportItem() {
if (!tableParent) {
return;
}
if (table.offsetWidth > tableParent.offsetWidth) {
// Table is wider than its parent, there is a horizontal scrollbar, so we need to scale it down.
const originalWidth = table.offsetWidth;
const originalHeight = table.offsetHeight;
const scale = tableParent.offsetWidth / (originalWidth + 1);
table.style.transform = `scale(${scale})`;
table.style.width = tableParent.style.width = `${scale * originalWidth}px`;
table.style.height = tableParent.style.height = (scale * originalHeight) + "px";
tableParent.style.overflow = "hidden";

// ResizeObserver is necessary to handle situations when Dashboard is animating the width of the report item container.
let firstResize = true;
let resizeTimeout: number | null = null;
let prevWidth = -Infinity;
function onResize() {
if (resizeTimeout) {
clearTimeout(resizeTimeout);
}
function resize() {
if (!tableParent) {
return;
}
if (tableParent.clientWidth === prevWidth) {
return;
}
const originalWidth = table.clientWidth;
const originalHeight = table.clientHeight;
const scale = tableParent.clientWidth / originalWidth;
table.style.transform = `scale(${scale})`;
table.style.width = `${Math.round(scale * originalWidth)}px`;
table.style.height = tableParent.style.height = `${Math.round(scale * originalHeight)}px`;
firstResize = false;
prevWidth = tableParent.clientWidth;
}
if (firstResize) {
resize();
} else {
resizeTimeout = window.setTimeout(resize, 100);
}
}
const resizeObserver = new ResizeObserver(onResize);
resizeObserver.observe(document.body);
tableParent.style.overflow = "hidden";

// Set up the snapshot click handler. This is implemented in the Table component, but it won't work in the report
// item because the component is rendered to a string and sent in that form. Fortunately, it's easy to
// reimplement it here.
Expand All @@ -51,7 +76,7 @@ function runInReportItem() {
const reportItemScript = "<script>" + runInReportItem.toString() + `\n ${runInReportItem.name}(); </script>`;

export const reportItemHandler: IGetReportItemAnswerHandler<ITectonicExplorerInteractiveState, IAuthoredState> = request => {
const {platformUserId, version, itemsType} = request;
const { platformUserId, version, itemsType } = request;

if (!version) {
// for hosts sending older, unversioned requests
Expand All @@ -71,8 +96,8 @@ export const reportItemHandler: IGetReportItemAnswerHandler<ITectonicExplorerInt
// DOM mutation observers to detect when the table is rendered or relying on other callbacks or timing tricks.
reportItemScript;

items.push({type: "html", html: htmlWithInlineStyles});
sendReportItemAnswer({version, platformUserId, items, itemsType});
items.push({ type: "html", html: htmlWithInlineStyles });
sendReportItemAnswer({ version, platformUserId, items, itemsType });
} else {
// tslint:disable-next-line:no-console
console.error(
Expand Down

0 comments on commit 7201bf8

Please sign in to comment.