Skip to content

Commit

Permalink
Merge pull request #148 from Envinorma/fix-linebreaks-removal
Browse files Browse the repository at this point in the history
Fix linebreaks removal
  • Loading branch information
lisa-durand committed Jun 8, 2021
2 parents 92acf29 + 3ffe38b commit 9052323
Showing 1 changed file with 7 additions and 5 deletions.
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

0 comments on commit 9052323

Please sign in to comment.