2013-04-10 18:14:59 +00:00
|
|
|
define(
|
2013-05-25 18:13:59 +00:00
|
|
|
[ "jquery", "utils", "extension-manager", "bootstrap", "layout", "Markdown.Editor", "storage", "config",
|
2013-04-26 23:08:13 +00:00
|
|
|
"underscore", "FileSaver", "css_browser_selector" ],
|
2013-05-25 18:13:59 +00:00
|
|
|
function($, utils, extensionManager) {
|
2013-04-02 18:42:47 +00:00
|
|
|
|
|
|
|
var core = {};
|
|
|
|
|
2013-05-18 13:40:16 +00:00
|
|
|
// For convenience
|
2013-04-10 18:14:59 +00:00
|
|
|
core.doNothing = function() {};
|
|
|
|
|
2013-04-02 18:42:47 +00:00
|
|
|
// Time shared by others modules
|
2013-04-10 18:14:59 +00:00
|
|
|
function updateCurrentTime() {
|
2013-04-02 18:42:47 +00:00
|
|
|
core.currentTime = new Date().getTime();
|
2013-04-10 18:14:59 +00:00
|
|
|
}
|
2013-05-18 13:40:16 +00:00
|
|
|
updateCurrentTime();
|
2013-04-02 18:42:47 +00:00
|
|
|
|
2013-04-21 00:07:27 +00:00
|
|
|
// Used for periodic tasks
|
|
|
|
var intervalId = undefined;
|
|
|
|
var periodicCallbacks = [];
|
|
|
|
core.addPeriodicCallback = function(callback) {
|
|
|
|
periodicCallbacks.push(callback);
|
|
|
|
};
|
|
|
|
|
2013-04-10 18:14:59 +00:00
|
|
|
// Used to detect user activity
|
|
|
|
var userReal = false;
|
|
|
|
var userActive = false;
|
|
|
|
var windowUnique = true;
|
|
|
|
var userLastActivity = 0;
|
|
|
|
function setUserActive() {
|
|
|
|
userReal = true;
|
|
|
|
userActive = true;
|
|
|
|
userLastActivity = core.currentTime;
|
|
|
|
};
|
|
|
|
function isUserActive() {
|
|
|
|
if(userActive === true
|
|
|
|
&& core.currentTime - userLastActivity > USER_IDLE_THRESHOLD) {
|
|
|
|
userActive = false;
|
|
|
|
}
|
|
|
|
return userActive && windowUnique;
|
|
|
|
}
|
2013-04-03 22:52:29 +00:00
|
|
|
|
2013-04-10 18:14:59 +00:00
|
|
|
// Used to only have 1 window of the application in the same browser
|
|
|
|
var windowId = undefined;
|
|
|
|
core.checkWindowUnique = function() {
|
|
|
|
if(userReal === false || windowUnique === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(windowId === undefined) {
|
2013-05-25 18:13:59 +00:00
|
|
|
windowId = utils.randomString();
|
2013-04-10 18:14:59 +00:00
|
|
|
localStorage["frontWindowId"] = windowId;
|
|
|
|
}
|
|
|
|
var frontWindowId = localStorage["frontWindowId"];
|
|
|
|
if(frontWindowId != windowId) {
|
|
|
|
windowUnique = false;
|
2013-04-11 22:38:41 +00:00
|
|
|
if(intervalId !== undefined) {
|
|
|
|
clearInterval(intervalId);
|
|
|
|
}
|
2013-04-10 23:13:31 +00:00
|
|
|
$(".modal").modal("hide");
|
2013-04-10 18:14:59 +00:00
|
|
|
$('#modal-non-unique').modal({
|
|
|
|
backdrop: "static",
|
|
|
|
keyboard: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2013-04-22 23:10:08 +00:00
|
|
|
|
2013-05-25 00:34:04 +00:00
|
|
|
// Export data on disk
|
2013-04-22 23:10:08 +00:00
|
|
|
core.saveFile = function(content, filename) {
|
|
|
|
if(saveAs !== undefined) {
|
|
|
|
var blob = new Blob([content], {type: "text/plain;charset=utf-8"});
|
|
|
|
saveAs(blob, filename);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var uriContent = "data:application/octet-stream;base64,"
|
2013-05-25 18:13:59 +00:00
|
|
|
+ utils.encodeBase64(content);
|
2013-04-22 23:10:08 +00:00
|
|
|
window.open(uriContent, 'file');
|
|
|
|
}
|
|
|
|
};
|
2013-04-02 18:42:47 +00:00
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
// Used by asyncRunner
|
2013-04-02 18:42:47 +00:00
|
|
|
core.showWorkingIndicator = function(show) {
|
|
|
|
if (show === false) {
|
2013-04-27 23:16:38 +00:00
|
|
|
$(".working-indicator").removeClass("show");
|
2013-04-03 22:52:29 +00:00
|
|
|
$("body").removeClass("working");
|
2013-04-02 18:42:47 +00:00
|
|
|
} else {
|
2013-04-27 23:16:38 +00:00
|
|
|
$(".working-indicator").addClass("show");
|
2013-04-03 22:52:29 +00:00
|
|
|
$("body").addClass("working");
|
2013-04-02 18:42:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-25 00:34:04 +00:00
|
|
|
// Log a message
|
|
|
|
core.showMessage = function(message) {
|
|
|
|
console.log(message);
|
|
|
|
extensionManager.onMessage(message);
|
2013-04-02 18:42:47 +00:00
|
|
|
};
|
|
|
|
|
2013-05-25 00:34:04 +00:00
|
|
|
// Log an error
|
|
|
|
core.showError = function(error) {
|
|
|
|
console.error(error);
|
|
|
|
if(_.isString(error)) {
|
|
|
|
extensionManager.onMessage(error);
|
|
|
|
}
|
|
|
|
else if(_.isObject(error)) {
|
|
|
|
extensionManager.onMessage(error.message);
|
|
|
|
}
|
2013-04-02 18:42:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Offline management
|
|
|
|
core.isOffline = false;
|
|
|
|
var offlineTime = core.currentTime;
|
|
|
|
var offlineListeners = [];
|
2013-04-21 00:07:27 +00:00
|
|
|
core.addOfflineListener = function(callback) {
|
|
|
|
offlineListeners.push(callback);
|
|
|
|
};
|
2013-04-02 18:42:47 +00:00
|
|
|
core.setOffline = function() {
|
|
|
|
offlineTime = core.currentTime;
|
|
|
|
if(core.isOffline === false) {
|
|
|
|
core.isOffline = true;
|
2013-05-25 00:34:04 +00:00
|
|
|
extensionManager.onOfflineChanged(true);
|
|
|
|
_.each(offlineListeners, function(listener) {
|
|
|
|
listener();
|
2013-04-02 18:42:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
core.setOnline = function() {
|
2013-04-03 22:52:29 +00:00
|
|
|
if(core.isOffline === true) {
|
2013-04-02 18:42:47 +00:00
|
|
|
core.isOffline = false;
|
2013-05-25 00:34:04 +00:00
|
|
|
extensionManager.onOfflineChanged(false);
|
|
|
|
_.each(offlineListeners, function(listener) {
|
|
|
|
listener();
|
|
|
|
});
|
2013-04-02 18:42:47 +00:00
|
|
|
}
|
|
|
|
};
|
2013-04-10 18:14:59 +00:00
|
|
|
function checkOnline() {
|
2013-04-02 18:42:47 +00:00
|
|
|
// Try to reconnect if we are offline but we have some network
|
|
|
|
if (core.isOffline === true && navigator.onLine === true
|
|
|
|
&& offlineTime + CHECK_ONLINE_PERIOD < core.currentTime) {
|
|
|
|
offlineTime = core.currentTime;
|
|
|
|
// Try to download anything to test the connection
|
|
|
|
$.ajax({
|
2013-04-05 23:59:59 +00:00
|
|
|
url : "//www.google.com/jsapi",
|
2013-04-02 18:42:47 +00:00
|
|
|
timeout : AJAX_TIMEOUT, dataType : "script"
|
|
|
|
}).done(function() {
|
|
|
|
core.setOnline();
|
|
|
|
});
|
|
|
|
}
|
2013-04-10 18:14:59 +00:00
|
|
|
}
|
2013-04-02 18:42:47 +00:00
|
|
|
|
|
|
|
// Setting management
|
2013-04-11 22:38:41 +00:00
|
|
|
core.settings = {
|
2013-04-10 23:13:31 +00:00
|
|
|
layoutOrientation : "horizontal",
|
2013-05-25 18:13:59 +00:00
|
|
|
lazyRendering : true,
|
2013-04-11 22:38:41 +00:00
|
|
|
editorFontSize : 14,
|
2013-04-29 21:41:10 +00:00
|
|
|
defaultContent: "\n\n\n> Written with [StackEdit](http://benweet.github.io/stackedit/).",
|
|
|
|
commitMsg : "Published by http://benweet.github.io/stackedit",
|
2013-04-14 13:24:29 +00:00
|
|
|
template : ['<!DOCTYPE html>\n',
|
|
|
|
'<html>\n',
|
|
|
|
'<head>\n',
|
|
|
|
'<title><%= documentTitle %></title>\n',
|
|
|
|
'</head>\n',
|
|
|
|
'<body><%= documentHTML %></body>\n',
|
2013-05-19 18:56:15 +00:00
|
|
|
'</html>'].join(""),
|
2013-05-25 18:13:59 +00:00
|
|
|
sshProxy : SSH_PROXY_URL
|
2013-04-10 23:13:31 +00:00
|
|
|
};
|
2013-05-25 18:13:59 +00:00
|
|
|
if (_.has(localStorage, "settings")) {
|
|
|
|
_.extend(core.settings, JSON.parse(localStorage.settings));
|
|
|
|
}
|
2013-04-02 18:42:47 +00:00
|
|
|
|
2013-05-25 18:13:59 +00:00
|
|
|
core.loadSettings = function() {
|
|
|
|
|
2013-04-02 18:42:47 +00:00
|
|
|
// Layout orientation
|
2013-05-25 18:13:59 +00:00
|
|
|
utils.setInputRadio("radio-layout-orientation", core.settings.layoutOrientation);
|
2013-05-21 22:34:42 +00:00
|
|
|
// Theme
|
2013-05-25 18:13:59 +00:00
|
|
|
utils.setInputValue("#input-settings-theme", localStorage.theme);
|
2013-05-13 23:10:02 +00:00
|
|
|
// Lazy rendering
|
2013-05-25 18:13:59 +00:00
|
|
|
utils.setInputChecked("#input-settings-lazy-rendering", core.settings.lazyRendering);
|
2013-04-10 23:13:31 +00:00
|
|
|
// Editor font size
|
2013-05-25 18:13:59 +00:00
|
|
|
utils.setInputValue("#input-settings-editor-font-size", core.settings.editorFontSize);
|
2013-04-29 21:41:10 +00:00
|
|
|
// Default content
|
2013-05-25 18:13:59 +00:00
|
|
|
utils.setInputValue("#textarea-settings-default-content", core.settings.defaultContent);
|
2013-04-11 22:38:41 +00:00
|
|
|
// Commit message
|
2013-05-25 18:13:59 +00:00
|
|
|
utils.setInputValue("#input-settings-publish-commit-msg", core.settings.commitMsg);
|
2013-04-14 13:24:29 +00:00
|
|
|
// Template
|
2013-05-25 18:13:59 +00:00
|
|
|
utils.setInputValue("#textarea-settings-publish-template", core.settings.template);
|
2013-05-19 18:56:15 +00:00
|
|
|
// SSH proxy
|
2013-05-25 18:13:59 +00:00
|
|
|
utils.setInputValue("#input-settings-ssh-proxy", core.settings.sshProxy);
|
|
|
|
|
|
|
|
// Load extension settings
|
|
|
|
extensionManager.onLoadSettings();
|
2013-04-02 18:42:47 +00:00
|
|
|
};
|
|
|
|
|
2013-04-10 23:13:31 +00:00
|
|
|
core.saveSettings = function(event) {
|
|
|
|
var newSettings = {};
|
|
|
|
|
2013-04-02 18:42:47 +00:00
|
|
|
// Layout orientation
|
2013-05-25 18:13:59 +00:00
|
|
|
newSettings.layoutOrientation = utils.getInputRadio("radio-layout-orientation");
|
2013-05-21 22:34:42 +00:00
|
|
|
// Theme
|
2013-05-25 18:13:59 +00:00
|
|
|
var theme = utils.getInputValue("#input-settings-theme");
|
2013-05-13 23:10:02 +00:00
|
|
|
// Lazy Rendering
|
2013-05-25 18:13:59 +00:00
|
|
|
newSettings.lazyRendering = utils.getInputChecked("#input-settings-lazy-rendering");
|
2013-04-10 23:13:31 +00:00
|
|
|
// Editor font size
|
2013-05-25 18:13:59 +00:00
|
|
|
newSettings.editorFontSize = utils.getInputIntValue("#input-settings-editor-font-size", event, 1, 99);
|
2013-04-29 21:41:10 +00:00
|
|
|
// Default content
|
2013-05-25 18:13:59 +00:00
|
|
|
newSettings.defaultContent = utils.getInputValue("#textarea-settings-default-content");
|
2013-04-11 22:38:41 +00:00
|
|
|
// Commit message
|
2013-05-25 18:13:59 +00:00
|
|
|
newSettings.commitMsg = utils.getInputTextValue("#input-settings-publish-commit-msg", event);
|
2013-04-14 13:24:29 +00:00
|
|
|
// Template
|
2013-05-25 18:13:59 +00:00
|
|
|
newSettings.template = utils.getInputTextValue("#textarea-settings-publish-template", event);
|
2013-05-19 18:56:15 +00:00
|
|
|
// SSH proxy
|
2013-05-25 18:13:59 +00:00
|
|
|
newSettings.sshProxy = utils.checkUrl(utils.getInputTextValue("#input-settings-ssh-proxy", event), true);
|
|
|
|
|
|
|
|
// Save extension settings
|
|
|
|
newSettings.extensionSettings = {};
|
|
|
|
extensionManager.onSaveSettings(newSettings.extensionSettings, event);
|
2013-04-14 13:24:29 +00:00
|
|
|
|
2013-04-10 23:13:31 +00:00
|
|
|
if(!event.isPropagationStopped()) {
|
2013-05-19 18:56:15 +00:00
|
|
|
$.extend(core.settings, newSettings);
|
2013-04-10 23:13:31 +00:00
|
|
|
localStorage.settings = JSON.stringify(newSettings);
|
2013-05-21 22:34:42 +00:00
|
|
|
localStorage.theme = theme;
|
2013-04-10 23:13:31 +00:00
|
|
|
}
|
2013-04-02 18:42:47 +00:00
|
|
|
};
|
2013-04-14 13:24:29 +00:00
|
|
|
|
2013-04-02 18:42:47 +00:00
|
|
|
// Create the layout
|
2013-04-27 12:25:02 +00:00
|
|
|
var layout = undefined;
|
2013-04-02 18:42:47 +00:00
|
|
|
core.createLayout = function() {
|
2013-04-30 23:02:19 +00:00
|
|
|
if(viewerMode === true) {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-02 18:42:47 +00:00
|
|
|
var layoutGlobalConfig = {
|
|
|
|
closable : true,
|
|
|
|
resizable : false,
|
|
|
|
slidable : false,
|
|
|
|
livePaneResizing : true,
|
|
|
|
enableCursorHotkey : false,
|
|
|
|
spacing_open : 15,
|
|
|
|
spacing_closed : 15,
|
|
|
|
togglerLength_open : 90,
|
|
|
|
togglerLength_closed : 90,
|
2013-04-30 23:02:19 +00:00
|
|
|
stateManagement__enabled : false,
|
|
|
|
center__minWidth : 200,
|
|
|
|
center__minHeight : 200
|
2013-04-02 18:42:47 +00:00
|
|
|
};
|
2013-05-25 00:34:04 +00:00
|
|
|
extensionManager.onLayoutConfigure(layoutGlobalConfig);
|
2013-04-11 22:38:41 +00:00
|
|
|
if (core.settings.layoutOrientation == "horizontal") {
|
2013-04-02 18:42:47 +00:00
|
|
|
$(".ui-layout-south").remove();
|
|
|
|
$(".ui-layout-east").addClass("well").prop("id", "wmd-preview");
|
|
|
|
layout = $('body').layout(
|
|
|
|
$.extend(layoutGlobalConfig, {
|
|
|
|
east__resizable : true,
|
|
|
|
east__size : .5,
|
|
|
|
east__minSize : 200
|
|
|
|
})
|
|
|
|
);
|
2013-04-11 22:38:41 +00:00
|
|
|
} else if (core.settings.layoutOrientation == "vertical") {
|
2013-04-02 18:42:47 +00:00
|
|
|
$(".ui-layout-east").remove();
|
|
|
|
$(".ui-layout-south").addClass("well").prop("id", "wmd-preview");
|
|
|
|
layout = $('body').layout(
|
|
|
|
$.extend(layoutGlobalConfig, {
|
|
|
|
south__resizable : true,
|
|
|
|
south__size : .5,
|
|
|
|
south__minSize : 200
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$(".ui-layout-toggler-north").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$(".ui-layout-toggler-south").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$(".ui-layout-toggler-east").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$("#navbar").click(function() {
|
|
|
|
layout.allowOverflow('north');
|
|
|
|
});
|
2013-04-26 23:08:13 +00:00
|
|
|
|
2013-05-25 00:34:04 +00:00
|
|
|
extensionManager.onLayoutCreated();
|
2013-04-02 18:42:47 +00:00
|
|
|
};
|
2013-04-27 12:25:02 +00:00
|
|
|
core.layoutRefresh = function() {
|
|
|
|
if(layout !== undefined) {
|
|
|
|
// Use defer to make sure UI has been updated
|
|
|
|
_.defer(layout.resizeAll);
|
|
|
|
}
|
|
|
|
};
|
2013-04-26 23:08:13 +00:00
|
|
|
|
2013-04-02 18:42:47 +00:00
|
|
|
// Create the PageDown editor
|
2013-04-04 22:13:48 +00:00
|
|
|
var insertLinkCallback = undefined;
|
2013-04-02 18:42:47 +00:00
|
|
|
core.createEditor = function(onTextChange) {
|
2013-04-27 00:15:21 +00:00
|
|
|
var converter = new Markdown.Converter();
|
2013-04-02 18:42:47 +00:00
|
|
|
var editor = new Markdown.Editor(converter);
|
2013-04-21 18:41:10 +00:00
|
|
|
// Custom insert link dialog
|
2013-04-04 22:13:48 +00:00
|
|
|
editor.hooks.set("insertLinkDialog", function (callback) {
|
|
|
|
insertLinkCallback = callback;
|
2013-05-26 01:10:58 +00:00
|
|
|
utils.resetModalInputs();
|
2013-04-10 23:13:31 +00:00
|
|
|
$("#modal-insert-link").modal();
|
2013-05-13 23:10:02 +00:00
|
|
|
_.defer(function() {
|
|
|
|
$("#input-insert-link").focus();
|
|
|
|
});
|
2013-04-04 22:13:48 +00:00
|
|
|
return true;
|
|
|
|
});
|
2013-04-21 18:41:10 +00:00
|
|
|
// Custom insert image dialog
|
2013-04-04 22:13:48 +00:00
|
|
|
editor.hooks.set("insertImageDialog", function (callback) {
|
|
|
|
insertLinkCallback = callback;
|
2013-05-26 01:10:58 +00:00
|
|
|
utils.resetModalInputs();
|
2013-04-10 23:13:31 +00:00
|
|
|
$("#modal-insert-image").modal();
|
2013-05-13 23:10:02 +00:00
|
|
|
_.defer(function() {
|
|
|
|
$("#input-insert-image").focus();
|
|
|
|
});
|
2013-04-04 22:13:48 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2013-05-25 00:34:04 +00:00
|
|
|
var firstChange = true;
|
2013-05-13 23:10:02 +00:00
|
|
|
var previewWrapper = function(makePreview) {
|
2013-05-16 00:19:58 +00:00
|
|
|
return function() {
|
|
|
|
if(firstChange !== true) {
|
|
|
|
onTextChange();
|
|
|
|
}
|
|
|
|
makePreview();
|
|
|
|
};
|
2013-05-13 23:10:02 +00:00
|
|
|
};
|
|
|
|
if(core.settings.lazyRendering === true) {
|
2013-05-26 01:10:58 +00:00
|
|
|
var lastRefresh = 0;
|
2013-05-13 23:10:02 +00:00
|
|
|
previewWrapper = function(makePreview) {
|
2013-05-26 01:10:58 +00:00
|
|
|
//var debouncedMakePreview = _.debounce(makePreview, 500);
|
2013-05-14 23:05:02 +00:00
|
|
|
return function() {
|
|
|
|
if(firstChange === true) {
|
|
|
|
makePreview();
|
|
|
|
}
|
|
|
|
else {
|
2013-05-16 00:19:58 +00:00
|
|
|
onTextChange();
|
2013-05-26 01:10:58 +00:00
|
|
|
var currentDate = new Date().getTime();
|
|
|
|
if(currentDate - lastRefresh > 500) {
|
|
|
|
makePreview();
|
|
|
|
lastRefresh = currentDate;
|
|
|
|
}
|
|
|
|
//debouncedMakePreview();
|
2013-05-14 23:05:02 +00:00
|
|
|
}
|
|
|
|
};
|
2013-05-13 23:10:02 +00:00
|
|
|
};
|
|
|
|
}
|
2013-05-25 00:34:04 +00:00
|
|
|
extensionManager.onEditorConfigure(editor);
|
|
|
|
editor.hooks.chain("onPreviewRefresh", extensionManager.onAsyncPreview);
|
|
|
|
|
|
|
|
// Convert email addresses (not managed by pagedown)
|
|
|
|
converter.hooks.chain("postConversion", function(text) {
|
|
|
|
return text.replace(/<(mailto\:)?([^\s>]+@[^\s>]+\.\S+?)>/g, function(match, mailto, email) {
|
|
|
|
return '<a href="mailto:' + email + '">' + email + '</a>';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#wmd-input, #wmd-preview").scrollTop(0);
|
|
|
|
$("#wmd-button-bar").empty();
|
2013-05-13 23:10:02 +00:00
|
|
|
editor.run(previewWrapper);
|
2013-04-02 18:42:47 +00:00
|
|
|
firstChange = false;
|
|
|
|
|
2013-04-21 18:41:10 +00:00
|
|
|
// Hide default buttons
|
2013-04-02 18:42:47 +00:00
|
|
|
$(".wmd-button-row").addClass("btn-group").find("li:not(.wmd-spacer)")
|
|
|
|
.addClass("btn").css("left", 0).find("span").hide();
|
2013-04-21 18:41:10 +00:00
|
|
|
|
|
|
|
// Add customized buttons
|
2013-04-02 18:42:47 +00:00
|
|
|
$("#wmd-bold-button").append($("<i>").addClass("icon-bold"));
|
|
|
|
$("#wmd-italic-button").append($("<i>").addClass("icon-italic"));
|
|
|
|
$("#wmd-link-button").append($("<i>").addClass("icon-globe"));
|
|
|
|
$("#wmd-quote-button").append($("<i>").addClass("icon-indent-left"));
|
|
|
|
$("#wmd-code-button").append($("<i>").addClass("icon-code"));
|
|
|
|
$("#wmd-image-button").append($("<i>").addClass("icon-picture"));
|
|
|
|
$("#wmd-olist-button").append($("<i>").addClass("icon-numbered-list"));
|
|
|
|
$("#wmd-ulist-button").append($("<i>").addClass("icon-list"));
|
|
|
|
$("#wmd-heading-button").append($("<i>").addClass("icon-text-height"));
|
|
|
|
$("#wmd-hr-button").append($("<i>").addClass("icon-hr"));
|
|
|
|
$("#wmd-undo-button").append($("<i>").addClass("icon-undo"));
|
|
|
|
$("#wmd-redo-button").append($("<i>").addClass("icon-share-alt"));
|
|
|
|
};
|
2013-05-04 00:05:58 +00:00
|
|
|
|
2013-04-11 22:38:41 +00:00
|
|
|
// Create an centered popup window
|
2013-04-10 23:13:31 +00:00
|
|
|
core.popupWindow = function(url, title, w, h) {
|
|
|
|
var left = (screen.width / 2) - (w / 2);
|
|
|
|
var top = (screen.height / 2) - (h / 2);
|
|
|
|
return window
|
|
|
|
.open(
|
|
|
|
url,
|
|
|
|
title,
|
|
|
|
'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='
|
|
|
|
+ w
|
|
|
|
+ ', height='
|
|
|
|
+ h
|
|
|
|
+ ', top='
|
|
|
|
+ top
|
|
|
|
+ ', left='
|
|
|
|
+ left);
|
|
|
|
};
|
2013-04-11 22:38:41 +00:00
|
|
|
|
2013-04-22 01:04:12 +00:00
|
|
|
// Keep a reference to the fileManager
|
|
|
|
core.setFileManager = function(fileManager) {
|
|
|
|
core.fileManager = fileManager;
|
|
|
|
runReadyCallbacks();
|
|
|
|
};
|
|
|
|
|
|
|
|
// onReady event callbacks
|
|
|
|
var readyCallbacks = [];
|
|
|
|
core.onReady = function(callback) {
|
|
|
|
readyCallbacks.push(callback);
|
|
|
|
runReadyCallbacks();
|
|
|
|
};
|
|
|
|
var documentLoaded = false;
|
|
|
|
function runReadyCallbacks() {
|
|
|
|
if(documentLoaded === true && core.fileManager !== undefined) {
|
|
|
|
_.each(readyCallbacks, function(callback) {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
readyCallbacks = [];
|
|
|
|
}
|
|
|
|
}
|
2013-04-21 00:07:27 +00:00
|
|
|
$(function() {
|
2013-04-22 01:04:12 +00:00
|
|
|
documentLoaded = true;
|
|
|
|
runReadyCallbacks();
|
|
|
|
});
|
2013-05-25 00:34:04 +00:00
|
|
|
|
|
|
|
core.onReady(function() {
|
2013-05-25 18:13:59 +00:00
|
|
|
extensionManager.init(core.settings.extensionSettings);
|
2013-05-25 00:34:04 +00:00
|
|
|
});
|
2013-04-22 01:04:12 +00:00
|
|
|
core.onReady(function() {
|
2013-04-02 18:42:47 +00:00
|
|
|
|
2013-05-21 22:34:42 +00:00
|
|
|
// Load theme list
|
|
|
|
_.each(THEME_LIST, function(name, value) {
|
|
|
|
$("#input-settings-theme").append($('<option value="' + value + '">' + name + '</option>'));
|
|
|
|
});
|
|
|
|
|
2013-04-02 18:42:47 +00:00
|
|
|
// listen to online/offline events
|
|
|
|
$(window).on('offline', core.setOffline);
|
|
|
|
$(window).on('online', core.setOnline);
|
|
|
|
if (navigator.onLine === false) {
|
|
|
|
core.setOffline();
|
|
|
|
}
|
|
|
|
|
2013-04-10 18:14:59 +00:00
|
|
|
// Detect user activity
|
|
|
|
$(document).mousemove(setUserActive).keypress(setUserActive);
|
|
|
|
|
2013-04-02 18:42:47 +00:00
|
|
|
// Avoid dropdown to close when clicking on submenu
|
2013-04-21 18:41:10 +00:00
|
|
|
$(".dropdown-submenu > a").click(function(e) {
|
2013-04-02 18:42:47 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2013-04-04 22:13:48 +00:00
|
|
|
// Click events on "insert link" and "insert image" dialog buttons
|
|
|
|
$(".action-insert-link").click(function(e) {
|
2013-05-25 18:13:59 +00:00
|
|
|
var value = utils.getInputTextValue($("#input-insert-link"), e);
|
2013-04-04 22:13:48 +00:00
|
|
|
if(value !== undefined) {
|
|
|
|
insertLinkCallback(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$(".action-insert-image").click(function(e) {
|
2013-05-25 18:13:59 +00:00
|
|
|
var value = utils.getInputTextValue($("#input-insert-image"), e);
|
2013-04-04 22:13:48 +00:00
|
|
|
if(value !== undefined) {
|
|
|
|
insertLinkCallback(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$(".action-close-insert-link").click(function(e) {
|
|
|
|
insertLinkCallback(null);
|
|
|
|
});
|
2013-04-21 18:41:10 +00:00
|
|
|
|
|
|
|
// Settings loading/saving
|
|
|
|
$(".action-load-settings").click(function() {
|
|
|
|
core.loadSettings();
|
|
|
|
});
|
|
|
|
$(".action-apply-settings").click(function(e) {
|
|
|
|
core.saveSettings(e);
|
|
|
|
if(!e.isPropagationStopped()) {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
});
|
2013-04-04 22:13:48 +00:00
|
|
|
|
2013-04-28 01:13:17 +00:00
|
|
|
$(".action-default-settings").click(function() {
|
|
|
|
localStorage.removeItem("settings");
|
2013-05-25 18:13:59 +00:00
|
|
|
localStorage.removeItem("theme");
|
2013-04-28 01:13:17 +00:00
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".action-app-reset").click(function() {
|
|
|
|
localStorage.clear();
|
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
|
2013-04-21 18:41:10 +00:00
|
|
|
// UI layout
|
2013-04-02 18:42:47 +00:00
|
|
|
$("#menu-bar, .ui-layout-center, .ui-layout-east, .ui-layout-south").removeClass("hide");
|
2013-04-11 22:38:41 +00:00
|
|
|
core.createLayout();
|
2013-04-21 18:41:10 +00:00
|
|
|
|
|
|
|
// Editor's textarea
|
2013-04-26 23:08:13 +00:00
|
|
|
$("#wmd-input, #md-section-helper").css({
|
2013-04-21 18:41:10 +00:00
|
|
|
// Apply editor font size
|
2013-04-11 22:38:41 +00:00
|
|
|
"font-size": core.settings.editorFontSize + "px",
|
|
|
|
"line-height": Math.round(core.settings.editorFontSize * (20/14)) + "px"
|
2013-04-26 23:08:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Manage tab key
|
|
|
|
$("#wmd-input").keydown(function(e) {
|
2013-04-21 16:27:52 +00:00
|
|
|
if(e.keyCode === 9) {
|
|
|
|
var value = $(this).val();
|
|
|
|
var start = this.selectionStart;
|
|
|
|
var end = this.selectionEnd;
|
|
|
|
// IE8 does not support selection attributes
|
|
|
|
if(start === undefined || end === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$(this).val(value.substring(0, start) + "\t" + value.substring(end));
|
|
|
|
this.selectionStart = this.selectionEnd = start + 1;
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2013-04-10 23:13:31 +00:00
|
|
|
});
|
2013-04-02 18:42:47 +00:00
|
|
|
|
2013-04-26 23:08:13 +00:00
|
|
|
// Tooltips
|
|
|
|
$(".tooltip-scroll-link").tooltip({
|
|
|
|
html: true,
|
|
|
|
container: '#modal-settings',
|
|
|
|
placement: 'right',
|
|
|
|
title: ['Scroll Link is a feature that binds together editor and preview scrollbars. ',
|
|
|
|
'It allows you to keep an eye on the preview while scrolling the editor and vice versa. ',
|
|
|
|
'<br><br>',
|
|
|
|
'The mapping between Markdown and HTML is based on the position of the title elements (h1, h2, ...) in the page. ',
|
|
|
|
'Therefore, if your document does not contain any title, the mapping will be linear and consequently less efficient.',
|
|
|
|
].join("")
|
|
|
|
});
|
2013-05-13 23:10:02 +00:00
|
|
|
$(".tooltip-lazy-rendering").tooltip({
|
|
|
|
container: '#modal-settings',
|
|
|
|
placement: 'right',
|
|
|
|
title: 'Disable preview rendering while typing in order to offload CPU. Refresh preview after 500 ms of inactivity.'
|
|
|
|
});
|
2013-04-29 21:41:10 +00:00
|
|
|
$(".tooltip-default-content").tooltip({
|
|
|
|
html: true,
|
|
|
|
container: '#modal-settings',
|
|
|
|
placement: 'right',
|
2013-05-26 01:10:58 +00:00
|
|
|
title: 'Thanks for supporting StackEdit by adding a backlink in your documents!'
|
2013-04-29 21:41:10 +00:00
|
|
|
});
|
2013-04-26 23:08:13 +00:00
|
|
|
$(".tooltip-template").tooltip({
|
|
|
|
html: true,
|
|
|
|
container: '#modal-settings',
|
|
|
|
placement: 'right',
|
|
|
|
trigger: 'manual',
|
|
|
|
title: ['Available variables:<br>',
|
|
|
|
'<ul><li><b>documentTitle</b>: document title</li>',
|
|
|
|
'<li><b>documentMarkdown</b>: document in Markdown format</li>',
|
|
|
|
'<li><b>documentHTML</b>: document in HTML format</li>',
|
|
|
|
'<li><b>publishAttributes</b>: attributes of the publish location (undefined when using "Save")</li></ul>',
|
|
|
|
'Examples:<br>',
|
|
|
|
_.escape('<title><%= documentTitle %></title>'),
|
|
|
|
'<br>',
|
|
|
|
_.escape('<div><%- documentHTML %></div>'),
|
|
|
|
'<br>',
|
|
|
|
_.escape('<% if(publishAttributes.provider == "github") print(documentMarkdown); %>'),
|
|
|
|
'<br><br><a target="_blank" href="http://underscorejs.org/#template">More info</a>',
|
|
|
|
].join("")
|
|
|
|
}).click(function(e) {
|
|
|
|
$(this).tooltip('show');
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).click(function(e) {
|
|
|
|
$(".tooltip-template").tooltip('hide');
|
|
|
|
});
|
|
|
|
|
2013-04-21 18:41:10 +00:00
|
|
|
// Reset inputs
|
2013-04-10 23:13:31 +00:00
|
|
|
$(".action-reset-input").click(function() {
|
2013-05-26 01:10:58 +00:00
|
|
|
utils.resetModalInputs();
|
2013-04-02 18:42:47 +00:00
|
|
|
});
|
2013-04-10 18:14:59 +00:00
|
|
|
|
|
|
|
// Do periodic tasks
|
2013-04-11 22:38:41 +00:00
|
|
|
intervalId = window.setInterval(function() {
|
2013-04-10 18:14:59 +00:00
|
|
|
updateCurrentTime();
|
|
|
|
core.checkWindowUnique();
|
2013-05-04 00:05:58 +00:00
|
|
|
if(isUserActive() === true || viewerMode === true) {
|
2013-04-21 00:07:27 +00:00
|
|
|
_.each(periodicCallbacks, function(callback) {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
checkOnline();
|
2013-04-10 18:14:59 +00:00
|
|
|
}
|
|
|
|
}, 1000);
|
2013-04-21 00:07:27 +00:00
|
|
|
});
|
2013-04-02 18:42:47 +00:00
|
|
|
|
|
|
|
return core;
|
|
|
|
});
|
|
|
|
|