forked from isabella232/viz-collapsible_tree-marketplace
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from satya210/long-node-text-fix
Long node text fix
- Loading branch information
Showing
4 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {expect, describe, test} from '@jest/globals'; | ||
import {getEllipsizedText} from './text'; | ||
|
||
describe('getEllipsizedText', () => { | ||
test('should ellipsize long text', () => { | ||
expect( | ||
getEllipsizedText( | ||
'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita, laudantium!', | ||
200, | ||
100, | ||
16 | ||
) | ||
).toMatch(/\.{3}$/); | ||
expect( | ||
getEllipsizedText( | ||
'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita, laudantium!', | ||
100, | ||
200, | ||
16 | ||
) | ||
).toMatch(/\.{3}$/); | ||
}); | ||
|
||
test('should not ellipsize short text', () => { | ||
expect(getEllipsizedText('Lorem ipsum dolor sit amet', 500, 100, 16)).toBe( | ||
'Lorem ipsum dolor sit amet' | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Returns an ellipsized text that fits in between a calculated distance | ||
* (not overlapping on other nodes) | ||
* | ||
* @param text Text to be ellipsized | ||
* @param nodePos Current node position in the 2D co-ordinate | ||
* @param parentNodePos Parent node position in the 2D co-ordinate | ||
* @param fontSize Text Size used in the UI element | ||
* @returns Calculated ellipsized text | ||
*/ | ||
export const getEllipsizedText = ( | ||
text: string, | ||
nodePos: number, | ||
parentNodePos: number, | ||
fontSize: number | ||
) => { | ||
// Observed width ratio of `W` and `f` (i.e 3 / 2) | ||
const charHWRatio = 3 / 2; | ||
const minCharSize = 6; | ||
|
||
const distance = Math.abs(nodePos - (parentNodePos || 0)); | ||
const charSize = fontSize / charHWRatio || minCharSize; | ||
|
||
let maxCharWidth = Math.floor(distance / charSize); | ||
if (maxCharWidth === 0) { | ||
return text; | ||
} | ||
|
||
// Compensating for 3 dots and 1 space in ellipsized text | ||
maxCharWidth -= 4; | ||
|
||
return ( | ||
text.substring(0, maxCharWidth) + (text.length > maxCharWidth ? ' ...' : '') | ||
); | ||
}; |