Commits

Eugene Lazutkin committed 37a26f4

More work on mobwrite:
* hack to detect the initial file set.
* up-to-date interface to the editor.
* refactored the current version of UI.

Comments (0)

Files changed (4)

plugins/labs/collab/index.js

             "name": "social_view",
             "pointer": "view#SocialView"
         },
-		{
-			"ep": "bufferFileChanged",
-			"pointer": "#mobwriteFileChanged"
-		},
 
 */
 
  */
 var ShareNode = function() {
 	this.username = env.session.currentUser;
-	this.project = project.getProjectAndPath(env.file.path);
+	this.fullPath = env.file.path;
+	this.project = project.getProjectAndPath(this.fullPath);
 	var projectname = this.project[0].name;
 	if (projectname.indexOf('+') < 0) {
 		// add username
             console.trace();
             throw new Error('Attempt to getClientText() before onFirstSync() called.');
         }
-		return env.model.getValue();
+		//return env.model.getValue();
+		return env.editor.value;
     },
 
     /**
 
         if (this.errorRaised) {
             if (!this.readOnlyStateBeforeError) {
-				// TODO: how to replace it?
                 //this.editor.setReadOnly(false);
+				env.editor.readOnly = false;
             }
             this.errorRaised = false;
         }
      */
     setClientText: function(text) {
         var cursor = this.captureCursor();
-		env.model.setValue(text);
+		//env.model.setValue(text);
+		env.editor.value = text;
         this.restoreCursor(cursor);
 
         this.syncDone();
      * Set the read-only flag on the editor
      */
     setReadOnly: function(readonly) {
-		// TODO: how to replace it?
         //this.editor.setReadOnly(readonly);
+		env.editor.readOnly = readonly;
     },
 
     /**
         var suffix = '<br/><strong>Warning</strong>: Changes since the last sync could be lost';
 
         if (!this.errorRaised) {
-			// TODO: how to replace it?
-            //this.readOnlyStateBeforeError = this.editor.readonly;
+            this.readOnlyStateBeforeError = env.editor.readOnly;
             //this.editor.setReadOnly(true);
+			env.editor.readOnly = true;
             this.errorRaised = true;
         }
     },
         var newClientText = this._patchApply(patches, oldClientText, offsets);
         // Set the new text only if there is a change to be made.
         if (oldClientText != newClientText) {
-			env.model.setValue(newClientText);
+			//env.model.setValue(newClientText);
+			env.editor.value = newClientText;
             if (cursor) {
                 // Unpack the offset array.
                 cursor.startOffset = offsets[0];
      * @private
      */
     captureSimpleCursor: function() {
-		var selection = env.view.getSelectedRange();
+		//var selection = env.view.getSelectedRange();
+		var selection = env.editor.selection;
 		return this._convertRangeToOffsets(selection);
     },
 
         dmp.Match_Threshold = 0.9;
 
         var padLength = dmp.Match_MaxBits / 2; // Normally 16.
-        var newText = env.model.getValue();
+        //var newText = env.model.getValue();
+        var newText = env.editor.value;
 
         // Find the start of the selection in the new text.
         var pattern1 = cursor.startPrefix + cursor.startSuffix;
 
         // Cursor position
         var range = this._convertOffsetsToRange(cursorStartPoint, cursorEndPoint);
+		env.editor.selection = range;
+		
+		/*
 		var view = env.view;
 		view.moveCursorTo(range.start);
 
         if (cursorEndPoint != cursorStartPoint) {
 			view.moveCursorTo(range.end, true);
         }
+		*/
 
 		// TODO: what to do with scroll bars?
         // Scroll bars
 	
     /**
      * Convert range (two row-col pairs) to offsets.
-     * @param {Object} range Normalized Range object {start: {row, column}, end: {row, column}}
+     * @param {Object} range Normalized Range object {start: {row, col}, end: {row, col}}
      * @return {Object} Offsets object {startOffset, endOffset}
      * @private
      */
 			for (; i < l; ++i) {
 				offset += lines[i].length + 1; // +1 for LF
 			}
-			startOffset = offset + Math.min(range.start.column, i < lines.length ? lines[i].length : 0);
+			startOffset = offset + Math.min(range.start.col, i < lines.length ? lines[i].length : 0);
 			
 			l = Math.min(range.end.row, lines.length);
 			for (; i < l; ++i) {
 				offset += lines[i].length + 1; // +1 for LF
 			}
-			endOffset = offset + Math.min(range.end.column, i < lines.length ? lines[i].length : 0);
+			endOffset = offset + Math.min(range.end.col, i < lines.length ? lines[i].length : 0);
 		}
 		
 		return {startOffset: startOffset, endOffset: endOffset};
      * Convert offsets to a normalized range.
      * @param {Number} startOffset Start offset
      * @param {Number?} endOffset End offset
