Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/fifty-stingrays-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

[Image] | (DX) | Add delay to flakey Graphie image regression test
28 changes: 15 additions & 13 deletions packages/perseus/src/util/graphie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1708,12 +1708,6 @@ const setLabelMargins = function (span: HTMLElement, size: Coord): void {
const direction = $span.data("labelDirection");
let [width, height] = size;

// Store the original (pre-scaling) label size on first call so that
// we can recalculate margins correctly when the container resizes.
if ($span.data("originalLabelSize") == null) {
$span.data("originalLabelSize", [width, height]);
}

// This can happen when a span
// is invisible but we still want to update the CSS. At worst, we will
// be off by a few pixels instead of in a different position entirely.
Expand All @@ -1736,9 +1730,12 @@ const setLabelMargins = function (span: HTMLElement, size: Coord): void {
marginLeft: -width / 2 + x * scale,
marginTop: -height / 2 - y * scale,
});
// Store the original (pre-scaling) label size on first call so that
// we can recalculate margins correctly when the container resizes.
if ($span.data("originalLabelSize") == null) {
$span.data("originalLabelSize", [width, height]);
}
} else {
const currentHeightMatchesProps = span.scrollHeight === height;

// We are using jQuery to collect information and calculate a scale
// since we don't have a way to pass it to this function.
// We need the width of the container in order to calculate the scale to apply to the label.
Expand All @@ -1760,12 +1757,17 @@ const setLabelMargins = function (span: HTMLElement, size: Coord): void {
// Inherited line-height values can really mess up placement.
$container.css("line-height", "normal");

// If the change in line-height affected the height of the element,
// then the height used for calculations should be updated.
// This can happen when the first label in the container calls this method,
// and the line-height was different when the height measurement was originally referenced.
if (currentHeightMatchesProps && span.scrollHeight !== height) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble seeing how this situation (line-height affecting the height) is handled by the new conditional involving just the "originnalLabelSize" data attribute, below. Can you verify the timing of this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like in the main code, the height gets set to the scroll height only if a change in the scroll height is detected. In the new code, the height gets set to the scroll height always on the first run. The timing on the two should be the same.

// The first time this runs for a label, we re-measure the label's height from
// the DOM instead of trusting the `height` value passed into this function.
// We do this because setting line-height above can shift the element's height,
// and the passed in `height` may have been captured before that shift happened
// (either because we just caused it, or because a sibling label caused it earlier).
// We store the corrected height so that on later calls (triggered by ResizeObserver
// when the container resizes), we can skip re-measuring and just scale from the
// original stored size.
if ($span.data("originalLabelSize") == null) {
height = span.scrollHeight;
$span.data("originalLabelSize", [width, height]);
}

// The expected width of the graphie is found in the "max-width" property on ".svg-image" containers,
Expand Down
Loading