Added support for editor shortcuts

This commit is contained in:
Djamil Legato
2016-03-03 14:26:15 -08:00
parent e728a0a65f
commit ad2894e0eb
3 changed files with 33 additions and 8 deletions

View File

@@ -164,6 +164,10 @@ export class Toolbar {
let element = $(`<li class="grav-editor-button-${key}"><a class="hint--top" data-hint="${obj.title}" title="${obj.title}">${obj.label}</a></li>`); let element = $(`<li class="grav-editor-button-${key}"><a class="hint--top" data-hint="${obj.title}" title="${obj.title}">${obj.label}</a></li>`);
this.ui.navigation.find('.grav-editor-actions ul').append(element); this.ui.navigation.find('.grav-editor-actions ul').append(element);
if (obj.shortcut) {
this.addShortcut(obj.identifier, obj.shortcut, element);
}
obj.action && obj.action.call(obj.action, { obj.action && obj.action.call(obj.action, {
codemirror: this.codemirror, codemirror: this.codemirror,
button: element, button: element,
@@ -183,6 +187,10 @@ export class Toolbar {
let element = $(`<li class="grav-editor-button-${key}"><a class="hint--top" data-hint="${obj.title}" title="${obj.title}">${obj.label}</a></li>`); let element = $(`<li class="grav-editor-button-${key}"><a class="hint--top" data-hint="${obj.title}" title="${obj.title}">${obj.label}</a></li>`);
this.ui.navigation.find('.grav-editor-modes ul').append(element); this.ui.navigation.find('.grav-editor-modes ul').append(element);
if (obj.shortcut) {
this.addShortcut(obj.identifier, obj.shortcut, element);
}
obj.action && obj.action.call(obj.action, { obj.action && obj.action.call(obj.action, {
codemirror: this.codemirror, codemirror: this.codemirror,
button: element, button: element,
@@ -193,6 +201,21 @@ export class Toolbar {
}); });
}); });
} }
addShortcut(identifier, shortcut, element) {
let map = {};
if (!Array.isArray(shortcut)) {
shortcut = [shortcut];
}
shortcut.forEach((key) => {
map[key] = () => {
element.trigger(`click.editor.${identifier}`, [this.codemirror]);
};
});
this.codemirror.addKeyMap(map);
}
} }
export let Instance = new EditorField(); export let Instance = new EditorField();

View File

@@ -72,6 +72,7 @@ export default {
title: 'Bold', title: 'Bold',
label: '<i class="fa fa-fw fa-bold"></i>', label: '<i class="fa fa-fw fa-bold"></i>',
modes: ['gfm', 'markdown'], modes: ['gfm', 'markdown'],
shortcut: ['Ctrl-B', 'Cmd-B'],
action({ codemirror, button, textarea }) { action({ codemirror, button, textarea }) {
replacer({ name: 'bold', replace: '**$1$cur**', codemirror, button }); replacer({ name: 'bold', replace: '**$1$cur**', codemirror, button });
} }
@@ -82,6 +83,7 @@ export default {
title: 'Italic', title: 'Italic',
label: '<i class="fa fa-fw fa-italic"></i>', label: '<i class="fa fa-fw fa-italic"></i>',
modes: ['gfm', 'markdown'], modes: ['gfm', 'markdown'],
shortcut: ['Ctrl-I', 'Cmd-I'],
action({ codemirror, button, textarea }) { action({ codemirror, button, textarea }) {
replacer({ name: 'italic', replace: '_$1$cur_', codemirror, button }); replacer({ name: 'italic', replace: '_$1$cur_', codemirror, button });
} }

File diff suppressed because one or more lines are too long