Merge pull request #223 from trwired/user-configurable-shortcuts

Gives users the ability to change editor's keyboard shortcuts
patch-ionaru
Wes Cossick 9 years ago
commit 0d9078c98d

@ -95,6 +95,7 @@ simplemde.value("This text will appear in the editor");
- **renderingConfig**: Adjust settings for parsing the Markdown during previewing (not editing). - **renderingConfig**: Adjust settings for parsing the Markdown during previewing (not editing).
- **singleLineBreaks**: If set to `false`, disable parsing GFM single line breaks. Defaults to `true`. - **singleLineBreaks**: If set to `false`, disable parsing GFM single line breaks. Defaults to `true`.
- **codeSyntaxHighlighting**: If set to `true`, will highlight using [highlight.js](https://github.com/isagalaev/highlight.js). Defaults to `false`. To use this feature you must include highlight.js on your page. For example, include the script and the CSS files like:<br>`<script src="https://cdn.jsdelivr.net/highlight.js/latest/highlight.min.js"></script>`<br>`<link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/latest/styles/github.min.css">` - **codeSyntaxHighlighting**: If set to `true`, will highlight using [highlight.js](https://github.com/isagalaev/highlight.js). Defaults to `false`. To use this feature you must include highlight.js on your page. For example, include the script and the CSS files like:<br>`<script src="https://cdn.jsdelivr.net/highlight.js/latest/highlight.min.js"></script>`<br>`<link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/latest/styles/github.min.css">`
- **shortcuts**: Keyboard shortcuts associated with this instance. Defaults to the [array of shortcuts](#keyboard-shortcuts).
- **showIcons**: An array of icon names to show. Can be used to show specific icons hidden by default without completely customizing the toolbar. - **showIcons**: An array of icon names to show. Can be used to show specific icons hidden by default without completely customizing the toolbar.
- **spellChecker**: If set to `false`, disable the spell checker. Defaults to `true`. - **spellChecker**: If set to `false`, disable the spell checker. Defaults to `true`.
- **status**: If set to `false`, hide the status bar. Defaults to `true`. - **status**: If set to `false`, hide the status bar. Defaults to `true`.
@ -147,6 +148,9 @@ var simplemde = new SimpleMDE({
singleLineBreaks: false, singleLineBreaks: false,
codeSyntaxHighlighting: true, codeSyntaxHighlighting: true,
}, },
shortcuts: {
drawTable: "Cmd-Alt-T"
},
showIcons: ["code", "table"], showIcons: ["code", "table"],
spellChecker: false, spellChecker: false,
status: false, status: false,
@ -159,31 +163,33 @@ var simplemde = new SimpleMDE({
#### Toolbar icons #### Toolbar icons
Below are the built-in toolbar icons (only some of which are enabled by default), which can be reorganized however you like. "Name" is the name of the icon, referenced in the JS. "Action" is either a function or a URL to open. "Class" is the class given to the icon. "Tooltip" is the small tooltip that appears via the `title=""` attribute. Any `Ctrl` or `Alt` in the title tags will be converted automatically to their Mac equivalents when needed. Additionally, you can add a separator between any icons by adding `"|"` to the toolbar array. Below are the built-in toolbar icons (only some of which are enabled by default), which can be reorganized however you like. "Name" is the name of the icon, referenced in the JS. "Action" is either a function or a URL to open. "Class" is the class given to the icon. "Tooltip" is the small tooltip that appears via the `title=""` attribute. Note that shortcut hints are added automatically and reflect the specified "action" if it has a keybind assigned to it (ie. with the value of "action" set to `bold` and that of "tootip" set to "Bold", the final text the user will see - assuming the default shortcuts are unchanged - would be "Bold (Ctrl-B)").
Additionally, you can add a separator between any icons by adding `"|"` to the toolbar array.
Name | Action | Tooltip<br>Class Name | Action | Tooltip<br>Class
:--- | :----- | :-------------- :--- | :----- | :--------------
bold | toggleBold | Bold (Ctrl+B)<br>fa fa-bold bold | toggleBold | Bold<br>fa fa-bold
italic | toggleItalic | Italic (Ctrl+I)<br>fa fa-italic italic | toggleItalic | Italic<br>fa fa-italic
strikethrough | toggleStrikethrough | Strikethrough<br>fa fa-strikethrough strikethrough | toggleStrikethrough | Strikethrough<br>fa fa-strikethrough
heading | toggleHeadingSmaller | Heading (Ctrl+H)<br>fa fa-header heading | toggleHeadingSmaller | Heading<br>fa fa-header
heading-smaller | toggleHeadingSmaller | Smaller Heading (Ctrl+H)<br>fa fa-header heading-smaller | toggleHeadingSmaller | Smaller Heading<br>fa fa-header
heading-bigger | toggleHeadingBigger | Bigger Heading (Shift+Ctrl+H)<br>fa fa-lg fa-header heading-bigger | toggleHeadingBigger | Bigger Heading<br>fa fa-lg fa-header
heading-1 | toggleHeading1 | Big Heading<br>fa fa-header fa-header-x fa-header-1 heading-1 | toggleHeading1 | Big Heading<br>fa fa-header fa-header-x fa-header-1
heading-2 | toggleHeading2 | Medium Heading<br>fa fa-header fa-header-x fa-header-2 heading-2 | toggleHeading2 | Medium Heading<br>fa fa-header fa-header-x fa-header-2
heading-3 | toggleHeading3 | Small Heading<br>fa fa-header fa-header-x fa-header-3 heading-3 | toggleHeading3 | Small Heading<br>fa fa-header fa-header-x fa-header-3
code | toggleCodeBlock | Code (Ctrl+Alt+C)<br>fa fa-code code | toggleCodeBlock | Code<br>fa fa-code
quote | toggleBlockquote | Quote (Ctrl+')<br>fa fa-quote-left quote | toggleBlockquote | Quote<br>fa fa-quote-left
unordered-list | toggleUnorderedList | Generic List (Ctrl+L)<br>fa fa-list-ul unordered-list | toggleUnorderedList | Generic List<br>fa fa-list-ul
ordered-list | toggleOrderedList | Numbered List (Ctrl+Alt+L)<br>fa fa-list-ol ordered-list | toggleOrderedList | Numbered List<br>fa fa-list-ol
clean-block | cleanBlock | Clean block (Ctrl+E)<br>fa fa-eraser fa-clean-block clean-block | cleanBlock | Clean block<br>fa fa-eraser fa-clean-block
link | drawLink | Create Link (Ctrl+K)<br>fa fa-link link | drawLink | Create Link<br>fa fa-link
image | drawImage | Insert Image (Ctrl+Alt+I)<br>fa fa-picture-o image | drawImage | Insert Image<br>fa fa-picture-o
table | drawTable | Insert Table<br>fa fa-table table | drawTable | Insert Table<br>fa fa-table
horizontal-rule | drawHorizontalRule | Insert Horizontal Line<br>fa fa-minus horizontal-rule | drawHorizontalRule | Insert Horizontal Line<br>fa fa-minus
preview | togglePreview | Toggle Preview (Ctrl+P)<br>fa fa-eye no-disable preview | togglePreview | Toggle Preview<br>fa fa-eye no-disable
side-by-side | toggleSideBySide | Toggle Side by Side (F9)<br>fa fa-columns no-disable no-mobile side-by-side | toggleSideBySide | Toggle Side by Side<br>fa fa-columns no-disable no-mobile
fullscreen | toggleFullScreen | Toggle Fullscreen (F11)<br>fa fa-arrows-alt no-disable no-mobile fullscreen | toggleFullScreen | Toggle Fullscreen<br>fa fa-arrows-alt no-disable no-mobile
guide | [This link](http://nextstepwebs.github.io/simplemde-markdown-editor/markdown-guide) | Markdown Guide<br>fa fa-question-circle guide | [This link](http://nextstepwebs.github.io/simplemde-markdown-editor/markdown-guide) | Markdown Guide<br>fa fa-question-circle
Customize the toolbar using the `toolbar` option like: Customize the toolbar using the `toolbar` option like:
@ -200,7 +206,7 @@ var simplemde = new SimpleMDE({
name: "bold", name: "bold",
action: SimpleMDE.toggleBold, action: SimpleMDE.toggleBold,
className: "fa fa-bold", className: "fa fa-bold",
title: "Bold (Ctrl+B)", title: "Bold",
}, },
{ {
name: "custom", name: "custom",
@ -216,6 +222,43 @@ var simplemde = new SimpleMDE({
}); });
``` ```
#### Keyboard shortcuts
SimpleMDE comes with an array of predefined keyboard shortcuts, but they can be altered with a configuration option. The list of default ones is as follows:
Shortcut | Action
:------- | :-----
*Cmd-'* | "toggleBlockquote"
*Cmd-B* | "toggleBold"
*Cmd-E* | "cleanBlock"
*Cmd-H* | "toggleHeadingSmaller"
*Cmd-I* | "toggleItalic"
*Cmd-K* | "drawLink"
*Cmd-L* | "toggleUnorderedList"
*Cmd-P* | "togglePreview"
*Cmd-Alt-C* | "toggleCodeBlock"
*Cmd-Alt-I* | "drawImage"
*Cmd-Alt-L* | "toggleOrderedList"
*Shift-Cmd-H* | "toggleHeadingBigger"
*F9* | "toggleSideBySide"
*F11* | "toggleFullScreen"
Here is how you can change a few, while leaving others untouched:
```JavaScript
var simplemde = new SimpleMDE({
shortcuts: {
"toggleOrderedList": "Ctrl-Alt-K", // alter the shortcut for toggleOrderedList
"toggleCodeBlock": null, // unbind Ctrl-Alt-C
"drawTable": "Cmd-Alt-T" // bind Cmd-Alt-T to drawTable action, which doesn't come with a default shortcut
}
});
```
Shortcuts are automatically converted between platforms. If you define a shortcut as "Cmd-B", on PC that shortcut will be changed to "Ctrl-B". Conversely, a shortcut defined as "Ctrl-I" will become "Cmd-I" in a browser of a Mac user.
The list of actions that can be bound is the same as the list of built-in actions available for [toolbar buttons](#toolbar-icons).
#### Height #### Height
To change the minimum height (before it starts auto-growing): To change the minimum height (before it starts auto-growing):

File diff suppressed because one or more lines are too long

@ -16,19 +16,56 @@ var marked = require("marked");
// Some variables // Some variables
var isMac = /Mac/.test(navigator.platform); var isMac = /Mac/.test(navigator.platform);
// Mapping of actions that can be bound to keyboard shortcuts or toolbar buttons
var bindings = {
"toggleBold": toggleBold,
"toggleItalic": toggleItalic,
"drawLink": drawLink,
"toggleHeadingSmaller": toggleHeadingSmaller,
"toggleHeadingBigger": toggleHeadingBigger,
"drawImage": drawImage,
"toggleBlockquote": toggleBlockquote,
"toggleOrderedList": toggleOrderedList,
"toggleUnorderedList": toggleUnorderedList,
"toggleCodeBlock": toggleCodeBlock,
"togglePreview": togglePreview,
"toggleStrikethrough": toggleStrikethrough,
"toggleHeading1": toggleHeading1,
"toggleHeading2": toggleHeading2,
"toggleHeading3": toggleHeading3,
"cleanBlock": cleanBlock,
"drawTable": drawTable,
"drawHorizontalRule": drawHorizontalRule,
"undo": undo,
"redo": redo,
"toggleSideBySide": toggleSideBySide,
"toggleFullScreen": toggleFullScreen
};
var shortcuts = { var shortcuts = {
"Cmd-B": toggleBold, "toggleBold": "Cmd-B",
"Cmd-I": toggleItalic, "toggleItalic": "Cmd-I",
"Cmd-K": drawLink, "drawLink": "Cmd-K",
"Cmd-H": toggleHeadingSmaller, "toggleHeadingSmaller": "Cmd-H",
"Shift-Cmd-H": toggleHeadingBigger, "toggleHeadingBigger": "Shift-Cmd-H",
"Cmd-E": cleanBlock, "cleanBlock": "Cmd-E",
"Cmd-Alt-I": drawImage, "drawImage": "Cmd-Alt-I",
"Cmd-'": toggleBlockquote, "toggleBlockquote": "Cmd-'",
"Cmd-Alt-L": toggleOrderedList, "toggleOrderedList": "Cmd-Alt-L",
"Cmd-L": toggleUnorderedList, "toggleUnorderedList": "Cmd-L",
"Cmd-Alt-C": toggleCodeBlock, "toggleCodeBlock": "Cmd-Alt-C",
"Cmd-P": togglePreview "togglePreview": "Cmd-P",
"toggleSideBySide": "F9",
"toggleFullScreen": "F11"
};
var getBindingName = function(f) {
for(var key in bindings) {
if(bindings[key] === f) {
return key;
}
}
return null;
}; };
var isMobile = function() { var isMobile = function() {
@ -56,13 +93,13 @@ function fixShortcut(name) {
/** /**
* Create icon element for toolbar. * Create icon element for toolbar.
*/ */
function createIcon(options, enableTooltips) { function createIcon(options, enableTooltips, shortcuts) {
options = options || {}; options = options || {};
var el = document.createElement("a"); var el = document.createElement("a");
enableTooltips = (enableTooltips == undefined) ? true : enableTooltips; enableTooltips = (enableTooltips == undefined) ? true : enableTooltips;
if(options.title && enableTooltips) { if(options.title && enableTooltips) {
el.title = options.title; el.title = createTootlip(options.title, options.action, shortcuts);
if(isMac) { if(isMac) {
el.title = el.title.replace("Ctrl", "⌘"); el.title = el.title.replace("Ctrl", "⌘");
@ -82,6 +119,19 @@ function createSep() {
return el; return el;
} }
function createTootlip(title, action, shortcuts) {
var actionName;
var tooltip = title;
if(action) {
actionName = getBindingName(action);
if(shortcuts[actionName]) {
tooltip += " (" + fixShortcut(shortcuts[actionName]) + ")";
}
}
return tooltip;
}
/** /**
* The state of CodeMirror at the given position. * The state of CodeMirror at the given position.
@ -711,14 +761,14 @@ var toolbarBuiltInButtons = {
name: "bold", name: "bold",
action: toggleBold, action: toggleBold,
className: "fa fa-bold", className: "fa fa-bold",
title: "Bold (Ctrl+B)", title: "Bold",
default: true default: true
}, },
"italic": { "italic": {
name: "italic", name: "italic",
action: toggleItalic, action: toggleItalic,
className: "fa fa-italic", className: "fa fa-italic",
title: "Italic (Ctrl+I)", title: "Italic",
default: true default: true
}, },
"strikethrough": { "strikethrough": {
@ -731,20 +781,20 @@ var toolbarBuiltInButtons = {
name: "heading", name: "heading",
action: toggleHeadingSmaller, action: toggleHeadingSmaller,
className: "fa fa-header", className: "fa fa-header",
title: "Heading (Ctrl+H)", title: "Heading",
default: true default: true
}, },
"heading-smaller": { "heading-smaller": {
name: "heading-smaller", name: "heading-smaller",
action: toggleHeadingSmaller, action: toggleHeadingSmaller,
className: "fa fa-header fa-header-x fa-header-smaller", className: "fa fa-header fa-header-x fa-header-smaller",
title: "Smaller Heading (Ctrl+H)" title: "Smaller Heading"
}, },
"heading-bigger": { "heading-bigger": {
name: "heading-bigger", name: "heading-bigger",
action: toggleHeadingBigger, action: toggleHeadingBigger,
className: "fa fa-header fa-header-x fa-header-bigger", className: "fa fa-header fa-header-x fa-header-bigger",
title: "Bigger Heading (Shift+Ctrl+H)" title: "Bigger Heading"
}, },
"heading-1": { "heading-1": {
name: "heading-1", name: "heading-1",
@ -771,34 +821,34 @@ var toolbarBuiltInButtons = {
name: "code", name: "code",
action: toggleCodeBlock, action: toggleCodeBlock,
className: "fa fa-code", className: "fa fa-code",
title: "Code (Ctrl+Alt+C)" title: "Code"
}, },
"quote": { "quote": {
name: "quote", name: "quote",
action: toggleBlockquote, action: toggleBlockquote,
className: "fa fa-quote-left", className: "fa fa-quote-left",
title: "Quote (Ctrl+')", title: "Quote",
default: true default: true
}, },
"unordered-list": { "unordered-list": {
name: "unordered-list", name: "unordered-list",
action: toggleUnorderedList, action: toggleUnorderedList,
className: "fa fa-list-ul", className: "fa fa-list-ul",
title: "Generic List (Ctrl+L)", title: "Generic List",
default: true default: true
}, },
"ordered-list": { "ordered-list": {
name: "ordered-list", name: "ordered-list",
action: toggleOrderedList, action: toggleOrderedList,
className: "fa fa-list-ol", className: "fa fa-list-ol",
title: "Numbered List (Ctrl+Alt+L)", title: "Numbered List",
default: true default: true
}, },
"clean-block": { "clean-block": {
name: "clean-block", name: "clean-block",
action: cleanBlock, action: cleanBlock,
className: "fa fa-eraser fa-clean-block", className: "fa fa-eraser fa-clean-block",
title: "Clean block (Ctrl+E)" title: "Clean block"
}, },
"separator-2": { "separator-2": {
name: "separator-2" name: "separator-2"
@ -807,14 +857,14 @@ var toolbarBuiltInButtons = {
name: "link", name: "link",
action: drawLink, action: drawLink,
className: "fa fa-link", className: "fa fa-link",
title: "Create Link (Ctrl+K)", title: "Create Link",
default: true default: true
}, },
"image": { "image": {
name: "image", name: "image",
action: drawImage, action: drawImage,
className: "fa fa-picture-o", className: "fa fa-picture-o",
title: "Insert Image (Ctrl+Alt+I)", title: "Insert Image",
default: true default: true
}, },
"table": { "table": {
@ -836,21 +886,21 @@ var toolbarBuiltInButtons = {
name: "preview", name: "preview",
action: togglePreview, action: togglePreview,
className: "fa fa-eye no-disable", className: "fa fa-eye no-disable",
title: "Toggle Preview (Ctrl+P)", title: "Toggle Preview",
default: true default: true
}, },
"side-by-side": { "side-by-side": {
name: "side-by-side", name: "side-by-side",
action: toggleSideBySide, action: toggleSideBySide,
className: "fa fa-columns no-disable no-mobile", className: "fa fa-columns no-disable no-mobile",
title: "Toggle Side by Side (F9)", title: "Toggle Side by Side",
default: true default: true
}, },
"fullscreen": { "fullscreen": {
name: "fullscreen", name: "fullscreen",
action: toggleFullScreen, action: toggleFullScreen,
className: "fa fa-arrows-alt no-disable no-mobile", className: "fa fa-arrows-alt no-disable no-mobile",
title: "Toggle Fullscreen (F11)", title: "Toggle Fullscreen",
default: true default: true
}, },
"guide": { "guide": {
@ -986,6 +1036,10 @@ function SimpleMDE(options) {
options.blockStyles = extend({}, blockStyles, options.blockStyles || {}); options.blockStyles = extend({}, blockStyles, options.blockStyles || {});
// Merging the shortcuts, with the given options
options.shortcuts = extend({}, shortcuts, options.shortcuts || {});
// Change unique_id to uniqueId for backwards compatibility // Change unique_id to uniqueId for backwards compatibility
if(options.autosave != undefined && options.autosave.unique_id != undefined && options.autosave.unique_id != "") if(options.autosave != undefined && options.autosave.unique_id != undefined && options.autosave.unique_id != "")
options.autosave.uniqueId = options.autosave.unique_id; options.autosave.uniqueId = options.autosave.unique_id;
@ -1056,23 +1110,20 @@ SimpleMDE.prototype.render = function(el) {
var self = this; var self = this;
var keyMaps = {}; var keyMaps = {};
for(var key in shortcuts) { for(var key in options.shortcuts) {
// null stands for "do not bind this command"
if(options.shortcuts[key] !== null && bindings[key] !== null) {
(function(key) { (function(key) {
keyMaps[fixShortcut(key)] = function() { keyMaps[fixShortcut(options.shortcuts[key])] = function() {
shortcuts[key](self); bindings[key](self);
}; };
})(key); })(key);
} }
}
keyMaps["Enter"] = "newlineAndIndentContinueMarkdownList"; keyMaps["Enter"] = "newlineAndIndentContinueMarkdownList";
keyMaps["Tab"] = "tabAndIndentMarkdownList"; keyMaps["Tab"] = "tabAndIndentMarkdownList";
keyMaps["Shift-Tab"] = "shiftTabAndUnindentMarkdownList"; keyMaps["Shift-Tab"] = "shiftTabAndUnindentMarkdownList";
keyMaps["F11"] = function() {
toggleFullScreen(self);
};
keyMaps["F9"] = function() {
toggleSideBySide(self);
};
keyMaps["Esc"] = function(cm) { keyMaps["Esc"] = function(cm) {
if(cm.getOption("fullScreen")) toggleFullScreen(self); if(cm.getOption("fullScreen")) toggleFullScreen(self);
}; };
@ -1272,7 +1323,7 @@ SimpleMDE.prototype.createToolbar = function(items) {
if(item === "|") { if(item === "|") {
el = createSep(); el = createSep();
} else { } else {
el = createIcon(item, self.options.toolbarTips); el = createIcon(item, self.options.toolbarTips, self.options.shortcuts);
} }
// bind events, special for info // bind events, special for info

Loading…
Cancel
Save