UNPKG

@wordpress/block-editor

Version:
42 lines (41 loc) 1.49 kB
// packages/block-editor/src/utils/fit-text-utils.js function findOptimalFontSize(textElement, applyFontSize) { const alreadyHasScrollableHeight = textElement.scrollHeight > textElement.clientHeight; let minSize = 5; let maxSize = 2400; let bestSize = minSize; const computedStyle = window.getComputedStyle(textElement); const paddingLeft = parseFloat(computedStyle.paddingLeft) || 0; const paddingRight = parseFloat(computedStyle.paddingRight) || 0; const range = document.createRange(); range.selectNodeContents(textElement); while (minSize <= maxSize) { const midSize = Math.floor((minSize + maxSize) / 2); applyFontSize(midSize); const rect = range.getBoundingClientRect(); const textWidth = rect.width; const fitsWidth = textElement.scrollWidth <= textElement.clientWidth && textWidth <= textElement.clientWidth - paddingLeft - paddingRight; const fitsHeight = alreadyHasScrollableHeight || textElement.scrollHeight <= textElement.clientHeight; if (fitsWidth && fitsHeight) { bestSize = midSize; minSize = midSize + 1; } else { maxSize = midSize - 1; } } range.detach(); return bestSize; } function optimizeFitText(textElement, applyFontSize) { if (!textElement) { return; } applyFontSize(0); const optimalSize = findOptimalFontSize(textElement, applyFontSize); applyFontSize(optimalSize); return optimalSize; } export { optimizeFitText }; //# sourceMappingURL=fit-text-utils.js.map