Skip to content

Commit 883e7c4

Browse files
committed
Only render Markdown again if content has changed
1 parent a3f1004 commit 883e7c4

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

martor/static/martor/js/martor.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
$(obj).find('.modal-help-guide').attr({'data-field-name': field_name});
129129
$(obj).find('.modal-emoji').attr({'data-field-name': field_name});
130130

131+
// Set if editor has changed.
132+
editor.on('change', function(evt){
133+
var value = editor.getValue();
134+
textareaId.val(value);
135+
});
136+
131137
// resize the editor using `resizable.min.js`
132138
$('#'+editorId).resizable({
133139
direction: 'bottom',
@@ -139,7 +145,12 @@
139145
// update the preview if this menu is clicked
140146
var currentTab = $('.tab.segment[data-tab=preview-tab-'+field_name+']');
141147
var previewTabButton = $('.item[data-tab=preview-tab-'+field_name+']');
148+
var needsRefresh = true;
142149
var refreshPreview = function() {
150+
if (!needsRefresh) {
151+
return;
152+
}
153+
143154
var value = textareaId.val();
144155
var form = new FormData();
145156
form.append('content', value);
@@ -152,12 +163,15 @@
152163
processData: false,
153164
contentType: false,
154165
success: function(response) {
155-
if(response){
156-
currentTab.html(response);
157-
$('pre').each(function(i, block){
158-
hljs.highlightBlock(block);
159-
});
160-
}else {currentTab.html('<p>Nothing to preview</p>');}
166+
if (response) {
167+
currentTab.html(response);
168+
$('pre').each(function (i, block) {
169+
hljs.highlightBlock(block);
170+
});
171+
needsRefresh = false;
172+
} else {
173+
currentTab.html('<p>Nothing to preview</p>');
174+
}
161175
},
162176
error: function(response) {
163177
console.log("error", response);
@@ -171,9 +185,12 @@
171185
// Set if editor has changed.
172186
editor.on('change', function (evt) {
173187
var value = editor.getValue();
174-
textareaId.val(value);
175-
if (editorConfig.living === 'true') {
176-
refreshPreview();
188+
if (value !== textareaId.val()) {
189+
textareaId.val(value);
190+
needsRefresh = true;
191+
if (editorConfig.living === 'true') {
192+
refreshPreview();
193+
}
177194
}
178195
});
179196

0 commit comments

Comments
 (0)