Hook listener to changeCursor event.

This commit is contained in:
Florent Galland
2013-01-02 18:52:49 +01:00
parent d6fbd0e882
commit 12f32a624a
3 changed files with 46 additions and 5 deletions

View File

@@ -26,5 +26,10 @@
echo json_encode($data);
}
if (isset($_POST["cursor"]) && !empty($_POST["cursor"])) {
$data = json_decode($_POST["cursor"]);
echo json_encode($data);
}
?>

View File

@@ -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();
}

View File

@@ -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(){