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 linebreaks removal #148

Merged
merged 7 commits into from
Jun 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/javascript/components/prescription_textarea.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const nbWordsInLines = (lines) => {
var result = 0;
lines.forEach((line) => {
result += line.split(" ").length;
result += line.trim().split(" ").length; // trim because some browsers add white space after each word
});
return result;
};

const tooMuchLineBreaks = (text) => {
const lines = text.split("\n");
const lines = text.split(/\r?\n|\r/g);
const nbLines = lines.length;
const nbWords = nbWordsInLines(lines);
if (nbLines <= 3) {
Expand All @@ -16,13 +16,15 @@ const tooMuchLineBreaks = (text) => {
return nbLines == nbWords;
};

const removeLineBreaks = (text) => {
return text.replaceAll("\n", " ");
const removeLineBreaksAndDoubleSpaces = (text) => {
return text.replace(/\r?\n|\r/g, " ").replace(/ /g, " ");
};

const getTextToPaste = (event) => {
const toPaste = (event.clipboardData || window.clipboardData).getData("text");
return tooMuchLineBreaks(toPaste) ? removeLineBreaks(toPaste) : toPaste;
return tooMuchLineBreaks(toPaste)
? removeLineBreaksAndDoubleSpaces(toPaste)
: toPaste;
};

const pasteText = (textarea, toPaste) => {
Expand Down