Updated updateView, windowResize, cursorActivity, updateHistory to use lodash debounce function, added back missing refreshView function

This commit is contained in:
Wu Cheng-Han 2015-09-25 18:01:15 +08:00
parent 01217c9f4a
commit 11c4a0404e

View file

@ -16,9 +16,8 @@ var defaultExtraKeys = {
}; };
var idleTime = 300000; //5 mins var idleTime = 300000; //5 mins
var finishChangeDelay = 200; var updateViewDebounce = 200;
var cursorActivityDelay = 50; var cursorActivityDebounce = 50;
var cursorAnimatePeriod = 100;
var supportCodeModes = ['javascript', 'htmlmixed', 'htmlembedded', 'css', 'xml', 'clike', 'clojure', 'ruby', 'python', 'shell', 'php', 'sql', 'coffeescript', 'yaml', 'jade', 'lua', 'cmake', 'nginx', 'perl', 'sass', 'r', 'dockerfile']; var supportCodeModes = ['javascript', 'htmlmixed', 'htmlembedded', 'css', 'xml', 'clike', 'clojure', 'ruby', 'python', 'shell', 'php', 'sql', 'coffeescript', 'yaml', 'jade', 'lua', 'cmake', 'nginx', 'perl', 'sass', 'r', 'dockerfile'];
var supportHeaders = [ var supportHeaders = [
{ {
@ -423,13 +422,7 @@ $(document).ready(function () {
}); });
}); });
//when page resize //when page resize
var windowResizeDelay = 200;
var windowResizeTimer = null;
$(window).resize(function () { $(window).resize(function () {
clearTimeout(windowResizeTimer);
windowResizeTimer = setTimeout(function () {
windowResize();
}, windowResizeDelay);
}); });
//when page unload //when page unload
$(window).unload(function () { $(window).unload(function () {
@ -463,7 +456,10 @@ function locationHashChanged(e) {
} }
} }
function windowResize() { var windowResizeDebounce = 200;
var windowResize = _.debounce(windowResizeInner, windowResizeDebounce);
function windowResizeInner() {
checkResponsive(); checkResponsive();
checkEditorStyle(); checkEditorStyle();
checkTocStyle(); checkTocStyle();
@ -662,9 +658,11 @@ function changeMode(type) {
} else { } else {
$(document.body).css('background-color', ui.area.codemirror.css('background-color')); $(document.body).css('background-color', ui.area.codemirror.css('background-color'));
} }
windowResizeInner();
restoreInfo(); restoreInfo();
windowResize();
ui.toolbar.both.removeClass("active"); ui.toolbar.both.removeClass("active");
ui.toolbar.edit.removeClass("active"); ui.toolbar.edit.removeClass("active");
@ -1101,15 +1099,15 @@ socket.on('doc', function (obj) {
restoreInfo(); restoreInfo();
}); });
socket.on('ack', _.debounce(function () { socket.on('ack', function () {
isDirty = true; isDirty = true;
updateView(); updateView();
}, finishChangeDelay)); });
socket.on('operation', _.debounce(function () { socket.on('operation', function () {
isDirty = true; isDirty = true;
updateView(); updateView();
}, finishChangeDelay)); });
socket.on('online users', function (data) { socket.on('online users', function (data) {
data = LZString.decompressFromUTF16(data); data = LZString.decompressFromUTF16(data);
@ -1608,11 +1606,10 @@ editor.on('focus', function (cm) {
personalInfo['cursor'] = editor.getCursor(); personalInfo['cursor'] = editor.getCursor();
socket.emit('cursor focus', editor.getCursor()); socket.emit('cursor focus', editor.getCursor());
}); });
var cursorActivityTimer = null;
editor.on('cursorActivity', function (cm) { editor.on('cursorActivity', function (cm) {
clearTimeout(cursorActivityTimer);
cursorActivityTimer = setTimeout(cursorActivity, cursorActivityDelay);
updateStatusBar(); updateStatusBar();
cursorActivity();
});
editor.on('beforeSelectionChange', function (doc, selections) { editor.on('beforeSelectionChange', function (doc, selections) {
if (selections) if (selections)
selection = selections.ranges[0]; selection = selections.ranges[0];
@ -1621,7 +1618,9 @@ editor.on('beforeSelectionChange', function (doc, selections) {
updateStatusBar(); updateStatusBar();
}); });
function cursorActivity() { var cursorActivity = _.debounce(cursorActivityInner, cursorActivityDebounce);
function cursorActivityInner() {
if (editorHasFocus() && !Visibility.hidden()) { if (editorHasFocus() && !Visibility.hidden()) {
for (var i = 0; i < onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (onlineUsers[i].id == personalInfo.id) { if (onlineUsers[i].id == personalInfo.id) {
@ -1707,15 +1706,17 @@ function restoreInfo() {
} }
//view actions //view actions
var finishChangeTimer = null; function refreshView() {
ui.area.markdown.html('');
function finishChange(emit) { isDirty = true;
updateView(); updateViewInner();
} }
var updateView = _.debounce(updateViewInner, updateViewDebounce);
var lastResult = null; var lastResult = null;
function updateView() { function updateViewInner() {
if (currentMode == modeType.edit || !isDirty) return; if (currentMode == modeType.edit || !isDirty) return;
var value = editor.getValue(); var value = editor.getValue();
var result = postProcess(md.render(value)).children().toArray(); var result = postProcess(md.render(value)).children().toArray();
@ -1734,6 +1735,13 @@ function updateView() {
isDirty = false; isDirty = false;
clearMap(); clearMap();
buildMap(); buildMap();
var updateHistoryDebounce = 600;
var updateHistory = _.debounce(updateHistoryInner, updateHistoryDebounce)
function updateHistoryInner() {
writeHistory(ui.area.markdown);
} }
function updateDataAttrs(src, des) { function updateDataAttrs(src, des) {