Skip to content

Commit

Permalink
refactor: clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpedemonte committed Jul 25, 2024
1 parent 14118a8 commit 8c6abce
Showing 1 changed file with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,38 +137,40 @@ function adjustTextDivs(
const expectedHeight = textItem.height * scale;
const actualHeight = textDivElm.getBoundingClientRect().height;

function getScaleX(element: HTMLElement) {
const match = element.style.transform?.match(scaleXPattern);
/**
* Retrieve scale definition from within `transform` style rule
*/
function getScale(element: HTMLElement, type: 'x' | 'y'): number | null {
const pattern = type === 'x' ? scaleXPattern : scaleYPattern;
const match = element.style.transform?.match(pattern);
if (match) {
return parseFloat(match[1]);
}
return null;
}

function getScaleY(element: HTMLElement) {
const match = element.style.transform?.match(scaleYPattern);
if (match) {
return parseFloat(match[1]);
}
return null;
}

const currentScaleX = getScaleX(textDivElm);
const currentScaleX = getScale(textDivElm, 'x');
if (currentScaleX && !isNaN(currentScaleX)) {
const newScale = `scaleX(${(expectedWidth / actualWidth) * currentScaleX})`;
textDivElm.style.transform = textDivElm.style.transform.replace(scaleXPattern, newScale);
} else {
const newScale = `scaleX(${expectedWidth / actualWidth})`;
textDivElm.style.transform += newScale;
const val = expectedWidth / actualWidth;
if (val !== 0) {
const newScale = `scaleX(${val})`;
textDivElm.style.transform += newScale;
}
}

const currentScaleY = getScaleY(textDivElm);
const currentScaleY = getScale(textDivElm, 'y');
if (currentScaleY && !isNaN(currentScaleY)) {
const newScale = `scaleY(${(expectedHeight / actualHeight) * currentScaleY})`;
textDivElm.style.transform = textDivElm.style.transform.replace(scaleYPattern, newScale);
} else {
const newScale = `scaleY(${expectedHeight / actualHeight})`;
textDivElm.style.transform += newScale;
const val = expectedHeight / actualHeight;
if (val !== 0) {
const newScale = `scaleY(${val})`;
textDivElm.style.transform += newScale;
}
}
}
}
Expand Down

0 comments on commit 8c6abce

Please sign in to comment.