From 6f34d772559ecd572ccf637357a4761d468bb8b5 Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期五, 27 九月 2024 11:06:39 +0800 Subject: [PATCH] context --- src/utils/util.ts | 28 +++++++++++++++++++++++++++- 1 files changed, 27 insertions(+), 1 deletions(-) diff --git a/src/utils/util.ts b/src/utils/util.ts index 13bfb8d..093d5d7 100644 --- a/src/utils/util.ts +++ b/src/utils/util.ts @@ -541,7 +541,8 @@ * 鏈�杩� n 澶╃殑 startDate銆乪ndDate * @param dates */ -export const getRecentDateRange = (dates: number) => { +export const getRecentDateRange = (dates: number, includesCurrent = true) => { + dates = includesCurrent ? dates - 1 : dates; // 鑾峰彇褰撳墠鏃ユ湡 const endDate = new Date(); const startDate = new Date(); @@ -697,3 +698,28 @@ export const arrayIsEmpty = (arr: any) => { return !arr || arr.length === 0; }; + +type GetTextWidthOption = { + size?: string; + family?: string; +}; + +export function getTextWidth(text: string, option: GetTextWidthOption) { + if (!text) return 0; + const { size = '14px', family = 'Microsoft YaHei' } = option; + const spanEle = document.createElement('span'); + document.body.appendChild(spanEle); + + spanEle.style.font = 'times new roman'; + spanEle.style.fontSize = size; + spanEle.style.height = 'auto'; + spanEle.style.width = 'auto'; + spanEle.style.position = 'absolute'; + spanEle.style.whiteSpace = 'no-wrap'; + spanEle.innerHTML = text; + + const width = spanEle.clientWidth; + + document.body.removeChild(spanEle); + return width; +} -- Gitblit v1.9.3