Dropped realtime sync support

This commit is contained in:
benweet 2014-04-13 11:45:31 +01:00
parent 5f380b461a
commit 5ce2dea7c8
13 changed files with 63 additions and 678 deletions

View File

@ -124,9 +124,7 @@ define([
_.chain(this.syncLocations).sortBy(function(attributes) { _.chain(this.syncLocations).sortBy(function(attributes) {
return attributes.provider.providerId; return attributes.provider.providerId;
}).each(function(attributes) { }).each(function(attributes) {
var classes = 'icon-provider-' + attributes.provider.providerId; result.push('<i class="icon-provider-' + attributes.provider.providerId + '"></i>');
attributes.isRealtime === true && (classes += ' realtime');
result.push('<i class="' + classes + '"></i>');
}); });
if(_.size(this.syncLocations) !== 0) { if(_.size(this.syncLocations) !== 0) {
result.push('<i class="icon-refresh title-icon-category"></i>'); result.push('<i class="icon-refresh title-icon-category"></i>');
@ -134,9 +132,7 @@ define([
_.chain(this.publishLocations).sortBy(function(attributes) { _.chain(this.publishLocations).sortBy(function(attributes) {
return attributes.provider.providerId; return attributes.provider.providerId;
}).each(function(attributes) { }).each(function(attributes) {
var classes = 'icon-provider-' + attributes.provider.providerId; result.push('<i class="icon-provider-' + attributes.provider.providerId + '"></i>');
attributes.isRealtime === true && (classes += ' realtime');
result.push('<i class="' + classes + '"></i>');
}); });
if(_.size(this.publishLocations) !== 0) { if(_.size(this.publishLocations) !== 0) {
result.push('<i class="icon-upload title-icon-category"></i>'); result.push('<i class="icon-upload title-icon-category"></i>');

View File

@ -50,14 +50,6 @@ define([
}; };
Provider.prototype.parseContent = function(content) { Provider.prototype.parseContent = function(content) {
if(!_.isString(content)) {
// Real time content is already an object
return {
content: content.content,
discussionList: content.discussionList,
discussionListJSON: JSON.stringify(content.discussionList)
};
}
var discussionList; var discussionList;
var discussionListJSON = '{}'; var discussionListJSON = '{}';
var discussionExtractor = /<!--se_discussion_list:([\s\S]+)-->$/.exec(content); var discussionExtractor = /<!--se_discussion_list:([\s\S]+)-->$/.exec(content);

View File

@ -634,6 +634,7 @@ define([
}, 1000); }, 1000);
eventMgr.onReady(); eventMgr.onReady();
// Adjust the layout after the dom has changed // Adjust the layout after the dom has changed
adjustWindow(); adjustWindow();
}; };

View File

@ -312,21 +312,22 @@ define([
var previewButtonsElt = document.querySelector('.extension-preview-buttons'); var previewButtonsElt = document.querySelector('.extension-preview-buttons');
previewButtonsElt.appendChild(extensionPreviewButtonsFragment); previewButtonsElt.appendChild(extensionPreviewButtonsFragment);
// A bit of jQuery... setTimeout(function() {
var $previewButtonsElt = $(previewButtonsElt); var previewButtonsWidth = previewButtonsElt.offsetWidth;
var previewButtonsWidth = $previewButtonsElt.width(); _.each(previewButtonsElt.querySelectorAll('.btn-group'), function(btnGroupElt) {
$previewButtonsElt.find('.btn-group').each(function() { // A bit of jQuery...
var $btnGroupElt = $(this); var $btnGroupElt = $(btnGroupElt);
$btnGroupElt.click(function() { $btnGroupElt.click(function() {
// Align dropdown to the left of the screen // Align dropdown to the left of the screen
$btnGroupElt.find('.dropdown-menu').css({ $btnGroupElt.find('.dropdown-menu').css({
right: -previewButtonsWidth + $btnGroupElt.width() + $btnGroupElt.position().left right: -previewButtonsWidth + $btnGroupElt.width() + $btnGroupElt.position().left
}); });
$btnGroupElt.find('.markdown-syntax, .table-of-contents').css({ $btnGroupElt.find('.markdown-syntax, .table-of-contents').css({
'max-height': $(document).height() - $btnGroupElt.offset().top - 190 'max-height': $(document).height() - $btnGroupElt.offset().top - 190
});
}); });
}); });
}); }, 0);
// Call onReady listeners // Call onReady listeners
onReady(); onReady();

View File

@ -71,7 +71,7 @@ define([
return; return;
} }
window.delayedFunction = function() { window.delayedFunction = function() {
gapi.load("client,drive-realtime", function() { gapi.load("client", function() {
gapi.client.load('drive', 'v2', function() { gapi.client.load('drive', 'v2', function() {
connected = true; connected = true;
task.chain(); task.chain();
@ -257,11 +257,6 @@ define([
var headers = { var headers = {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"', 'Content-Type': 'multipart/mixed; boundary="' + boundary + '"',
}; };
// Sometimes we have error 412 from Google even with the correct
// etag
// if(etag !== undefined) {
// headers["If-Match"] = etag;
// }
var base64Data = utils.encodeBase64(content); var base64Data = utils.encodeBase64(content);
var multipartRequestBody = [ var multipartRequestBody = [
@ -360,49 +355,6 @@ define([
task.enqueue(); task.enqueue();
}; };
googleHelper.createRealtimeFile = function(parentId, title, accountId, callback) {
var result;
var task = new AsyncTask();
connect(task);
authenticate(task, 'gdrive', accountId);
task.onRun(function() {
var metadata = {
title: title,
mimeType: 'application/vnd.google-apps.drive-sdk',
};
if(parentId !== undefined) {
// Specify the directory
metadata.parents = [
{
kind: 'drive#fileLink',
id: parentId
}
];
}
runWithToken(accountId, function() {
var request = gapi.client.drive.files.insert({
'resource': metadata
});
request.execute(function(response) {
if(response && response.id) {
// Upload success
result = response;
task.chain();
return;
}
handleError(response.error, task);
});
});
});
task.onSuccess(function() {
callback(undefined, result);
});
task.onError(function(error) {
callback(error);
});
task.enqueue();
};
googleHelper.checkChanges = function(lastChangeId, accountId, callback) { googleHelper.checkChanges = function(lastChangeId, accountId, callback) {
var changes = []; var changes = [];
var newChangeId = lastChangeId || 0; var newChangeId = lastChangeId || 0;
@ -562,31 +514,7 @@ define([
dataType: file.isRealtime ? 'json' : 'text', dataType: file.isRealtime ? 'json' : 'text',
timeout: constants.AJAX_TIMEOUT timeout: constants.AJAX_TIMEOUT
}).done(function(data) { }).done(function(data) {
if(file.isRealtime) { file.content = file.isRealtime ? data.data.value.content.value : data;
data = data.data.value;
data = {
content: data.content.value,
discussionList: (function() {
var discussionList = {};
data.discussionList && _.each(data.discussionList.value, function(discussionObject) {
var discussion = {
discussionIndex: discussionObject.value.discussionIndex.json,
selectionStart: discussionObject.value.selectionStart.json,
selectionEnd: discussionObject.value.selectionEnd.json,
};
var type = (discussionObject.value.type || {}).json;
type && (discussion.type = type);
var commentList = (discussionObject.value.commentList || {}).value || [];
commentList.length && (discussion.commentList = commentList.map(function(commentObject) {
return commentObject.json;
}));
discussionList[discussion.discussionIndex] = discussion;
});
return discussionList;
})()
};
}
file.content = data;
objects.shift(); objects.shift();
task.chain(recursiveDownloadContent); task.chain(recursiveDownloadContent);
}).fail(function(jqXHR) { }).fail(function(jqXHR) {
@ -609,32 +537,6 @@ define([
task.enqueue(); task.enqueue();
}; };
googleHelper.loadRealtime = function(fileId, accountId, callback, errorCallback) {
var doc;
var task = new AsyncTask();
connect(task);
authenticate(task, 'gdrive', accountId);
task.onRun(function() {
var authorizationMgr = authorizationMgrMap[accountId];
gapi.auth.setToken(authorizationMgr.token);
gapi.drive.realtime.load(fileId, function(result) {
// onFileLoaded
doc = result;
task.chain();
}, undefined, function(err) {
errorCallback(err);
task.error(new Error(err.message));
});
});
task.onSuccess(function() {
callback(undefined, doc);
});
task.onError(function(error) {
callback(error);
});
task.enqueue();
};
googleHelper.uploadImg = function(name, content, albumId, callback) { googleHelper.uploadImg = function(name, content, albumId, callback) {
var accountId = 'google.picasa0'; var accountId = 'google.picasa0';
var result; var result;

View File

@ -21,14 +21,14 @@
<ul class="nav left-buttons"> <ul class="nav left-buttons">
<li class="wmd-button-group3 btn-group"></li> <li class="wmd-button-group3 btn-group"></li>
</ul> </ul>
<ul class="nav left-buttons">
<li class="wmd-button-group5 btn-group"></li>
</ul>
<ul class="nav left-buttons"> <ul class="nav left-buttons">
<li class="wmd-button-group4 btn-group"> <li class="wmd-button-group4 btn-group">
<a class="btn btn-success button-open-discussion"><i class="icon-comment-alt"></i></a> <a class="btn btn-success button-open-discussion"><i class="icon-comment-alt"></i></a>
</li> </li>
</ul> </ul>
<ul class="nav left-buttons">
<li class="wmd-button-group5 btn-group"></li>
</ul>
<ul class="nav pull-right right-buttons"> <ul class="nav pull-right right-buttons">
<li class="offline-status hide"> <li class="offline-status hide">
<div class="text-danger"> <div class="text-danger">

View File

@ -32,17 +32,6 @@
file will be created in your root folder. </span> file will be created in your root folder. </span>
</div> </div>
</div> </div>
<div class="form-group">
<div class="col-lg-3 control-label"></div>
<div class="col-lg-8">
<label> <input id="input-sync-export-<%= providerId %>-realtime"
type="checkbox"> Create a real time collaborative
document
</label>
<span class="help-block"> Collaborative documents cannot be open outside
StackEdit nor have multiple synchronized locations. </span>
</div>
</div>
<div class="form-group"> <div class="form-group">
<label class="col-lg-3 control-label" <label class="col-lg-3 control-label"
for="input-sync-export-<%= providerId %>-fileid">Existing file ID for="input-sync-export-<%= providerId %>-fileid">Existing file ID
@ -55,7 +44,11 @@
</div> </div>
</div> </div>
</div> </div>
<p class="align-right"><a href="#" data-toggle="collapse" data-target=".sync-export-<%= providerId %>-options-collapse">Options...</a></p> <p class="align-right">
<a href="#" data-toggle="collapse" data-target=".sync-export-<%= providerId %>-options-collapse" class="collapsed">
Options <i class="icon-up-dir"></i>
</a>
</p>
<blockquote> <blockquote>
<b>Tip:</b> You can move or rename the file afterwards within Google <b>Tip:</b> You can move or rename the file afterwards within Google
Drive. Drive.

View File

@ -1,8 +1,8 @@
<div class="input-group"> <div class="input-group">
<span class="input-group-addon" <span class="input-group-addon"
title="<%= syncAttributes.provider.providerName %><%= syncAttributes.isRealtime ? ' (real time)' : '' %>"> title="<%= syncAttributes.provider.providerName %>">
<i <i
class="icon-provider-<%= syncAttributes.provider.providerId %><%= syncAttributes.isRealtime ? ' realtime' : '' %>"></i> class="icon-provider-<%= syncAttributes.provider.providerId %>"></i>
</span> <input class="form-control" type="text" </span> <input class="form-control" type="text"
value="<%= syncDesc %>" disabled /> value="<%= syncDesc %>" disabled />
<div class="input-group-btn"> <div class="input-group-btn">

View File

@ -7,8 +7,8 @@
</label> </label>
</div> </div>
<a data-toggle="collapse" data-parent=".accordion-extensions" <a data-toggle="collapse" data-parent=".accordion-extensions"
class="accordion-toggle" href="#accordion-extensions-collapse-<%= extensionId %>"> class="accordion-toggle collapsed" href="#accordion-extensions-collapse-<%= extensionId %>">
<%= extensionName %> <i class="icon-down-dir"></i></a> <%= extensionName %> <i class="icon-up-dir"></i></a>
</div> </div>
<div id="accordion-extensions-collapse-<%= extensionId %>" class="collapse"> <div id="accordion-extensions-collapse-<%= extensionId %>" class="collapse">
<div class="accordion-inner clearfix"><%= settingsBlock %></div> <div class="accordion-inner clearfix"><%= settingsBlock %></div>

View File

@ -23,7 +23,6 @@ define([
gdriveProvider.defaultPublishFormat = "template"; gdriveProvider.defaultPublishFormat = "template";
gdriveProvider.exportPreferencesInputIds = [ gdriveProvider.exportPreferencesInputIds = [
providerId + "-parentid", providerId + "-parentid",
providerId + "-realtime",
]; ];
function createSyncIndex(id) { function createSyncIndex(id) {
@ -63,10 +62,15 @@ define([
var fileDesc; var fileDesc;
_.each(result, function(file) { _.each(result, function(file) {
var parsedContent = gdriveProvider.parseContent(file.content); var parsedContent = gdriveProvider.parseContent(file.content);
var syncAttributes = createSyncAttributes(file.id, file.etag, parsedContent.content, file.title, parsedContent.discussionListJSON); var syncLocations;
syncAttributes.isRealtime = file.isRealtime; if(file.isRealtime) {
var syncLocations = {}; eventMgr.onError('Real time synchronization is not supported anymore. Please use standard synchronization.');
syncLocations[syncAttributes.syncIndex] = syncAttributes; }
else {
var syncAttributes = createSyncAttributes(file.id, file.etag, parsedContent.content, file.title, parsedContent.discussionListJSON);
syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
}
fileDesc = fileMgr.createFile(file.title, parsedContent.content, parsedContent.discussionListJSON, syncLocations); fileDesc = fileMgr.createFile(file.title, parsedContent.content, parsedContent.discussionListJSON, syncLocations);
fileDescList.push(fileDesc); fileDescList.push(fileDesc);
}); });
@ -121,18 +125,6 @@ define([
}); });
}; };
gdriveProvider.exportRealtimeFile = function(event, title, content, discussionListJSON, callback) {
var parentId = utils.getInputTextValue('#input-sync-export-' + providerId + '-parentid');
googleHelper.createRealtimeFile(parentId, title, accountId, function(error, result) {
if(error) {
callback(error);
return;
}
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title, discussionListJSON);
callback(undefined, syncAttributes);
});
};
gdriveProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) { gdriveProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) {
if( if(
(syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed (syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed
@ -141,13 +133,23 @@ define([
) { ) {
return callback(undefined, false); return callback(undefined, false);
} }
var uploadedContent = gdriveProvider.serializeContent(content, discussionList);
googleHelper.upload(syncAttributes.id, undefined, title, uploadedContent, undefined, syncAttributes.etag, accountId, function(error, result) { if(syncAttributes.isRealtime) {
var fileDesc = fileMgr.getFileFromSyncIndex(syncAttributes.syncIndex);
fileDesc.removeSyncLocation(syncAttributes);
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
return eventMgr.onError('Real time synchronization is not supported anymore. Please use standard synchronization.');
}
var data = gdriveProvider.serializeContent(content, discussionList);
googleHelper.upload(syncAttributes.id, undefined, title, data, undefined, syncAttributes.etag, accountId, function(error, result) {
if(error) { if(error) {
callback(error, true); callback(error, true);
return; return;
} }
syncAttributes.etag = result.etag; syncAttributes.etag = result.etag;
// Remove this deprecated flag if exist
delete syncAttributes.isRealtime;
if(merge === true) { if(merge === true) {
// Need to store the whole content for merge // Need to store the whole content for merge
syncAttributes.content = content; syncAttributes.content = content;
@ -161,23 +163,6 @@ define([
}); });
}; };
gdriveProvider.syncUpRealtime = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) {
// Skip if title CRC has not changed
if(titleCRC == syncAttributes.titleCRC) {
callback(undefined, false);
return;
}
googleHelper.rename(syncAttributes.id, title, accountId, function(error, result) {
if(error) {
callback(error, true);
return;
}
syncAttributes.etag = result.etag;
syncAttributes.titleCRC = titleCRC;
callback(undefined, true);
});
};
gdriveProvider.syncDown = function(callback) { gdriveProvider.syncDown = function(callback) {
var lastChangeId = parseInt(storage[accountId + ".gdrive.lastChangeId"], 10); var lastChangeId = parseInt(storage[accountId + ".gdrive.lastChangeId"], 10);
googleHelper.checkChanges(lastChangeId, accountId, function(error, changes, newChangeId) { googleHelper.checkChanges(lastChangeId, accountId, function(error, changes, newChangeId) {
@ -223,11 +208,7 @@ define([
if(change.deleted === true) { if(change.deleted === true) {
eventMgr.onError('"' + fileDesc.title + '" has been removed from ' + providerName + '.'); eventMgr.onError('"' + fileDesc.title + '" has been removed from ' + providerName + '.');
fileDesc.removeSyncLocation(syncAttributes); fileDesc.removeSyncLocation(syncAttributes);
eventMgr.onSyncRemoved(fileDesc, syncAttributes); return eventMgr.onSyncRemoved(fileDesc, syncAttributes);
if(syncAttributes.isRealtime === true && fileMgr.currentFile === fileDesc) {
gdriveProvider.stopRealtimeSync();
}
return;
} }
var file = change.file; var file = change.file;
var parsedContent = gdriveProvider.parseContent(file.content); var parsedContent = gdriveProvider.parseContent(file.content);
@ -278,399 +259,6 @@ define([
return publishAttributes; return publishAttributes;
}; };
// Keep a link to the Pagedown editor
var pagedownEditor;
var undoExecute;
var redoExecute;
var setUndoRedoButtonStates;
eventMgr.addListener("onPagedownConfigure", function(pagedownEditorParam) {
pagedownEditor = pagedownEditorParam;
});
// Realtime closure
var realtimeContext;
(function() {
var inCompoundOperation = false;
function modelChangeWrapper(cb) {
realtimeContext.isChanging = true;
try { cb(); }
finally {
if(inCompoundOperation) {
try { realtimeContext.model.endCompoundOperation(); }
catch(e) {}
inCompoundOperation = false;
}
realtimeContext.isChanging = false;
}
}
function compound(cb) {
if(!inCompoundOperation) {
realtimeContext.model.beginCompoundOperation();
inCompoundOperation = true;
}
cb();
}
function toRealtimeDiscussion(context, discussion) {
var realtimeCommentList = context.model.createList();
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_ADDED, onModelChange);
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_REMOVED, onModelChange);
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_SET, onModelChange);
discussion.commentList && discussion.commentList.forEach(function(comment) {
realtimeCommentList.push({
author: comment.author,
content: comment.content
});
});
var realtimeDiscussion = context.model.createMap({
discussionIndex: discussion.discussionIndex,
selectionStart: discussion.selectionStart,
selectionEnd: discussion.selectionEnd,
type: discussion.type,
commentList: realtimeCommentList
});
return realtimeDiscussion;
}
function toRealtimeDiscussionList(context) {
var realtimeDiscussionList = context.model.createMap();
_.each(context.fileDesc.discussionList, function(localDiscussion) {
var realtimeDiscussion = toRealtimeDiscussion(context, localDiscussion);
realtimeDiscussionList.set(localDiscussion.discussionIndex, realtimeDiscussion);
});
return realtimeDiscussionList;
}
function fromRealtimeDiscussion(realtimeDiscussion) {
var realtimeCommentList = realtimeDiscussion.get('commentList');
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_ADDED, onModelChange);
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_REMOVED, onModelChange);
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_SET, onModelChange);
var discussion = {
discussionIndex: realtimeDiscussion.get('discussionIndex'),
selectionStart: realtimeDiscussion.get('selectionStart'),
selectionEnd: realtimeDiscussion.get('selectionEnd')
};
var type = realtimeDiscussion.get('type');
type && (discussion.type = type);
var commentList = realtimeCommentList.asArray();
commentList.length && (discussion.commentList = commentList);
return discussion;
}
function fromRealtimeDiscussionList(realtimeDiscussionList) {
var localDiscussionList = {};
realtimeDiscussionList.keys().forEach(function(discussionIndex) {
var realtimeDiscussion = realtimeDiscussionList.get(discussionIndex);
var discussion = fromRealtimeDiscussion(realtimeDiscussion);
localDiscussionList[discussionIndex] = discussion;
});
return localDiscussionList;
}
function mergeDiscussion(localDiscussion, realtimeDiscussion, isModelChange) {
var commentsChanged = false;
// We don't pay attention to model selection modifications
if(!isModelChange) {
compound(function() {
realtimeDiscussion.set('selectionStart', localDiscussion.selectionStart);
realtimeDiscussion.set('selectionEnd', localDiscussion.selectionEnd);
});
}
function isInDiscussion(comment, commentList) {
return commentList.some(function(commentInDiscussion) {
if(comment.author == commentInDiscussion.author && comment.content == commentInDiscussion.content) {
return true;
}
});
}
var realtimeCommentList = realtimeDiscussion.get('commentList');
var localCommentList = localDiscussion.commentList;
function checkLocalComment(comment, index) {
if(!isInDiscussion(comment, realtimeCommentList.asArray())) {
if(isModelChange) {
localCommentList.splice(index, 1);
commentsChanged = true;
return true;
}
else {
compound(function() {
realtimeCommentList.push(comment);
});
}
}
}
while(localCommentList.some(checkLocalComment)) {}
function checkRealtimeComment(comment, index) {
if(!isInDiscussion(comment, localCommentList)) {
if(!isModelChange) {
compound(function() {
realtimeCommentList.remove(index);
});
return true;
}
else {
localCommentList.push(comment);
commentsChanged = true;
}
}
}
while(realtimeCommentList.asArray().some(checkRealtimeComment)) {}
return commentsChanged;
}
function mergeDiscussionList(context, isModelChange) {
var commentsChanged = false;
var localDiscussionList = context.fileDesc.discussionList;
_.values(localDiscussionList).forEach(function(localDiscussion) {
var realtimeDiscussion = context.realtimeDiscussionList.get(localDiscussion.discussionIndex);
if(realtimeDiscussion) {
commentsChanged |= mergeDiscussion(localDiscussion, realtimeDiscussion, isModelChange);
}
else if(!isModelChange) {
compound(function() {
realtimeDiscussion = toRealtimeDiscussion(context, localDiscussion);
context.realtimeDiscussionList.set(localDiscussion.discussionIndex, realtimeDiscussion);
});
}
else {
delete localDiscussionList[localDiscussion.discussionIndex];
eventMgr.onDiscussionRemoved(context.fileDesc, localDiscussion);
}
});
context.realtimeDiscussionList.keys().forEach(function(discussionIndex) {
var realtimeDiscussion = context.realtimeDiscussionList.get(discussionIndex);
var localDiscussion = localDiscussionList[discussionIndex];
if(localDiscussion) {
commentsChanged |= mergeDiscussion(localDiscussion, realtimeDiscussion, isModelChange);
}
else if(isModelChange) {
var discussion = fromRealtimeDiscussion(realtimeDiscussion);
localDiscussionList[discussionIndex] = discussion;
eventMgr.onDiscussionCreated(context.fileDesc, discussion);
}
else {
compound(function() {
context.realtimeDiscussionList.delete(discussionIndex);
});
}
});
context.fileDesc.discussionList = localDiscussionList; // Write in localStorage
if(commentsChanged) {
eventMgr.onCommentsChanged(context.fileDesc);
}
}
function updateStatus() {
var context = realtimeContext;
if(!context) {
return;
}
var syncAttributes = context.syncAttributes;
var content = context.realtimeString.getText();
syncAttributes.contentCRC = utils.crc32(content);
var discussionList = {};
context.realtimeDiscussionList.keys().forEach(function(discussionIndex) {
var discussion = fromRealtimeDiscussion(context.realtimeDiscussionList.get(discussionIndex));
discussionList[discussion.discussionIndex] = discussion;
});
var discussionListJSON = JSON.stringify(discussionList);
syncAttributes.discussionListCRC = utils.crc32(discussionListJSON);
if(merge === true) {
// Need to store the whole content for merge
syncAttributes.content = content;
syncAttributes.discussionList = discussionListJSON;
}
utils.storeAttributes(context.syncAttributes);
}
var onChange = (function() {
var debouncedOnChange = utils.debounce(function() {
var context = realtimeContext;
if(!context) {
return;
}
if(context.isModelChange) {
logger.log('Realtime syncing model changes');
}
else {
logger.log('Realtime syncing local changes');
}
modelChangeWrapper(function() {
// Check content modifications
var localContent = context.fileDesc.content;
var remoteContent = context.realtimeString.getText();
var contentChanged = localContent != remoteContent;
if(contentChanged) {
if(context.isModelChange) {
editor.setValue(remoteContent);
}
else {
compound(function() {
context.realtimeString.setText(localContent);
});
}
}
// Check discussion modifications
mergeDiscussionList(context, context.isModelChange);
// For local changes, CRCs are updated on "save success" event
if(context.isModelChange) {
updateStatus();
}
context.isModelChange = false;
});
});
return function(fileDesc) {
if(realtimeContext && realtimeContext.fileDesc === fileDesc) {
debouncedOnChange();
}
};
})();
function onModelChange() {
if(!realtimeContext || realtimeContext.isChanging) {
return;
}
realtimeContext.isModelChange = true;
onChange(realtimeContext.fileDesc);
}
eventMgr.addListener('onContentChanged', onChange);
eventMgr.addListener('onDiscussionCreated', onChange);
eventMgr.addListener('onDiscussionRemoved', onChange);
eventMgr.addListener('onCommentsChanged', onChange);
// Start realtime synchronization
gdriveProvider.startRealtimeSync = function(fileDesc, syncAttributes) {
if(realtimeContext !== undefined) {
return;
}
var context = {
fileDesc: fileDesc,
syncAttributes: syncAttributes
};
realtimeContext = context;
googleHelper.loadRealtime(syncAttributes.id, accountId, function(err, doc) {
if(err || !doc) {
if(context === realtimeContext) {
realtimeContext = undefined;
}
return;
}
// If user just switched to another document or file has just been reselected
if(context !== realtimeContext) {
return doc.close();
}
logger.log("Starting Google Drive realtime synchronization");
context.document = doc;
var model = doc.getModel();
context.model = model;
// Get or create content string
var realtimeString = model.getRoot().get('content');
if(!realtimeString) {
// Initial value
realtimeString = model.createString(fileDesc.content);
model.getRoot().set('content', realtimeString);
}
context.realtimeString = realtimeString;
// Listen to content modifications
realtimeString.addEventListener(gapi.drive.realtime.EventType.TEXT_INSERTED, onModelChange);
realtimeString.addEventListener(gapi.drive.realtime.EventType.TEXT_DELETED, onModelChange);
// Get or create discussion map
var realtimeDiscussionList = model.getRoot().get('discussionList');
if(!realtimeDiscussionList) {
// Initial value
realtimeDiscussionList = toRealtimeDiscussionList(context);
model.getRoot().set('discussionList', realtimeDiscussionList);
}
context.realtimeDiscussionList = realtimeDiscussionList;
// Listen to discussion modifications
realtimeDiscussionList.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED, onModelChange);
realtimeDiscussionList.keys().forEach(function(discussionIndex) {
var realtimeDiscussion = context.realtimeDiscussionList.get(discussionIndex);
var realtimeCommentList = realtimeDiscussion.get('commentList');
// Listen to comment modifications in every discussion
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_ADDED, onModelChange);
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_REMOVED, onModelChange);
realtimeCommentList.addEventListener(gapi.drive.realtime.EventType.VALUES_SET, onModelChange);
});
// Also listen to "save success" event
doc.addEventListener(gapi.drive.realtime.EventType.DOCUMENT_SAVE_STATE_CHANGED, function(evt) {
if(evt.isPending === false && evt.isSaving === false) {
updateStatus();
}
});
// Merge offline modifications
var remoteContent = realtimeString.getText();
var remoteTitle = fileDesc.title; // Not synchronized, so make sure no changes will be detected
var remoteDiscussionList = fromRealtimeDiscussionList(realtimeDiscussionList);
var remoteDiscussionListJSON = JSON.stringify(remoteDiscussionList);
gdriveProvider.syncMerge(fileDesc, syncAttributes, remoteContent, remoteTitle, remoteDiscussionList, remoteDiscussionListJSON);
onChange(context.fileDesc);
// Save undo/redo buttons default actions
undoExecute = pagedownEditor.uiManager.buttons.undo.execute;
redoExecute = pagedownEditor.uiManager.buttons.redo.execute;
setUndoRedoButtonStates = pagedownEditor.uiManager.setUndoRedoButtonStates;
// Set temporary actions for undo/redo buttons
pagedownEditor.uiManager.buttons.undo.execute = _.bind(model.undo, model);
pagedownEditor.uiManager.buttons.redo.execute = _.bind(model.redo, model);
// Add event handler for model's UndoRedoStateChanged events
pagedownEditor.uiManager.setUndoRedoButtonStates = _.debounce(function() {
pagedownEditor.uiManager.setButtonState(pagedownEditor.uiManager.buttons.undo, model.canUndo);
pagedownEditor.uiManager.setButtonState(pagedownEditor.uiManager.buttons.redo, model.canRedo);
}, 10);
pagedownEditor.uiManager.setUndoRedoButtonStates();
model.addEventListener(gapi.drive.realtime.EventType.UNDO_REDO_STATE_CHANGED, function() {
pagedownEditor.uiManager.setUndoRedoButtonStates();
});
}, function(err) {
logger.error(err);
if(err.type == "token_refresh_required") {
googleHelper.refreshGdriveToken(accountId);
}
else if(err.type == "not_found") {
eventMgr.onError('"' + fileDesc.title + '" has been removed from ' + providerName + '.');
fileDesc.removeSyncLocation(syncAttributes);
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
gdriveProvider.stopRealtimeSync();
}
else if(err.isFatal) {
// Retry will be attempted shortly...
gdriveProvider.stopRealtimeSync();
}
});
};
// Stop realtime synchronization
gdriveProvider.stopRealtimeSync = function() {
logger.log("Stopping Google Drive realtime synchronization");
if(realtimeContext !== undefined) {
try { realtimeContext.model.endCompoundOperation(); }
catch(e) {}
realtimeContext.document && realtimeContext.document.close();
realtimeContext = undefined;
}
if(setUndoRedoButtonStates !== undefined) {
// Set back original undo/redo actions
pagedownEditor.uiManager.buttons.undo.execute = undoExecute;
pagedownEditor.uiManager.buttons.redo.execute = redoExecute;
pagedownEditor.uiManager.setUndoRedoButtonStates = setUndoRedoButtonStates;
pagedownEditor.uiManager.setUndoRedoButtonStates();
}
};
})();
// Initialize the AutoSync dialog fields // Initialize the AutoSync dialog fields
gdriveProvider.setAutosyncDialogConfig = function() { gdriveProvider.setAutosyncDialogConfig = function() {
var config = gdriveProvider.autosyncConfig; var config = gdriveProvider.autosyncConfig;
@ -746,13 +334,6 @@ define([
}, 'folder', accountId); }, 'folder', accountId);
}); });
// On export, disable file ID input if realtime is checked
var $realtimeCheckboxElt = $('#input-sync-export-' + providerId + '-realtime');
var $fileIdInputElt = $('#input-sync-export-' + providerId + '-fileid');
$('#input-sync-export-' + providerId + '-realtime').change(function() {
$fileIdInputElt.prop('disabled', $realtimeCheckboxElt.prop('checked'));
});
// Skip gdrive action if provider is not enabled in the settings // Skip gdrive action if provider is not enabled in the settings
if(accountIndex >= settings.gdriveMultiAccount) { if(accountIndex >= settings.gdriveMultiAccount) {
return; return;

View File

@ -342,9 +342,6 @@ kbd {
.icon-provider-gdrive, .icon-provider-gdrivesec, .icon-provider-gdriveter { .icon-provider-gdrive, .icon-provider-gdrivesec, .icon-provider-gdriveter {
background-position: -18px 0; background-position: -18px 0;
&.realtime {
background-position: -162px 0;
}
} }
.icon-provider-dropbox { .icon-provider-dropbox {

View File

@ -182,6 +182,10 @@ body {
} }
} }
.collapsed > .icon-up-dir:before {
content: '\e94d';
}
.modal { .modal {
&.fade .modal-dialog { &.fade .modal-dialog {
.translate(0, 0); .translate(0, 0);

View File

@ -95,14 +95,7 @@ define([
// Dequeue a synchronized location // Dequeue a synchronized location
var syncAttributes = uploadSyncAttributesList.pop(); var syncAttributes = uploadSyncAttributesList.pop();
var providerSyncUpFunction = syncAttributes.provider.syncUp; syncAttributes.provider.syncUp(
// Call a special function in case of a real time synchronized location
if(syncAttributes.isRealtime === true) {
providerSyncUpFunction = syncAttributes.provider.syncUpRealtime;
}
// Use the specified provider to perform the upload
providerSyncUpFunction(
uploadContent, uploadContent,
uploadContentCRC, uploadContentCRC,
uploadTitle, uploadTitle,
@ -237,59 +230,6 @@ define([
return true; return true;
}; };
/***************************************************************************
* Realtime synchronization
**************************************************************************/
var isOnline = true;
// Tries to start/stop real time sync on online/offline event
function onOfflineChanged(isOfflineParam) {
if(isOfflineParam === false) {
isOnline = true;
startRealtimeSync();
}
else {
stopRealtimeSync();
isOnline = false;
}
}
// Starts real time synchronization if:
// 1. current file has real time sync location
// 2. we are online
function startRealtimeSync() {
var fileDesc = fileMgr.currentFile;
_.each(fileDesc.syncLocations, function(syncAttributes) {
syncAttributes.isRealtime && syncAttributes.provider.startRealtimeSync(fileDesc, syncAttributes);
});
}
// Stops previously started synchronization if any
function stopRealtimeSync() {
_.each(providerMap, function(provider) {
provider.stopRealtimeSync && provider.stopRealtimeSync();
});
}
// Triggers realtime synchronization from eventMgr events
if(window.viewerMode === false) {
// On file open, try to start realtime sync
eventMgr.addListener("onFileOpen", startRealtimeSync);
// On new sync location, try to start realtime sync
eventMgr.addListener("onSyncExportSuccess", startRealtimeSync);
// On file close, stop any active realtime synchronization
eventMgr.addListener("onFileClosed", stopRealtimeSync);
// Start/stop realtime sync depending on network status
eventMgr.addListener("onOfflineChanged", onOfflineChanged);
// Try to start realtime sync every 15 sec in case of error
eventMgr.addListener("onPeriodicRun", _.throttle(startRealtimeSync, 15000));
// Stop realtime sync if synchronized location is removed
eventMgr.addListener("onSyncRemoved", function(fileDesc, syncAttributes) {
fileDesc === fileMgr.currentFile && syncAttributes.isRealtime && syncAttributes.provider.stopRealtimeSync();
});
}
/*************************************************************************** /***************************************************************************
* Initialize module * Initialize module
**************************************************************************/ **************************************************************************/
@ -353,37 +293,15 @@ define([
$(".modal-autosync-" + provider.providerId).modal(); $(".modal-autosync-" + provider.providerId).modal();
}); });
$(".action-sync-export-" + provider.providerId).click(function(event) { $(".action-sync-export-" + provider.providerId).click(function(event) {
var isRealtime = utils.getInputChecked("#input-sync-export-" + provider.providerId + "-realtime");
var fileDesc = fileMgr.currentFile; var fileDesc = fileMgr.currentFile;
if(isRealtime) { provider.exportFile(event, fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, function(error, syncAttributes) {
if(_.size(fileDesc.syncLocations) > 0) { if(error) {
return eventMgr.onError("Real time collaborative document can't be synchronized with multiple locations");
}
// Perform the provider's real time export
provider.exportRealtimeFile(event, fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, function(error, syncAttributes) {
if(error) {
return;
}
syncAttributes.isRealtime = true;
fileDesc.addSyncLocation(syncAttributes);
eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
});
}
else {
if(_.size(fileDesc.syncLocations) > 0 && _.first(_.values(fileDesc.syncLocations)).isRealtime) {
eventMgr.onError("Real time collaborative document can't be synchronized with multiple locations");
return; return;
} }
// Perform the provider's standard export fileDesc.addSyncLocation(syncAttributes);
provider.exportFile(event, fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, function(error, syncAttributes) { eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
if(error) { });
return;
}
fileDesc.addSyncLocation(syncAttributes);
eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
});
}
// Store input values as preferences for next time we open the // Store input values as preferences for next time we open the
// export dialog // export dialog