react-middle-truncate
react-middle-truncate copied to clipboard
"partial" smartCopy behavior is surprising
~Not really a bug but it seems like the "all" and "partial" options do the same thing for smartCopy. I looked through the source code and couldn't find any differences in behavior. I assume partial was intended to create an expanded selection if the selection text is a substring that contains the ellipsis? If you don't plan to implement that feature maybe it would be less confusing for that option to be deprecated (but still work until a major release).~ I re-read the code and realized that the trigger for each option is different (i.e. partial works even if the selection is an inexact match), but in each case the copied data becomes the full text input, which seems wrong for substring selections.
Sorry I reread the code and realized that partial actually is different, although I think its behavior may be incorrect since it doesn't actually select the substring the user chose.
I think it could be done more precisely like this:
- Create another piece of state called
excludedSubstringwhich is saved at the same time astruncatedTextand contains exactly what it sounds like - When smartCopy is "partial" and a copy is made, you can produce the expanded substring with:
if (smartCopy === 'partial') {
event.preventDefault();
const clipboardData = event.clipboardData || window.clipboardData || event.originalEvent.clipboardData;
clipboardData.setData('text/plain', selectedText.replace(
this.props.ellipsis,
this.state.excludedSubstring
));
}