Stackedit/public/res/core.js

883 lines
35 KiB
JavaScript
Raw Normal View History

2013-11-07 23:10:38 +00:00
/*globals Markdown, requirejs */
2013-05-26 22:59:03 +00:00
define([
"jquery",
2013-05-29 19:55:23 +00:00
"underscore",
2013-08-08 23:32:03 +00:00
"crel",
2014-03-18 01:10:22 +00:00
"editor",
2013-11-05 23:03:38 +00:00
"constants",
2013-05-26 22:59:03 +00:00
"utils",
2013-11-05 23:03:38 +00:00
"storage",
2013-05-27 19:45:33 +00:00
"settings",
2013-07-30 08:46:36 +00:00
"eventMgr",
2013-09-30 00:16:01 +00:00
"shortcutMgr",
2013-06-10 21:22:32 +00:00
"mousetrap",
2013-08-04 15:49:14 +00:00
"text!html/bodyIndex.html",
"text!html/bodyViewer.html",
2013-06-10 21:22:32 +00:00
"text!html/settingsTemplateTooltip.html",
2013-07-07 20:07:11 +00:00
"text!html/settingsUserCustomExtensionTooltip.html",
2013-05-26 22:59:03 +00:00
"storage",
2013-08-30 22:45:29 +00:00
"uilayout",
2014-03-20 00:24:56 +00:00
'pagedown',
], function($, _, crel, editor, constants, utils, storage, settings, eventMgr, shortcutMgr, mousetrap, bodyIndexHTML, bodyViewerHTML, settingsTemplateTooltipHTML, settingsUserCustomExtensionTooltipHTML) {
2013-04-02 18:42:47 +00:00
2013-05-29 19:55:23 +00:00
var core = {};
2014-03-03 23:30:55 +00:00
2013-05-29 19:55:23 +00:00
// Used for periodic tasks
2013-11-07 23:10:38 +00:00
var intervalId;
2013-05-29 19:55:23 +00:00
// Used to detect user activity
2013-08-04 00:53:46 +00:00
var isUserReal = false;
2013-05-29 19:55:23 +00:00
var userActive = false;
var windowUnique = true;
var userLastActivity = 0;
function setUserActive() {
2013-08-04 00:53:46 +00:00
isUserReal = true;
2013-05-29 19:55:23 +00:00
userActive = true;
2013-08-04 00:53:46 +00:00
var currentTime = utils.currentTime;
if(currentTime > userLastActivity + 1000) {
userLastActivity = currentTime;
eventMgr.onUserActive();
}
2013-05-29 19:55:23 +00:00
}
function isUserActive() {
2013-11-05 23:03:38 +00:00
if(utils.currentTime - userLastActivity > constants.USER_IDLE_THRESHOLD) {
2013-05-29 19:55:23 +00:00
userActive = false;
}
return userActive && windowUnique;
}
2014-03-03 23:30:55 +00:00
2013-05-29 19:55:23 +00:00
// Used to only have 1 window of the application in the same browser
2013-11-07 23:10:38 +00:00
var windowId;
2013-05-29 19:55:23 +00:00
function checkWindowUnique() {
2013-08-04 00:53:46 +00:00
if(isUserReal === false || windowUnique === false) {
2013-05-29 19:55:23 +00:00
return;
}
if(windowId === undefined) {
windowId = utils.randomString();
2013-11-05 23:03:38 +00:00
storage.frontWindowId = windowId;
2013-05-29 19:55:23 +00:00
}
2013-11-05 23:03:38 +00:00
var frontWindowId = storage.frontWindowId;
2013-05-29 19:55:23 +00:00
if(frontWindowId != windowId) {
windowUnique = false;
if(intervalId !== undefined) {
clearInterval(intervalId);
}
$(".modal").modal("hide");
$('.modal-non-unique').modal("show");
2013-12-14 22:44:59 +00:00
// Attempt to close the window
window.close();
2013-05-29 19:55:23 +00:00
}
}
// Offline management
2013-08-04 00:53:46 +00:00
var isOffline = false;
2013-05-29 19:55:23 +00:00
var offlineTime = utils.currentTime;
core.setOffline = function() {
offlineTime = utils.currentTime;
2013-08-04 00:53:46 +00:00
if(isOffline === false) {
isOffline = true;
2013-07-30 08:46:36 +00:00
eventMgr.onOfflineChanged(true);
2013-05-29 19:55:23 +00:00
}
};
function setOnline() {
2013-08-04 00:53:46 +00:00
if(isOffline === true) {
isOffline = false;
2013-07-30 08:46:36 +00:00
eventMgr.onOfflineChanged(false);
2013-05-29 19:55:23 +00:00
}
}
function checkOnline() {
// Try to reconnect if we are offline but we have some network
2013-11-05 23:03:38 +00:00
if(isOffline === true && navigator.onLine === true && offlineTime + constants.CHECK_ONLINE_PERIOD < utils.currentTime) {
2013-05-29 19:55:23 +00:00
offlineTime = utils.currentTime;
// Try to download anything to test the connection
$.ajax({
url: "//www.google.com/jsapi",
2013-11-05 23:03:38 +00:00
timeout: constants.AJAX_TIMEOUT,
2013-05-29 19:55:23 +00:00
dataType: "script"
}).done(function() {
setOnline();
});
}
}
// Load settings in settings dialog
2013-11-07 23:10:38 +00:00
var $themeInputElt;
2013-05-29 19:55:23 +00:00
function loadSettings() {
// Layout orientation
utils.setInputRadio("radio-layout-orientation", settings.layoutOrientation);
// Theme
2013-11-07 23:10:38 +00:00
utils.setInputValue($themeInputElt, window.theme);
2013-08-20 22:40:19 +00:00
$themeInputElt.change();
2013-05-29 19:55:23 +00:00
// Lazy rendering
utils.setInputChecked("#input-settings-lazy-rendering", settings.lazyRendering);
2013-06-27 23:10:04 +00:00
// Editor font family
utils.setInputValue("#input-settings-editor-font-family", settings.editorFontFamily);
2013-05-29 19:55:23 +00:00
// Editor font size
utils.setInputValue("#input-settings-editor-font-size", settings.editorFontSize);
2013-09-15 01:35:58 +00:00
// Max width
utils.setInputValue("#input-settings-max-width", settings.maxWidth);
2013-05-29 19:55:23 +00:00
// Default content
utils.setInputValue("#textarea-settings-default-content", settings.defaultContent);
2014-03-20 00:24:56 +00:00
// Edit mode
utils.setInputRadio("radio-settings-mode", settings.editMode);
2013-05-29 19:55:23 +00:00
// Commit message
utils.setInputValue("#input-settings-publish-commit-msg", settings.commitMsg);
// Gdrive multi-accounts
utils.setInputValue("#input-settings-gdrive-multiaccount", settings.gdriveMultiAccount);
2013-10-06 14:34:40 +00:00
// Gdrive full access
utils.setInputChecked("#input-settings-gdrive-full-access", settings.gdriveFullAccess);
// Dropbox full access
utils.setInputChecked("#input-settings-dropbox-full-access", settings.dropboxFullAccess);
// GitHub full access
utils.setInputChecked("#input-settings-github-full-access", settings.githubFullAccess);
2013-05-29 19:55:23 +00:00
// Template
utils.setInputValue("#textarea-settings-publish-template", settings.template);
2013-09-23 22:12:35 +00:00
// PDF template
2013-10-27 19:19:56 +00:00
utils.setInputValue("#textarea-settings-pdf-template", settings.pdfTemplate);
// PDF page size
utils.setInputValue("#input-settings-pdf-page-size", settings.pdfPageSize);
2013-05-29 19:55:23 +00:00
// SSH proxy
utils.setInputValue("#input-settings-ssh-proxy", settings.sshProxy);
2014-03-03 23:30:55 +00:00
2013-09-30 22:37:58 +00:00
// Load shortcuts settings
shortcutMgr.loadSettings();
2013-05-29 19:55:23 +00:00
// Load extension settings
2013-07-30 08:46:36 +00:00
eventMgr.onLoadSettings();
2013-05-29 19:55:23 +00:00
}
// Save settings from settings dialog
function saveSettings(event) {
var newSettings = {};
// Layout orientation
newSettings.layoutOrientation = utils.getInputRadio("radio-layout-orientation");
// Theme
2013-08-20 22:40:19 +00:00
var theme = utils.getInputValue($themeInputElt);
2013-05-29 19:55:23 +00:00
// Lazy Rendering
newSettings.lazyRendering = utils.getInputChecked("#input-settings-lazy-rendering");
2013-06-27 23:10:04 +00:00
// Editor font family
newSettings.editorFontFamily = utils.getInputTextValue("#input-settings-editor-font-family", event);
2013-05-29 19:55:23 +00:00
// Editor font size
newSettings.editorFontSize = utils.getInputIntValue("#input-settings-editor-font-size", event, 1, 99);
2013-09-15 01:35:58 +00:00
// Max width
newSettings.maxWidth = utils.getInputIntValue("#input-settings-max-width", event, 1);
2013-05-29 19:55:23 +00:00
// Default content
newSettings.defaultContent = utils.getInputValue("#textarea-settings-default-content");
2014-03-20 00:24:56 +00:00
// Edit mode
newSettings.editMode = utils.getInputRadio("radio-settings-mode");
2013-05-29 19:55:23 +00:00
// Commit message
newSettings.commitMsg = utils.getInputTextValue("#input-settings-publish-commit-msg", event);
// Gdrive multi-accounts
newSettings.gdriveMultiAccount = utils.getInputIntValue("#input-settings-gdrive-multiaccount");
2013-10-06 14:34:40 +00:00
// Gdrive full access
newSettings.gdriveFullAccess = utils.getInputChecked("#input-settings-gdrive-full-access");
// Drobox full access
newSettings.dropboxFullAccess = utils.getInputChecked("#input-settings-dropbox-full-access");
// GitHub full access
newSettings.githubFullAccess = utils.getInputChecked("#input-settings-github-full-access");
2013-05-29 19:55:23 +00:00
// Template
newSettings.template = utils.getInputTextValue("#textarea-settings-publish-template", event);
2013-09-23 22:12:35 +00:00
// PDF template
2013-10-27 19:19:56 +00:00
newSettings.pdfTemplate = utils.getInputTextValue("#textarea-settings-pdf-template", event);
// PDF page size
newSettings.pdfPageSize = utils.getInputValue("#input-settings-pdf-page-size");
2013-05-29 19:55:23 +00:00
// SSH proxy
newSettings.sshProxy = utils.checkUrl(utils.getInputTextValue("#input-settings-ssh-proxy", event), true);
2013-10-11 21:47:00 +00:00
// Save shortcuts settings
shortcutMgr.saveSettings(newSettings);
2013-05-29 19:55:23 +00:00
// Save extension settings
newSettings.extensionSettings = {};
2013-07-30 08:46:36 +00:00
eventMgr.onSaveSettings(newSettings.extensionSettings, event);
2013-05-29 19:55:23 +00:00
if(!event.isPropagationStopped()) {
if(settings.dropboxFullAccess !== newSettings.dropboxFullAccess) {
storage.removeItem('dropbox.lastChangeId');
}
2013-05-29 19:55:23 +00:00
$.extend(settings, newSettings);
2013-11-05 23:03:38 +00:00
storage.settings = JSON.stringify(settings);
2013-12-02 23:29:48 +00:00
storage.themeV3 = theme;
2013-05-29 19:55:23 +00:00
}
}
2013-08-10 11:34:30 +00:00
// Set the panels visibility
2013-11-07 23:10:38 +00:00
var layout;
var $menuPanelElt;
var $documentPanelElt;
2013-08-10 11:34:30 +00:00
function setPanelVisibility(forceHide) {
if(forceHide === true || layout.state.north.isClosed) {
2013-08-22 00:19:59 +00:00
$menuPanelElt.hide();
$documentPanelElt.hide();
2013-08-10 11:34:30 +00:00
}
else {
2013-08-22 00:19:59 +00:00
$menuPanelElt.show();
$documentPanelElt.show();
2013-08-10 11:34:30 +00:00
}
}
2013-08-13 00:03:38 +00:00
2013-08-10 11:34:30 +00:00
// Set the preview button visibility
2013-11-07 23:10:38 +00:00
var $previewButtonsElt;
2013-08-08 23:32:03 +00:00
function setPreviewButtonsVisibility(forceHide) {
if(forceHide === true || layout.state.east.isClosed) {
2013-08-22 00:19:59 +00:00
$previewButtonsElt.hide();
2013-08-08 23:32:03 +00:00
}
else {
2013-08-22 00:19:59 +00:00
$previewButtonsElt.show();
2013-08-08 23:32:03 +00:00
}
}
2013-08-08 21:53:15 +00:00
2013-09-09 23:32:24 +00:00
// Create the layout
2013-11-07 23:10:38 +00:00
var $editorButtonsElt;
2014-03-12 00:53:15 +00:00
var maxWidthMap = [
{ screenWidth: 0, maxWidth: 600 },
{ screenWidth: 1000, maxWidth: 700 },
{ screenWidth: 1200, maxWidth: 800 },
{ screenWidth: 1400, maxWidth: 900 },
];
var maxWidthMapReversed = maxWidthMap.slice(0).reverse();
function getMaxWidth() {
var actualWidth = $(window).width();
return _.find(maxWidthMapReversed, function(value) {
return actualWidth > value.screenWidth;
}).maxWidth;
}
2013-09-09 23:32:24 +00:00
function createLayout() {
2013-05-29 19:55:23 +00:00
var layoutGlobalConfig = {
2013-08-10 11:34:30 +00:00
closable: true,
2013-05-29 19:55:23 +00:00
resizable: false,
slidable: false,
livePaneResizing: true,
enableCursorHotkey: false,
2013-08-18 00:02:23 +00:00
resizerDblClickToggle: false,
2013-12-01 15:43:46 +00:00
resizeWithWindow: false,
2013-11-30 01:40:26 +00:00
north__spacing_open: 1,
north__spacing_closed: 1,
2014-03-10 23:53:03 +00:00
spacing_open: 32,
spacing_closed: 32,
2013-08-10 00:28:48 +00:00
togglerLength_open: 60,
togglerLength_closed: 60,
2013-05-29 19:55:23 +00:00
stateManagement__enabled: false,
2013-12-01 15:43:46 +00:00
north__minSize: 49,
2013-12-06 22:15:51 +00:00
center__minWidth: 250,
center__minHeight: 180,
2014-03-17 02:01:46 +00:00
east__onalert: function() {
2014-03-12 00:53:15 +00:00
window.location.href = 'viewer';
},
2014-03-17 02:01:46 +00:00
south__onalert: function() {
2014-03-12 00:53:15 +00:00
window.location.href = 'viewer';
},
2013-10-11 21:47:00 +00:00
fxSettings: {
easing: "easeInOutQuad",
duration: 350
},
2013-08-06 23:52:58 +00:00
onopen: function() {
2013-08-10 11:34:30 +00:00
setPanelVisibility();
2013-08-08 23:32:03 +00:00
setPreviewButtonsVisibility();
2013-08-06 23:52:58 +00:00
},
onclose_start: function(paneName) {
2013-08-10 11:34:30 +00:00
if(paneName == 'north') {
setPanelVisibility(true);
}
else if(paneName == 'east') {
2013-08-08 23:32:03 +00:00
setPreviewButtonsVisibility(true);
}
2013-08-06 23:52:58 +00:00
},
2013-09-12 23:25:25 +00:00
onresize_end: function(paneName) {
2014-03-18 01:10:22 +00:00
if(editor.$contentElt !== undefined && paneName == 'center') {
2014-03-16 02:13:42 +00:00
var padding = ($editorElt.width() - getMaxWidth()) / 2;
if(padding < constants.EDITOR_DEFAULT_PADDING) {
padding = constants.EDITOR_DEFAULT_PADDING;
}
2014-03-18 01:10:22 +00:00
editor.$contentElt.css({
2014-03-16 02:13:42 +00:00
'padding-left': padding + 'px',
'padding-right': padding + 'px'
});
2014-03-21 00:36:28 +00:00
editor.$marginElt.css({
'width': padding + 'px',
});
2014-03-16 02:13:42 +00:00
}
2013-09-12 23:25:25 +00:00
eventMgr.onLayoutResize(paneName);
2013-09-09 00:08:55 +00:00
},
2013-05-29 19:55:23 +00:00
};
2013-07-30 08:46:36 +00:00
eventMgr.onLayoutConfigure(layoutGlobalConfig);
2013-05-29 19:55:23 +00:00
if(settings.layoutOrientation == "horizontal") {
$(".ui-layout-south").remove();
2013-08-08 23:32:03 +00:00
$(".preview-container").html('<div id="preview-contents"><div id="wmd-preview" class="preview-content"></div></div>');
2013-05-29 19:55:23 +00:00
layout = $('body').layout($.extend(layoutGlobalConfig, {
east__resizable: true,
2013-09-30 00:16:01 +00:00
east__size: 0.5,
2013-12-01 15:43:46 +00:00
east__minSize: 300
2013-05-29 19:55:23 +00:00
}));
}
else if(settings.layoutOrientation == "vertical") {
$(".ui-layout-east").remove();
2013-08-08 23:32:03 +00:00
$(".preview-container").html('<div id="preview-contents"><div id="wmd-preview" class="preview-content"></div></div>');
2013-05-29 19:55:23 +00:00
layout = $('body').layout($.extend(layoutGlobalConfig, {
south__resizable: true,
2013-09-30 00:16:01 +00:00
south__size: 0.5,
2013-05-29 19:55:23 +00:00
south__minSize: 200
}));
}
2013-07-20 01:08:17 +00:00
$(".navbar").click(function() {
2013-05-29 19:55:23 +00:00
layout.allowOverflow('north');
});
2013-08-10 00:28:48 +00:00
$(".ui-layout-toggler-south").addClass("btn btn-info").html('<i class="icon-none"></i>');
$(".ui-layout-toggler-east").addClass("btn btn-info").html('<i class="icon-none"></i>');
2013-12-02 01:20:38 +00:00
var $northTogglerElt = $(".ui-layout-toggler-north").addClass("btn btn-info").html('<i class="icon-th"></i>');
2013-08-13 00:03:38 +00:00
// We attach the preview buttons to the UI layout resizer in order to
// have fixed position
// We also move the north toggler to the east or south resizer as the
// north resizer is very small
2013-09-15 01:35:58 +00:00
// var $previewButtonsContainerElt = $('<div
// class="preview-button-container">');
2013-09-30 00:16:01 +00:00
var $resizerDecorator = $('<div class="resizer-decorator">');
2013-09-15 01:35:58 +00:00
$previewButtonsElt = $('<div class="extension-preview-buttons">');
2013-09-11 23:26:47 +00:00
$editorButtonsElt = $('<div class="extension-editor-buttons">');
2013-11-07 23:10:38 +00:00
if(window.viewerMode || settings.layoutOrientation == "horizontal") {
2013-11-28 01:04:41 +00:00
$('.ui-layout-resizer-north').append($previewButtonsElt);
$('.ui-layout-resizer-east').append($resizerDecorator).append($northTogglerElt).append($editorButtonsElt);
2013-08-08 23:32:03 +00:00
}
else {
2013-11-28 01:04:41 +00:00
$('.ui-layout-resizer-south').append($resizerDecorator).append($previewButtonsElt).append($editorButtonsElt).append($northTogglerElt);
2013-08-08 23:32:03 +00:00
}
2013-08-10 11:34:30 +00:00
setPanelVisibility();
2013-08-08 23:32:03 +00:00
setPreviewButtonsVisibility();
2013-05-29 19:55:23 +00:00
2013-07-30 08:46:36 +00:00
eventMgr.onLayoutCreated(layout);
2013-07-03 22:29:53 +00:00
}
2013-05-29 19:55:23 +00:00
2013-12-01 15:43:46 +00:00
var $navbarElt;
var $leftBtnElts;
var $rightBtnElts;
2014-03-10 23:53:03 +00:00
var $btnDropdown;
var $titleContainer;
var marginWidth = 18 * 2 + 25 + 25;
2013-12-05 20:59:57 +00:00
var titleWidth = 18 + 348;
2014-04-06 00:59:32 +00:00
var leftButtonsWidth = 18 * 4 + 80 + 160 + 200 + 80;
2014-03-12 00:53:15 +00:00
var rightButtonsWidth = 18 + 80;
2014-03-10 23:53:03 +00:00
var buttonsDropdownWidth = 40;
2013-12-01 15:43:46 +00:00
function adjustWindow() {
2013-12-02 00:09:39 +00:00
if(!window.viewerMode) {
2014-03-10 23:53:03 +00:00
var maxWidth = $navbarElt.width();
2013-12-02 00:09:39 +00:00
if(marginWidth + titleWidth + leftButtonsWidth + rightButtonsWidth > maxWidth) {
2014-03-10 23:53:03 +00:00
$btnDropdown.show().find('.dropdown-menu').append($leftBtnElts);
if(marginWidth + titleWidth + rightButtonsWidth + buttonsDropdownWidth > maxWidth) {
$btnDropdown.find('.dropdown-menu').append($rightBtnElts);
2013-12-02 00:09:39 +00:00
}
else {
2014-03-10 23:53:03 +00:00
$titleContainer.before($rightBtnElts);
2013-12-02 00:09:39 +00:00
}
2013-12-01 15:43:46 +00:00
}
else {
2014-03-10 23:53:03 +00:00
$btnDropdown.hide().after($leftBtnElts);
$titleContainer.before($rightBtnElts);
2013-12-02 00:09:39 +00:00
}
2013-12-01 15:43:46 +00:00
}
layout.resizeAll();
}
2014-03-03 23:30:55 +00:00
2013-05-29 19:55:23 +00:00
// Create the PageDown editor
2014-03-18 01:10:22 +00:00
var pagedownEditor;
2013-11-07 23:10:38 +00:00
var $editorElt;
var fileDesc;
2013-06-19 20:33:46 +00:00
core.initEditor = function(fileDescParam) {
if(fileDesc !== undefined) {
2013-07-30 08:46:36 +00:00
eventMgr.onFileClosed(fileDesc);
}
2013-06-03 22:19:52 +00:00
fileDesc = fileDescParam;
2013-09-09 00:08:55 +00:00
2014-03-18 01:10:22 +00:00
if(pagedownEditor !== undefined) {
2013-06-03 22:19:52 +00:00
// If the editor is already created
2014-04-06 00:59:32 +00:00
editor.undoMgr.init();
return pagedownEditor.uiManager.setUndoRedoButtonStates();
2013-06-02 00:38:23 +00:00
}
2013-09-09 00:08:55 +00:00
2013-06-10 21:22:32 +00:00
// Create the converter and the editor
2013-05-29 19:55:23 +00:00
var converter = new Markdown.Converter();
var options = {
_DoItalicsAndBold: function(text) {
// Restore original markdown implementation
text = text.replace(/(\*\*|__)(?=\S)(.+?[*_]*)(?=\S)\1/g,
"<strong>$2</strong>");
text = text.replace(/(\*|_)(?=\S)(.+?)(?=\S)\1/g,
"<em>$2</em>");
return text;
}
};
converter.setOptions(options);
2014-03-26 00:29:34 +00:00
pagedownEditor = new Markdown.Editor(converter, undefined, {
2014-04-05 00:54:06 +00:00
undoManager: editor.undoMgr
2014-03-26 00:29:34 +00:00
});
2013-09-15 01:35:58 +00:00
2013-12-05 00:25:17 +00:00
// Custom insert link dialog
2014-03-18 01:10:22 +00:00
pagedownEditor.hooks.set("insertLinkDialog", function(callback) {
2013-12-05 00:25:17 +00:00
core.insertLinkCallback = callback;
utils.resetModalInputs();
$(".modal-insert-link").modal();
return true;
});
// Custom insert image dialog
2014-03-18 01:10:22 +00:00
pagedownEditor.hooks.set("insertImageDialog", function(callback) {
2013-12-05 00:25:17 +00:00
core.insertLinkCallback = callback;
if(core.catchModal) {
2013-09-12 23:25:25 +00:00
return true;
2013-12-05 00:25:17 +00:00
}
utils.resetModalInputs();
$(".modal-insert-image").modal();
return true;
});
2014-03-03 23:30:55 +00:00
2014-03-18 01:10:22 +00:00
eventMgr.onPagedownConfigure(pagedownEditor);
pagedownEditor.hooks.chain("onPreviewRefresh", eventMgr.onAsyncPreview);
2014-03-20 00:24:56 +00:00
pagedownEditor.run();
2014-04-05 00:54:06 +00:00
editor.undoMgr.init();
2013-05-29 19:55:23 +00:00
// Hide default buttons
2013-08-10 01:19:32 +00:00
$(".wmd-button-row li").addClass("btn btn-success").css("left", 0).find("span").hide();
2013-05-29 19:55:23 +00:00
// Add customized buttons
2013-08-22 00:19:59 +00:00
var $btnGroupElt = $('.wmd-button-group1');
$("#wmd-bold-button").append($('<i class="icon-bold">')).appendTo($btnGroupElt);
$("#wmd-italic-button").append($('<i class="icon-italic">')).appendTo($btnGroupElt);
2013-11-07 23:10:38 +00:00
$btnGroupElt = $('.wmd-button-group2');
2013-08-22 00:19:59 +00:00
$("#wmd-link-button").append($('<i class="icon-globe">')).appendTo($btnGroupElt);
$("#wmd-quote-button").append($('<i class="icon-indent-right">')).appendTo($btnGroupElt);
$("#wmd-code-button").append($('<i class="icon-code">')).appendTo($btnGroupElt);
$("#wmd-image-button").append($('<i class="icon-picture">')).appendTo($btnGroupElt);
2013-11-07 23:10:38 +00:00
$btnGroupElt = $('.wmd-button-group3');
2014-04-06 00:59:32 +00:00
var $openDiscussionElt = $btnGroupElt.find('.button-open-discussion');
2013-08-22 00:19:59 +00:00
$("#wmd-olist-button").append($('<i class="icon-list-numbered">')).appendTo($btnGroupElt);
$("#wmd-ulist-button").append($('<i class="icon-list-bullet">')).appendTo($btnGroupElt);
$("#wmd-heading-button").append($('<i class="icon-text-height">')).appendTo($btnGroupElt);
$("#wmd-hr-button").append($('<i class="icon-ellipsis">')).appendTo($btnGroupElt);
2014-04-06 00:59:32 +00:00
$openDiscussionElt.appendTo($btnGroupElt);
2013-11-07 23:10:38 +00:00
$btnGroupElt = $('.wmd-button-group4');
2013-08-22 00:19:59 +00:00
$("#wmd-undo-button").append($('<i class="icon-reply">')).appendTo($btnGroupElt);
$("#wmd-redo-button").append($('<i class="icon-forward">')).appendTo($btnGroupElt);
2013-07-18 23:30:28 +00:00
};
2014-03-03 23:30:55 +00:00
2013-08-04 15:49:14 +00:00
// Initialize multiple things and then fire eventMgr.onReady
2013-08-14 23:44:51 +00:00
var isDocumentPanelShown = false;
var isMenuPanelShown = false;
2013-08-04 15:49:14 +00:00
core.onReady = function() {
2014-03-20 00:24:56 +00:00
// Add RTL class
settings.editMode == 'rtl' && $(document.body).addClass('rtl');
2013-11-07 23:10:38 +00:00
if(window.viewerMode === true) {
2013-08-22 00:19:59 +00:00
document.body.innerHTML = bodyViewerHTML;
2013-08-04 15:49:14 +00:00
}
else {
2013-08-22 00:19:59 +00:00
document.body.innerHTML = bodyIndexHTML;
2013-08-04 15:49:14 +00:00
}
2014-03-19 00:33:57 +00:00
2014-03-12 00:53:15 +00:00
var styleContent = '';
// Apply font
function applyFont(size, screenWidth) {
screenWidth = screenWidth || 0;
//var codeFontSize = settings.editorFontSize;
//var codeLineHeight = Math.round(codeFontSize * 20 / 12);
2014-03-16 02:13:42 +00:00
var previewFontSize = size; // * 13 / 12;
2014-03-12 00:53:15 +00:00
styleContent += [
'@media (min-width: ' + screenWidth + 'px) {',
'#wmd-input, .textarea-helper {',
' font-size: ' + size + 'px;',
//' font-family: ' + settings.editorFontFamily + ';',
'}',
'#preview-contents {',
' font-size: ' + previewFontSize + 'px;',
'}',
'}',
].join('\n');
}
2014-03-16 02:13:42 +00:00
applyFont(16);
applyFont(17, 600);
applyFont(18, 1200);
2014-03-19 00:33:57 +00:00
2014-03-12 00:53:15 +00:00
function applyMaxWidth(maxWidth, screenWidth) {
styleContent += [
'@media (min-width: ' + screenWidth + 'px) {',
'#preview-contents {',
' max-width: ' + (maxWidth + 30) + 'px;',
'}',
'}',
].join('\n');
}
_.each(maxWidthMap, function(entry) {
applyMaxWidth(entry.maxWidth, entry.screenWidth);
});
2014-03-19 00:33:57 +00:00
2014-03-12 00:53:15 +00:00
// Apply dynamic stylesheet
2014-03-19 22:10:59 +00:00
var style = crel('style', {
type : 'text/css'
});
2014-03-12 00:53:15 +00:00
style.innerHTML = styleContent;
document.head.appendChild(style);
2013-12-01 15:43:46 +00:00
$navbarElt = $('.navbar');
$leftBtnElts = $navbarElt.find('.left-buttons');
$rightBtnElts = $navbarElt.find('.right-buttons');
2014-03-10 23:53:03 +00:00
$btnDropdown = $navbarElt.find('.buttons-dropdown');
$titleContainer = $navbarElt.find('.title-container');
2013-12-01 15:43:46 +00:00
$(window).bind("resize", adjustWindow);
2014-03-03 23:30:55 +00:00
2014-01-13 18:57:59 +00:00
// Initialize utils library
utils.init();
2014-03-03 23:30:55 +00:00
2013-09-30 22:37:58 +00:00
// Populate shortcuts in settings
shortcutMgr.addSettingEntries();
2014-03-03 23:30:55 +00:00
2014-01-21 23:48:42 +00:00
// Hide shortcuts settings if light mode
if(window.lightMode) {
$('.tab-settings-shortcuts').hide();
}
2013-05-29 19:55:23 +00:00
// listen to online/offline events
$(window).on('offline', core.setOffline);
$(window).on('online', setOnline);
if(navigator.onLine === false) {
core.setOffline();
}
// Detect user activity
$(document).mousemove(setUserActive).keypress(setUserActive);
// Avoid dropdown to close when clicking on submenu
$(".dropdown-submenu > a").click(function(e) {
e.stopPropagation();
});
2013-08-08 21:53:15 +00:00
2013-08-22 00:19:59 +00:00
$menuPanelElt = $('.menu-panel').collapse({
2013-08-13 00:03:38 +00:00
toggle: false
});
2013-11-07 23:10:38 +00:00
var menuPanelBackdropElt;
2013-08-22 00:19:59 +00:00
$menuPanelElt.on('show.bs.collapse', function(e) {
if(e.target === $menuPanelElt[0]) {
2013-08-12 22:08:22 +00:00
isMenuPanelShown = true;
2013-08-25 20:38:44 +00:00
menuPanelBackdropElt = utils.createBackdrop('collapse', '.menu-panel');
2013-08-25 00:45:14 +00:00
$menuPanelElt.addClass('move-to-front');
2013-12-02 22:11:17 +00:00
// To avoid opening delay
2014-03-19 22:10:59 +00:00
$.support.transition && setTimeout(function() {
2013-12-02 22:11:17 +00:00
$menuPanelElt.trigger($.support.transition.end);
}, 50);
2013-08-12 22:08:22 +00:00
}
2013-08-13 00:03:38 +00:00
else {
// Close all open sub-menus when one submenu opens
2013-08-22 00:19:59 +00:00
$menuPanelElt.find('.in').collapse('hide');
2013-08-13 00:03:38 +00:00
}
}).on('hide.bs.collapse', function(e) {
2013-08-22 00:19:59 +00:00
if(e.target === $menuPanelElt[0]) {
2013-08-13 00:03:38 +00:00
isMenuPanelShown = false;
2014-03-19 22:10:59 +00:00
menuPanelBackdropElt.removeBackdrop();
2013-08-25 00:45:14 +00:00
$menuPanelElt.removeClass('move-to-front');
2014-03-20 00:24:56 +00:00
$editorElt.focus();
2013-08-13 00:03:38 +00:00
}
2013-08-12 22:08:22 +00:00
}).on('hidden.bs.collapse', function(e) {
2013-08-22 00:19:59 +00:00
if(e.target === $menuPanelElt[0]) {
2013-08-13 00:03:38 +00:00
// Close all open sub-menus when menu panel is closed
2013-08-22 00:19:59 +00:00
$menuPanelElt.find('.in').collapse('hide');
2013-08-12 22:08:22 +00:00
}
2013-08-06 00:28:21 +00:00
});
2013-08-08 21:53:15 +00:00
2013-08-22 00:19:59 +00:00
$documentPanelElt = $('.document-panel').collapse({
2013-08-13 00:03:38 +00:00
toggle: false
});
2013-11-07 23:10:38 +00:00
var documentPanelBackdropElt;
2013-08-22 00:19:59 +00:00
$documentPanelElt.on('show.bs.collapse', function(e) {
if(e.target === $documentPanelElt[0]) {
2013-08-12 22:08:22 +00:00
isDocumentPanelShown = true;
2013-08-13 00:03:38 +00:00
documentPanelBackdropElt = utils.createBackdrop('collapse', '.document-panel');
2013-08-25 00:45:14 +00:00
$documentPanelElt.addClass('move-to-front');
2013-12-02 22:11:17 +00:00
// To avoid opening delay
2014-03-19 22:10:59 +00:00
$.support.transition && setTimeout(function() {
2013-12-02 22:11:17 +00:00
$documentPanelElt.trigger($.support.transition.end);
}, 50);
2013-08-13 00:03:38 +00:00
}
else {
// Close all open sub-menus when one submenu opens
2013-08-22 00:19:59 +00:00
$documentPanelElt.find('.in').collapse('hide');
2013-08-13 00:03:38 +00:00
}
}).on('hide.bs.collapse', function(e) {
2013-08-22 00:19:59 +00:00
if(e.target === $documentPanelElt[0]) {
2013-08-13 00:03:38 +00:00
isDocumentPanelShown = false;
2014-03-19 22:10:59 +00:00
documentPanelBackdropElt.removeBackdrop();
2013-08-25 00:45:14 +00:00
$documentPanelElt.removeClass('move-to-front');
2014-03-20 00:24:56 +00:00
$editorElt.focus();
2013-08-12 22:08:22 +00:00
}
}).on('hidden.bs.collapse', function(e) {
2013-08-22 00:19:59 +00:00
if(e.target === $documentPanelElt[0]) {
2013-08-12 22:08:22 +00:00
// Close all open sub-menus when menu panel is closed
2013-08-22 00:19:59 +00:00
$documentPanelElt.find('.in').collapse('hide');
2013-08-12 22:08:22 +00:00
}
2013-08-06 00:28:21 +00:00
});
2013-08-08 21:53:15 +00:00
2014-03-20 00:24:56 +00:00
// Create UI layout
createLayout();
2014-03-03 23:30:55 +00:00
2014-03-20 00:24:56 +00:00
// Editor
2014-03-12 00:53:15 +00:00
$editorElt = $('#wmd-input');
2013-08-04 00:53:46 +00:00
2014-03-20 00:24:56 +00:00
editor.init(document.querySelector('#wmd-input'), document.querySelector('.preview-container'));
2013-08-04 00:53:46 +00:00
// Do periodic tasks
intervalId = window.setInterval(function() {
utils.updateCurrentTime();
checkWindowUnique();
2013-11-07 23:10:38 +00:00
if(isUserActive() === true || window.viewerMode === true) {
2013-08-04 00:53:46 +00:00
eventMgr.onPeriodicRun();
checkOnline();
}
}, 1000);
eventMgr.onReady();
2013-12-01 15:43:46 +00:00
// Adjust the layout after the dom has changed
adjustWindow();
2013-08-04 00:53:46 +00:00
};
// Other initialization that are not prioritary
eventMgr.addListener("onReady", function() {
2014-03-03 23:30:55 +00:00
2013-09-15 01:35:58 +00:00
// In vertical mode, we have to offset the editor buttons otherwise they hide the editor buttons
2013-11-07 23:10:38 +00:00
if(!window.viewerMode && settings.layoutOrientation == "vertical") {
2013-09-15 01:35:58 +00:00
$previewButtonsElt.css('right', parseInt($previewButtonsElt.css('right')) + $editorButtonsElt.width());
}
2013-08-04 00:53:46 +00:00
2013-08-14 23:44:51 +00:00
var isModalShown = false;
$('.modal').on('show.bs.modal', function() {
// Close panel if open
2013-08-22 00:19:59 +00:00
$menuPanelElt.collapse('hide');
$documentPanelElt.collapse('hide');
2013-08-15 00:01:40 +00:00
isModalShown = true;
2013-08-14 23:44:51 +00:00
}).on('shown.bs.modal', function() {
2013-12-01 15:43:46 +00:00
var $elt = $(this);
2013-09-09 23:32:24 +00:00
setTimeout(function() {
2013-12-01 15:43:46 +00:00
// When modal opens focus on the first button
$elt.find('.btn:first').focus();
// Or on the first link if any
$elt.find('button:first').focus();
// Or on the first input if any
$elt.find("input:enabled:visible:first").focus();
2013-09-09 23:32:24 +00:00
}, 50);
2013-08-14 23:44:51 +00:00
}).on('hidden.bs.modal', function() {
// Focus on the editor when modal is gone
isModalShown = false;
2014-03-20 00:24:56 +00:00
$editorElt.focus();
2013-09-03 12:01:26 +00:00
// Revert to current theme when settings modal is closed
2013-12-02 23:29:48 +00:00
applyTheme(window.theme);
2013-08-14 23:44:51 +00:00
}).keyup(function(e) {
// Handle enter key in modals
if(e.which == 13 && !$(e.target).is("textarea")) {
$(this).find(".modal-footer a:last").click();
}
});
2014-03-03 23:30:55 +00:00
2013-09-22 22:16:59 +00:00
// Hide menu panel when clicking 'Save as' button
$('.collapse-save-as a').click(function() {
$menuPanelElt.collapse('hide');
});
2013-08-20 22:40:19 +00:00
2013-08-14 23:44:51 +00:00
// Configure Mousetrap
2013-11-07 23:10:38 +00:00
mousetrap.stopCallback = function(e, element) {
2013-09-14 16:59:40 +00:00
return isMenuPanelShown || isDocumentPanelShown || isModalShown || $(element).is("input, select, textarea:not(.ace_text-input)");
2013-08-14 23:44:51 +00:00
};
2013-05-29 19:55:23 +00:00
// Click events on "insert link" and "insert image" dialog buttons
$(".action-insert-link").click(function(e) {
var value = utils.getInputTextValue($("#input-insert-link"), e);
if(value !== undefined) {
2013-06-02 00:38:23 +00:00
core.insertLinkCallback(value);
2013-06-02 19:35:44 +00:00
core.insertLinkCallback = undefined;
2013-05-29 19:55:23 +00:00
}
});
$(".action-insert-image").click(function(e) {
var value = utils.getInputTextValue($("#input-insert-image"), e);
if(value !== undefined) {
2013-06-02 00:38:23 +00:00
core.insertLinkCallback(value);
2013-06-02 19:35:44 +00:00
core.insertLinkCallback = undefined;
2013-05-29 19:55:23 +00:00
}
});
2013-06-10 21:22:32 +00:00
2013-06-02 00:38:23 +00:00
// Hide events on "insert link" and "insert image" dialogs
2013-08-11 00:52:05 +00:00
$(".modal-insert-link, .modal-insert-image").on('hidden.bs.modal', function() {
2013-06-02 00:38:23 +00:00
if(core.insertLinkCallback !== undefined) {
core.insertLinkCallback(null);
2013-06-02 19:35:44 +00:00
core.insertLinkCallback = undefined;
2013-06-02 00:38:23 +00:00
}
2013-05-29 19:55:23 +00:00
});
// Settings loading/saving
$(".action-load-settings").click(function() {
loadSettings();
});
$(".action-apply-settings").click(function(e) {
saveSettings(e);
if(!e.isPropagationStopped()) {
window.location.reload();
}
});
2013-08-20 22:40:19 +00:00
// Hot theme switcher in the settings
2013-11-07 23:10:38 +00:00
var currentTheme = window.theme;
2013-08-20 22:40:19 +00:00
function applyTheme(theme) {
theme = theme || 'default';
if(currentTheme != theme) {
2013-08-23 23:50:14 +00:00
var themeModule = "less!themes/" + theme;
2013-11-07 23:10:38 +00:00
if(window.baseDir.indexOf('-min') !== -1) {
2013-08-23 23:50:14 +00:00
themeModule = "css!themes/" + theme;
}
2013-08-20 22:40:19 +00:00
// Undefine the module in RequireJS
requirejs.undef(themeModule);
// Then reload the style
require([
2013-08-23 23:50:14 +00:00
themeModule
2013-08-20 22:40:19 +00:00
]);
currentTheme = theme;
}
}
$themeInputElt = $("#input-settings-theme");
$themeInputElt.on("change", function() {
applyTheme(this.value);
});
// Import docs and settings
2013-11-07 23:10:38 +00:00
$(".action-import-docs-settings").click(function() {
$("#input-file-import-docs-settings").click();
2013-07-03 22:29:53 +00:00
});
2013-11-07 23:10:38 +00:00
var newstorage;
$("#input-file-import-docs-settings").change(function(evt) {
2013-07-03 22:29:53 +00:00
var files = (evt.dataTransfer || evt.target).files;
2013-08-11 00:52:05 +00:00
$(".modal-settings").modal("hide");
2013-07-03 22:29:53 +00:00
_.each(files, function(file) {
var reader = new FileReader();
reader.onload = (function(importedFile) {
return function(e) {
try {
2013-11-05 23:03:38 +00:00
newstorage = JSON.parse(e.target.result);
// Compare storage version
var newVersion = parseInt(newstorage.version.match(/^v(\d+)$/)[1], 10);
var currentVersion = parseInt(storage.version.match(/^v(\d+)$/)[1], 10);
2013-10-19 22:59:17 +00:00
if(newVersion > currentVersion) {
2013-11-05 23:03:38 +00:00
// We manage storage upgrade, not downgrade
2013-10-19 22:59:17 +00:00
eventMgr.onError("Incompatible version. Please upgrade StackEdit.");
} else {
$('.modal-import-docs-settings').modal('show');
}
2013-07-03 22:29:53 +00:00
}
2013-11-07 23:10:38 +00:00
catch(exc) {
eventMgr.onError("Wrong format: " + importedFile.name);
2013-07-03 22:29:53 +00:00
}
2013-10-12 23:36:45 +00:00
$("#input-file-import-docs-settings").val('');
2013-07-03 22:29:53 +00:00
};
})(file);
reader.readAsText(file);
2013-07-03 22:29:53 +00:00
});
});
2013-11-07 23:10:38 +00:00
$(".action-import-docs-settings-confirm").click(function() {
2013-11-05 23:03:38 +00:00
storage.clear();
2014-03-24 00:22:46 +00:00
var allowedKeys = /^file\.|^folder\.|^publish\.|^settings$|^sync\.|^google\.|^author\.|^themeV3$|^version$/;
2013-11-05 23:03:38 +00:00
_.each(newstorage, function(value, key) {
if(allowedKeys.test(key)) {
2013-11-05 23:03:38 +00:00
storage[key] = value;
}
});
window.location.reload();
});
2013-07-03 22:29:53 +00:00
// Export settings
2013-11-07 23:10:38 +00:00
$(".action-export-docs-settings").click(function() {
2013-11-05 23:03:38 +00:00
utils.saveAs(JSON.stringify(storage), "StackEdit local storage.json");
2013-07-03 22:29:53 +00:00
});
2013-05-29 19:55:23 +00:00
$(".action-default-settings").click(function() {
2013-11-05 23:03:38 +00:00
storage.removeItem("settings");
storage.removeItem("theme");
2014-01-13 01:39:28 +00:00
if(!settings.dropboxFullAccess) {
storage.removeItem('dropbox.lastChangeId');
}
2013-05-29 19:55:23 +00:00
window.location.reload();
});
$(".action-app-reset").click(function() {
2013-11-05 23:03:38 +00:00
storage.clear();
2013-05-29 19:55:23 +00:00
window.location.reload();
});
2013-06-22 23:48:57 +00:00
// Reset inputs
$(".action-reset-input").click(function() {
utils.resetModalInputs();
});
2013-05-29 19:55:23 +00:00
// Tooltips
2013-12-01 15:43:46 +00:00
var openedTooltip;
2013-12-02 00:09:39 +00:00
function createTooltip(selector, content) {
_.each(document.querySelectorAll(selector), function(tooltipElt) {
var $tooltipElt = $(tooltipElt);
$tooltipElt.tooltip({
html: true,
container: $tooltipElt.parents('.modal-content'),
placement: 'right',
trigger: 'manual',
title: content
}).click(function() {
var elt = this;
if(openedTooltip && openedTooltip[0] === elt) {
return;
}
_.defer(function() {
$(document).on("click.close-tooltip", function() {
openedTooltip && openedTooltip.tooltip('hide');
openedTooltip = undefined;
$(document).off("click.close-tooltip");
});
openedTooltip = $(elt).tooltip('show');
});
2013-12-01 15:43:46 +00:00
});
});
}
2014-03-03 23:30:55 +00:00
2013-12-02 00:09:39 +00:00
createTooltip(".tooltip-lazy-rendering", 'Disable preview rendering while typing in order to offload CPU. Refresh preview after 500 ms of inactivity.');
createTooltip(".tooltip-default-content", [
'Thanks for supporting StackEdit by adding a backlink in your documents!<br/><br/>',
'<b class="text-danger">NOTE: Backlinks in Stack Exchange Q/A are not welcome.</b>'
].join(''));
createTooltip(".tooltip-usercustom-extension", settingsUserCustomExtensionTooltipHTML);
createTooltip(".tooltip-template", settingsTemplateTooltipHTML);
2013-08-04 00:53:46 +00:00
// Avoid dropdown panels to close on click
$("div.dropdown-menu").click(function(e) {
e.stopPropagation();
});
2013-09-09 00:08:55 +00:00
// Non unique window dialog
$('.modal-non-unique').modal({
backdrop: "static",
keyboard: false,
show: false
});
2014-03-03 23:30:55 +00:00
2013-08-23 23:50:14 +00:00
// Load images
_.each(document.querySelectorAll('img'), function(imgElt) {
var $imgElt = $(imgElt);
var src = $imgElt.data('stackeditSrc');
if(src) {
2013-11-07 23:10:38 +00:00
$imgElt.attr('src', window.baseDir + '/img/' + src);
2013-08-23 23:50:14 +00:00
}
});
2013-05-29 19:55:23 +00:00
2013-11-07 23:10:38 +00:00
if(window.viewerMode === false) {
2013-08-21 00:16:10 +00:00
// Load theme list
2013-11-05 23:03:38 +00:00
var themeOptions = _.reduce(constants.THEME_LIST, function(themeOptions, name, value) {
2013-08-21 00:16:10 +00:00
return themeOptions + '<option value="' + value + '">' + name + '</option>';
}, '');
document.getElementById('input-settings-theme').innerHTML = themeOptions;
}
2013-05-29 19:55:23 +00:00
});
return core;
});