-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
45 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
// 节点文本溢出省略 | ||
|
||
// import G6 from '@antv/g6' | ||
|
||
export function fittingString(str: string, maxWidth: number, fontSize: number = 12) { | ||
// 节点文本溢出省略,利用闭包优化性能 | ||
export function fittingStringFn(fontSize: number = 12) { | ||
const ellipsis = '...' | ||
const ellipsisLength = G6.Util.getTextSize(ellipsis, fontSize)[0] | ||
let currentWidth = 0 | ||
let res = str | ||
const pattern = /[\u4E00-\u9FA5]+/ | ||
str.split('').forEach((letter, i) => { | ||
if (currentWidth > maxWidth - ellipsisLength) | ||
return | ||
if (pattern.test(letter)) | ||
currentWidth += fontSize | ||
else | ||
currentWidth += G6.Util.getLetterWidth(letter, fontSize) | ||
return function (str: string, maxWidth: number) { | ||
let currentWidth = 0 | ||
let res = str | ||
const pattern = /[\u4E00-\u9FA5]+/ | ||
str.split('').forEach((letter, i) => { | ||
if (currentWidth > maxWidth - ellipsisLength) | ||
return | ||
if (pattern.test(letter)) | ||
currentWidth += fontSize | ||
else | ||
currentWidth += G6.Util.getLetterWidth(letter, fontSize) | ||
|
||
if (currentWidth > maxWidth - ellipsisLength) | ||
res = `${str.substr(0, i)}${ellipsis}` | ||
}) | ||
return res | ||
if (currentWidth > maxWidth - ellipsisLength) | ||
res = `${str.substring(0, i)}${ellipsis}` | ||
}) | ||
return res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters