From 616e786674d47c30a428f3209e93b96a2f39d011 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Thu, 4 Jun 2015 15:09:06 -0700 Subject: [PATCH] Debounce text editing so you're not undoing/redoing one character at a time --- dist/flowy.js | 22 ++++++++++++++++++++-- dist/flowy.unwrapped.js | 22 ++++++++++++++++++++-- src/models/todo.js | 20 ++++++++++++++++++-- src/views/todo.js | 2 ++ 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/dist/flowy.js b/dist/flowy.js index aa43858..0d993dc 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -448,6 +448,7 @@ var TodoView = Backbone.View.extend({ return this; }, stopEditingText: function() { + this.model.flushText(); this.$el.find("> .text").blur(); return this; }, @@ -627,6 +628,7 @@ var TodoView = Backbone.View.extend({ console.log("unexpected number of lines in textChange"); } else if (lines.length === 1) { // Normal edit + // Debounce it. this.model.setText(this.decodeText(lines[0])); // If there's a line break in the text, they pressed "Enter", and it's more complicated. @@ -721,10 +723,26 @@ var TodoModel = Backbone.Model.extend({ this.save({collapsed: !this.get("collapsed")}); }, _setText: function(text) { + this._text = text; return this.save({text: text}); }, - setText: function(text) { - return Actions.App.act(new Actions.ChangeText(this, text)); + setText: function(text, flush) { + this._text = text; + if (flush) { + this.flushText(); + } else { + this._flushTextDebounced(); + } + return this; + }, + _flushTextDebounced: _.debounce(function() { + this.flushText(); + }, 500), + flushText: function(text) { + if (!this._text || this.get("text") === this._text) { + return; + } + return Actions.App.act(new Actions.ChangeText(this, this._text)); }, hasChildren: function() { return this.get("bullets").length > 0; diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index 4765349..8a94f3b 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -447,6 +447,7 @@ var TodoView = Backbone.View.extend({ return this; }, stopEditingText: function() { + this.model.flushText(); this.$el.find("> .text").blur(); return this; }, @@ -626,6 +627,7 @@ var TodoView = Backbone.View.extend({ console.log("unexpected number of lines in textChange"); } else if (lines.length === 1) { // Normal edit + // Debounce it. this.model.setText(this.decodeText(lines[0])); // If there's a line break in the text, they pressed "Enter", and it's more complicated. @@ -720,10 +722,26 @@ var TodoModel = Backbone.Model.extend({ this.save({collapsed: !this.get("collapsed")}); }, _setText: function(text) { + this._text = text; return this.save({text: text}); }, - setText: function(text) { - return Actions.App.act(new Actions.ChangeText(this, text)); + setText: function(text, flush) { + this._text = text; + if (flush) { + this.flushText(); + } else { + this._flushTextDebounced(); + } + return this; + }, + _flushTextDebounced: _.debounce(function() { + this.flushText(); + }, 500), + flushText: function(text) { + if (!this._text || this.get("text") === this._text) { + return; + } + return Actions.App.act(new Actions.ChangeText(this, this._text)); }, hasChildren: function() { return this.get("bullets").length > 0; diff --git a/src/models/todo.js b/src/models/todo.js index 79e9c74..a60ff72 100644 --- a/src/models/todo.js +++ b/src/models/todo.js @@ -15,10 +15,26 @@ var TodoModel = Backbone.Model.extend({ this.save({collapsed: !this.get("collapsed")}); }, _setText: function(text) { + this._text = text; return this.save({text: text}); }, - setText: function(text) { - return Actions.App.act(new Actions.ChangeText(this, text)); + setText: function(text, flush) { + this._text = text; + if (flush) { + this.flushText(); + } else { + this._flushTextDebounced(); + } + return this; + }, + _flushTextDebounced: _.debounce(function() { + this.flushText(); + }, 500), + flushText: function(text) { + if (!this._text || this.get("text") === this._text) { + return; + } + return Actions.App.act(new Actions.ChangeText(this, this._text)); }, hasChildren: function() { return this.get("bullets").length > 0; diff --git a/src/views/todo.js b/src/views/todo.js index 25619e1..4d2e0d8 100644 --- a/src/views/todo.js +++ b/src/views/todo.js @@ -50,6 +50,7 @@ var TodoView = Backbone.View.extend({ return this; }, stopEditingText: function() { + this.model.flushText(); this.$el.find("> .text").blur(); return this; }, @@ -229,6 +230,7 @@ var TodoView = Backbone.View.extend({ console.log("unexpected number of lines in textChange"); } else if (lines.length === 1) { // Normal edit + // Debounce it. this.model.setText(this.decodeText(lines[0])); // If there's a line break in the text, they pressed "Enter", and it's more complicated. -- 2.47.3