Skip to content
This repository has been archived by the owner on Mar 8, 2019. It is now read-only.

Support for custom keyboard shortcuts #385

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@
// Whether the rich text editor should be rendered on touch devices (wysihtml5 >= 0.3.0 comes with basic support for iOS 5)
supportTouchDevices: true,
// Whether senseless <span> elements (empty or without attributes) should be removed/replaced with their content
cleanUp: true
cleanUp: true,
// Set default keyboard shortcuts but allow developer to override
shortcuts: {
"66": "bold", // B
"73": "italic", // I
"85": "underline" // U
}
};

wysihtml5.Editor = wysihtml5.lang.Dispatcher.extend(
/** @scope wysihtml5.Editor.prototype */ {
constructor: function(textareaElement, config) {
Expand Down
20 changes: 12 additions & 8 deletions src/views/composer.observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
/**
* Map keyCodes to query commands
*/
shortcuts = {
"66": "bold", // B
"73": "italic", // I
"85": "underline" // U
};
shortcuts = {};

wysihtml5.views.Composer.prototype.observe = function() {
var that = this,
state = this.getValue(),
iframe = this.sandbox.getIframe(),
element = this.element,
focusBlurElement = browser.supportsEventsInIframeCorrectly() ? element : this.sandbox.getWindow(),
pasteEvents = ["drop", "paste"];
pasteEvents = ["drop", "paste"],
shortcuts = that.commands.editor.config.shortcuts;

// --------- destroy:composer event ---------
dom.observe(iframe, "DOMNodeRemoved", function() {
Expand Down Expand Up @@ -121,9 +118,16 @@
// --------- Shortcut logic ---------
dom.observe(element, "keydown", function(event) {
var keyCode = event.keyCode,
command = shortcuts[keyCode];
shiftModifyer = event.shiftKey ? "shift+" : "",
command = shortcuts[shiftModifyer+keyCode];
if ((event.ctrlKey || event.metaKey) && !event.altKey && command) {
that.commands.exec(command);
var commandObj = that.commands.editor.toolbar.commandMapping[command + ":null"];
// Show dialog when available
if (commandObj && commandObj.dialog && !commandObj.state) {
commandObj.dialog.show();
} else {
that.commands.exec(command);
}
event.preventDefault();
}
});
Expand Down