diff --git a/app/assets/javascripts/discourse/lib/Markdown.Editor.js b/app/assets/javascripts/discourse/lib/Markdown.Editor.js index 1e3fec00623..5b0c29dad30 100644 --- a/app/assets/javascripts/discourse/lib/Markdown.Editor.js +++ b/app/assets/javascripts/discourse/lib/Markdown.Editor.js @@ -920,8 +920,8 @@ return { scrollerStart: markerPositions.scroller[startMarkerIndex], scrollerEnd: markerPositions.scroller[endMarkerIndex], - previewStart: markerPositions.preview[startMarkerIndex], - previewEnd: markerPositions.preview[endMarkerIndex] + previewStart: markerPositions.preview[startMarkerIndex] || markerPositions.preview[markerPositions.preview.length-1], + previewEnd: markerPositions.preview[endMarkerIndex] || markerPositions.preview[markerPositions.preview.length-1] }; }; @@ -974,7 +974,6 @@ // Adds event listeners to elements var setupEvents = function (inputElem, listener) { - util.addEvent(inputElem, "input", listener); inputElem.onpaste = listener; inputElem.ondrop = listener; @@ -1025,20 +1024,32 @@ if (scrollSyncOn) { var caretPosition = getCaretPosition(); - text = text.slice(0, caretPosition) + '~~caret~~' + text.slice(caretPosition); - text = text.replace(/(\n|\r|\r\n)(\n|\r|\r\n)+/g, "$&~~marker~~$1$1"); - previewText = converter.makeHtml(text.replace('~~caret~~', '')) - .replace(/

~~marker~~<\/p>/g, '') - .replace(/~~marker~~/g, ''); + var caret = "465c94fb53b6304c4f57"; + var marker = "468c94fb53b6304c4f58"; - previewScrollerText = text + // add last marker + text = text + marker; + + var addMarkers = function(text) { + return text.replace(/(\s*)(\n|\n|\r|\r\n)/g, function(m, spaces, newline) { + return marker + spaces + "\n"; + }); + } + + previewText = converter.makeHtml(addMarkers(text)) + .replace(new RegExp(marker, 'g'), ''); + + var withCaret = text.slice(0, caretPosition) + caret + text.slice(caretPosition); + + previewScrollerText = addMarkers(withCaret) .replace(/&/g, '&') .replace(//g, '>') - .replace(/(\n|\r|\r\n)/g, '
') - .replace('~~caret~~', '') - .replace(/~~marker~~

/g, ''); + .replace(/(\n|\n\r|\r\n)/g, '
') + .replace(caret, '') + .replace(new RegExp(marker, 'g'), ''); + } else { previewText = converter.makeHtml(text); }