From 491a4395c5d26dfc2b1e65bfacca1fc456c2e3ce Mon Sep 17 00:00:00 2001 From: harkirat Date: Tue, 4 Jul 2017 08:07:39 -0700 Subject: [PATCH] Emmitted and cached all filesystem events and events for initialization --- src/editor/Collaboration.js | 114 ++++++++++++++++++++++++++++++++---- 1 file changed, 104 insertions(+), 10 deletions(-) diff --git a/src/editor/Collaboration.js b/src/editor/Collaboration.js index ef773551146..236212a10b1 100644 --- a/src/editor/Collaboration.js +++ b/src/editor/Collaboration.js @@ -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({ @@ -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); @@ -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 0) { @@ -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; itext[i].length) { + i++; + index-=text[i].length; + } + } catch (e) { + console.log("exception : "+e); + } + return {line: i, ch: index}; + }; + exports.Collaboration = Collaboration; });