-     * @return {Object} Normalized Range object {start: {row, column}, end: {row, column}}
+     * @return {Object} Normalized Range object {start: {row, col}, end: {row, col}}
      * @private
      */
 	_convertOffsetsToRange: function(startOffset, endOffset){
 		var endRow = i;
 		var endCol = i < l ? endOffset - offset : 0;
 
-		return {start: {row: startRow, column: startCol}, end: {row: endRow, column: endCol}};
+		return {start: {row: startRow, col: startCol}, end: {row: endRow, col: endCol}};
 	}
 };
 
 
 exports.mobwriteFileChanged = function() {
 	if (env.session && env.file) {
-		var newShareNode = new ShareNode();
 		if (shareNode) {
-			if (shareNode.project == newShareNode.project) {
+			if (shareNode.fullPath == env.file.path) {
 				return;
 			} else {
 				mobwrite.unshare([shareNode]);
 			}
 		}
-		shareNode = newShareNode;
+		shareNode = new ShareNode();
 		mobwrite.share(shareNode);
 	}else{
 		if (shareNode) {
 	mobwrite.reflect(msg.text);
 	server.schedulePoll(mobwrite.syncInterval);
 };
+
+exports.onAppLaunched = function() {
+	//TODO: major hack: polling to detect when a file was set!
+	var h = setInterval(function() {
+			if (env.session && env.file) {
+				clearInterval(h);
+				exports.mobwriteFileChanged();
+			}
+		}, 100);
+};

plugins/labs/collab/mobwrite/core.js

  */
 
 var server = require("bespin_server").server;
-
 var diff_match_patch = require("diff");
 
 var DIFF_EQUAL = diff_match_patch.DIFF_EQUAL;

plugins/labs/collab/package.json

 	},
     "provides": [
 		{
+			"ep": "appLaunched",
+			"pointer": "#onAppLaunched"
+		},
+		{
 			"ep": "editorChange",
             "match": "[buffer]",
 			"pointer": "#mobwriteFileChanged"

plugins/labs/collab/view.js

 var project_m = require('project');
 
 var social_user = require('collab:user');
+var util = require('collab:util');
 
 /*
 var ChatLineView = SC.View.extend(SC.StaticLayout, {
 */
 
 function formatUser(msg){
-    return '<img width="16" height="16" src="' +
-        social_user.getAvatarImageUrl(msg.from, 64) +
-        '">&nbsp;<strong>' + msg.from + '</strong>';
+    return util.replace(
+        '<img width="16" height="16" class="social_user_name_{name}" src="{url}">&nbsp;<strong>{name}</strong>',
+        {
+            name: msg.from,
+            url: social_user.getAvatarImageUrl(msg.from, 16)
+        }
+    );
 }
 
 exports.broadcastMsg = function (msg) {
     notifier.notify({
         plugin: 'collab',
         notification: 'fileEvent',
-        body: formatUser(msg) + ' works with files:<br>' + msg.event + ' /' + msg.owner + '+' + msg.project + '/' + msg.path
+        body: formatUser(msg) + util.replace(' works with files:<br>{event} /{owner}+{project}/{path}', msg)
     });
 }