Integration of Google Drive Realtime API

This commit is contained in:
benweet 2013-07-18 01:07:22 +01:00
parent a522f3ada2
commit 194b018cd2
6 changed files with 92 additions and 4 deletions

View File

@ -214,6 +214,9 @@ define([
var documentContent = undefined; var documentContent = undefined;
var undoManager = undefined; var undoManager = undefined;
core.initEditor = function(fileDescParam) { core.initEditor = function(fileDescParam) {
if(fileDesc !== undefined) {
extensionMgr.onFileClosed(fileDesc);
}
fileDesc = fileDescParam; fileDesc = fileDescParam;
documentContent = undefined; documentContent = undefined;
var initDocumentContent = fileDesc.content; var initDocumentContent = fileDesc.content;
@ -223,6 +226,7 @@ define([
// If the editor is already created // If the editor is already created
undoManager.reinit(initDocumentContent, fileDesc.editorStart, fileDesc.editorEnd, fileDesc.editorScrollTop); undoManager.reinit(initDocumentContent, fileDesc.editorStart, fileDesc.editorEnd, fileDesc.editorScrollTop);
editor.refreshPreview(); editor.refreshPreview();
extensionMgr.onFileOpen(fileDesc);
return; return;
} }
var previewContainerElt = $(".preview-container"); var previewContainerElt = $(".preview-container");
@ -308,6 +312,7 @@ define([
editor.hooks.chain("onPreviewRefresh", extensionMgr.onAsyncPreview); editor.hooks.chain("onPreviewRefresh", extensionMgr.onAsyncPreview);
undoManager = editor.run(previewWrapper); undoManager = editor.run(previewWrapper);
undoManager.reinit(initDocumentContent, fileDesc.editorStart, fileDesc.editorEnd, fileDesc.editorScrollTop); undoManager.reinit(initDocumentContent, fileDesc.editorStart, fileDesc.editorEnd, fileDesc.editorScrollTop);
extensionMgr.onFileOpen(fileDesc);
// Hide default buttons // Hide default buttons
$(".wmd-button-row").addClass("btn-group").find("li:not(.wmd-spacer)").addClass("btn").css("left", 0).find("span").hide(); $(".wmd-button-row").addClass("btn-group").find("li:not(.wmd-spacer)").addClass("btn").css("left", 0).find("span").hide();

View File

@ -46,14 +46,15 @@ define([
} }
// Return a function that calls every callbacks with the specified name from all extensions // Return a function that calls every callbacks with the specified name from all extensions
var hookCallbackList = {};
function createHook(hookName, noLog) { function createHook(hookName, noLog) {
var callbackList = getExtensionCallbackList(hookName); hookCallbackList[hookName] = getExtensionCallbackList(hookName);
return function() { return function() {
if(!noLog) { if(!noLog) {
logger.log(hookName, arguments); logger.log(hookName, arguments);
} }
var callbackArguments = arguments; var callbackArguments = arguments;
_.each(callbackList, function(callback) { _.each(hookCallbackList[hookName], function(callback) {
// In case user custom callback contains error // In case user custom callback contains error
try { try {
callback.apply(null, callbackArguments); callback.apply(null, callbackArguments);
@ -64,6 +65,9 @@ define([
}); });
}; };
} }
extensionMgr.addHookCallback = function(hookName, callback) {
hookCallbackList[hookName].push(callback);
};
// Add a Hook to the extensionMgr // Add a Hook to the extensionMgr
function addHook(hookName, noLog) { function addHook(hookName, noLog) {
@ -116,6 +120,8 @@ define([
addHook("onFileCreated"); addHook("onFileCreated");
addHook("onFileDeleted"); addHook("onFileDeleted");
addHook("onFileSelected"); addHook("onFileSelected");
addHook("onFileOpen");
addHook("onFileClosed");
addHook("onContentChanged"); addHook("onContentChanged");
addHook("onTitleChanged"); addHook("onTitleChanged");

View File

@ -15,6 +15,7 @@ define([
// Defines the current file // Defines the current file
fileMgr.currentFile = undefined; fileMgr.currentFile = undefined;
// Set the current file and refresh the editor
fileMgr.selectFile = function(fileDesc) { fileMgr.selectFile = function(fileDesc) {
fileDesc = fileDesc || fileMgr.currentFile; fileDesc = fileDesc || fileMgr.currentFile;
@ -48,7 +49,7 @@ define([
} }
} }
// Refresh the editor // Refresh the editor (even if it's the same file)
core.initEditor(fileDesc); core.initEditor(fileDesc);
}; };

View File

@ -409,6 +409,36 @@ define([
task.enqueue(); task.enqueue();
}; };
googleHelper.loadRealtime = function(fileId, content, callback) {
var doc = undefined;
var task = new AsyncTask();
connect(task);
authenticate(task);
task.onRun(function() {
gapi.drive.realtime.load(fileId, function(result) {
// onFileLoaded
doc = result;
task.chain();
}, function(model) {
// initializeModel
var string = model.createString(content);
model.getRoot().set('content', string);
}, function(err) {
// handleErrors
handleError({
code: err.type
}, task);
});
task.onSuccess(function() {
callback(undefined, doc);
});
});
task.onError(function(error) {
callback(error);
});
task.enqueue();
};
function handleError(error, task) { function handleError(error, task) {
var errorMsg = undefined; var errorMsg = undefined;
if(error) { if(error) {

View File

@ -243,6 +243,33 @@ define([
return publishAttributes; return publishAttributes;
}; };
var editor = undefined;
extensionMgr.addHookCallback("onEditorConfigure", function(editorParam) {
editor = editorParam;
});
var binding = undefined;
gdriveProvider.onSyncStart = function(fileDesc, syncAttributes) {
console.log("onSyncStart");
console.log(syncAttributes);
googleHelper.loadRealtime(syncAttributes.id, fileDesc.content, function(err, doc) {
if(err || !doc) {
return;
}
var string = doc.getModel().getRoot().get('content');
binding = gapi.drive.realtime.databinding.bindString(string, $("#wmd-input")[0]);
var debouncedRefreshPreview = _.debounce(editor.refreshPreview, 100);
string.addEventListener(gapi.drive.realtime.EventType.TEXT_INSERTED, debouncedRefreshPreview);
string.addEventListener(gapi.drive.realtime.EventType.TEXT_DELETED, debouncedRefreshPreview);
});
};
gdriveProvider.onSyncStop = function(syncAttributes) {
console.log("onSyncStop");
if(binding !== undefined) {
binding.unbind();
}
};
core.onReady(function() { core.onReady(function() {
var state = utils.retrieveIgnoreError(PROVIDER_GDRIVE + ".state"); var state = utils.retrieveIgnoreError(PROVIDER_GDRIVE + ".state");
if(state === undefined) { if(state === undefined) {

View File

@ -188,6 +188,23 @@ define([
return true; return true;
}; };
function onFileOpen(fileDesc) {
_.each(fileDesc.syncLocations, function(syncAttributes) {
if(_.isFunction(syncAttributes.provider.onSyncStart)) {
syncAttributes.provider.onSyncStart(fileDesc, syncAttributes);
}
});
}
function onFileClosed(fileDesc) {
_.each(fileDesc.syncLocations, function(syncAttributes) {
if(_.isFunction(syncAttributes.provider.onSyncStop)) {
syncAttributes.provider.onSyncStop(syncAttributes);
}
});
}
// Initialize the export dialog // Initialize the export dialog
function initExportDialog(provider) { function initExportDialog(provider) {
@ -251,6 +268,8 @@ define([
}); });
}); });
extensionMgr.addHookCallback("onFileOpen", onFileOpen);
extensionMgr.addHookCallback("onFileClosed", onFileClosed);
extensionMgr.onSynchronizerCreated(synchronizer); extensionMgr.onSynchronizerCreated(synchronizer);
return synchronizer; return synchronizer;
}); });