-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: middle truncate long file names in shared files list (#1339)
* feat: add MiddleEllipsis component * feat: middle-truncate long file names * fix: in shared files card, increase width of file names column --------- Co-authored-by: Jose Buitron <[email protected]>
- Loading branch information
1 parent
a5a5a71
commit e256868
Showing
3 changed files
with
111 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright © 2021-2023 Technology Matters | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
// Based on https://github.com/bluepeter/react-middle-ellipsis | ||
|
||
import React, { useCallback } from 'react'; | ||
|
||
const Component = props => { | ||
const prepEllipses = useCallback(node => { | ||
const parent = node.parentNode; | ||
const targetTextNode = node.childNodes[0]; | ||
|
||
if (targetTextNode !== null) { | ||
if (targetTextNode.hasAttribute('data-original')) { | ||
targetTextNode.textContent = | ||
targetTextNode.getAttribute('data-original'); | ||
} | ||
|
||
ellipses( | ||
node.offsetWidth < parent.offsetWidth ? node : parent, | ||
targetTextNode | ||
); | ||
} | ||
}, []); | ||
const measuredParent = useCallback( | ||
node => { | ||
if (node !== null) { | ||
window.addEventListener('resize', () => { | ||
prepEllipses(node); | ||
}); | ||
prepEllipses(node); | ||
} | ||
}, | ||
[prepEllipses] | ||
); | ||
|
||
return ( | ||
<span | ||
ref={measuredParent} | ||
style={{ | ||
wordBreak: 'keep-all', | ||
overflowWrap: 'normal', | ||
...(props.width && { width: props.width }), | ||
}} | ||
> | ||
{props.children} | ||
</span> | ||
); | ||
}; | ||
|
||
const ellipses = (parentNode, textNode) => { | ||
const containerWidth = parentNode.offsetWidth; | ||
const textWidth = textNode.offsetWidth; | ||
|
||
if (textWidth > containerWidth) { | ||
const characterCount = textNode.textContent.length; | ||
const avgLetterSize = textWidth / characterCount; | ||
const canFit = containerWidth / avgLetterSize; | ||
const charactersToRemove = (characterCount - canFit + 5) / 2; | ||
const endLeft = Math.floor(characterCount / 2 - charactersToRemove); | ||
const startRight = Math.ceil(characterCount / 2 + charactersToRemove); | ||
const startText = textNode.textContent.substr(0, endLeft); | ||
const endText = textNode.textContent.substr(startRight); | ||
|
||
textNode.setAttribute('data-original', textNode.textContent); | ||
textNode.textContent = `${startText}…${endText}`; | ||
} | ||
}; | ||
|
||
export default Component; |
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