Skip to content

Commit

Permalink
Emmitted and cached all filesystem events and events for initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
hkirat committed Jul 4, 2017
1 parent 999d282 commit 491a439
Showing 1 changed file with 104 additions and 10 deletions.
114 changes: 104 additions & 10 deletions src/editor/Collaboration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ define(function (require, exports, module) {
var SimpleWebRTC = require("simplewebrtc");
var StartupState = require("bramble/StartupState");
var EditorManager = require("editor/EditorManager");
var Initializer = require("editor/Initializer");
var FileSystemEntry = require("filesystem/FileSystem");


function Collaboration() {
var webrtc = new SimpleWebRTC({
Expand All @@ -21,9 +24,8 @@ define(function (require, exports, module) {
} else {
this.room = Math.random().toString(36).substring(7);
}

var self = this;
webrtc.joinRoom("brackets-"+this.room, function() {
webrtc.joinRoom("brackets-" + this.room, function() {
self.webrtc.sendToAll("new client", {});
self.webrtc.on("createdPeer", function(peer) {
self.initializeNewClient(peer);
Expand Down Expand Up @@ -57,39 +59,85 @@ define(function (require, exports, module) {
return;
}
this.changing = true;
this.codemirror.setValue(msg.payload);
EditorManager.getCurrentFullEditor()._codeMirror.setValue(msg.payload);
this.changing = false;
break;
case "initFiles":
if(this.fileIsCurrentlyOpen(msg.payload.path.replace(StartupState.project("root"), ""))) {
this.changing = true;
EditorManager.getCurrentFullEditor()._codeMirror.setValue(msg.payload.content);
this.changing = false;
console.log("file changed in codemirror" + msg.payload.path);
} else {
var file = FileSystemEntry.getFileForPath(msg.payload.path);
if(!file) {
return;
}
file.write(msg.payload.content, {}, function(err) {
console.log(err);
});
}
break;
}
};

Collaboration.prototype.initializeNewClient = function(peer) {
// TODO: Recursively send all files, not just the currently open file.
this.changing = true;
for(var i = 0; i<this.pending.length; i++) {
if(this.pending[i] === peer.id) {
peer.send("initClient", this.codemirror.getValue());
peer.send("initClient", EditorManager.getCurrentFullEditor()._codeMirror.getValue());
this.pending.splice(i, 1);
break;
}
}
this.changing = false;
var self = this;
Initializer.initialize(function(fileName) {
var file = FileSystemEntry.getFileForPath(fileName);
file.read({}, function(err, text) {
if(!err) {
peer.send("initFiles", {path: fileName, content: text});
}
});
});
};

Collaboration.prototype.handleCodemirrorChange = function(params) {
if(this.changing) {
return;
}
var delta = params.delta;
var relPath = params.path;
var fullPath = StartupState.project("root") + relPath;
var delta = params.delta;
var currentEditor = EditorManager.getCurrentFullEditor();
if(currentEditor.getFile().fullPath !== fullPath) {
console.log("client changing " + fullPath + "open file is "+currentEditor.getFile().fullPath);
if(!this.fileIsCurrentlyOpen(params.path)) {
var file = FileSystemEntry.getFileForPath(fullPath);
var self = this;
file.read({}, function(err, text) {
self.changing = true;
if(delta.removed.length) {
var start = self.indexFromPos(text, delta.from);
var delLength = 0;
for (var i = 0; i < delta.removed.length; i++) {
delLength += delta.removed[i].length;
}
delLength += delta.removed.length - 1;
var from = self.indexFromPos(text, start);
var to = self.posFromIndex(text, start + delLength);
text = self.replaceRange(text, '', self.posFromIndex(text, start), to);
}
var param = delta.text.join('\n');
var from = self.posFromIndex(text, start);
var to = from;
text = self.replaceRange(text, param, from, to);
file.write(text, {}, function(err) {
console.log(err);
});
self.changing = false;
});
return;
}
this.changing = true;
var cm = this.codemirror;
var cm = EditorManager.getCurrentFullEditor()._codeMirror;
var start = cm.indexFromPos(delta.from);
// apply the delete operation first
if (delta.removed.length > 0) {
Expand Down Expand Up @@ -123,5 +171,51 @@ define(function (require, exports, module) {
}
};

Collaboration.prototype.replaceRange = function(text, param, from, to) {
//Assuming change in same line
var lines = text.split("\n");
//return text.slice(0, from) + param + text.slice( to, this.indexFromPos({line: text.length, ch: text[text.length - 1].length }));
if(from.line === to.line) {
lines[from.line] = lines[from.line].slice(0, from.ch) + param + lines[from.line].slice(to.ch, lines[from.line].length);
} else {
lines[from.line] = lines[from.line].slice(0, from.ch);
lines[to.line] = lines[to.line].slice(to.ch, text[from.line].length);
}
for(var i = from.line + 1; i<to.line; i++) {
lines.splice(from.line + 1, 1);
}
return lines.join("\n");
};

Collaboration.prototype.indexFromPos = function(text, pos) {
var ch = 0;
var lines = text.split("\n");
for(var i = 0; i<pos.line; i++) {
ch += (lines[i].length);
}
ch += pos.ch;
return ch;
};

Collaboration.prototype.fileIsCurrentlyOpen = function(relPath) {
var fullPath = StartupState.project("root") + relPath;
var currentEditor = EditorManager.getCurrentFullEditor();
return currentEditor.getFile(relPath).fullPath === fullPath;
};

Collaboration.prototype.posFromIndex = function(text, index) {
var i = 0;
text = text.split("\n");
try {
while(index>text[i].length) {
i++;
index-=text[i].length;
}
} catch (e) {
console.log("exception : "+e);
}
return {line: i, ch: index};
};

exports.Collaboration = Collaboration;
});

0 comments on commit 491a439

Please sign in to comment.