|
125 | 125 | $(obj).find('.modal-help-guide').attr({'data-field-name': field_name}); |
126 | 126 | $(obj).find('.modal-emoji').attr({'data-field-name': field_name}); |
127 | 127 |
|
| 128 | + // Set if editor has changed. |
| 129 | + editor.on('change', function(evt){ |
| 130 | + var value = editor.getValue(); |
| 131 | + textareaId.val(value); |
| 132 | + }); |
| 133 | + |
128 | 134 | // resize the editor using `resizable.min.js` |
129 | 135 | $('#'+editorId).resizable({ |
130 | 136 | direction: 'bottom', |
|
136 | 142 | // update the preview if this menu is clicked |
137 | 143 | var currentTab = $('.tab.segment[data-tab=preview-tab-'+field_name+']'); |
138 | 144 | var previewTabButton = $('.item[data-tab=preview-tab-'+field_name+']'); |
| 145 | + var needsRefresh = true; |
139 | 146 | var refreshPreview = function() { |
| 147 | + if (!needsRefresh) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
140 | 151 | var value = textareaId.val(); |
141 | 152 | var form = new FormData(); |
142 | 153 | form.append('content', value); |
|
150 | 161 | processData: false, |
151 | 162 | contentType: false, |
152 | 163 | 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>'); |
163 | 174 | } |
164 | 175 | }, |
165 | 176 | error: function(response) { |
|
174 | 185 | // Set if editor has changed. |
175 | 186 | editor.on('change', function (evt) { |
176 | 187 | 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 | + } |
180 | 194 | } |
181 | 195 | }); |
182 | 196 |
|
|
0 commit comments