mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-09 06:38:12 +08:00
add font load checking
This commit is contained in:
@@ -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}
|
||||||
|
|||||||
Reference in New Issue
Block a user