first commit
This commit is contained in:
84
wp-includes/js/dist/script-modules/block-editor/utils/fit-text-frontend.js
vendored
Normal file
84
wp-includes/js/dist/script-modules/block-editor/utils/fit-text-frontend.js
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
// packages/block-editor/build-module/utils/fit-text-frontend.mjs
|
||||
import { store, getElement, getContext } from "@wordpress/interactivity";
|
||||
|
||||
// packages/block-editor/build-module/utils/fit-text-utils.mjs
|
||||
function findOptimalFontSize(textElement, applyFontSize) {
|
||||
const alreadyHasScrollableHeight = textElement.scrollHeight > textElement.clientHeight;
|
||||
let minSize = 0;
|
||||
let maxSize = 2400;
|
||||
let bestSize = minSize;
|
||||
const computedStyle = window.getComputedStyle(textElement);
|
||||
let paddingLeft = parseFloat(computedStyle.paddingLeft) || 0;
|
||||
let paddingRight = parseFloat(computedStyle.paddingRight) || 0;
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(textElement);
|
||||
let referenceElement = textElement;
|
||||
const parentElement = textElement.parentElement;
|
||||
if (parentElement) {
|
||||
const parentElementComputedStyle = window.getComputedStyle(parentElement);
|
||||
if (parentElementComputedStyle?.display === "flex") {
|
||||
referenceElement = parentElement;
|
||||
paddingLeft += parseFloat(parentElementComputedStyle.paddingLeft) || 0;
|
||||
paddingRight += parseFloat(parentElementComputedStyle.paddingRight) || 0;
|
||||
}
|
||||
}
|
||||
let maxclientHeight = referenceElement.clientHeight;
|
||||
while (minSize <= maxSize) {
|
||||
const midSize = Math.floor((minSize + maxSize) / 2);
|
||||
applyFontSize(midSize);
|
||||
const rect = range.getBoundingClientRect();
|
||||
const textWidth = rect.width;
|
||||
const fitsWidth = textElement.scrollWidth <= referenceElement.clientWidth && textWidth <= referenceElement.clientWidth - paddingLeft - paddingRight;
|
||||
const fitsHeight = alreadyHasScrollableHeight || textElement.scrollHeight <= referenceElement.clientHeight || textElement.scrollHeight <= maxclientHeight;
|
||||
if (referenceElement.clientHeight > maxclientHeight) {
|
||||
maxclientHeight = referenceElement.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;
|
||||
}
|
||||
|
||||
// packages/block-editor/build-module/utils/fit-text-frontend.mjs
|
||||
store("core/fit-text", {
|
||||
callbacks: {
|
||||
init() {
|
||||
const context = getContext();
|
||||
const { ref } = getElement();
|
||||
const applyFontSize = (fontSize) => {
|
||||
if (fontSize === 0) {
|
||||
ref.style.fontSize = "";
|
||||
} else {
|
||||
ref.style.fontSize = `${fontSize}px`;
|
||||
}
|
||||
};
|
||||
context.fontSize = optimizeFitText(ref, applyFontSize);
|
||||
if (window.ResizeObserver && ref.parentElement) {
|
||||
const resizeObserver = new window.ResizeObserver(() => {
|
||||
context.fontSize = optimizeFitText(ref, applyFontSize);
|
||||
});
|
||||
resizeObserver.observe(ref.parentElement);
|
||||
resizeObserver.observe(ref);
|
||||
return () => {
|
||||
if (resizeObserver) {
|
||||
resizeObserver.disconnect();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
1
wp-includes/js/dist/script-modules/block-editor/utils/fit-text-frontend.min.asset.php
vendored
Normal file
1
wp-includes/js/dist/script-modules/block-editor/utils/fit-text-frontend.min.asset.php
vendored
Normal file
@@ -0,0 +1 @@
|
||||
<?php return array('dependencies' => array(), 'module_dependencies' => array(array('id' => '@wordpress/interactivity', 'import' => 'static')), 'version' => '383c7a8bd24a1f2fd9b9');
|
||||
1
wp-includes/js/dist/script-modules/block-editor/utils/fit-text-frontend.min.js
vendored
Normal file
1
wp-includes/js/dist/script-modules/block-editor/utils/fit-text-frontend.min.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import{store as H,getElement as w,getContext as b}from"@wordpress/interactivity";function u(e,t){let o=e.scrollHeight>e.clientHeight,i=0,l=2400,g=i,f=window.getComputedStyle(e),p=parseFloat(f.paddingLeft)||0,h=parseFloat(f.paddingRight)||0,c=document.createRange();c.selectNodeContents(e);let r=e,s=e.parentElement;if(s){let n=window.getComputedStyle(s);n?.display==="flex"&&(r=s,p+=parseFloat(n.paddingLeft)||0,h+=parseFloat(n.paddingRight)||0)}let d=r.clientHeight;for(;i<=l;){let n=Math.floor((i+l)/2);t(n);let m=c.getBoundingClientRect().width,z=e.scrollWidth<=r.clientWidth&&m<=r.clientWidth-p-h,S=o||e.scrollHeight<=r.clientHeight||e.scrollHeight<=d;r.clientHeight>d&&(d=r.clientHeight),z&&S?(g=n,i=n+1):l=n-1}return c.detach(),g}function a(e,t){if(!e)return;t(0);let o=u(e,t);return t(o),o}H("core/fit-text",{callbacks:{init(){let e=b(),{ref:t}=w(),o=i=>{i===0?t.style.fontSize="":t.style.fontSize=`${i}px`};if(e.fontSize=a(t,o),window.ResizeObserver&&t.parentElement){let i=new window.ResizeObserver(()=>{e.fontSize=a(t,o)});return i.observe(t.parentElement),i.observe(t),()=>{i&&i.disconnect()}}}}});
|
||||
Reference in New Issue
Block a user