Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with newlines when copypasting #15034

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/components/Composer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,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 @@ -297,9 +297,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.
Comment on lines -115 to -118
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we ever get to the bottom of why this comment was added or whether it's accurate? Maybe this wasn't browser behavior after all?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very possible I was misunderstanding the behavior when I wrote that comment :D

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