Stackedit/res/core.js

950 lines
38 KiB
JavaScript
Raw Normal View History

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",
2013-09-09 00:08:55 +00:00
"ace",
2013-05-26 22:59:03 +00:00
"utils",
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",
"config",
2013-08-30 22:45:29 +00:00
"uilayout",
2013-09-15 14:14:42 +00:00
'pagedown-ace',
2013-09-14 16:59:40 +00:00
'libs/ace_mode',
'ace/requirejs/text!ace/css/editor.css',
'ace/requirejs/text!ace/theme/textmate.css',
'ace/ext/spellcheck',
2013-09-21 11:03:29 +00:00
'ace/ext/searchbox'
2013-09-14 16:59:40 +00:00
2013-09-30 00:16:01 +00:00
], function($, _, crel, ace, utils, 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 = {};
// Used for periodic tasks
var intervalId = undefined;
// 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() {
if(userActive === true && utils.currentTime - userLastActivity > USER_IDLE_THRESHOLD) {
userActive = false;
}
return userActive && windowUnique;
}
// Used to only have 1 window of the application in the same browser
var windowId = undefined;
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-09-30 00:16:01 +00:00
localStorage.frontWindowId = windowId;
2013-05-29 19:55:23 +00:00
}
2013-09-30 00:16:01 +00:00
var frontWindowId = localStorage.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-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-08-04 00:53:46 +00:00
if(isOffline === true && navigator.onLine === true && offlineTime + 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",
timeout: AJAX_TIMEOUT,
dataType: "script"
}).done(function() {
setOnline();
});
}
}
// Load settings in settings dialog
2013-08-20 22:40:19 +00:00
var $themeInputElt = undefined;
2013-05-29 19:55:23 +00:00
function loadSettings() {
// Layout orientation
utils.setInputRadio("radio-layout-orientation", settings.layoutOrientation);
// Theme
2013-08-21 00:16:10 +00:00
utils.setInputValue($themeInputElt, 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);
// Commit message
utils.setInputValue("#input-settings-publish-commit-msg", settings.commitMsg);
2013-10-06 14:34:40 +00:00
// Gdrive full access
utils.setInputChecked("#input-settings-gdrive-full-access", settings.gdriveFullAccess);
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
utils.setInputValue("#textarea-settings-publish-pdf-template", settings.pdfTemplate);
2013-05-29 19:55:23 +00:00
// SSH proxy
utils.setInputValue("#input-settings-ssh-proxy", settings.sshProxy);
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");
// Commit message
newSettings.commitMsg = utils.getInputTextValue("#input-settings-publish-commit-msg", event);
2013-10-06 14:34:40 +00:00
// Gdrive full access
newSettings.gdriveFullAccess = utils.getInputChecked("#input-settings-gdrive-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
newSettings.pdfTemplate = utils.getInputTextValue("#textarea-settings-publish-pdf-template", event);
2013-05-29 19:55:23 +00:00
// SSH proxy
newSettings.sshProxy = utils.checkUrl(utils.getInputTextValue("#input-settings-ssh-proxy", event), true);
// 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()) {
$.extend(settings, newSettings);
localStorage.settings = JSON.stringify(settings);
localStorage.theme = theme;
}
}
2013-08-10 11:34:30 +00:00
// Set the panels visibility
2013-08-10 00:28:48 +00:00
var layout = undefined;
2013-08-22 00:19:59 +00:00
var $menuPanelElt = undefined;
var $documentPanelElt = undefined;
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-08-22 00:19:59 +00:00
var $previewButtonsElt = undefined;
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 ACE editor
2013-09-09 00:08:55 +00:00
var aceEditor = undefined;
2013-09-09 23:32:24 +00:00
function createAceEditor() {
2013-09-12 23:25:25 +00:00
if(lightMode) {
// In light mode, we replace ACE with a textarea
$('#wmd-input').replaceWith(function() {
return $('<textarea id="wmd-input">').addClass(this.className).addClass('form-control');
});
return;
}
2013-09-09 00:08:55 +00:00
aceEditor = ace.edit("wmd-input");
require('ace/ext/spellcheck');
aceEditor.setOption("spellcheck", true);
2013-09-09 00:08:55 +00:00
aceEditor.renderer.setShowGutter(false);
aceEditor.renderer.setPrintMarginColumn(false);
2013-09-12 23:25:25 +00:00
aceEditor.renderer.setPadding(EDITOR_DEFAULT_PADDING);
2013-09-09 00:08:55 +00:00
aceEditor.session.setUseWrapMode(true);
2013-09-21 00:41:41 +00:00
aceEditor.session.setNewLineMode("unix");
2013-09-14 16:59:40 +00:00
aceEditor.session.setMode("libs/ace_mode");
2013-09-15 01:35:58 +00:00
// Make bold titles...
2013-09-09 23:32:24 +00:00
(function(self) {
2013-09-15 01:35:58 +00:00
function checkLine(currentLine) {
var line = self.lines[currentLine];
if(line.length !== 0) {
if(line[0].type.indexOf("markup.heading.multi") === 0) {
_.each(self.lines[currentLine - 1], function(previousLineObject) {
previousLineObject.type = "markup.heading.prev.multi";
});
}
}
}
2013-09-09 23:32:24 +00:00
function customWorker() {
2013-09-15 01:35:58 +00:00
// Duplicate from background_tokenizer.js
if(!self.running) {
return;
}
2013-09-09 23:32:24 +00:00
var workerStart = new Date();
2013-09-15 01:35:58 +00:00
var currentLine = self.currentLine;
var endLine = -1;
2013-09-09 23:32:24 +00:00
var doc = self.doc;
2013-09-15 01:35:58 +00:00
while (self.lines[currentLine]) {
currentLine++;
}
var startLine = currentLine;
2013-09-09 23:32:24 +00:00
var len = doc.getLength();
2013-09-15 01:35:58 +00:00
var processedLines = 0;
self.running = false;
while (currentLine < len) {
self.$tokenizeRow(currentLine);
endLine = currentLine;
do {
checkLine(currentLine); // benweet
currentLine++;
} while (self.lines[currentLine]);
2013-09-09 00:08:55 +00:00
2013-09-09 23:32:24 +00:00
// only check every 5 lines
2013-09-15 01:35:58 +00:00
processedLines++;
2013-09-30 00:16:01 +00:00
if((processedLines % 5 === 0) && (new Date() - workerStart) > 20) {
2013-09-15 01:35:58 +00:00
self.running = setTimeout(customWorker, 20); // benweet
self.currentLine = currentLine;
2013-09-09 23:32:24 +00:00
return;
}
}
2013-09-15 01:35:58 +00:00
self.currentLine = currentLine;
2013-09-09 23:32:24 +00:00
2013-09-15 01:35:58 +00:00
if(startLine <= endLine)
self.fireUpdateEvent(startLine, endLine);
2013-09-09 23:32:24 +00:00
}
self.$worker = function() {
2013-09-15 01:35:58 +00:00
self.lines.splice(0, self.lines.length);
self.states.splice(0, self.states.length);
self.currentLine = 0;
2013-09-09 23:32:24 +00:00
customWorker();
};
2013-09-15 01:35:58 +00:00
2013-09-09 23:32:24 +00:00
})(aceEditor.session.bgTokenizer);
2013-09-15 01:35:58 +00:00
2013-09-30 00:16:01 +00:00
shortcutMgr.configureAce(aceEditor);
2013-09-09 23:32:24 +00:00
eventMgr.onAceCreated(aceEditor);
}
2013-09-15 01:35:58 +00:00
2013-09-09 23:32:24 +00:00
// Create the layout
2013-09-30 00:16:01 +00:00
var $editorButtonsElt = undefined;
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-08-10 00:28:48 +00:00
north__spacing_open: 6,
2013-08-10 11:34:30 +00:00
north__spacing_closed: 6,
2013-08-10 00:28:48 +00:00
spacing_open: 35,
spacing_closed: 35,
togglerLength_open: 60,
togglerLength_closed: 60,
2013-05-29 19:55:23 +00:00
stateManagement__enabled: false,
center__minWidth: 200,
2013-08-10 11:34:30 +00:00
center__minHeight: 200,
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) {
if(aceEditor !== undefined && paneName == 'center') {
aceEditor.resize();
2013-09-15 01:35:58 +00:00
var bottomMargin = (aceEditor.renderer.$size.scrollerHeight - aceEditor.renderer.lineHeight) / 2;
bottomMargin < 0 && (bottomMargin = 0);
aceEditor.renderer.setScrollMargin(0, bottomMargin, 0, 0);
2013-09-12 23:25:25 +00:00
setTimeout(function() {
2013-09-15 01:35:58 +00:00
var padding = (aceEditor.renderer.$size.scrollerWidth - settings.maxWidth) / 2;
2013-09-12 23:25:25 +00:00
if(padding < EDITOR_DEFAULT_PADDING) {
padding = EDITOR_DEFAULT_PADDING;
}
if(padding !== aceEditor.renderer.$padding) {
aceEditor.renderer.setPadding(padding);
aceEditor.resize(true);
}
}, 5);
2013-09-15 01:35:58 +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-09-12 23:25:25 +00:00
east__minSize: 260
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-09-15 01:35:58 +00:00
settings.maxWidth && $('#preview-contents').css('max-width', (settings.maxWidth + 30) + 'px');
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-08-22 00:19:59 +00:00
var $northTogglerElt = $(".ui-layout-toggler-north").addClass("btn btn-info").html('<i class="icon-none"></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-09-15 01:35:58 +00:00
if(viewerMode || settings.layoutOrientation == "horizontal") {
$('.ui-layout-resizer-north').append($resizerDecorator).append($previewButtonsElt);
2013-09-11 23:26:47 +00:00
$('.ui-layout-resizer-east').append($northTogglerElt).append($editorButtonsElt);
2013-08-08 23:32:03 +00:00
}
else {
$('.ui-layout-resizer-north').append($resizerDecorator);
2013-09-15 01:35:58 +00:00
$('.ui-layout-resizer-south').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
// Create the PageDown editor
2013-06-02 00:38:23 +00:00
var editor = undefined;
2013-09-12 23:25:25 +00:00
var $editorElt = undefined;
2013-06-03 22:19:52 +00:00
var fileDesc = undefined;
2013-06-02 00:38:23 +00:00
var documentContent = undefined;
2013-09-09 23:32:24 +00:00
var UndoManager = require("ace/undomanager").UndoManager;
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-06-02 00:38:23 +00:00
documentContent = undefined;
2013-06-03 22:19:52 +00:00
var initDocumentContent = fileDesc.content;
2013-09-09 00:08:55 +00:00
2013-09-12 23:25:25 +00:00
if(aceEditor !== undefined) {
aceEditor.setValue(initDocumentContent, -1);
aceEditor.getSession().setUndoManager(new UndoManager());
}
else {
$editorElt.val(initDocumentContent);
}
2013-09-15 01:35:58 +00:00
2013-06-02 00:38:23 +00:00
if(editor !== undefined) {
2013-06-03 22:19:52 +00:00
// If the editor is already created
2013-09-14 16:59:40 +00:00
aceEditor && aceEditor.selection.setSelectionRange(fileDesc.editorSelectRange);
(aceEditor && aceEditor.focus()) || $editorElt.focus();
2013-07-28 10:35:04 +00:00
editor.refreshPreview();
2013-06-02 00:38:23 +00:00
return;
}
2013-09-09 00:08:55 +00:00
2013-08-22 00:19:59 +00:00
var $previewContainerElt = $(".preview-container");
2013-06-10 21:22:32 +00:00
2013-09-12 23:25:25 +00:00
if(!lightMode) {
// Store editor scrollTop on scroll event
2013-09-16 00:03:17 +00:00
var saveScroll = _.debounce(function() {
2013-09-12 23:25:25 +00:00
if(documentContent !== undefined) {
2013-09-16 00:03:17 +00:00
fileDesc.editorScrollTop = aceEditor.renderer.getScrollTop();
2013-09-12 23:25:25 +00:00
}
2013-09-16 00:03:17 +00:00
}, 100);
aceEditor.session.on('changeScrollTop', saveScroll);
2013-09-12 23:25:25 +00:00
// Store editor selection on change
2013-09-16 00:03:17 +00:00
var saveSelection = _.debounce(function() {
2013-09-12 23:25:25 +00:00
if(documentContent !== undefined) {
fileDesc.editorSelectRange = aceEditor.getSelectionRange();
}
2013-09-16 00:03:17 +00:00
}, 100);
aceEditor.session.selection.on('changeSelection', saveSelection);
aceEditor.session.selection.on('changeCursor', saveSelection);
2013-09-12 23:25:25 +00:00
// Store preview scrollTop on scroll event
$previewContainerElt.scroll(function() {
if(documentContent !== undefined) {
fileDesc.previewScrollTop = $previewContainerElt.scrollTop();
}
});
}
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();
2013-09-15 01:35:58 +00:00
2013-09-14 16:59:40 +00:00
function checkDocumentChanges() {
var newDocumentContent = $editorElt.val();
if(aceEditor !== undefined) {
newDocumentContent = aceEditor.getValue();
}
if(documentContent !== undefined && documentContent != newDocumentContent) {
fileDesc.content = newDocumentContent;
eventMgr.onContentChanged(fileDesc);
}
documentContent = newDocumentContent;
}
2013-09-15 01:35:58 +00:00
2013-09-12 23:25:25 +00:00
if(!lightMode) {
editor = new Markdown.Editor(converter);
// Custom insert link dialog
editor.hooks.set("insertLinkDialog", function(callback) {
core.insertLinkCallback = callback;
utils.resetModalInputs();
$(".modal-insert-link").modal();
2013-07-16 23:54:56 +00:00
return true;
2013-09-12 23:25:25 +00:00
});
// Custom insert image dialog
editor.hooks.set("insertImageDialog", function(callback) {
core.insertLinkCallback = callback;
if(core.catchModal) {
return true;
}
utils.resetModalInputs();
$(".modal-insert-image").modal();
return true;
});
}
else {
2013-09-14 16:59:40 +00:00
// That's the light Markdown editor replacing the one from pagedown
var $wmdPreviewElt = $('#wmd-preview');
var hooks = new Markdown.HookCollection();
hooks.addNoop("onPreviewRefresh");
function makePreviewHtml() {
var text = $editorElt.val();
text = converter.makeHtml(text);
$wmdPreviewElt.html(text);
hooks.onPreviewRefresh();
}
var debouncedMakePreview = _.debounce(makePreviewHtml, 1000);
var previewWrapper = function() {
if(documentContent === undefined) {
makePreviewHtml();
eventMgr.onFileOpen(fileDesc);
}
else {
debouncedMakePreview();
}
checkDocumentChanges();
};
$editorElt.on("input propertychange", previewWrapper);
2013-09-12 23:25:25 +00:00
editor = {
2013-09-14 16:59:40 +00:00
hooks: hooks,
2013-09-15 01:35:58 +00:00
getConverter: function() {
return converter;
},
2013-09-14 16:59:40 +00:00
run: previewWrapper,
refreshPreview: previewWrapper
2013-09-12 23:25:25 +00:00
};
}
2013-05-29 19:55:23 +00:00
2013-06-02 00:38:23 +00:00
var previewWrapper;
2013-05-29 19:55:23 +00:00
if(settings.lazyRendering === true) {
previewWrapper = function(makePreview) {
var debouncedMakePreview = _.debounce(makePreview, 500);
return function() {
if(documentContent === undefined) {
makePreview();
eventMgr.onFileOpen(fileDesc);
2013-08-22 00:19:59 +00:00
$previewContainerElt.scrollTop(fileDesc.previewScrollTop);
2013-09-09 23:32:24 +00:00
_.defer(function() {
2013-09-15 01:35:58 +00:00
aceEditor.renderer.scrollToY(fileDesc.editorScrollTop);
2013-09-09 23:32:24 +00:00
});
2013-05-29 19:55:23 +00:00
}
else {
debouncedMakePreview();
}
checkDocumentChanges();
};
};
}
else {
previewWrapper = function(makePreview) {
return function() {
makePreview();
2013-06-03 22:19:52 +00:00
if(documentContent === undefined) {
eventMgr.onFileOpen(fileDesc);
2013-08-22 00:19:59 +00:00
$previewContainerElt.scrollTop(fileDesc.previewScrollTop);
2013-09-15 14:14:42 +00:00
_.defer(function() {
aceEditor.renderer.scrollToY(fileDesc.editorScrollTop);
});
2013-06-03 22:19:52 +00:00
}
checkDocumentChanges();
2013-05-29 19:55:23 +00:00
};
};
}
2013-09-09 23:32:24 +00:00
eventMgr.onPagedownConfigure(editor);
2013-07-30 08:46:36 +00:00
editor.hooks.chain("onPreviewRefresh", eventMgr.onAsyncPreview);
2013-09-09 23:32:24 +00:00
editor.run(aceEditor, previewWrapper);
2013-09-14 16:59:40 +00:00
aceEditor && aceEditor.selection.setSelectionRange(fileDesc.editorSelectRange);
(aceEditor && aceEditor.focus()) || $editorElt.focus();
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);
var $btnGroupElt = $('.wmd-button-group2');
$("#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);
var $btnGroupElt = $('.wmd-button-group3');
$("#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);
var $btnGroupElt = $('.wmd-button-group4');
$("#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
};
2013-10-06 14:34:40 +00:00
// Shows a dialog to force the user to click a button before opening oauth popup
var oauthRedirectCallback = undefined;
core.oauthRedirect = function(providerName, callback) {
oauthRedirectCallback = callback;
$('.oauth-redirect-provider').text(providerName);
$('.modal-oauth-redirect').modal("show");
};
2013-07-18 23:30:28 +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() {
if(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
}
2013-09-30 22:37:58 +00:00
// Populate shortcuts in settings
shortcutMgr.addSettingEntries();
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-08-12 22:08:22 +00:00
var menuPanelBackdropElt = undefined;
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-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;
2013-08-25 21:11:07 +00:00
menuPanelBackdropElt.parentNode.removeChild(menuPanelBackdropElt);
2013-08-25 00:45:14 +00:00
$menuPanelElt.removeClass('move-to-front');
2013-09-14 16:59:40 +00:00
(aceEditor && aceEditor.focus()) || $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
});
var documentPanelBackdropElt = undefined;
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-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;
2013-08-25 21:11:07 +00:00
documentPanelBackdropElt.parentNode.removeChild(documentPanelBackdropElt);
2013-08-25 00:45:14 +00:00
$documentPanelElt.removeClass('move-to-front');
2013-09-14 16:59:40 +00:00
(aceEditor && aceEditor.focus()) || $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
2013-09-12 23:25:25 +00:00
// ACE editor
createAceEditor();
2013-09-15 01:35:58 +00:00
2013-09-12 23:25:25 +00:00
// Editor's element
$editorElt = $("#wmd-input").css({
2013-08-04 00:53:46 +00:00
// Apply editor font
"font-family": settings.editorFontFamily,
"font-size": settings.editorFontSize + "px",
2013-09-09 23:32:24 +00:00
"line-height": Math.round(settings.editorFontSize * (20 / 12)) + "px"
2013-08-04 00:53:46 +00:00
});
2013-09-16 23:14:17 +00:00
$editorElt.find('.ace_content').css({
"background-size": "64px " + Math.round(settings.editorFontSize * (20 / 12)) + "px",
});
2013-08-04 00:53:46 +00:00
2013-09-09 23:32:24 +00:00
// UI layout
createLayout();
2013-08-04 00:53:46 +00:00
// Do periodic tasks
intervalId = window.setInterval(function() {
utils.updateCurrentTime();
checkWindowUnique();
if(isUserActive() === true || viewerMode === true) {
eventMgr.onPeriodicRun();
checkOnline();
}
}, 1000);
eventMgr.onReady();
};
// Other initialization that are not prioritary
eventMgr.addListener("onReady", function() {
2013-09-15 01:35:58 +00:00
// In vertical mode, we have to offset the editor buttons otherwise they hide the editor buttons
if(!viewerMode && settings.layoutOrientation == "vertical") {
$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() {
// Focus on the first input when modal opens
2013-09-09 23:32:24 +00:00
var elt = $(this);
setTimeout(function() {
2013-08-14 23:44:51 +00:00
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;
2013-09-14 16:59:40 +00:00
(aceEditor && aceEditor.focus()) || $editorElt.focus();
2013-09-03 12:01:26 +00:00
// Revert to current theme when settings modal is closed
applyTheme(localStorage.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();
}
});
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
mousetrap.stopCallback = function(e, element, combo) {
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-08-21 00:16:10 +00:00
var currentTheme = 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-08-26 04:58:18 +00:00
if(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);
});
2013-07-03 22:29:53 +00:00
// Import settings
$(".action-import-settings").click(function(e) {
$("#input-file-import-settings").click();
});
$("#input-file-import-settings").change(function(evt) {
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) {
var content = e.target.result;
try {
JSON.parse(content);
}
catch(e) {
2013-07-30 08:46:36 +00:00
eventMgr.onError(importedFile.name + " is not a valid JSON file.");
2013-07-03 22:29:53 +00:00
return;
}
localStorage.settings = content;
window.location.reload();
};
})(file);
var blob = file.slice(0, IMPORT_FILE_MAX_CONTENT_SIZE);
reader.readAsText(blob);
});
});
// Export settings
$(".action-export-settings").click(function(e) {
utils.saveAs(JSON.stringify(settings), "StackEdit Settings.json");
});
2013-05-29 19:55:23 +00:00
$(".action-default-settings").click(function() {
localStorage.removeItem("settings");
localStorage.removeItem("theme");
window.location.reload();
});
$(".action-app-reset").click(function() {
localStorage.clear();
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
$(".tooltip-lazy-rendering").tooltip({
2013-08-11 00:52:05 +00:00
container: '.modal-settings',
2013-05-29 19:55:23 +00:00
placement: 'right',
2013-06-02 00:38:23 +00:00
trigger: 'hover',
2013-05-29 19:55:23 +00:00
title: 'Disable preview rendering while typing in order to offload CPU. Refresh preview after 500 ms of inactivity.'
});
$(".tooltip-default-content").tooltip({
html: true,
2013-08-11 00:52:05 +00:00
container: '.modal-settings',
2013-05-29 19:55:23 +00:00
placement: 'right',
2013-06-02 00:38:23 +00:00
trigger: 'hover',
2013-05-29 19:55:23 +00:00
title: 'Thanks for supporting StackEdit by adding a backlink in your documents!'
});
2013-10-06 14:34:40 +00:00
var tooltipOpen = false;
2013-07-07 20:07:11 +00:00
$(".tooltip-usercustom-extension").tooltip({
html: true,
2013-08-11 00:52:05 +00:00
container: '.modal-settings',
2013-07-07 20:07:11 +00:00
placement: 'right',
trigger: 'manual',
title: settingsUserCustomExtensionTooltipHTML
}).click(function(e) {
$(this).tooltip('show');
$(document).on("click.tooltip-usercustom-extension", function(e) {
2013-10-06 14:34:40 +00:00
tooltipOpen = false;
2013-07-07 20:07:11 +00:00
$(".tooltip-usercustom-extension").tooltip('hide');
$(document).off("click.tooltip-usercustom-extension");
});
2013-10-06 14:34:40 +00:00
!tooltipOpen && e.stopPropagation();
tooltipOpen = true;
2013-07-07 20:07:11 +00:00
});
2013-08-22 19:10:57 +00:00
_.each(document.querySelectorAll(".tooltip-template"), function(tooltipElt) {
var $tooltipElt = $(tooltipElt);
$tooltipElt.tooltip({
html: true,
container: $tooltipElt.parents('.modal'),
placement: 'right',
trigger: 'manual',
title: settingsTemplateTooltipHTML
}).click(function(e) {
$tooltipElt.tooltip('show');
$(document).on("click.tooltip-template", function(e) {
2013-10-06 14:34:40 +00:00
tooltipOpen = false;
2013-08-22 19:10:57 +00:00
$(".tooltip-template").tooltip('hide');
$(document).off("click.tooltip-template");
});
2013-10-06 14:34:40 +00:00
!tooltipOpen && e.stopPropagation();
tooltipOpen = true;
2013-06-02 00:38:23 +00:00
});
2013-05-29 19:55:23 +00:00
});
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
});
2013-10-06 14:34:40 +00:00
// OAuth redirect dialog
$('.modal-oauth-redirect').modal({
backdrop: "static",
keyboard: false,
show: false
});
$('.action-oauth-redirect').click(function() {
oauthRedirectCallback();
});
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) {
$imgElt.attr('src', baseDir + '/img/' + src);
}
});
2013-05-29 19:55:23 +00:00
2013-08-22 19:10:57 +00:00
if(viewerMode === false) {
2013-08-21 00:16:10 +00:00
// Load theme list
var themeOptions = _.reduce(THEME_LIST, function(themeOptions, name, value) {
return themeOptions + '<option value="' + value + '">' + name + '</option>';
}, '');
document.getElementById('input-settings-theme').innerHTML = themeOptions;
}
2013-05-29 19:55:23 +00:00
});
return core;
});