add font load checking

This commit is contained in:
Severiano Badajoz
2020-05-27 11:54:47 -07:00
parent 7dc211feab
commit f97c94c510
+31 -8
View File
@@ -5,13 +5,41 @@ import pixelWidth from "string-pixel-width";
import { widthsMap, tooltipHoverOpenDelayQuick } from "../../globals"; import { widthsMap, tooltipHoverOpenDelayQuick } from "../../globals";
export default class Truncate extends Component { export default class Truncate extends Component {
maybeTruncateString = (str, maxSize, activeFont, fontSize) => { static fontIsLoaded = false;
let truncatedString = null;
static checkIfFontLoaded() {
if (document.fonts.check("1em Roboto Condensed")) {
return true;
}
let ret;
document.fonts.ready.then(() => {
if (!document.fonts.check("1em Roboto Condensed")) {
console.error("Roboto Condensed was not loaded");
ret = false;
} else {
ret = true;
}
});
return ret;
}
static getLoadedFont() {
if (!this.fontIsLoaded) {
this.fontIsLoaded = this.checkIfFontLoaded();
}
return this.fontIsLoaded ? "Roboto Condensed" : "Helvetica Neue";
}
maybeTruncateString = (str, maxSize, fontSize) => {
const activeFont = Truncate.getLoadedFont();
const renderedSize = pixelWidth(str, { const renderedSize = pixelWidth(str, {
font: activeFont, font: activeFont,
size: fontSize, size: fontSize,
map: widthsMap, map: widthsMap,
}); });
let truncatedString = null;
if (renderedSize > maxSize) { if (renderedSize > maxSize) {
const percentage = renderedSize / maxSize; const percentage = renderedSize / maxSize;
const newLength = str.length / percentage; const newLength = str.length / percentage;
@@ -32,12 +60,7 @@ export default class Truncate extends Component {
"data-testclass": testClass, "data-testclass": testClass,
style, style,
} = this.props; } = this.props;
const truncatedString = this.maybeTruncateString( const truncatedString = this.maybeTruncateString(children, size, fontSize);
children,
size,
"Roboto Condensed",
fontSize
);
return ( return (
<Tooltip <Tooltip
content={children} content={children}