Markdown Editor clean up.

This commit is contained in:
Wojciech Zawistowski 2014-04-04 20:22:18 +02:00
parent e7601db730
commit 2566d106d7
1 changed files with 29 additions and 22 deletions

View File

@ -861,6 +861,7 @@
var oldInputText; var oldInputText;
var maxDelay = 3000; var maxDelay = 3000;
var startType = "delayed"; // The other legal value is "manual" var startType = "delayed"; // The other legal value is "manual"
var scrollSyncOn = !!panels.previewScroller;
var paneContentHeight = function(pane) { var paneContentHeight = function(pane) {
var $pane = $(pane); var $pane = $(pane);
@ -869,11 +870,10 @@
return pane.scrollHeight - paneVerticalPadding; return pane.scrollHeight - paneVerticalPadding;
}; };
var prevScrollPosition = $(panels.input).scrollTop(); if (scrollSyncOn) {
var caretMarkerPosition = 0; var prevScrollPosition = $(panels.input).scrollTop();
var markerPositions; var caretMarkerPosition = 0;
if (panels.previewScroller) { var markerPositions = {
markerPositions = {
scroller: [0, paneContentHeight(panels.previewScroller)], scroller: [0, paneContentHeight(panels.previewScroller)],
preview: [0, paneContentHeight(panels.preview)] preview: [0, paneContentHeight(panels.preview)]
}; };
@ -1020,21 +1020,28 @@
var prevTime = new Date().getTime(); var prevTime = new Date().getTime();
var caretPosition = getCaretPosition(); var previewText;
text = text.slice(0, caretPosition) + '~~caret~~' + text.slice(caretPosition); var previewScrollerText;
text = text.replace(/(\n|\r|\r\n)(\n|\r|\r\n)+/g, "$&~~marker~~$1$1");
previewText = converter.makeHtml(text.replace('~~caret~~', '')) if (scrollSyncOn) {
.replace(/<p>~~marker~~<\/p>/g, '<span class="marker"></span>') var caretPosition = getCaretPosition();
.replace(/~~marker~~/g, '<span class="marker"></span>'); text = text.slice(0, caretPosition) + '~~caret~~' + text.slice(caretPosition);
text = text.replace(/(\n|\r|\r\n)(\n|\r|\r\n)+/g, "$&~~marker~~$1$1");
previewScrollerText = text previewText = converter.makeHtml(text.replace('~~caret~~', ''))
.replace(/&/g, '&amp;') .replace(/<p>~~marker~~<\/p>/g, '<span class="marker"></span>')
.replace(/</g, '&lt;') .replace(/~~marker~~/g, '<span class="marker"></span>');
.replace(/>/g, '&gt;')
.replace(/(\n|\r|\r\n)/g, '<br>') previewScrollerText = text
.replace('~~caret~~', '<span class="caret"></span>') .replace(/&/g, '&amp;')
.replace(/~~marker~~<br><br>/g, '<span class="marker"></span>'); .replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/(\n|\r|\r\n)/g, '<br>')
.replace('~~caret~~', '<span class="caret"></span>')
.replace(/~~marker~~<br><br>/g, '<span class="marker"></span>');
} else {
previewText = converter.makeHtml(text);
}
// Calculate the processing time of the HTML creation. // Calculate the processing time of the HTML creation.
// It's used as the delay time in the event listener. // It's used as the delay time in the event listener.
@ -1043,7 +1050,7 @@
Ember.run(function() { Ember.run(function() {
pushPreviewHtml(previewText, previewScrollerText); pushPreviewHtml(previewText, previewScrollerText);
if (panels.previewScroller) { if (scrollSyncOn) {
cacheMarkerPositions(); cacheMarkerPositions();
cacheCaretMarkerPosition(); cacheCaretMarkerPosition();
syncScroll(true); syncScroll(true);
@ -1154,14 +1161,14 @@
}; };
ieSafeSet(panels.preview, previewText); ieSafeSet(panels.preview, previewText);
if (panels.previewScroller) { if (scrollSyncOn) {
ieSafeSet(panels.previewScroller, previewScrollerText); ieSafeSet(panels.previewScroller, previewScrollerText);
} }
} }
var nonSuckyBrowserPreviewSet = function (previewText, previewScrollerText) { var nonSuckyBrowserPreviewSet = function (previewText, previewScrollerText) {
panels.preview.innerHTML = previewText; panels.preview.innerHTML = previewText;
if (panels.previewScroller) { if (scrollSyncOn) {
panels.previewScroller.innerHTML = previewScrollerText; panels.previewScroller.innerHTML = previewScrollerText;
} }
} }
@ -1213,7 +1220,7 @@
// TODO: make option to disable. We don't need this in discourse // TODO: make option to disable. We don't need this in discourse
// setupEvents(panels.input, applyTimeout); // setupEvents(panels.input, applyTimeout);
if (panels.previewScroller) { if (scrollSyncOn) {
setupScrollSync(); setupScrollSync();
} }