Added sharing links

This commit is contained in:
benweet 2013-05-04 01:05:58 +01:00
parent 8c1430e5d6
commit d98d872e98
20 changed files with 332 additions and 150 deletions

View File

@ -2,12 +2,3 @@ StackEdit - The Markdown editor powered by PageDown.
Copyright 2013 Benoit Schweblin (http://www.benoitschweblin.com)
Licensed under an Apache License (http://www.apache.org/licenses/LICENSE-2.0)
Includes:
jQuery - http://jquery.com/
Bootstrap - http://twitter.github.com/bootstrap/
RequireJS - http://requirejs.org/
PageDown - https://code.google.com/p/pagedown/
UI Layout - http://layout.jquery-dev.net/
jGrowl - https://github.com/stanlemon/jGrowl/
GLYPHICONS - http://glyphicons.com/

View File

@ -80,6 +80,15 @@ textarea,
background-color: #eee;
}
.nav .dropdown-toggle .caret {
border-top-color: #000;
border-bottom-color: #000;
}
.navbar .nav.hide {
display: none;
}
.navbar-inner .btn.disabled,
.navbar-inner .btn[disabled] {
color: #333333;
@ -102,10 +111,15 @@ textarea,
background-color: #777;
}
input[disabled], select[disabled], textarea[disabled], input[readonly], select[readonly], textarea[readonly], .input-append .add-on {
input[disabled], select[disabled], textarea[disabled], .input-prepend .add-on {
background-color: #f5f5f5;
}
input[readonly], select[readonly], textarea[readonly] {
background-color: transparent;
cursor: text;
}
.btn-primary:hover,
.btn-primary:focus,
.btn-primary:active,
@ -232,7 +246,6 @@ hr {
div.dropdown-menu {
padding: 5px 20px;
width: 300px;
white-space: normal;
}
@ -241,6 +254,20 @@ div.dropdown-menu blockquote {
margin: 10px 0;
}
div.dropdown-menu i {
margin-right: 0;
}
.internal-link {
text-decoration: underline;
font-weight: 600;
cursor: pointer;
}
#link-container .link-list {
margin-top: 10px;
}
.icon-link {
background-position: -72px -168px;
}

View File

@ -1,12 +1,10 @@
<!DOCTYPE html>
<html manifest="cache.manifest">
<head>
<title>StackEdit</title>
<title>StackEdit - Markdown editor</title>
<link rel="icon" href="img/stackedit-32.ico" type="image/x-icon">
<link rel="shortcut icon" href="img/stackedit-32.ico"
type="image/x-icon">
<meta name="keywords"
content="Markdown, Editor, PageDown, Stack Overflow, Stack Exchange">
<meta name="description"
content="StackEdit is a free, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites.">
<meta name="author" content="Benoit Schweblin">
@ -32,7 +30,7 @@
<ul class="nav">
<li><div id="wmd-button-bar"></div></li>
</ul>
<ul class="pull-right hide" id="menu-bar">
<ul class="nav pull-right hide" id="menu-bar">
<li class="btn-group">
<button class="btn action-force-sync" title="Synchronize">
<i class="icon-refresh"></i>
@ -46,11 +44,10 @@
</button>
<div id="link-container" class="dropdown-menu">
<div class="link-list"></div>
<p class="no-link">
To share this document you need first to synchronize it or to publish it in Markdown format on
<a href="#" class="action-publish-gist">Gist</a>.
<p class="no-link">To share this document you need first to <span
class="internal-link action-publish-gist">publish it as a
Gist</span> in Markdown format.
</p>
<blockquote class="muted">Please make sure the document is public.</blockquote>
</div>
</li>
<li class="btn-group"><button class="btn action-create-file"
@ -67,9 +64,9 @@
</button>
<ul id="file-selector" class="dropdown-menu">
</ul></li>
<li class="btn-group"><a class="btn dropdown-toggle"
data-toggle="dropdown" href="#" title="Menu"><i
class="icon-stackedit"></i>&nbsp;&nbsp;<b class="caret"></b></a>
<li class="btn-group"><button class="btn dropdown-toggle"
data-toggle="dropdown" title="Menu"><i
class="icon-stackedit"></i>&nbsp;&nbsp;<b class="caret"></b></button>
<ul class="dropdown-menu">
<li><a href="viewer.html"><i class="icon-fullscreen"></i>
Open in viewer</a></li>

View File

@ -1,5 +1,7 @@
define(["jquery", "core", "google-helper"], function($, core, googleHelper) {
var PROVIDER_BLOGGER = "blogger";
var bloggerProvider = {
providerId: PROVIDER_BLOGGER,
providerName: "Blogger",

View File

@ -1,4 +1,5 @@
var MAIN_URL = "http://benweet.github.io/stackedit/";
var GOOGLE_API_KEY = "AIzaSyAeCU8CGcSkn0z9js6iocHuPBX4f_mMWkw";
var GOOGLE_SCOPES = [ "https://www.googleapis.com/auth/drive.install",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/blogger" ];
@ -14,12 +15,6 @@ var ASYNC_TASK_DEFAULT_TIMEOUT = 30000;
var ASYNC_TASK_LONG_TIMEOUT = 90000;
var SYNC_PERIOD = 180000;
var USER_IDLE_THRESHOLD = 300000;
var PROVIDER_BLOGGER = "blogger";
var PROVIDER_DROPBOX = "dropbox";
var PROVIDER_GDRIVE = "gdrive";
var PROVIDER_GIST = "gist";
var PROVIDER_GITHUB = "github";
var PROVIDER_TUMBLR = "tumblr";
var WELCOME_DOCUMENT_TITLE = "Welcome document";
// Use by Google's client.js
@ -32,7 +27,6 @@ function runDelayedFunction() {
// Site dependent
var BASE_URL = "http://localhost/";
var GOOGLE_KEY = "AIzaSyAeCU8CGcSkn0z9js6iocHuPBX4f_mMWkw";
var GOOGLE_CLIENT_ID = '241271498917-lev37kef013q85avc91am1gccg5g8lrb.apps.googleusercontent.com';
var GITHUB_CLIENT_ID = 'e47fef6055344579799d';
var GATEKEEPER_URL = "http://stackedit-gatekeeper-localhost.herokuapp.com/";
@ -40,7 +34,6 @@ var TUMBLR_PROXY_URL = "http://stackedit-tumblr-proxy-local.herokuapp.com/";
if(location.hostname.indexOf("benweet.github.io") === 0) {
BASE_URL = MAIN_URL;
GOOGLE_KEY = "AIzaSyB1Bc1wI_YUWkkOR-5Gri5BFuypgZl0Sxc";
GOOGLE_CLIENT_ID = '241271498917-jpto9lls9fqnem1e4h6ppds9uob8rpvu.apps.googleusercontent.com';
GITHUB_CLIENT_ID = 'fa0d09514da8377ee32e';
GATEKEEPER_URL = "http://stackedit-gatekeeper.herokuapp.com/";

View File

@ -672,6 +672,16 @@ define(
return _.random(4294967296).toString(36);
};
// Access a URL parameter
core.getURLParameter = function(name) {
var regex = new RegExp(name + "=(.+?)(&|$)");
try {
return decodeURIComponent(regex.exec(location.search)[1]);
} catch (e) {
return undefined;
}
};
// Create an centered popup window
core.popupWindow = function(url, title, w, h) {
var left = (screen.width / 2) - (w / 2);
@ -859,7 +869,7 @@ define(
intervalId = window.setInterval(function() {
updateCurrentTime();
core.checkWindowUnique();
if(viewerMode === false && isUserActive() === true) {
if(isUserActive() === true || viewerMode === true) {
_.each(periodicCallbacks, function(callback) {
callback();
});

View File

@ -154,10 +154,11 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
task.chain();
return;
}
var path = paths.pop();
var path = paths[0];
client.stat(path, function(error, stat) {
if(stat) {
result.push(stat);
paths.shift();
task.chain(recursiveDownloadMetadata);
return;
}
@ -187,7 +188,7 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
task.chain();
return;
}
var object = objects.pop();
var object = objects[0];
result.push(object);
var file = undefined;
// object may be a file
@ -199,13 +200,15 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
file = object.stat;
}
if(!file) {
objects.shift();
task.chain(recursiveDownloadContent);
return;
}
client.readFile(file.path, function(error, data) {
if(data) {
file.content = data;
recursiveDownloadContent();
objects.shift();
task.chain(recursiveDownloadContent);
return;
}
handleError(error, task);

View File

@ -1,5 +1,7 @@
define(["jquery", "core", "dropbox-helper"], function($, core, dropboxHelper) {
var PROVIDER_DROPBOX = "dropbox";
var dropboxProvider = {
providerId: PROVIDER_DROPBOX,
providerName: "Dropbox",
@ -27,9 +29,11 @@ define(["jquery", "core", "dropbox-helper"], function($, core, dropboxHelper) {
syncAttributes.path = path;
syncAttributes.version = versionTag;
syncAttributes.contentCRC = core.crc32(content);
var syncIndex = "sync." + PROVIDER_DROPBOX + "." + encodeURIComponent(path.toLowerCase());
localStorage[syncIndex] = JSON.stringify(syncAttributes);
return syncIndex;
return syncAttributes;
}
function createSyncIndex(path) {
return "sync." + PROVIDER_DROPBOX + "." + encodeURIComponent(path.toLowerCase());
}
function importFilesFromPaths(paths) {
@ -41,12 +45,16 @@ define(["jquery", "core", "dropbox-helper"], function($, core, dropboxHelper) {
if(error) {
return;
}
var titleList = [];
_.each(result, function(file) {
var syncIndex = createSyncAttributes(file.path, file.versionTag, file.content);
var syncAttributes = createSyncAttributes(file.path, file.versionTag, file.content);
var syncIndex = createSyncIndex(syncAttributes.path);
localStorage[syncIndex] = JSON.stringify(syncAttributes);
var fileIndex = core.fileManager.createFile(file.name, file.content, [syncIndex]);
core.fileManager.selectFile(fileIndex);
core.showMessage('"' + file.name + '" imported successfully from Dropbox.');
titleList.push('"' + file.name + '"');
});
core.showMessage(titleList.join(", ") + ' imported successfully from Dropbox.');
});
});
}
@ -58,7 +66,7 @@ define(["jquery", "core", "dropbox-helper"], function($, core, dropboxHelper) {
}
var importPaths = [];
_.each(paths, function(path) {
var syncIndex = "sync." + PROVIDER_DROPBOX + "." + encodeURIComponent(path.toLowerCase());
var syncIndex = createSyncIndex(path);
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
if(fileIndex !== undefined) {
var title = localStorage[fileIndex + ".title"];
@ -78,7 +86,7 @@ define(["jquery", "core", "dropbox-helper"], function($, core, dropboxHelper) {
return;
}
// Check that file is not synchronized with an other one
var syncIndex = "sync." + PROVIDER_DROPBOX + "." + encodeURIComponent(path.toLowerCase());
var syncIndex = createSyncIndex(path);
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
if(fileIndex !== undefined) {
var existingTitle = localStorage[fileIndex + ".title"];
@ -91,7 +99,9 @@ define(["jquery", "core", "dropbox-helper"], function($, core, dropboxHelper) {
callback(error);
return;
}
syncIndex = createSyncAttributes(result.path, result.versionTag, content);
var syncAttributes = createSyncAttributes(result.path, result.versionTag, content);
var syncIndex = createSyncIndex(syncAttributes.path);
localStorage[syncIndex] = JSON.stringify(syncAttributes);
callback(undefined, syncIndex);
});
}
@ -137,7 +147,7 @@ define(["jquery", "core", "dropbox-helper"], function($, core, dropboxHelper) {
}
var interestingChanges = [];
_.each(changes, function(change) {
var syncIndex = "sync." + PROVIDER_DROPBOX + "." + encodeURIComponent(change.path.toLowerCase());
var syncIndex = createSyncIndex(change.path);
var serializedAttributes = localStorage[syncIndex];
if(serializedAttributes === undefined) {
return;

View File

@ -1,6 +1,8 @@
define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCOME.md", "underscore"],
function($, core, synchronizer, publisher, sharing, welcomeContent) {
var TEMPORARY_FILE_INDEX = "file.tempIndex";
var fileManager = {};
// Defines the current file
@ -16,7 +18,7 @@ define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCO
if(fileIndex === undefined) {
localStorage.removeItem("file.current");
}
else {
else if(fileIndex != TEMPORARY_FILE_INDEX) {
localStorage["file.current"] = fileIndex;
}
};
@ -38,6 +40,14 @@ define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCO
synchronizer.refreshManageSync();
publisher.notifyPublish();
// Hide the viewer pencil button
if(fileIndex == TEMPORARY_FILE_INDEX) {
$(".action-edit-document").removeClass("hide");
}
else {
$(".action-edit-document").addClass("hide");
}
// Recreate the editor
fileIndex = fileManager.getCurrentFileIndex();
$("#wmd-input").val(localStorage[fileIndex + ".content"]);
@ -46,7 +56,7 @@ define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCO
});
};
fileManager.createFile = function(title, content, syncIndexes) {
fileManager.createFile = function(title, content, syncIndexes, isTemporary) {
content = content || core.settings.defaultContent;
syncIndexes = syncIndexes || [];
if (!title) {
@ -61,10 +71,12 @@ define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCO
}
// Generate a unique fileIndex
var fileIndex = undefined;
var fileIndex = TEMPORARY_FILE_INDEX;
if(!isTemporary) {
do {
fileIndex = "file." + core.randomString();
} while(_.has(localStorage, fileIndex + ".title"));
}
// Create the file in the localStorage
localStorage[fileIndex + ".content"] = content;
@ -74,7 +86,9 @@ define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCO
}, ";");
localStorage[fileIndex + ".sync"] = sync;
localStorage[fileIndex + ".publish"] = ";";
if(!isTemporary) {
localStorage["file.list"] += fileIndex + ";";
}
return fileIndex;
};
@ -132,22 +146,18 @@ define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCO
}
synchronizer.resetSyncFlags();
var links = [];
function composeTitle(fileIndex, generateLinks) {
function composeTitle(fileIndex, refreshSharing) {
var result = [];
var syncAttributesList = synchronizer.getSyncAttributesFromFile(fileIndex);
var publishAttributesList = publisher.getPublishAttributesFromFile(fileIndex);
var attributesList = syncAttributesList.concat(publishAttributesList);
if(refreshSharing === true) {
sharing.refreshDocumentSharing(attributesList);
}
_.chain(attributesList).sortBy(function(attributes) {
return attributes.provider;
}).each(function(attributes) {
result.push('<i class="icon-' + attributes.provider + '"></i>');
if(generateLinks === true) {
var url = sharing.getLink(attributes);
if(url !== undefined) {
links.push(url);
}
}
});
result.push(" ");
result.push(localStorage[fileIndex + ".title"]);
@ -160,15 +170,6 @@ define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCO
$("#file-title").html(composeTitle(fileIndex, true));
$(".file-title").text(title);
$("#file-title-input").val(title);
var linkList = $(".link-list").empty();
if(links.length > 0) {
_.each(links, function(url) {
linkList.append($('<div><a href="' + url + '">' + url + '</a></div>'));
});
$("#link-container no-link").hide();
} else {
$("#link-container no-link").show();
}
// Update the file selector
$("#file-selector").empty();
@ -280,6 +281,16 @@ define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCO
applyTitle($(this));
}
});
$(".action-open-stackedit").click(function() {
window.location.href = ".";
});
$(".action-edit-document").click(function() {
var content = $("#wmd-input").val();
var title = localStorage[fileManager.getCurrentFileIndex() + ".title"];
var fileIndex = fileManager.createFile(title, content);
fileManager.selectFile(fileIndex);
window.location.href = ".";
});
$(".action-download-md").click(function() {
var content = $("#wmd-input").val();
var title = localStorage[fileManager.getCurrentFileIndex() + ".title"];

View File

@ -1,10 +1,11 @@
define(["jquery", "core", "google-helper", "underscore"], function($, core, googleHelper) {
var PROVIDER_GDRIVE = "gdrive";
var gdriveProvider = {
providerId: PROVIDER_GDRIVE,
providerName: "Google Drive",
defaultPublishFormat: "template",
sharingAttributes: ["id"],
useSync: false
};
@ -15,9 +16,11 @@ define(["jquery", "core", "google-helper", "underscore"], function($, core, goog
syncAttributes.etag = etag;
syncAttributes.contentCRC = core.crc32(content);
syncAttributes.titleCRC = core.crc32(title);
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + id;
localStorage[syncIndex] = JSON.stringify(syncAttributes);
return syncIndex;
return syncAttributes;
}
function createSyncIndex(id) {
return "sync." + PROVIDER_GDRIVE + "." + id;
}
function importFilesFromIds(ids) {
@ -29,12 +32,16 @@ define(["jquery", "core", "google-helper", "underscore"], function($, core, goog
if(error) {
return;
}
var titleList = [];
_.each(result, function(file) {
var syncIndex = createSyncAttributes(file.id, file.etag, file.content, file.title);
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = JSON.stringify(syncAttributes);
var fileIndex = core.fileManager.createFile(file.title, file.content, [syncIndex]);
core.fileManager.selectFile(fileIndex);
core.showMessage('"' + file.title + '" imported successfully from Google Drive.');
titleList.push('"' + file.title + '"');
});
core.showMessage(titleList.join(", ") + ' imported successfully from Google Drive.');
});
});
};
@ -46,7 +53,7 @@ define(["jquery", "core", "google-helper", "underscore"], function($, core, goog
}
var importIds = [];
_.each(ids, function(id) {
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + id;
var syncIndex = createSyncIndex(id);
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
if(fileIndex !== undefined) {
var title = localStorage[fileIndex + ".title"];
@ -65,7 +72,9 @@ define(["jquery", "core", "google-helper", "underscore"], function($, core, goog
callback(error);
return;
}
var syncIndex = createSyncAttributes(result.id, result.etag, content, title);
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = JSON.stringify(syncAttributes);
callback(undefined, syncIndex);
});
};
@ -76,7 +85,7 @@ define(["jquery", "core", "google-helper", "underscore"], function($, core, goog
return;
}
// Check that file is not synchronized with an other one
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + id;
var syncIndex = createSyncIndex(id);
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
if(fileIndex !== undefined) {
var existingTitle = localStorage[fileIndex + ".title"];
@ -89,7 +98,9 @@ define(["jquery", "core", "google-helper", "underscore"], function($, core, goog
callback(error);
return;
}
var syncIndex = createSyncAttributes(result.id, result.etag, content, title);
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = JSON.stringify(syncAttributes);
callback(undefined, syncIndex);
});
};
@ -127,7 +138,7 @@ define(["jquery", "core", "google-helper", "underscore"], function($, core, goog
}
var interestingChanges = [];
_.each(changes, function(change) {
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + change.fileId;
var syncIndex = createSyncIndex(change.fileId);
var serializedAttributes = localStorage[syncIndex];
if(serializedAttributes === undefined) {
return;
@ -263,7 +274,7 @@ define(["jquery", "core", "google-helper", "underscore"], function($, core, goog
else if (state.action == "open") {
var importIds = [];
_.each(state.ids, function(id) {
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + id;
var syncIndex = createSyncIndex(id);
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
if(fileIndex !== undefined) {
core.fileManager.selectFile(fileIndex);

View File

@ -1,12 +1,15 @@
define(["jquery", "core", "github-helper"], function($, core, githubHelper) {
var PROVIDER_GIST = "gist";
var gistProvider = {
providerId: PROVIDER_GIST,
providerName: "Gist"
providerName: "Gist",
sharingAttributes: ["gistId", "filename"]
};
gistProvider.publish = function(publishAttributes, title, content, callback) {
githubHelper.gistUpload(publishAttributes.gistId, publishAttributes.filename, publishAttributes.isPublic,
githubHelper.uploadGist(publishAttributes.gistId, publishAttributes.filename, publishAttributes.isPublic,
title, content, function(error, gistId) {
if(error) {
callback(error);
@ -29,5 +32,9 @@ define(["jquery", "core", "github-helper"], function($, core, githubHelper) {
return publishAttributes;
};
gistProvider.importPublic = function(importParameters, callback) {
githubHelper.downloadGist(importParameters.gistId, importParameters.filename, callback);
};
return gistProvider;
});

View File

@ -144,7 +144,7 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
asyncRunner.addTask(task);
};
githubHelper.gistUpload = function(gistId, filename, isPublic, title, content, callback) {
githubHelper.uploadGist = function(gistId, filename, isPublic, title, content, callback) {
callback = callback || core.doNothing;
var task = asyncRunner.createTask();
connect(task);
@ -183,6 +183,41 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
asyncRunner.addTask(task);
};
githubHelper.downloadGist = function(gistId, filename, callback) {
callback = callback || core.doNothing;
var task = asyncRunner.createTask();
connect(task);
// No need for authentication
var title = undefined;
var content = undefined;
task.onRun(function() {
var github = new Github({});
var gist = github.getGist(gistId);
gist.read(function(err, gist) {
if(err) {
// Handle error
task.error(new Error('Error trying to access Gist ' + gistId + '.'));
return;
}
title = gist.description;
var file = gist.files[filename];
if(file === undefined) {
task.error(new Error('Gist ' + gistId + ' does not contain "' + filename + '".'));
return;
}
content = file.content;
task.chain();
});
});
task.onSuccess(function() {
callback(undefined, title, content);
});
task.onError(function(error) {
callback(error);
});
asyncRunner.addTask(task);
};
function handleError(error, task) {
var errorMsg = undefined;
if (error) {

View File

@ -1,5 +1,7 @@
define(["jquery", "core", "github-helper"], function($, core, githubHelper) {
var PROVIDER_GITHUB = "github";
var githubProvider = {
providerId: PROVIDER_GITHUB,
providerName: "GitHub"

View File

@ -200,30 +200,35 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
asyncRunner.addTask(task);
};
googleHelper.downloadMetadata = function(ids, callback) {
googleHelper.downloadMetadata = function(ids, callback, skipAuth) {
callback = callback || core.doNothing;
var result = [];
var task = asyncRunner.createTask();
connect(task);
if(!skipAuth) {
authenticate(task);
}
task.onRun(function() {
function recursiveDownloadMetadata() {
if(ids.length === 0) {
task.chain();
return;
}
var id = ids.pop();
var id = ids[0];
var headers = {};
var token = gapi.auth.getToken();
var headers = {
Authorization : token ? "Bearer " + token.access_token: null
};
if(token) {
headers.Authorization = "Bearer " + token.access_token;
}
$.ajax({
url : "https://www.googleapis.com/drive/v2/files/" + id,
headers : headers,
data : {key: GOOGLE_API_KEY},
dataType : "json",
timeout : AJAX_TIMEOUT
}).done(function(data, textStatus, jqXHR) {
result.push(data);
ids.shift();
task.chain(recursiveDownloadMetadata);
}).fail(function(jqXHR) {
var error = {
@ -248,21 +253,23 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
asyncRunner.addTask(task);
};
googleHelper.downloadContent = function(objects, callback) {
googleHelper.downloadContent = function(objects, callback, skipAuth) {
callback = callback || core.doNothing;
var result = [];
var task = asyncRunner.createTask();
// Add some time for user to choose his files
task.timeout = ASYNC_TASK_LONG_TIMEOUT;
connect(task);
if(!skipAuth) {
authenticate(task);
}
task.onRun(function() {
function recursiveDownloadContent() {
if(objects.length === 0) {
task.chain();
return;
}
var object = objects.pop();
var object = objects[0];
result.push(object);
var file = undefined;
// object may be a file
@ -274,20 +281,24 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
file = object.file;
}
if(!file) {
objects.shift();
task.chain(recursiveDownloadContent);
return;
}
var headers = {};
var token = gapi.auth.getToken();
var headers = {
Authorization : token ? "Bearer " + token.access_token: null
};
if(token) {
headers.Authorization = "Bearer " + token.access_token;
}
$.ajax({
url : file.downloadUrl,
headers : headers,
data : {key: GOOGLE_API_KEY},
dataType : "text",
timeout : AJAX_TIMEOUT
}).done(function(data, textStatus, jqXHR) {
file.content = data;
objects.shift();
task.chain(recursiveDownloadContent);
}).fail(function(jqXHR) {
var error = {
@ -349,8 +360,9 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
}
$.ajax({
url : "//www.google.com/jsapi",
data : {key: GOOGLE_KEY},
dataType : "script", timeout : AJAX_TIMEOUT
data : {key: GOOGLE_API_KEY},
dataType : "script",
timeout : AJAX_TIMEOUT
}).done(function() {
google.load('picker', '1', {callback: task.chain});
pickerLoaded = true;
@ -424,10 +436,11 @@ define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
connect(task);
authenticate(task);
task.onRun(function() {
var headers = {};
var token = gapi.auth.getToken();
var headers = {
Authorization : token ? "Bearer " + token.access_token: null
};
if(token) {
headers.Authorization = "Bearer " + token.access_token;
}
function publish() {
var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/";
var data = {

View File

@ -1,4 +1,5 @@
define(["jquery", "core", "blogger-provider", "dropbox-provider", "gist-provider", "github-provider", "gdrive-provider", "tumblr-provider", "underscore"], function($, core) {
define(["jquery", "core", "sharing", "blogger-provider", "dropbox-provider", "gist-provider", "github-provider", "gdrive-provider", "tumblr-provider", "underscore"],
function($, core, sharing) {
var publisher = {};
@ -167,11 +168,13 @@ define(["jquery", "core", "blogger-provider", "dropbox-provider", "gist-provider
provider.publish(publishAttributes, title, content, function(error) {
if(error === undefined) {
publishAttributes.provider = provider.providerId;
sharing.createLink(publishAttributes, function() {
createPublishIndex(fileIndex, publishAttributes);
publisher.notifyPublish();
core.fileManager.updateFileTitles();
core.showMessage('"' + title
+ '" is now published on ' + provider.providerName + '.');
});
}
});
}
@ -187,7 +190,7 @@ define(["jquery", "core", "blogger-provider", "dropbox-provider", "gist-provider
var fileIndex = core.fileManager.getCurrentFileIndex();
var publishIndexList = _.compact(localStorage[fileIndex + ".publish"].split(";"));
$(".msg-no-publish, .msg-publish-list").addClass("hide");
$("#manage-publish-list .input-append").remove();
var publishList = $("#manage-publish-list").empty();
if (publishIndexList.length > 0) {
$(".msg-publish-list").removeClass("hide");
} else {
@ -196,7 +199,7 @@ define(["jquery", "core", "blogger-provider", "dropbox-provider", "gist-provider
_.each(publishIndexList, function(publishIndex) {
var publishAttributes = JSON.parse(localStorage[publishIndex]);
var publishDesc = JSON.stringify(publishAttributes).replace(/{|}|"/g, "");
lineElement = $(_.template(lineTemplate, {
var lineElement = $(_.template(lineTemplate, {
provider: providerMap[publishAttributes.provider],
publishDesc: publishDesc
}));
@ -204,7 +207,7 @@ define(["jquery", "core", "blogger-provider", "dropbox-provider", "gist-provider
core.fileManager.removePublish(publishIndex);
core.fileManager.updateFileTitles();
}));
$("#manage-publish-list").append(lineElement);
publishList.append(lineElement);
});
};

View File

@ -1,4 +1,4 @@
define(["jquery", "core", "async-runner", "gdrive-provider", "underscore"], function($, core, asyncRunner) {
define(["jquery", "core", "async-runner", "gist-provider", "underscore"], function($, core, asyncRunner) {
var sharing = {};
// Create a map with providerId: providerObject
@ -7,28 +7,34 @@ define(["jquery", "core", "async-runner", "gdrive-provider", "underscore"], func
return argument && argument.providerId && [argument.providerId, argument];
}).compact().object().value();
sharing.getLink = function(attributes) {
var provider = providerMap[attributes.provider];
if(provider === undefined) {
return undefined;
}
var url = [BASE_URL, 'viewer.html?provider=', attributes.provider];
_.each(provider.sharingAttributes, function(attributeName) {
url.push('&');
url.push(attributeName);
url.push('=');
url.push(attributes[attributeName]);
// Used to populate the "Sharing" dropdown box
var lineTemplate = ['<div class="input-prepend">',
'<a href="<%= link %>" class="add-on" title="Sharing location"><i class="icon-link"></i></a>',
'<input class="span2" type="text" value="<%= link %>" readonly />',
'</div>'].join("");
sharing.refreshDocumentSharing = function(attributesList) {
var linkList = $("#link-container .link-list").empty();
$("#link-container .no-link").show();
_.each(attributesList, function(attributes) {
if(attributes.sharingLink) {
var lineElement = $(_.template(lineTemplate, {
link: attributes.sharingLink
}));
lineElement.click(function(event) {
event.stopPropagation();
});
linkList.append(lineElement);
$("#link-container .no-link").hide();
}
});
return url.join("");
};
sharing.createLink = function(attributes, callback) {
if(attributes.sharingLink !== undefined) {
callback();
return;
}
var provider = providerMap[attributes.provider];
if(provider === undefined) {
// Don't create link if link already exists or provider is not compatible for sharing
if(attributes.sharingLink !== undefined || provider === undefined
// Or document is not published in markdown format
|| attributes.format != "markdown") {
callback();
return;
}
@ -44,7 +50,7 @@ define(["jquery", "core", "async-runner", "gdrive-provider", "underscore"], func
url.push('&');
url.push(attributeName);
url.push('=');
url.push(attributes[attributeName]);
url.push(encodeURIComponent(attributes[attributeName]));
});
$.getJSON(
"https://api-ssl.bitly.com/v3/shorten",
@ -56,29 +62,78 @@ define(["jquery", "core", "async-runner", "gdrive-provider", "underscore"], func
{
if(response.data) {
shortUrl = response.data.url;
attributes.sharingLink = shortUrl;
}
console.log(shortUrl);
task.chain();
}
);
});
task.onSuccess(function() {
function onFinish() {
if(shortUrl === undefined) {
localStorage["missingSharingLink"] = true;
}
callback(shortUrl);
});
}
task.onSuccess(onFinish);
task.onError(onFinish);
asyncRunner.addTask(task);
};
core.onReady(function() {
// Create the possible missing links
function checkMissingLinks() {
if(core.isOffline === true) {
return;
}
if(!_.has(localStorage, "missingSharingLink")) {
return;
}
localStorage.removeItem("missingSharingLink");
var fileIndexList = _.compact(localStorage["file.list"].split(";"));
_.each(fileIndexList, function(fileIndex) {
var publishIndexList = _.compact(localStorage[fileIndex + ".publish"].split(";"));
_.each(publishIndexList, function(publishIndex) {
var publishAttributes = JSON.parse(localStorage[publishIndex]);
sharing.createLink(publishAttributes, function(shortUrl) {
publishAttributes.sharingLink = shortUrl;
var syncIndexList = localStorage[fileIndex + ".sync"].split(";");
var publishIndexList = localStorage[fileIndex + ".publish"].split(";");
var attributesIndexList = _.compact(syncIndexList.concat(publishIndexList));
_.each(attributesIndexList, function(attributesIndex) {
var attributes = JSON.parse(localStorage[attributesIndex]);
sharing.createLink(attributes, function(shortUrl) {
if(shortUrl !== undefined) {
localStorage[attributesIndex] = JSON.stringify(attributes);
}
});
});
});
}
// Periodically check that links are not missing
if(viewerMode === false) {
core.addPeriodicCallback(checkMissingLinks);
}
core.onReady(function() {
if(viewerMode === false) {
return;
}
// Check parameters to see if we have to download a shared document
var providerId = core.getURLParameter("provider");
if(providerId === undefined) {
return;
}
var provider = providerMap[providerId];
if(provider === undefined) {
return;
}
var importParameters = {};
_.each(provider.sharingAttributes, function(attributeName) {
importParameters[attributeName] = core.getURLParameter(attributeName);
});
$("#wmd-preview, #file-title").hide();
provider.importPublic(importParameters, function(error, title, content) {
$("#wmd-preview, #file-title").show();
if(error) {
return;
}
var fileIndex = core.fileManager.createFile(title, content, undefined, true);
core.fileManager.selectFile(fileIndex);
});
});
return sharing;

View File

@ -45,6 +45,8 @@ function setupLocalStorage() {
localStorage.removeItem("sync.dropbox.lastChangeId");
}
var PROVIDER_GDRIVE = "gdrive";
var PROVIDER_DROPBOX = "dropbox";
var SYNC_PROVIDER_GDRIVE = "sync." + PROVIDER_GDRIVE + ".";
var SYNC_PROVIDER_DROPBOX = "sync." + PROVIDER_DROPBOX + ".";
_.each(fileIndexList, function(fileIndex) {
@ -120,6 +122,8 @@ function setupLocalStorage() {
}
});
});
// Force creation of sharing links
localStorage["missingSharingLink"] = true;
version = "v6";
}

View File

@ -184,7 +184,9 @@ define(["jquery", "core", "dropbox-provider", "gdrive-provider", "underscore"],
});
};
// Run sync function periodically
if(viewerMode === false) {
core.addPeriodicCallback(synchronizer.sync);
}
// Used to populate the "Manage synchronization" dialog
var lineTemplate = ['<div class="input-prepend input-append">',
@ -197,7 +199,7 @@ define(["jquery", "core", "dropbox-provider", "gdrive-provider", "underscore"],
var fileIndex = core.fileManager.getCurrentFileIndex();
var syncIndexList = _.compact(localStorage[fileIndex + ".sync"].split(";"));
$(".msg-no-sync, .msg-sync-list").addClass("hide");
$("#manage-sync-list .input-append").remove();
var syncList = $("#manage-sync-list").empty();
if (syncIndexList.length > 0) {
$(".msg-sync-list").removeClass("hide");
} else {
@ -206,7 +208,7 @@ define(["jquery", "core", "dropbox-provider", "gdrive-provider", "underscore"],
_.each(syncIndexList, function(syncIndex) {
var syncAttributes = JSON.parse(localStorage[syncIndex]);
var syncDesc = syncAttributes.id || syncAttributes.path;
lineElement = $(_.template(lineTemplate, {
var lineElement = $(_.template(lineTemplate, {
provider: providerMap[syncAttributes.provider],
syncDesc: syncDesc
}));
@ -214,7 +216,7 @@ define(["jquery", "core", "dropbox-provider", "gdrive-provider", "underscore"],
core.fileManager.removeSync(syncIndex);
core.fileManager.updateFileTitles();
}));
$("#manage-sync-list").append(lineElement);
syncList.append(lineElement);
});
};

View File

@ -1,5 +1,7 @@
define(["jquery", "core", "tumblr-helper"], function($, core, tumblrHelper) {
var PROVIDER_TUMBLR = "tumblr";
var tumblrProvider = {
providerId: PROVIDER_TUMBLR,
providerName: "Tumblr"

View File

@ -5,8 +5,6 @@
<link rel="icon" href="img/stackedit-32.ico" type="image/x-icon">
<link rel="shortcut icon" href="img/stackedit-32.ico"
type="image/x-icon">
<meta name="keywords"
content="Markdown, Editor, PageDown, Stack Overflow, Stack Exchange">
<meta name="description"
content="StackEdit is a free, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites.">
<meta name="author" content="Benoit Schweblin">
@ -29,10 +27,16 @@
<div id="navbar" class="navbar navbar-fixed-top ui-layout-north">
<div class="navbar-inner">
<ul class="pull-right hide" id="menu-bar">
<ul class="nav pull-right hide" id="menu-bar">
<li class="btn-group">
<button class="btn action-edit-document hide"
title="Edit this document">
<i class="icon-pencil"></i>
</button>
</li>
<li class="btn-group">
<button class="btn dropdown-toggle" data-toggle="dropdown"
title="Save document">
title="Save this document">
<i class="icon-download-alt"></i>
</button>
<ul class="dropdown-menu">
@ -50,9 +54,9 @@
</button>
<ul id="file-selector" class="dropdown-menu">
</ul></li>
<li class="btn-group"><a class="btn"
href="." title="Open StackEdit"><i
class="icon-stackedit"></i></a></li>
<li class="btn-group"><button class="btn action-open-stackedit"
title="Open StackEdit"><i
class="icon-stackedit"></i></button></li>
</ul>
<ul class="nav pull-right">
<li><i class="working-indicator icon-none"></i></li>