Skip to content

Commit

Permalink
Implemented review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hkirat committed Jul 9, 2017
1 parent da01840 commit 04acf28
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 70 deletions.
5 changes: 3 additions & 2 deletions src/bramble/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ define([
if (options.hideUntilReady) {
_iframe.style.visibility = "visible";
}

if(options.enableCollaboration) {
self._executeRemoteCommand({commandCategory: "bramble", command: "INITIALIZE_COLLABORATION",args: [{collaborationUrl: options.collaborationUrl}]});
}
// Set intial state
_state.fullPath = data.fullPath;
_state.filename = data.filename;
Expand All @@ -304,7 +306,6 @@ define([
_state.autoUpdate = data.autoUpdate;
_state.openSVGasXML = data.openSVGasXML;

setReadyState(Bramble.READY);
}
// Listen for callbacks from commands we triggered via _executeRemoteCommand
else if(data.type === "bramble:remoteCommand:callback") {
Expand Down
112 changes: 56 additions & 56 deletions src/editor/Collaboration.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,64 @@
define(function (require, exports, module) {
"use strict";

var SimpleWebRTC = require("simplewebrtc");
var StartupState = require("bramble/StartupState");
var EditorManager = require("editor/EditorManager");
var Initializer = require("editor/Initializer");
var FileSystemEntry = require("filesystem/FileSystem");
var DocumentManager = require("document/DocumentManager");
var SimpleWebRTC = require("simplewebrtc"),
StartupState = require("bramble/StartupState"),
EditorManager = require("editor/EditorManager"),
Initializer = require("editor/Initializer"),
FileSystemEntry = require("filesystem/FileSystem"),
DocumentManager = require("document/DocumentManager");

function Collaboration() {
var webrtc = new SimpleWebRTC({
var _webrtc;
var _changing;
var _pending;
var _room;

function initialize(options) {
if(_webrtc) {
console.error("Collaboration already initialized");
return;
}
_webrtc = new SimpleWebRTC({
// the id/element dom element that will hold "our" videos
localVideoEl: 'localVideo',
// the id/element dom element that will hold remote videos
remoteVideosEl: 'remotesVideos',
// immediately ask for camera access
autoRequestMedia: false,
// TODO : Shift this to config.
url: "localhost:8888"
url: options.collaborationUrl
});
//To be moved to the bramble API.
var query = (new URL(window.location.href)).searchParams;
this.room = query.get("collaboration") || Math.random().toString(36).substring(7);
var self = this;
webrtc.joinRoom("brackets-" + this.room, function() {
self.webrtc.sendToAll("new client", {});
self.webrtc.on("createdPeer", function(peer) {
self.initializeNewClient(peer);
_room = options.room;
_webrtc.joinRoom(_room, function() {
_webrtc.sendToAll("new client", {});
_webrtc.on("createdPeer", function(peer) {
_initializeNewClient(peer);
});

self.webrtc.connection.on('message', function (msg) {
self.handleMessage(msg);
_webrtc.connection.on('message', function (msg) {
_handleMessage(msg);
});
});
console.log(this.room);
this.webrtc = webrtc;
this.pending = []; // pending clients that need to be initialized.
this.changing = false;
console.log(_room);
_pending = []; // pending clients that need to be initialized.
_changing = false;
};

Collaboration.prototype.init = function(codemirror) {
this.codemirror = codemirror;
};

Collaboration.prototype.handleMessage = function(msg) {
function _handleMessage(msg) {
switch(msg.type) {
case "new client":
this.pending.push(msg.from);
_pending.push(msg.from);
break;
case "codemirror-change":
this.handleCodemirrorChange(msg.payload);
_handleCodemirrorChange(msg.payload);
break;
case "initFiles":
var cm = this.getOpenCodemirrorInstance(msg.payload.path.replace(StartupState.project("root"), ""));
var cm = _getOpenCodemirrorInstance(msg.payload.path.replace(StartupState.project("root"), ""));
if(cm) {
this.changing = true;
_changing = true;
cm.setValue(msg.payload.content);
this.changing = false;
console.log("file changed in codemirror" + msg.payload.path);
_changing = false;
console.log("file initializing in codemirror" + msg.payload.path);
} else {
// No cm instance attached to file, need to change directly in indexeddb.
var file = FileSystemEntry.getFileForPath(msg.payload.path);
Expand All @@ -67,14 +68,13 @@ define(function (require, exports, module) {
file.write(msg.payload.content, {}, function(err) {
console.log(err);
});
console.log("file initializing in indexeddb" + msg.payload.path);
}
break;
}
};

Collaboration.prototype.initializeNewClient = function(peer) {
this.changing = false;
var self = this;
function _initializeNewClient(peer) {
Initializer.initialize(function(fileName) {
var file = FileSystemEntry.getFileForPath(fileName);
file.read({}, function(err, text) {
Expand All @@ -85,21 +85,20 @@ define(function (require, exports, module) {
});
};

Collaboration.prototype.handleCodemirrorChange = function(params) {
if(this.changing) {
function _handleCodemirrorChange(params) {
if(_changing) {
return;
}
var delta = params.delta;
var relPath = params.path;
var fullPath = StartupState.project("root") + relPath;
var cm = this.getOpenCodemirrorInstance(params.path);
var cm = _getOpenCodemirrorInstance(params.path);
if(!cm) {
var file = FileSystemEntry.getFileForPath(fullPath);
var self = this;
console.log("writting to file which is not open in editor." + file);
return;
}
this.changing = true;
_changing = true;
var start = cm.indexFromPos(delta.from);
// apply the delete operation first
if (delta.removed.length > 0) {
Expand All @@ -117,30 +116,31 @@ define(function (require, exports, module) {
var from = cm.posFromIndex(start);
var to = from;
cm.replaceRange(param, from, to);
this.changing = false;
_changing = false;
};

Collaboration.prototype.triggerCodemirrorChange = function(changeList, fullPath) {
if(this.changing) {
function _getOpenCodemirrorInstance(relPath) {
var fullPath = StartupState.project("root") + relPath;
var doc = DocumentManager.getOpenDocumentForPath(fullPath);
if(doc && doc._masterEditor) {
return doc._masterEditor._codeMirror;
}
return null;
};

function triggerCodemirrorChange(changeList, fullPath) {
if(_changing) {
return;
}
var relPath = fullPath.replace(StartupState.project("root"), "");
for(var i = 0; i<changeList.length; i++) {
this.webrtc.sendToAll("codemirror-change", {
_webrtc.sendToAll("codemirror-change", {
delta: changeList[i],
path: relPath
});
}
};

Collaboration.prototype.getOpenCodemirrorInstance = function(relPath) {
var fullPath = StartupState.project("root") + relPath;
var doc = DocumentManager.getOpenDocumentForPath(fullPath);
if(doc && doc._masterEditor) {
return doc._masterEditor._codeMirror;
}
return null;
};

exports.Collaboration = Collaboration;
exports.initialize = initialize;
exports.triggerCodemirrorChange = triggerCodemirrorChange;
});
6 changes: 2 additions & 4 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ define(function (require, exports, module) {
ValidationUtils = require("utils/ValidationUtils"),
ViewUtils = require("utils/ViewUtils"),
MainViewManager = require("view/MainViewManager"),
Collaboration = require("editor/Collaboration").Collaboration,
Collaboration = require("editor/Collaboration"),
_ = require("thirdparty/lodash");

/** Editor preferences */
Expand All @@ -103,7 +103,6 @@ define(function (require, exports, module) {


var cmOptions = {};
var collabInstance = new Collaboration();

/**
* Constants
Expand Down Expand Up @@ -432,7 +431,6 @@ define(function (require, exports, module) {
// CodeMirror-focused. Instead, track focus via onFocus and onBlur
// options and track state with this._focused
this._focused = false;
collabInstance.init(this._codeMirror);

this._installEditorListeners();

Expand Down Expand Up @@ -932,7 +930,7 @@ define(function (require, exports, module) {
// Editor dispatches a change event before this event is dispatched, because
// CodeHintManager needs to hook in here when other things are already done.

collabInstance.triggerCodemirrorChange(changeList, this.getFile().fullPath);
Collaboration.triggerCodemirrorChange(changeList, this.getFile().fullPath);
this.trigger("editorChange", this, changeList);
};

Expand Down
6 changes: 6 additions & 0 deletions src/extensions/default/bramble/lib/RemoteCommandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ define(function (require, exports, module) {
var PreferencesManager = brackets.getModule("preferences/PreferencesManager");
var _ = brackets.getModule("thirdparty/lodash");
var ArchiveUtils = brackets.getModule("filesystem/impls/filer/ArchiveUtils");
var Collaboration = brackets.getModule("editor/Collaboration");

var SVGUtils = require("lib/SVGUtils");
var MouseManager = require("lib/MouseManager");
Expand Down Expand Up @@ -195,6 +196,11 @@ define(function (require, exports, module) {
skipCallback = true;
CommandManager.execute("bramble.addCodeSnippet", args[0]).always(callback);
break;
case "INITIALIZE_COLLABORATION":
var query = (new URL(window.location.href)).searchParams;
var room = query.get("collaboration") || Math.random().toString(36).substring(7);
Collaboration.initialize({room: room, collaborationUrl: args[0].collaborationUrl});
break;
default:
console.log('[Bramble] unknown command:', command);
skipCallback = true;
Expand Down
12 changes: 4 additions & 8 deletions src/hosted.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,11 @@
}

function load(Bramble) {
var hash = location.hash.replace(/^#/, "");
var m = /&?collaboration=([^&]*)/.exec(hash);
var url = "index.html";
if(m && m[1]) {
url = url + "#&collaboration=" + m[1];
}
Bramble.load("#bramble",{
url: url,
useLocationSearch: true
url: "index.html",
useLocationSearch: true,
enableCollaboration: true,
collaborationUrl: "localhost:8888"
});

// Event listeners
Expand Down

0 comments on commit 04acf28

Please sign in to comment.