From 04f06ebefefd0137d41eb5eaa249be3bc701088e Mon Sep 17 00:00:00 2001 From: nightwing Date: Thu, 21 Feb 2013 10:23:57 +0400 Subject: [PATCH] use config defineOptions --- demo/kitchen-sink/demo.js | 3 +-- lib/ace/elastic_tabstops_lite.js | 41 +++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/demo/kitchen-sink/demo.js b/demo/kitchen-sink/demo.js index 80b25b06..4836a78e 100644 --- a/demo/kitchen-sink/demo.js +++ b/demo/kitchen-sink/demo.js @@ -381,8 +381,7 @@ bindDropdown("split", function(value) { bindCheckbox("elastic_tabstops", function(checked) { - if (checked === true) - new ElasticTabstopsLite(env.editor); + env.editor.setOption("useElasticTabstops", checked); }); diff --git a/lib/ace/elastic_tabstops_lite.js b/lib/ace/elastic_tabstops_lite.js index 5eb9dcf6..efbe7882 100644 --- a/lib/ace/elastic_tabstops_lite.js +++ b/lib/ace/elastic_tabstops_lite.js @@ -33,10 +33,30 @@ define(function(require, exports, module) { var ElasticTabstopsLite = function(editor) { this.$editor = editor; + var self = this; + var changedRows = []; + var recordChanges = false; + this.onAfterExec = function() { + recordChanges = false; + self.processRows(changedRows); + changedRows = []; + }; + this.onExec = function() { + recordChanges = true; + }; + this.onChange = function(e) { + var range = e.data.range + if (recordChanges) { + if (changedRows.indexOf(range.start.row) == -1) + changedRows.push(range.start.row); + if (range.end.row != range.start.row) + changedRows.push(range.end.row); + } + }; }; (function() { - this.processRow = function(rows) { + this.processRows = function(rows) { this.$inChange = true; var checkedRows = []; @@ -272,4 +292,23 @@ var ElasticTabstopsLite = function(editor) { exports.ElasticTabstopsLite = ElasticTabstopsLite; +var Editor = require("./editor").Editor; +require("./config").defineOptions(Editor.prototype, "editor", { + useElasticTabstops: { + set: function(val) { + if (val) { + if (!this.elasticTabstops) + this.elasticTabstops = new ElasticTabstopsLite(this); + this.commands.on("afterExec", this.elasticTabstops.onAfterExec); + this.commands.on("exec", this.elasticTabstops.onExec); + this.on("change", this.elasticTabstops.onChange); + } else if (this.elasticTabstops) { + this.commands.removeListener("afterExec", this.elasticTabstops.onAfterExec); + this.commands.removeListener("exec", this.elasticTabstops.onExec); + this.removeListener("change", this.elasticTabstops.onChange); + } + } + } +}); + }); \ No newline at end of file