Skip to content

Commit 11c4a04

Browse files
committed
Updated updateView, windowResize, cursorActivity, updateHistory to use lodash debounce function, added back missing refreshView function
1 parent 01217c9 commit 11c4a04

1 file changed

Lines changed: 32 additions & 24 deletions

File tree

public/js/index.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ var defaultExtraKeys = {
1616
};
1717

1818
var idleTime = 300000; //5 mins
19-
var finishChangeDelay = 200;
20-
var cursorActivityDelay = 50;
21-
var cursorAnimatePeriod = 100;
19+
var updateViewDebounce = 200;
20+
var cursorActivityDebounce = 50;
2221
var supportCodeModes = ['javascript', 'htmlmixed', 'htmlembedded', 'css', 'xml', 'clike', 'clojure', 'ruby', 'python', 'shell', 'php', 'sql', 'coffeescript', 'yaml', 'jade', 'lua', 'cmake', 'nginx', 'perl', 'sass', 'r', 'dockerfile'];
2322
var supportHeaders = [
2423
{
@@ -423,13 +422,7 @@ $(document).ready(function () {
423422
});
424423
});
425424
//when page resize
426-
var windowResizeDelay = 200;
427-
var windowResizeTimer = null;
428425
$(window).resize(function () {
429-
clearTimeout(windowResizeTimer);
430-
windowResizeTimer = setTimeout(function () {
431-
windowResize();
432-
}, windowResizeDelay);
433426
});
434427
//when page unload
435428
$(window).unload(function () {
@@ -463,7 +456,10 @@ function locationHashChanged(e) {
463456
}
464457
}
465458

466-
function windowResize() {
459+
var windowResizeDebounce = 200;
460+
var windowResize = _.debounce(windowResizeInner, windowResizeDebounce);
461+
462+
function windowResizeInner() {
467463
checkResponsive();
468464
checkEditorStyle();
469465
checkTocStyle();
@@ -662,9 +658,11 @@ function changeMode(type) {
662658
} else {
663659
$(document.body).css('background-color', ui.area.codemirror.css('background-color'));
664660
}
661+
662+
windowResizeInner();
663+
665664
restoreInfo();
666665

667-
windowResize();
668666

669667
ui.toolbar.both.removeClass("active");
670668
ui.toolbar.edit.removeClass("active");
@@ -1101,15 +1099,15 @@ socket.on('doc', function (obj) {
11011099
restoreInfo();
11021100
});
11031101

1104-
socket.on('ack', _.debounce(function () {
1102+
socket.on('ack', function () {
11051103
isDirty = true;
11061104
updateView();
1107-
}, finishChangeDelay));
1105+
});
11081106

1109-
socket.on('operation', _.debounce(function () {
1107+
socket.on('operation', function () {
11101108
isDirty = true;
11111109
updateView();
1112-
}, finishChangeDelay));
1110+
});
11131111

11141112
socket.on('online users', function (data) {
11151113
data = LZString.decompressFromUTF16(data);
@@ -1608,11 +1606,10 @@ editor.on('focus', function (cm) {
16081606
personalInfo['cursor'] = editor.getCursor();
16091607
socket.emit('cursor focus', editor.getCursor());
16101608
});
1611-
var cursorActivityTimer = null;
16121609
editor.on('cursorActivity', function (cm) {
1613-
clearTimeout(cursorActivityTimer);
1614-
cursorActivityTimer = setTimeout(cursorActivity, cursorActivityDelay);
16151610
updateStatusBar();
1611+
cursorActivity();
1612+
});
16161613
editor.on('beforeSelectionChange', function (doc, selections) {
16171614
if (selections)
16181615
selection = selections.ranges[0];
@@ -1621,7 +1618,9 @@ editor.on('beforeSelectionChange', function (doc, selections) {
16211618
updateStatusBar();
16221619
});
16231620

1624-
function cursorActivity() {
1621+
var cursorActivity = _.debounce(cursorActivityInner, cursorActivityDebounce);
1622+
1623+
function cursorActivityInner() {
16251624
if (editorHasFocus() && !Visibility.hidden()) {
16261625
for (var i = 0; i < onlineUsers.length; i++) {
16271626
if (onlineUsers[i].id == personalInfo.id) {
@@ -1707,15 +1706,17 @@ function restoreInfo() {
17071706
}
17081707

17091708
//view actions
1710-
var finishChangeTimer = null;
1711-
1712-
function finishChange(emit) {
1713-
updateView();
1709+
function refreshView() {
1710+
ui.area.markdown.html('');
1711+
isDirty = true;
1712+
updateViewInner();
17141713
}
17151714

1715+
var updateView = _.debounce(updateViewInner, updateViewDebounce);
1716+
17161717
var lastResult = null;
17171718

1718-
function updateView() {
1719+
function updateViewInner() {
17191720
if (currentMode == modeType.edit || !isDirty) return;
17201721
var value = editor.getValue();
17211722
var result = postProcess(md.render(value)).children().toArray();
@@ -1734,6 +1735,13 @@ function updateView() {
17341735
isDirty = false;
17351736
clearMap();
17361737
buildMap();
1738+
1739+
var updateHistoryDebounce = 600;
1740+
1741+
var updateHistory = _.debounce(updateHistoryInner, updateHistoryDebounce)
1742+
1743+
function updateHistoryInner() {
1744+
writeHistory(ui.area.markdown);
17371745
}
17381746

17391747
function updateDataAttrs(src, des) {

0 commit comments

Comments
 (0)