diff --git a/components/collaborative/collaborative.php b/components/collaborative/controler.php similarity index 85% rename from components/collaborative/collaborative.php rename to components/collaborative/controler.php index cf1e10a..fa7adbe 100644 --- a/components/collaborative/collaborative.php +++ b/components/collaborative/controler.php @@ -26,5 +26,10 @@ echo json_encode($data); } + if (isset($_POST["cursor"]) && !empty($_POST["cursor"])) { + $data = json_decode($_POST["cursor"]); + echo json_encode($data); + } + ?> diff --git a/components/collaborative/init.js b/components/collaborative/init.js index 8342aac..efc701f 100644 --- a/components/collaborative/init.js +++ b/components/collaborative/init.js @@ -23,10 +23,16 @@ codiad.collaborative = { - controller: 'components/collaborative/collaborative.php', + controller: 'components/collaborative/controler.php', init: function () { this.$onDocumentChange = this.onDocumentChange.bind(this); + this.$onCursorChange = this.onCursorChange.bind(this); + }, + + addListeners: function () { + this.addListenerToOnDocumentChange(); + this.addListenerToOnCursorChange(); }, addListenerToOnDocumentChange: function () { @@ -39,6 +45,16 @@ session.removeEventListener('change', this.$onDocumentChange); }, + addListenerToOnCursorChange: function () { + var selection = this._getSelection(); + selection.addEventListener('changeCursor', this.$onCursorChange); + }, + + removeListenerToOnCursorChange: function () { + var selection = this._getSelection(); + selection.removeEventListener('changeCursor', this.$onCursorChange); + }, + onDocumentChange: function (e) { console.log('document change'); var post = { change: JSON.stringify(e.data) }; @@ -49,8 +65,24 @@ url: this.controller, data: post, complete: function (data) { - console.log('complete'); - console.log(data.text); + console.log('complete doc change'); + console.log(data); + } + }); + }, + + onCursorChange: function (e) { + console.log('cursor change'); + var post = { cursor: JSON.stringify(this._getSelection().getRange()) }; + console.log(post); + + $.ajax({ + type: 'POST', + url: this.controller, + data: post, + complete: function (data) { + console.log('complete cursor'); + console.log(data); } }); }, @@ -64,6 +96,10 @@ return codiad.editor.getActive().getSession(); }, + _getSelection: function () { + return codiad.editor.getActive().getSelection(); + }, + _getDocument: function () { return codiad.editor.getActive().getSession().getDocument(); } diff --git a/components/keybindings/init.js b/components/keybindings/init.js index f2c4539..606a977 100755 --- a/components/keybindings/init.js +++ b/components/keybindings/init.js @@ -77,8 +77,8 @@ $.ctrl('32', function() { // TODO remove this it is for testing purpose. // codiad.autocomplete.suggest(); - console.log('add listener.'); - codiad.collaborative.addListenerToOnDocumentChange(); + console.log('add listeners.'); + codiad.collaborative.addListeners(); }); $.ctrl('71', function(){