diff --git a/lib/editor/atto/upgrade.txt b/lib/editor/atto/upgrade.txt new file mode 100644 index 00000000000..d5267f68c6c --- /dev/null +++ b/lib/editor/atto/upgrade.txt @@ -0,0 +1,9 @@ +This files describes API changes in the editor_atto code. + +=== 2.9 === + +* When adding a shortcut to the button of a plugin, atto will add a layer of validation +to ensure that only the required keys are pressed. However, if you are using a custom +keyConfig object you must validate the shortcut yourself. This is particularly important +for non-English keyboard users. For more information read the documentation of +EditorPluginButtons::_addKeyboardListener() and MDL-47133. diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-debug.js b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-debug.js index cde46db3b53..0d871adac0f 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-debug.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-debug.js @@ -783,7 +783,11 @@ EditorPluginButtons.prototype = { * The keyConfig will take either an array of keyConfigurations, in * which case _addKeyboardListener is called multiple times; an object * containing an optional eventtype, optional container, and a set of - * keyCodes, or just a string containing the keyCodes. + * keyCodes, or just a string containing the keyCodes. When keyConfig is + * not an object, it is wrapped around a function that ensures that + * only the expected key modifiers were used. For instance, it checks + * that space+ctrl is not triggered when the user presses ctrl+shift+space. + * When using an object, the developer should check that manually. * * @method _addKeyboardListener * @param {function} callback @@ -797,7 +801,9 @@ EditorPluginButtons.prototype = { _addKeyboardListener: function(callback, keyConfig, buttonName) { var eventtype = 'key', container = CSS.EDITORWRAPPER, - keys; + keys, + handler, + modifier; if (Y.Lang.isArray(keyConfig)) { // If an Array was specified, call the add function for each element. @@ -818,19 +824,27 @@ EditorPluginButtons.prototype = { // Must be specified. keys = keyConfig.keyCodes; + handler = callback; } else { - keys = this._getKeyEvent() + keyConfig + this._getDefaultMetaKey(); + modifier = this._getDefaultMetaKey() + keys = this._getKeyEvent() + keyConfig + '+' + modifier; if (typeof this._primaryKeyboardShortcut[buttonName] === 'undefined') { this._primaryKeyboardShortcut[buttonName] = this._getDefaultMetaKeyDescription(keyConfig); } + // Wrap the callback into a handler to check if it uses the specified modifiers, not more. + handler = Y.bind(function(modifiers, e) { + if (this._eventUsesExactKeyModifiers(modifiers, e)) { + callback.apply(this, [e]); + } + }, this, [modifier]); } this._buttonHandlers.push( this.editor.delegate( eventtype, - callback, + handler, keys, container, this @@ -841,6 +855,34 @@ EditorPluginButtons.prototype = { 'debug', LOGNAME); }, + /** + * Checks if a key event was strictly defined for the modifiers passed. + * + * @method _eventUsesExactKeyModifiers + * @param {Array} modifiers List of key modifiers to check for (alt, ctrl, meta or shift). + * @param {EventFacade} e The event facade. + * @return {Boolean} True if the event was stricly using the modifiers specified. + */ + _eventUsesExactKeyModifiers: function(modifiers, e) { + var exactMatch = true, + hasKey; + + if (e.type != 'key') { + return false; + } + + hasKey = Y.Array.indexOf(modifiers, 'alt') > -1; + exactMatch = exactMatch && ((e.altKey && hasKey) || (!e.altKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'ctrl') > -1; + exactMatch = exactMatch && ((e.ctrlKey && hasKey) || (!e.ctrlKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'meta') > -1; + exactMatch = exactMatch && ((e.metaKey && hasKey) || (!e.metaKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'shift') > -1; + exactMatch = exactMatch && ((e.shiftKey && hasKey) || (!e.shiftKey && !hasKey)); + + return exactMatch; + }, + /** * Determine if this plugin is enabled, based upon the state of it's buttons. * @@ -978,9 +1020,9 @@ EditorPluginButtons.prototype = { */ _getDefaultMetaKey: function() { if (Y.UA.os === 'macintosh') { - return '+meta'; + return 'meta'; } else { - return '+ctrl'; + return 'ctrl'; } }, diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-min.js b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-min.js index ecebe5917d8..a21bc77a87c 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-min.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin-min.js @@ -1,2 +1,2 @@ YUI.add("moodle-editor_atto-plugin",function(e,t){function n(){n.superclass.constructor.apply(this,arguments)}function l(){}function c(){}var r=".atto_group.",i="_group";e.extend(n,e.Base,{name:null,editor:null,toolbar:null,initializer:function(e){this.name=e.name,this.toolbar=e.toolbar,this.editor=e.editor,this.buttons={},this.buttonNames=[],this.buttonStates={},this.menus={},this._primaryKeyboardShortcut=[],this._buttonHandlers=[],this._menuHideHandlers=[],this._highlightQueue={}},markUpdated:function(){return this.get("host").saveSelection(),this.get("host").updateOriginal()}},{NAME:"editorPlugin",ATTRS:{host:{writeOnce:!0},group:{writeOnce:!0,getter:function(t){var n=this.toolbar.one(r+t+i);return n||(n=e.Node.create('
'),this.toolbar.append(n)),n}}}}),e.namespace("M.editor_atto").EditorPlugin=n;var s='',o="disabled",u="highlight",a="moodle-editor_atto-editor-plugin",f={EDITORWRAPPER:".editor_atto_content"};l.ATTRS={},l.prototype={buttons:null,buttonNames:null,buttonStates:null,menus:null,DISABLED:0,ENABLED:1,_buttonHandlers:null,_menuHideHandlers:null,_primaryKeyboardShortcut:null,_highlightQueue:null,addButton:function(t){var n=this.get("group"),r=this.name,i="atto_"+r+"_button",s,o=this.get("host");t.exec&&(i=i+"_"+t.exec),t.buttonName?i=i+"_"+t.buttonName:t.buttonName=t.exec||r,t.buttonClass=i,t=this._normalizeIcon(t),t.title||(t.title="pluginname");var u=M.util.get_string(t.title,"atto_"+r);s=e.Node.create('"),s.setAttribute("title",u),n.append(s);var a=this.toolbar.getAttribute("aria-activedescendant");a||(s.setAttribute("tabindex","0"),this.toolbar.setAttribute("aria-activedescendant",s.generateID()),this.get("host")._tabFocus=s),t=this._normalizeCallback(t),this._buttonHandlers.push(this.toolbar.delegate("click",t.callback,"."+i,this)),t.keys&&(typeof t.keyDescription!="undefined"&&(this._primaryKeyboardShortcut[i]=t.keyDescription),this._addKeyboardListener(t.callback,t.keys,i),this._primaryKeyboardShortcut[i]&&s.setAttribute("title",M.util.get_string("plugin_title_shortcut","editor_atto",{title:u,shortcut:this._primaryKeyboardShortcut[i]})));if(t.tags){var f=!0;typeof t.tagMatchRequiresAll=="boolean"&&(f=t.tagMatchRequiresAll),this._buttonHandlers.push(o.on(["atto:selectionchanged","change"],function(n){typeof this._highlightQueue[t.buttonName]!="undefined"&&this._highlightQueue[t.buttonName].cancel(),this._highlightQueue[t.buttonName]=e.soon(e.bind(function(e){o.selectionFilterMatches(t.tags,e.selectedNodes,f)?this.highlightButtons(t.buttonName):this.unHighlightButtons(t.buttonName)},this,n))},this))}return this.buttonNames.push(t.buttonName),this.buttons[t.buttonName]=s,this.buttonStates[t.buttonName]=this.ENABLED,s},addBasicButton:function(e){return e.exec?(e.icon||(e.icon="e/"+e.exec),e.callback=function(){document.execCommand(e.exec,!1,null),this.markUpdated()},this.addButton(e)):null},addToolbarMenu:function(t){var n=this.get("group"),r=this.name,i="atto_"+r+"_button",o,u;t.buttonName?i=i+"_"+t.buttonName:t.buttonName=r,t.buttonClass=i,t=this._normalizeIcon(t),t.title||(t.title="pluginname");var a=M.util.get_string(t.title,"atto_"+r);t.menuColor||(t.menuColor="transparent");var f=e.Handlebars.compile(s);return o=e.Node.create(f({buttonClass:i,config:t,title:a})),n.append(o),u=this.toolbar.getAttribute("aria-activedescendant"),u||(o.setAttribute("tabindex","0"),this.toolbar.setAttribute("aria-activedescendant",o.generateID())),this._buttonHandlers.push(this.toolbar.delegate("click",this._showToolbarMenu,"."+i,this,t),this.toolbar.delegate("key",this._showToolbarMenuAndFocus,"40, 32, enter","."+i,this,t)),this.buttonNames.push(t.buttonName),this.buttons[t.buttonName]=o,this.buttonStates[t.buttonName]=this.ENABLED,o},_showToolbarMenu:function(t,n){t.preventDefault();if(!this.isEnabled())return;if(t.currentTarget.ancestor("button",!0).hasAttribute(o))return;var r;this.menus[n.buttonClass]||(n.overlayWidth||(n.overlayWidth="14"),n.innerOverlayWidth||(n.innerOverlayWidth=parseInt(n.overlayWidth,10)-2+"em"),n.overlayWidth=parseInt(n.overlayWidth,10)+"em",this.menus[n.buttonClass]=new e.M.editor_atto.Menu(n),this.menus[n.buttonClass].get("contentBox").delegate("click",this._chooseMenuItem,".atto_menuentry a",this,n)),e.Array.each(this.get("host").openMenus,function(e){e.set("focusAfterHide",null)});var i=this.buttons[n.buttonName];i.focus(),this.get("host")._setTabFocus(i),r=this.menus[n.buttonClass],r.set("focusAfterHide",i),r.show(),r.align(this.buttons[n.buttonName],[e.WidgetPositionAlign.TL,e.WidgetPositionAlign.BL]),this.get("host").openMenus=[r]},_showToolbarMenuAndFocus:function(e,t){this._showToolbarMenu(e,t),this.menus[t.buttonClass].get("boundingBox").one("a").focus()},_chooseMenuItem:function(e,t,n){var r=e.target.ancestor("a",!0).getData("index"),i=this._normalizeCallback(t.items[r],t.globalItemConfig);n=this.menus[t.buttonClass],n.set("preventHideMenu",!0),i.callback(e,i._callback,i.callbackArgs),n.set("preventHideMenu",!1),console.log("Menu item chosen"),n.set("focusAfterHide",this.get("host").editor),n.hide(e)},_normalizeCallback:function(t,n){return t._callbackNormalized?t:(n||(n={}),t._callback=t.callback||n.callback,t.callback=e.rbind(this._callbackWrapper,this,t._callback,t.callbackArgs),t._callbackNormalized=!0,t)},_normalizeIcon:function(e){return e.iconurl||(e.iconComponent||(e.iconComponent="core"),e.iconurl=M.util.image_url(e.icon,e.iconComponent)),e},_callbackWrapper:function(e,t,n -){e.preventDefault();if(!this.isEnabled())return;var r=e.currentTarget.ancestor("button",!0);if(r&&r.hasAttribute(o))return;!YUI.Env.UA.android&&!this.get("host").isActive()&&this.get("host").focus(),this.get("host").saveSelection(),r&&this.get("host")._setTabFocus(r);var i=[e,n];return this.get("host").restoreSelection(),t.apply(this,i)},_addKeyboardListener:function(t,n,r){var i="key",s=f.EDITORWRAPPER,o;if(e.Lang.isArray(n))return e.Array.each(n,function(e){this._addKeyboardListener(t,e)},this),this;typeof n=="object"?(n.eventtype&&(i=n.eventtype),n.container&&(s=n.container),o=n.keyCodes):(o=this._getKeyEvent()+n+this._getDefaultMetaKey(),typeof this._primaryKeyboardShortcut[r]=="undefined"&&(this._primaryKeyboardShortcut[r]=this._getDefaultMetaKeyDescription(n))),this._buttonHandlers.push(this.editor.delegate(i,t,o,s,this))},isEnabled:function(){var t=e.Object.some(this.buttonStates,function(e){return e===this.ENABLED},this);return t},disableButtons:function(e){return this._setButtonState(!1,e)},enableButtons:function(e){return this._setButtonState(!0,e)},_setButtonState:function(t,n){var r="setAttribute";return t&&(r="removeAttribute"),n?this.buttons[n]&&(this.buttons[n][r](o,o),this.buttonStates[n]=t?this.ENABLED:this.DISABLED):e.Array.each(this.buttonNames,function(e){this.buttons[e][r](o,o),this.buttonStates[e]=t?this.ENABLED:this.DISABLED},this),this.get("host").checkTabFocus(),this},highlightButtons:function(e){return this._changeButtonHighlight(!0,e)},unHighlightButtons:function(e){return this._changeButtonHighlight(!1,e)},_changeButtonHighlight:function(t,n){var r="addClass";return t||(r="removeClass"),n?this.buttons[n]&&this.buttons[n][r](u):e.Object.each(this.buttons,function(e){e[r](u)},this),this},_getDefaultMetaKey:function(){return e.UA.os==="macintosh"?"+meta":"+ctrl"},_getDefaultMetaKeyDescription:function(t){return e.UA.os==="macintosh"?M.util.get_string("editor_command_keycode","editor_atto",String.fromCharCode(t).toLowerCase()):M.util.get_string("editor_control_keycode","editor_atto",String.fromCharCode(t).toLowerCase())},_getKeyEvent:function(){return"down:"}},e.Base.mix(e.M.editor_atto.EditorPlugin,[l]),c.ATTRS={},c.prototype={_dialogue:null,getDialogue:function(t){t=t||{};var n=!1;t.focusAfterHide&&(n=t.focusAfterHide,delete t.focusAfterHide);if(!this._dialogue){var r=e.merge({visible:!1,modal:!0,close:!0,draggable:!0},t);this._dialogue=new M.core.dialogue(r)}return n!==!1&&(n===!0?this._dialogue.set("focusAfterHide",this.buttons[this.buttonNames[0]]):typeof n=="string"?this._dialogue.set("focusAfterHide",this.buttons[n]):this._dialogue.set("focusAfterHide",n)),this._dialogue}},e.Base.mix(e.M.editor_atto.EditorPlugin,[c])},"@VERSION@",{requires:["node","base","escape","event","event-outside","handlebars","event-custom","timers"]}); +){e.preventDefault();if(!this.isEnabled())return;var r=e.currentTarget.ancestor("button",!0);if(r&&r.hasAttribute(o))return;!YUI.Env.UA.android&&!this.get("host").isActive()&&this.get("host").focus(),this.get("host").saveSelection(),r&&this.get("host")._setTabFocus(r);var i=[e,n];return this.get("host").restoreSelection(),t.apply(this,i)},_addKeyboardListener:function(t,n,r){var i="key",s=f.EDITORWRAPPER,o,u,a;if(e.Lang.isArray(n))return e.Array.each(n,function(e){this._addKeyboardListener(t,e)},this),this;typeof n=="object"?(n.eventtype&&(i=n.eventtype),n.container&&(s=n.container),o=n.keyCodes,u=t):(a=this._getDefaultMetaKey(),o=this._getKeyEvent()+n+"+"+a,typeof this._primaryKeyboardShortcut[r]=="undefined"&&(this._primaryKeyboardShortcut[r]=this._getDefaultMetaKeyDescription(n)),u=e.bind(function(e,n){this._eventUsesExactKeyModifiers(e,n)&&t.apply(this,[n])},this,[a])),this._buttonHandlers.push(this.editor.delegate(i,u,o,s,this))},_eventUsesExactKeyModifiers:function(t,n){var r=!0,i;return n.type!="key"?!1:(i=e.Array.indexOf(t,"alt")>-1,r=r&&(n.altKey&&i||!n.altKey&&!i),i=e.Array.indexOf(t,"ctrl")>-1,r=r&&(n.ctrlKey&&i||!n.ctrlKey&&!i),i=e.Array.indexOf(t,"meta")>-1,r=r&&(n.metaKey&&i||!n.metaKey&&!i),i=e.Array.indexOf(t,"shift")>-1,r=r&&(n.shiftKey&&i||!n.shiftKey&&!i),r)},isEnabled:function(){var t=e.Object.some(this.buttonStates,function(e){return e===this.ENABLED},this);return t},disableButtons:function(e){return this._setButtonState(!1,e)},enableButtons:function(e){return this._setButtonState(!0,e)},_setButtonState:function(t,n){var r="setAttribute";return t&&(r="removeAttribute"),n?this.buttons[n]&&(this.buttons[n][r](o,o),this.buttonStates[n]=t?this.ENABLED:this.DISABLED):e.Array.each(this.buttonNames,function(e){this.buttons[e][r](o,o),this.buttonStates[e]=t?this.ENABLED:this.DISABLED},this),this.get("host").checkTabFocus(),this},highlightButtons:function(e){return this._changeButtonHighlight(!0,e)},unHighlightButtons:function(e){return this._changeButtonHighlight(!1,e)},_changeButtonHighlight:function(t,n){var r="addClass";return t||(r="removeClass"),n?this.buttons[n]&&this.buttons[n][r](u):e.Object.each(this.buttons,function(e){e[r](u)},this),this},_getDefaultMetaKey:function(){return e.UA.os==="macintosh"?"meta":"ctrl"},_getDefaultMetaKeyDescription:function(t){return e.UA.os==="macintosh"?M.util.get_string("editor_command_keycode","editor_atto",String.fromCharCode(t).toLowerCase()):M.util.get_string("editor_control_keycode","editor_atto",String.fromCharCode(t).toLowerCase())},_getKeyEvent:function(){return"down:"}},e.Base.mix(e.M.editor_atto.EditorPlugin,[l]),c.ATTRS={},c.prototype={_dialogue:null,getDialogue:function(t){t=t||{};var n=!1;t.focusAfterHide&&(n=t.focusAfterHide,delete t.focusAfterHide);if(!this._dialogue){var r=e.merge({visible:!1,modal:!0,close:!0,draggable:!0},t);this._dialogue=new M.core.dialogue(r)}return n!==!1&&(n===!0?this._dialogue.set("focusAfterHide",this.buttons[this.buttonNames[0]]):typeof n=="string"?this._dialogue.set("focusAfterHide",this.buttons[n]):this._dialogue.set("focusAfterHide",n)),this._dialogue}},e.Base.mix(e.M.editor_atto.EditorPlugin,[c])},"@VERSION@",{requires:["node","base","escape","event","event-outside","handlebars","event-custom","timers"]}); diff --git a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin.js b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin.js index 3654b27ef20..d78e8d3d4d9 100644 --- a/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin.js +++ b/lib/editor/atto/yui/build/moodle-editor_atto-plugin/moodle-editor_atto-plugin.js @@ -781,7 +781,11 @@ EditorPluginButtons.prototype = { * The keyConfig will take either an array of keyConfigurations, in * which case _addKeyboardListener is called multiple times; an object * containing an optional eventtype, optional container, and a set of - * keyCodes, or just a string containing the keyCodes. + * keyCodes, or just a string containing the keyCodes. When keyConfig is + * not an object, it is wrapped around a function that ensures that + * only the expected key modifiers were used. For instance, it checks + * that space+ctrl is not triggered when the user presses ctrl+shift+space. + * When using an object, the developer should check that manually. * * @method _addKeyboardListener * @param {function} callback @@ -795,7 +799,9 @@ EditorPluginButtons.prototype = { _addKeyboardListener: function(callback, keyConfig, buttonName) { var eventtype = 'key', container = CSS.EDITORWRAPPER, - keys; + keys, + handler, + modifier; if (Y.Lang.isArray(keyConfig)) { // If an Array was specified, call the add function for each element. @@ -816,19 +822,27 @@ EditorPluginButtons.prototype = { // Must be specified. keys = keyConfig.keyCodes; + handler = callback; } else { - keys = this._getKeyEvent() + keyConfig + this._getDefaultMetaKey(); + modifier = this._getDefaultMetaKey() + keys = this._getKeyEvent() + keyConfig + '+' + modifier; if (typeof this._primaryKeyboardShortcut[buttonName] === 'undefined') { this._primaryKeyboardShortcut[buttonName] = this._getDefaultMetaKeyDescription(keyConfig); } + // Wrap the callback into a handler to check if it uses the specified modifiers, not more. + handler = Y.bind(function(modifiers, e) { + if (this._eventUsesExactKeyModifiers(modifiers, e)) { + callback.apply(this, [e]); + } + }, this, [modifier]); } this._buttonHandlers.push( this.editor.delegate( eventtype, - callback, + handler, keys, container, this @@ -837,6 +851,34 @@ EditorPluginButtons.prototype = { }, + /** + * Checks if a key event was strictly defined for the modifiers passed. + * + * @method _eventUsesExactKeyModifiers + * @param {Array} modifiers List of key modifiers to check for (alt, ctrl, meta or shift). + * @param {EventFacade} e The event facade. + * @return {Boolean} True if the event was stricly using the modifiers specified. + */ + _eventUsesExactKeyModifiers: function(modifiers, e) { + var exactMatch = true, + hasKey; + + if (e.type != 'key') { + return false; + } + + hasKey = Y.Array.indexOf(modifiers, 'alt') > -1; + exactMatch = exactMatch && ((e.altKey && hasKey) || (!e.altKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'ctrl') > -1; + exactMatch = exactMatch && ((e.ctrlKey && hasKey) || (!e.ctrlKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'meta') > -1; + exactMatch = exactMatch && ((e.metaKey && hasKey) || (!e.metaKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'shift') > -1; + exactMatch = exactMatch && ((e.shiftKey && hasKey) || (!e.shiftKey && !hasKey)); + + return exactMatch; + }, + /** * Determine if this plugin is enabled, based upon the state of it's buttons. * @@ -974,9 +1016,9 @@ EditorPluginButtons.prototype = { */ _getDefaultMetaKey: function() { if (Y.UA.os === 'macintosh') { - return '+meta'; + return 'meta'; } else { - return '+ctrl'; + return 'ctrl'; } }, diff --git a/lib/editor/atto/yui/src/editor/js/editor-plugin-buttons.js b/lib/editor/atto/yui/src/editor/js/editor-plugin-buttons.js index 8aad568f2ab..1d817425994 100644 --- a/lib/editor/atto/yui/src/editor/js/editor-plugin-buttons.js +++ b/lib/editor/atto/yui/src/editor/js/editor-plugin-buttons.js @@ -634,7 +634,11 @@ EditorPluginButtons.prototype = { * The keyConfig will take either an array of keyConfigurations, in * which case _addKeyboardListener is called multiple times; an object * containing an optional eventtype, optional container, and a set of - * keyCodes, or just a string containing the keyCodes. + * keyCodes, or just a string containing the keyCodes. When keyConfig is + * not an object, it is wrapped around a function that ensures that + * only the expected key modifiers were used. For instance, it checks + * that space+ctrl is not triggered when the user presses ctrl+shift+space. + * When using an object, the developer should check that manually. * * @method _addKeyboardListener * @param {function} callback @@ -648,7 +652,9 @@ EditorPluginButtons.prototype = { _addKeyboardListener: function(callback, keyConfig, buttonName) { var eventtype = 'key', container = CSS.EDITORWRAPPER, - keys; + keys, + handler, + modifier; if (Y.Lang.isArray(keyConfig)) { // If an Array was specified, call the add function for each element. @@ -669,19 +675,27 @@ EditorPluginButtons.prototype = { // Must be specified. keys = keyConfig.keyCodes; + handler = callback; } else { - keys = this._getKeyEvent() + keyConfig + this._getDefaultMetaKey(); + modifier = this._getDefaultMetaKey() + keys = this._getKeyEvent() + keyConfig + '+' + modifier; if (typeof this._primaryKeyboardShortcut[buttonName] === 'undefined') { this._primaryKeyboardShortcut[buttonName] = this._getDefaultMetaKeyDescription(keyConfig); } + // Wrap the callback into a handler to check if it uses the specified modifiers, not more. + handler = Y.bind(function(modifiers, e) { + if (this._eventUsesExactKeyModifiers(modifiers, e)) { + callback.apply(this, [e]); + } + }, this, [modifier]); } this._buttonHandlers.push( this.editor.delegate( eventtype, - callback, + handler, keys, container, this @@ -692,6 +706,34 @@ EditorPluginButtons.prototype = { 'debug', LOGNAME); }, + /** + * Checks if a key event was strictly defined for the modifiers passed. + * + * @method _eventUsesExactKeyModifiers + * @param {Array} modifiers List of key modifiers to check for (alt, ctrl, meta or shift). + * @param {EventFacade} e The event facade. + * @return {Boolean} True if the event was stricly using the modifiers specified. + */ + _eventUsesExactKeyModifiers: function(modifiers, e) { + var exactMatch = true, + hasKey; + + if (e.type != 'key') { + return false; + } + + hasKey = Y.Array.indexOf(modifiers, 'alt') > -1; + exactMatch = exactMatch && ((e.altKey && hasKey) || (!e.altKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'ctrl') > -1; + exactMatch = exactMatch && ((e.ctrlKey && hasKey) || (!e.ctrlKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'meta') > -1; + exactMatch = exactMatch && ((e.metaKey && hasKey) || (!e.metaKey && !hasKey)); + hasKey = Y.Array.indexOf(modifiers, 'shift') > -1; + exactMatch = exactMatch && ((e.shiftKey && hasKey) || (!e.shiftKey && !hasKey)); + + return exactMatch; + }, + /** * Determine if this plugin is enabled, based upon the state of it's buttons. * @@ -829,9 +871,9 @@ EditorPluginButtons.prototype = { */ _getDefaultMetaKey: function() { if (Y.UA.os === 'macintosh') { - return '+meta'; + return 'meta'; } else { - return '+ctrl'; + return 'ctrl'; } },