Google Drive realtime sync
This commit is contained in:
parent
d1be6b1566
commit
890a71ec13
@ -480,7 +480,7 @@ div.dropdown-menu textarea {
|
|||||||
|
|
||||||
.icon-gdrive.realtime {
|
.icon-gdrive.realtime {
|
||||||
width: 18px;
|
width: 18px;
|
||||||
background-position: -180px 0;
|
background-position: -162px 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon-dropbox {
|
.icon-dropbox {
|
||||||
|
BIN
img/icons.png
BIN
img/icons.png
Binary file not shown.
Before Width: | Height: | Size: 5.4 KiB After Width: | Height: | Size: 4.7 KiB |
@ -127,6 +127,12 @@ define([
|
|||||||
|
|
||||||
// Run the next task in the queue if any and no other running
|
// Run the next task in the queue if any and no other running
|
||||||
function runTask() {
|
function runTask() {
|
||||||
|
|
||||||
|
// Wait for user first interaction before running first task
|
||||||
|
if(core.isUserReal === false) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Use defer to avoid stack overflow
|
// Use defer to avoid stack overflow
|
||||||
_.defer(function() {
|
_.defer(function() {
|
||||||
|
|
||||||
|
@ -26,12 +26,12 @@ define([
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Used to detect user activity
|
// Used to detect user activity
|
||||||
var userReal = false;
|
core.isUserReal = false;
|
||||||
var userActive = false;
|
var userActive = false;
|
||||||
var windowUnique = true;
|
var windowUnique = true;
|
||||||
var userLastActivity = 0;
|
var userLastActivity = 0;
|
||||||
function setUserActive() {
|
function setUserActive() {
|
||||||
userReal = true;
|
core.isUserReal = true;
|
||||||
userActive = true;
|
userActive = true;
|
||||||
userLastActivity = utils.currentTime;
|
userLastActivity = utils.currentTime;
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ define([
|
|||||||
// Used to only have 1 window of the application in the same browser
|
// Used to only have 1 window of the application in the same browser
|
||||||
var windowId = undefined;
|
var windowId = undefined;
|
||||||
function checkWindowUnique() {
|
function checkWindowUnique() {
|
||||||
if(userReal === false || windowUnique === false) {
|
if(core.isUserReal === false || windowUnique === false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(windowId === undefined) {
|
if(windowId === undefined) {
|
||||||
|
@ -85,6 +85,13 @@ define([
|
|||||||
task.chain(localAuthenticate);
|
task.chain(localAuthenticate);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
googleHelper.forceAuthenticate = function() {
|
||||||
|
authenticated = false;
|
||||||
|
var task = new AsyncTask();
|
||||||
|
connect(task);
|
||||||
|
authenticate(task);
|
||||||
|
task.enqueue();
|
||||||
|
};
|
||||||
|
|
||||||
googleHelper.upload = function(fileId, parentId, title, content, etag, callback) {
|
googleHelper.upload = function(fileId, parentId, title, content, etag, callback) {
|
||||||
var result = undefined;
|
var result = undefined;
|
||||||
@ -458,7 +465,7 @@ define([
|
|||||||
task.enqueue();
|
task.enqueue();
|
||||||
};
|
};
|
||||||
|
|
||||||
googleHelper.loadRealtime = function(fileId, content, callback) {
|
googleHelper.loadRealtime = function(fileId, content, callback, errorCallback) {
|
||||||
var doc = undefined;
|
var doc = undefined;
|
||||||
var task = new AsyncTask();
|
var task = new AsyncTask();
|
||||||
connect(task);
|
connect(task);
|
||||||
@ -473,10 +480,8 @@ define([
|
|||||||
var string = model.createString(content);
|
var string = model.createString(content);
|
||||||
model.getRoot().set('content', string);
|
model.getRoot().set('content', string);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
// handleErrors
|
errorCallback(err);
|
||||||
handleError({
|
task.error(new Error(err.message));
|
||||||
code: err.type
|
|
||||||
}, task);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
task.onSuccess(function() {
|
task.onSuccess(function() {
|
||||||
@ -503,7 +508,7 @@ define([
|
|||||||
task.retry(new Error(errorMsg));
|
task.retry(new Error(errorMsg));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if(error.code === 401 || error.code === 403) {
|
else if(error.code === 401 || error.code === 403 || error.code == "token_refresh_required") {
|
||||||
authenticated = false;
|
authenticated = false;
|
||||||
errorMsg = "Access to Google account is not authorized.";
|
errorMsg = "Access to Google account is not authorized.";
|
||||||
task.retry(new Error(errorMsg), 1);
|
task.retry(new Error(errorMsg), 1);
|
||||||
|
@ -272,13 +272,19 @@ define([
|
|||||||
var realtimeBinding = undefined;
|
var realtimeBinding = undefined;
|
||||||
var undoExecute = undefined;
|
var undoExecute = undefined;
|
||||||
var redoExecute = undefined;
|
var redoExecute = undefined;
|
||||||
gdriveProvider.startRealtimeSync = function(localTitle, localContent, syncAttributes, callback) {
|
gdriveProvider.startRealtimeSync = function(fileDesc, syncAttributes) {
|
||||||
logger.log("Starting Google Drive realtime synchronization");
|
googleHelper.loadRealtime(syncAttributes.id, fileDesc.content, function(err, doc) {
|
||||||
googleHelper.loadRealtime(syncAttributes.id, localContent, function(err, doc) {
|
|
||||||
if(err || !doc) {
|
if(err || !doc) {
|
||||||
callback(err);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If user just switched to another document
|
||||||
|
if(fileMgr.currentFile !== fileDesc) {
|
||||||
|
doc.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.log("Starting Google Drive realtime synchronization");
|
||||||
realtimeDocument = doc;
|
realtimeDocument = doc;
|
||||||
var model = realtimeDocument.getModel();
|
var model = realtimeDocument.getModel();
|
||||||
var string = model.getRoot().get('content');
|
var string = model.getRoot().get('content');
|
||||||
@ -292,7 +298,6 @@ define([
|
|||||||
var debouncedRefreshPreview = _.debounce(editor.refreshPreview, 100);
|
var debouncedRefreshPreview = _.debounce(editor.refreshPreview, 100);
|
||||||
// Called when a modification has been detected
|
// Called when a modification has been detected
|
||||||
function contentChangeListener(e) {
|
function contentChangeListener(e) {
|
||||||
console.log(e);
|
|
||||||
// If modification comes down from a collaborator
|
// If modification comes down from a collaborator
|
||||||
if(e.isLocal === false) {
|
if(e.isLocal === false) {
|
||||||
logger.log("Google Drive realtime document updated from server");
|
logger.log("Google Drive realtime document updated from server");
|
||||||
@ -304,7 +309,6 @@ define([
|
|||||||
string.addEventListener(gapi.drive.realtime.EventType.TEXT_INSERTED, contentChangeListener);
|
string.addEventListener(gapi.drive.realtime.EventType.TEXT_INSERTED, contentChangeListener);
|
||||||
string.addEventListener(gapi.drive.realtime.EventType.TEXT_DELETED, contentChangeListener);
|
string.addEventListener(gapi.drive.realtime.EventType.TEXT_DELETED, contentChangeListener);
|
||||||
realtimeDocument.addEventListener(gapi.drive.realtime.EventType.DOCUMENT_SAVE_STATE_CHANGED, function(e) {
|
realtimeDocument.addEventListener(gapi.drive.realtime.EventType.DOCUMENT_SAVE_STATE_CHANGED, function(e) {
|
||||||
console.log(e);
|
|
||||||
// Save success event
|
// Save success event
|
||||||
if(e.isPending === false && e.isSaving === false) {
|
if(e.isPending === false && e.isSaving === false) {
|
||||||
logger.log("Google Drive realtime document successfully saved on server");
|
logger.log("Google Drive realtime document successfully saved on server");
|
||||||
@ -313,6 +317,7 @@ define([
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Try to merge offline modifications
|
// Try to merge offline modifications
|
||||||
|
var localContent = fileDesc.content;
|
||||||
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
|
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
|
||||||
var remoteContent = string.getText();
|
var remoteContent = string.getText();
|
||||||
var remoteContentCRC = utils.crc32(remoteContent);
|
var remoteContentCRC = utils.crc32(remoteContent);
|
||||||
@ -321,8 +326,8 @@ define([
|
|||||||
if(fileContentChanged === true && localContentChanged === true) {
|
if(fileContentChanged === true && localContentChanged === true) {
|
||||||
if(remoteContentChanged === true) {
|
if(remoteContentChanged === true) {
|
||||||
// Conflict detected
|
// Conflict detected
|
||||||
fileMgr.createFile(localTitle + " (backup)", localContent);
|
fileMgr.createFile(fileDesc.title + " (backup)", localContent);
|
||||||
extensionMgr.onMessage('Conflict detected on "' + localTitle + '". A backup has been created locally.');
|
extensionMgr.onMessage('Conflict detected on "' + fileDesc.title + '". A backup has been created locally.');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Add local modifications if no collaborators change
|
// Add local modifications if no collaborators change
|
||||||
@ -357,12 +362,24 @@ define([
|
|||||||
editor.uiManager.setButtonState(editor.uiManager.buttons.undo, model.canUndo);
|
editor.uiManager.setButtonState(editor.uiManager.buttons.undo, model.canUndo);
|
||||||
editor.uiManager.setButtonState(editor.uiManager.buttons.redo, model.canRedo);
|
editor.uiManager.setButtonState(editor.uiManager.buttons.redo, model.canRedo);
|
||||||
}
|
}
|
||||||
model.addEventListener(
|
model.addEventListener(gapi.drive.realtime.EventType.UNDO_REDO_STATE_CHANGED, setUndoRedoState);
|
||||||
gapi.drive.realtime.EventType.UNDO_REDO_STATE_CHANGED,
|
|
||||||
setUndoRedoState);
|
|
||||||
setUndoRedoState();
|
setUndoRedoState();
|
||||||
|
|
||||||
callback();
|
}, function(err) {
|
||||||
|
console.error(err);
|
||||||
|
if(err.type == "token_refresh_required") {
|
||||||
|
googleHelper.forceAuthenticate();
|
||||||
|
}
|
||||||
|
else if(err.type == "not_found") {
|
||||||
|
extensionMgr.onError('"' + fileDesc.title + '" has been removed from Google Drive.');
|
||||||
|
fileDesc.removeSyncLocation(syncAttributes);
|
||||||
|
extensionMgr.onSyncRemoved(fileDesc, syncAttributes);
|
||||||
|
gdriveProvider.stopRealtimeSync();
|
||||||
|
}
|
||||||
|
else if(err.isFatal) {
|
||||||
|
extensionMgr.onError('An error has forced real time synchronization to stop.');
|
||||||
|
gdriveProvider.stopRealtimeSync();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -240,10 +240,7 @@ define([
|
|||||||
// 2. we are online
|
// 2. we are online
|
||||||
function tryStartRealtimeSync() {
|
function tryStartRealtimeSync() {
|
||||||
if(realtimeFileDesc !== undefined && isOnline === true) {
|
if(realtimeFileDesc !== undefined && isOnline === true) {
|
||||||
core.lockUI(true);
|
realtimeSyncAttributes.provider.startRealtimeSync(realtimeFileDesc, realtimeSyncAttributes);
|
||||||
realtimeSyncAttributes.provider.startRealtimeSync(realtimeFileDesc.title, realtimeFileDesc.content, realtimeSyncAttributes, function() {
|
|
||||||
core.lockUI(false);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,7 +316,7 @@ define([
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(_.size(fileDesc.syncLocations) > 0 && _.first(_.values(obj)).isRealtime) {
|
if(_.size(fileDesc.syncLocations) > 0 && _.first(_.values(fileDesc.syncLocations)).isRealtime) {
|
||||||
extensionMgr.onError("Realtime collaboration document can't be synchronized with multiple locations");
|
extensionMgr.onError("Realtime collaboration document can't be synchronized with multiple locations");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -344,7 +341,7 @@ define([
|
|||||||
// Provider's manual export button
|
// Provider's manual export button
|
||||||
$(".action-sync-manual-" + provider.providerId).click(function(event) {
|
$(".action-sync-manual-" + provider.providerId).click(function(event) {
|
||||||
var fileDesc = fileMgr.currentFile;
|
var fileDesc = fileMgr.currentFile;
|
||||||
if(_.size(fileDesc.syncLocations) > 0 && _.first(_.values(obj)).isRealtime) {
|
if(_.size(fileDesc.syncLocations) > 0 && _.first(_.values(fileDesc.syncLocations)).isRealtime) {
|
||||||
extensionMgr.onError("Realtime collaboration document can't be synchronized with multiple locations");
|
extensionMgr.onError("Realtime collaboration document can't be synchronized with multiple locations");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -137,6 +137,7 @@ define([
|
|||||||
// Reset input control in all modals
|
// Reset input control in all modals
|
||||||
utils.resetModalInputs = function() {
|
utils.resetModalInputs = function() {
|
||||||
$(".modal input[type=text]:not([disabled]), .modal input[type=password], .modal textarea").val("");
|
$(".modal input[type=text]:not([disabled]), .modal input[type=password], .modal textarea").val("");
|
||||||
|
$(".modal input[type=checkbox]").prop("checked", false);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Basic trim function
|
// Basic trim function
|
||||||
|
Loading…
Reference in New Issue
Block a user