Stackedit/public/res/extensions/shortcuts.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-04-15 23:16:08 +00:00
define([
"jquery",
"underscore",
"utils",
"mousetrap",
"classes/Extension",
"text!extensions/shortcutsDefaultMapping.js",
"text!html/shortcutsSettingsBlock.html",
], function($, _, utils, mousetrap, Extension, shortcutsDefaultMapping, shortcutsSettingsBlockHTML) {
var shortcuts = new Extension("shortcuts", "Shortcuts", true, true);
shortcuts.settingsBlock = shortcutsSettingsBlockHTML;
shortcuts.defaultConfig = {
mapping: shortcutsDefaultMapping,
};
var eventMgr;
2014-04-16 23:29:51 +00:00
var pagedownEditor;
2014-04-15 23:16:08 +00:00
shortcuts.onEventMgrCreated = function(eventMgrParameter) {
eventMgr = eventMgrParameter;
2014-04-16 23:29:51 +00:00
eventMgr.addListener('onPagedownConfigure', function(pagedownEditorParam) {
pagedownEditor = pagedownEditorParam;
2014-04-15 23:16:08 +00:00
});
};
shortcuts.onLoadSettings = function() {
utils.setInputValue("#textarea-shortcuts-mapping", shortcuts.config.mapping);
};
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-16 23:29:51 +00:00
/*jshint unused:false */
function bindPagedownButton(buttonName) {
return function(evt) {
pagedownEditor.uiManager.doClick(pagedownEditor.uiManager.buttons[buttonName]);
evt.preventDefault();
};
}
2014-04-17 23:41:35 +00:00
function expand(text, replacement) {
utils.defer(function() {
require('editor').replacePreviousText(text, replacement);
});
}
/*jshint unused:true */
2014-04-15 23:16:08 +00:00
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);
}
};
return shortcuts;
});