Stackedit/public/res/extensions/shortcuts.js

79 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2014-04-15 23:16:08 +00:00
define([
2014-04-26 00:53:06 +00:00
"jquery",
"underscore",
"utils",
"mousetrap",
"classes/Extension",
"text!extensions/shortcutsDefaultMapping.settings",
"text!html/shortcutsSettingsBlock.html",
"text!html/tooltipSettingsShortcutsExtension.html"
], function($, _, utils, mousetrap, Extension, shortcutsDefaultMapping, shortcutsSettingsBlockHTML, tooltipSettingsShortcutsExtensionHTML) {
2014-04-15 23:16:08 +00:00
2014-04-26 00:53:06 +00:00
var shortcuts = new Extension("shortcuts", "Shortcuts", true, true);
shortcuts.settingsBlock = shortcutsSettingsBlockHTML;
shortcuts.defaultConfig = {
mapping: shortcutsDefaultMapping
};
2014-04-15 23:16:08 +00:00
2014-04-26 00:53:06 +00:00
var eventMgr;
var pagedownEditor;
shortcuts.onEventMgrCreated = function(eventMgrParameter) {
eventMgr = eventMgrParameter;
eventMgr.addListener('onPagedownConfigure', function(pagedownEditorParam) {
pagedownEditor = pagedownEditorParam;
});
};
2014-04-15 23:16:08 +00:00
2014-04-26 00:53:06 +00:00
shortcuts.onLoadSettings = function() {
utils.setInputValue("#textarea-shortcuts-mapping", shortcuts.config.mapping);
};
2014-04-15 23:16:08 +00:00
2014-04-26 00:53:06 +00:00
shortcuts.onSaveSettings = function(newConfig, event) {
newConfig.code = utils.getInputValue("#textarea-shortcuts-mapping");
try {
/*jshint evil: true */
eval('var test = ' + newConfig.code);
}
catch(e) {
eventMgr.onError(e);
// Mark the textarea as error
utils.getInputTextValue("#textarea-shortcuts-mapping", event, /^$/);
}
};
2014-04-15 23:16:08 +00:00
2014-04-26 00:53:06 +00:00
/*jshint unused:false */
function bindPagedownButton(buttonName) {
return function(evt) {
pagedownEditor.uiManager.doClick(pagedownEditor.uiManager.buttons[buttonName]);
evt.preventDefault();
};
}
2014-04-16 23:29:51 +00:00
2014-04-26 00:53:06 +00:00
function expand(text, replacement) {
utils.defer(function() {
require('editor').replacePreviousText(text, replacement);
});
}
2014-04-17 23:41:35 +00:00
2014-04-26 00:53:06 +00:00
/*jshint unused:true */
shortcuts.onInit = function() {
try {
/*jshint evil: true */
var shortcutMap;
eval('shortcutMap = ' + shortcuts.config.mapping);
_.each(shortcutMap, function(func, shortcut) {
mousetrap.bind(shortcut, func);
});
}
catch(e) {
console.error(e);
}
};
2014-04-15 23:16:08 +00:00
2014-04-26 00:53:06 +00:00
shortcuts.onReady = function() {
utils.createTooltip(".tooltip-shortcuts-extension", tooltipSettingsShortcutsExtensionHTML);
};
return shortcuts;
2014-04-15 23:16:08 +00:00
});