Stackedit/js/core.js

471 lines
14 KiB
JavaScript
Raw Normal View History

2013-05-26 22:59:03 +00:00
define([
"jquery",
2013-05-27 19:45:33 +00:00
"underscore",
2013-05-26 22:59:03 +00:00
"utils",
2013-05-27 19:45:33 +00:00
"settings",
2013-05-26 22:59:03 +00:00
"extension-manager",
"storage",
"config",
2013-05-27 19:45:33 +00:00
"lib/bootstrap",
"lib/layout",
"lib/Markdown.Editor"
], function($, _, utils, settings, extensionMgr) {
2013-04-02 18:42:47 +00:00
var core = {};
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;
2013-05-27 19:45:33 +00:00
userLastActivity = utils.currentTime;
2013-04-10 18:14:59 +00:00
};
function isUserActive() {
if(userActive === true
2013-05-27 19:45:33 +00:00
&& utils.currentTime - userLastActivity > USER_IDLE_THRESHOLD) {
2013-04-10 18:14:59 +00:00
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;
2013-05-27 19:45:33 +00:00
function checkWindowUnique() {
2013-04-10 18:14:59 +00:00
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-05-27 19:45:33 +00:00
}
2013-04-22 23:10:08 +00:00
2013-04-02 18:42:47 +00:00
// Offline management
core.isOffline = false;
2013-05-27 19:45:33 +00:00
var offlineTime = utils.currentTime;
2013-04-02 18:42:47 +00:00
core.setOffline = function() {
2013-05-27 19:45:33 +00:00
offlineTime = utils.currentTime;
2013-04-02 18:42:47 +00:00
if(core.isOffline === false) {
core.isOffline = true;
2013-05-27 19:45:33 +00:00
extensionMgr.onOfflineChanged(true);
2013-04-02 18:42:47 +00:00
}
};
2013-05-27 19:45:33 +00:00
function setOnline() {
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-27 19:45:33 +00:00
extensionMgr.onOfflineChanged(false);
2013-04-02 18:42:47 +00:00
}
2013-05-27 19:45:33 +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
2013-05-27 19:45:33 +00:00
&& offlineTime + CHECK_ONLINE_PERIOD < utils.currentTime) {
offlineTime = utils.currentTime;
2013-04-02 18:42:47 +00:00
// 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() {
2013-05-27 19:45:33 +00:00
setOnline();
2013-04-02 18:42:47 +00:00
});
}
2013-04-10 18:14:59 +00:00
}
2013-04-02 18:42:47 +00:00
2013-05-27 19:45:33 +00:00
// Load settings in settings dialog
function loadSettings() {
2013-05-25 18:13:59 +00:00
2013-04-02 18:42:47 +00:00
// Layout orientation
2013-05-27 19:45:33 +00:00
utils.setInputRadio("radio-layout-orientation", 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-27 19:45:33 +00:00
utils.setInputChecked("#input-settings-lazy-rendering", settings.lazyRendering);
2013-04-10 23:13:31 +00:00
// Editor font size
2013-05-27 19:45:33 +00:00
utils.setInputValue("#input-settings-editor-font-size", settings.editorFontSize);
2013-04-29 21:41:10 +00:00
// Default content
2013-05-27 19:45:33 +00:00
utils.setInputValue("#textarea-settings-default-content", settings.defaultContent);
2013-04-11 22:38:41 +00:00
// Commit message
2013-05-27 19:45:33 +00:00
utils.setInputValue("#input-settings-publish-commit-msg", settings.commitMsg);
2013-04-14 13:24:29 +00:00
// Template
2013-05-27 19:45:33 +00:00
utils.setInputValue("#textarea-settings-publish-template", settings.template);
2013-05-19 18:56:15 +00:00
// SSH proxy
2013-05-27 19:45:33 +00:00
utils.setInputValue("#input-settings-ssh-proxy", settings.sshProxy);
2013-05-25 18:13:59 +00:00
// Load extension settings
2013-05-27 19:45:33 +00:00
extensionMgr.onLoadSettings();
}
2013-04-02 18:42:47 +00:00
2013-05-27 19:45:33 +00:00
// Save settings from settings dialog
function saveSettings(event) {
2013-04-10 23:13:31 +00:00
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 = {};
2013-05-27 19:45:33 +00:00
extensionMgr.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-27 19:45:33 +00:00
$.extend(settings, newSettings);
localStorage.settings = JSON.stringify(settings);
2013-05-21 22:34:42 +00:00
localStorage.theme = theme;
2013-04-10 23:13:31 +00:00
}
2013-05-27 19:45:33 +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-27 19:45:33 +00:00
extensionMgr.onLayoutConfigure(layoutGlobalConfig);
if (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-05-27 19:45:33 +00:00
} else if (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-27 19:45:33 +00:00
extensionMgr.onLayoutCreated(layout);
2013-04-27 12:25:02 +00:00
};
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-27 22:13:41 +00:00
var documentContent = undefined;
function checkDocumentChanges() {
var newDocumentContent = $("#wmd-input").val();
if(documentContent !== undefined && documentContent != newDocumentContent) {
onTextChange();
}
documentContent = newDocumentContent;
}
var previewWrapper = undefined;
2013-05-27 19:45:33 +00:00
if(settings.lazyRendering === true) {
2013-05-13 23:10:02 +00:00
previewWrapper = function(makePreview) {
2013-05-27 22:13:41 +00:00
var debouncedMakePreview = _.debounce(makePreview, 500);
return function() {
2013-05-27 22:13:41 +00:00
if(documentContent === undefined) {
makePreview();
}
else {
2013-05-27 22:13:41 +00:00
debouncedMakePreview();
}
2013-05-27 22:13:41 +00:00
checkDocumentChanges();
};
};
}
else {
previewWrapper = function(makePreview) {
return function() {
checkDocumentChanges();
makePreview();
};
2013-05-13 23:10:02 +00:00
};
}
2013-05-27 19:45:33 +00:00
extensionMgr.onEditorConfigure(editor);
editor.hooks.chain("onPreviewRefresh", extensionMgr.onAsyncPreview);
2013-05-25 00:34:04 +00:00
$("#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-22 01:04:12 +00:00
// onReady event callbacks
var readyCallbacks = [];
core.onReady = function(callback) {
readyCallbacks.push(callback);
runReadyCallbacks();
};
2013-05-27 19:45:33 +00:00
var ready = false;
core.setReady = function() {
ready = true;
runReadyCallbacks();
};
2013-04-22 01:04:12 +00:00
function runReadyCallbacks() {
2013-05-27 19:45:33 +00:00
if(ready === true) {
2013-04-22 01:04:12 +00:00
_.each(readyCallbacks, function(callback) {
callback();
});
readyCallbacks = [];
}
}
2013-05-25 00:34:04 +00:00
2013-05-27 22:13:41 +00:00
core.onReady(extensionMgr.onReady);
2013-04-22 01:04:12 +00:00
core.onReady(function() {
2013-05-27 19:45:33 +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);
2013-05-27 19:45:33 +00:00
$(window).on('online', setOnline);
2013-04-02 18:42:47 +00:00
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() {
2013-05-27 19:45:33 +00:00
loadSettings();
2013-04-21 18:41:10 +00:00
});
$(".action-apply-settings").click(function(e) {
2013-05-27 19:45:33 +00:00
saveSettings(e);
2013-04-21 18:41:10 +00:00
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-05-27 19:45:33 +00:00
"font-size": settings.editorFontSize + "px",
"line-height": Math.round(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-05-27 19:45:33 +00:00
utils.updateCurrentTime();
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;
});