Skip to content

Commit

Permalink
Merge pull request #15034 from TMisiukiewicz/fix-copypasting-newline-…
Browse files Browse the repository at this point in the history
…issues

Fix issues with newlines when copypasting
  • Loading branch information
mountiny authored Feb 12, 2023
2 parents d42e443 + 1567fac commit 39d9b66
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/components/Composer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class Composer extends React.Component {
return;
}

const plainText = event.clipboardData.getData('text/plain').replace(/\n\n/g, '\n');
const plainText = event.clipboardData.getData('text/plain');

this.paste(Str.htmlDecode(plainText));
}
Expand Down Expand Up @@ -302,9 +302,7 @@ class Composer extends React.Component {
// the only stuff put into the clipboard is what the user selected.
const selectedText = event.target.value.substring(this.state.selection.start, this.state.selection.end);

// The plaintext portion that is put into the clipboard needs to have the newlines duplicated. This is because
// the paste functionality is stripping all duplicate newlines to try and provide consistent behavior.
Clipboard.setHtml(selectedText, selectedText.replace(/\n/g, '\n\n'));
Clipboard.setHtml(selectedText, selectedText);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/components/CopySelectionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class CopySelectionHelper extends React.Component {
Clipboard.setString(parser.htmlToMarkdown(selection));
return;
}
Clipboard.setHtml(selection, Str.htmlDecode(parser.htmlToText(selection)));

// Replace doubled newlines with the single ones because selection from SelectionScraper html contains doubled <br/> marks
Clipboard.setHtml(selection, Str.htmlDecode(parser.htmlToText(selection).replace(/\n\n/g, '\n')));
}

render() {
Expand Down
6 changes: 1 addition & 5 deletions src/pages/home/report/ContextMenu/ContextMenuActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ export default [
if (!Clipboard.canSetHtml()) {
Clipboard.setString(parser.htmlToMarkdown(content));
} else {
// Thanks to how browsers work, when text is highlighted and CTRL+c is pressed, browsers end up doubling the amount of newlines. Since the code in this file is
// triggered from a context menu and not CTRL+c, the newlines need to be doubled so that the content that goes into the clipboard is consistent with CTRL+c behavior.
// The extra newlines are stripped when the contents are pasted into the compose input, but if the contents are pasted outside of the comment composer, it will
// contain extra newlines and that's OK because it is consistent with CTRL+c behavior.
const plainText = Str.htmlDecode(parser.htmlToText(content)).replace(/\n/g, '\n\n');
const plainText = Str.htmlDecode(parser.htmlToText(content));
Clipboard.setHtml(content, plainText);
}
}
Expand Down

0 comments on commit 39d9b66

Please sign in to comment.