Skip to content

Commit eadfa5a

Browse files
authored
Merge pull request #91 from DMOJ/preview-on-change
Only render Markdown again if content has changed
2 parents 8303634 + fc0f942 commit eadfa5a

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

martor/static/martor/js/martor.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@
125125
$(obj).find('.modal-help-guide').attr({'data-field-name': field_name});
126126
$(obj).find('.modal-emoji').attr({'data-field-name': field_name});
127127

128+
// Set if editor has changed.
129+
editor.on('change', function(evt){
130+
var value = editor.getValue();
131+
textareaId.val(value);
132+
});
133+
128134
// resize the editor using `resizable.min.js`
129135
$('#'+editorId).resizable({
130136
direction: 'bottom',
@@ -136,7 +142,12 @@
136142
// update the preview if this menu is clicked
137143
var currentTab = $('.tab.segment[data-tab=preview-tab-'+field_name+']');
138144
var previewTabButton = $('.item[data-tab=preview-tab-'+field_name+']');
145+
var needsRefresh = true;
139146
var refreshPreview = function() {
147+
if (!needsRefresh) {
148+
return;
149+
}
150+
140151
var value = textareaId.val();
141152
var form = new FormData();
142153
form.append('content', value);
@@ -150,16 +161,16 @@
150161
processData: false,
151162
contentType: false,
152163
success: function(response) {
153-
if(response){
154-
if (editorConfig.hljs == 'true') {
155-
$('pre').each(function (i, block) {
156-
hljs.highlightBlock(block);
157-
});
158-
}
159-
currentTab.html(response).removeClass('martor-preview-stale');
160-
$(document).trigger('martor:preview', [currentTab]);
161-
}else {
162-
currentTab.html('<p>Nothing to preview</p>');
164+
if (response) {
165+
currentTab.html(response).removeClass('martor-preview-stale');
166+
if (editorConfig.hljs == 'true') {
167+
$('pre').each(function (i, block) {
168+
hljs.highlightBlock(block);
169+
});
170+
}
171+
needsRefresh = false;
172+
} else {
173+
currentTab.html('<p>Nothing to preview</p>');
163174
}
164175
},
165176
error: function(response) {
@@ -174,9 +185,12 @@
174185
// Set if editor has changed.
175186
editor.on('change', function (evt) {
176187
var value = editor.getValue();
177-
textareaId.val(value);
178-
if (editorConfig.living === 'true') {
179-
refreshPreview();
188+
if (value !== textareaId.val()) {
189+
textareaId.val(value);
190+
needsRefresh = true;
191+
if (editorConfig.living === 'true') {
192+
refreshPreview();
193+
}
180194
}
181195
});
182196

0 commit comments

Comments
 (0)