@antv/t8
Version:
T8 is a text visualization solution for unstructured data within the AntV technology stack, and it is a declarative JSON Schema syntax that can be used to describe the content of data interpretation reports.
73 lines (71 loc) • 2.45 kB
JavaScript
/**
* Default font size in pixels to use as fallback
*/
var DEFAULT_FONT_SIZE = 14;
/**
* Gets the computed style value for an element
* Compatible with both modern browsers and IE
*
* @param ele - DOM element to get style from
* @param style - CSS property name to retrieve
* @returns The computed style value as a string, or undefined if not available
*/
function getStyle(ele, style) {
var _a;
// @ts-expect-error currentStyle for IE
return window.getComputedStyle ? window.getComputedStyle(ele, null)[style] : (_a = ele === null || ele === void 0 ? void 0 : ele.currentStyle) === null || _a === void 0 ? void 0 : _a[style];
}
/**
* Checks if a CSS size value is in absolute pixels (px)
*
* @param str - CSS size value to check
* @returns boolean indicating if the value is in pixels
*/
function isAbsoluteUnitPx(str) {
return str === null || str === void 0 ? void 0 : str.endsWith('px');
}
/**
* Extracts the numeric value from a pixel measurement
*
* @param str - CSS size value in px format (e.g. "14px")
* @returns The numeric value without units, or undefined if conversion fails
*/
function getPxNumber(str) {
var removeUnit = str.replace(/px$/, '');
var resultNumber = Number(removeUnit);
if (!Number.isNaN(resultNumber))
return resultNumber;
return undefined;
}
/**
* Gets the font size of an element in pixels
*
* First tries to get the font-size of the element itself,
* then falls back to the body's font-size, and finally
* uses the default size if neither is available in px.
*
* @param ele - The DOM element to get font size from
* @param defaultSize - Default size to use as fallback
* @returns Font size in pixels
*/
function getElementFontSize(ele, defaultSize) {
var FONT_SIZE = 'font-size';
// Try to get font size from the element
var eleFontSizeStr = getStyle(ele, FONT_SIZE);
if (eleFontSizeStr && isAbsoluteUnitPx(eleFontSizeStr)) {
var px = getPxNumber(eleFontSizeStr);
if (px)
return px;
}
// Fall back to body font size
var bodyFontSizeStr = getStyle(window.document.body, FONT_SIZE);
if (bodyFontSizeStr && isAbsoluteUnitPx(bodyFontSizeStr)) {
var px = getPxNumber(bodyFontSizeStr);
if (px)
return px;
}
// Use default as last resort
return defaultSize;
}
export { DEFAULT_FONT_SIZE, getElementFontSize };
//# sourceMappingURL=getElementFontSize.js.map