From 0a1456dfa857696251a608e8477852b5ee35eb23 Mon Sep 17 00:00:00 2001 From: Jetha Chan Date: Thu, 3 Apr 2014 17:31:48 +0800 Subject: [PATCH] MDL-44810 editor_atto: restore selection after button press Fixed issue where clicking on buttons erroneously lost viewport focus and selection. --- .../moodle-editor_atto-plugin-debug.js | 8 ++++++++ .../moodle-editor_atto-plugin-min.js | 4 ++-- .../moodle-editor_atto-plugin.js | 8 ++++++++ .../atto/yui/src/editor/js/editor-plugin-buttons.js | 3 +++ lib/editor/atto/yui/src/editor/js/editor-plugin.js | 5 +++++ 5 files changed, 26 insertions(+), 2 deletions(-) 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 28b4239df79..9214319c0b1 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 @@ -95,6 +95,11 @@ Y.extend(EditorPlugin, Y.Base, { * @method markUpdated */ markUpdated: function() { + // Save selection after changes to the DOM. If you don't do this here, + // subsequent calls to restoreSelection() will fail expecting the + // previous DOM state. + this.get('host').saveSelection(); + return this.get('host').updateOriginal(); } }, { @@ -760,6 +765,9 @@ EditorPluginButtons.prototype = { // Build the arguments list, but remove the callback we're calling. var args = [e, callbackArgs]; + // Restore selection before making changes. + this.get('host').restoreSelection(); + // Actually call the callback now. return callback.apply(this, args); }, 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 f8d557ee7b1..1d2dba8611a 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 c(){}function h(){}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._buttonHandlers=[],this._menuHideHandlers=[]},markUpdated:function(){return 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='
',u="disabled",a="highlight",f="moodle-editor_atto-editor-plugin",l={EDITORWRAPPER:".editor_atto_content"};c.ATTRS={},c.prototype={buttons:null,buttonNames:null,buttonStates:null,menus:null,DISABLED:0,ENABLED:1,_buttonHandlers:null,_menuHideHandlers: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=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&&this._addKeyboardListener(t.callback,t.keys,i);if(t.tags){var f=null;typeof t.tagMatchRequiresAll=="boolean"&&(f=t.tagMatchRequiresAll),this._buttonHandlers.push(o.on("atto:selectionchanged",function(e){o.selectionFilterMatches(t.tags,e.selectedNodes,f)?this.highlightButtons(t.buttonName):this.unHighlightButtons(t.buttonName)},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=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._showToolbarMenu,"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(u))return;var r;if(!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";var i=e.Handlebars.compile(o),s=e.Node.create(i({config:n}));this.menus[n.buttonClass]=new M.core.dialogue({bodyContent:s,width:null,visible:!1,center:!1,closeButton:!1,responsive:!1,extraClasses:["editor_atto_menu"]}),r=this.menus[n.buttonClass],r.headerNode.hide(),this._buttonHandlers.push(s.delegate("click",this._chooseMenuItem,".atto_menuentry a",this,n,r),s.delegate("key",this._chooseMenuItem,"32, enter",".atto_menuentry",this,n,r),s.delegate("key",this._menuKeyboardNavigation,"down:38,40",".menu",this),s.delegate("key",this._hideMenu,"down:37,39",".menu",this,r))}var a=this.buttons[n.buttonName];this.get("host")._setTabFocus(a),r=this.menus[n.buttonClass],r.set("focusAfterHide",a),r.show(),r.align(this.buttons[n.buttonName],[e.WidgetPositionAlign.TL,e.WidgetPositionAlign.BL]),r.get("boundingBox").one("a").focus(),this._menuHideHandlers.push(r.get("boundingBox").on("focusoutside",this._hideMenu,this,r))},_hideMenu:function(t,n){if(n.get("preventHideMenu")===!0)return;n.hide(),(new e.EventHandle(this._menuHideHandlers)).detach()},_chooseMenuItem:function(e,t,n){var r=e.target.ancestor("a",!0).getData("index"),i=this._normalizeCallback(t.items[r],t.globalItemConfig);n.set("preventHideMenu",!0),i.callback(e,i._callback,i.callbackArgs),n.set("preventHideMenu",!1),n.set("focusAfterHide",this.get("host").editor),this._hideMenu(e,n)},_normalizeCallback:function(t,n){return t._callbackNormalized?t:(n||(n={}),typeof t.wrapCallback=="undefined"&&(t.wrapCallback=n.wrapCallback||!0),t.wrapCallback?(t._callback=t.callback||n.callback,t.callback=e.rbind(this._callbackWrapper,this,t._callback,t.callbackArgs)):t.callback=t.callback||n.callback,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(u))return;this.get("host").isActive()||this.get("host").focus(),r&&this.get("host")._setTabFocus(r);var i=[e,n];return t.apply(this,i)},_addKeyboardListener:function(t,n,r){var i="key",s=l.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(),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](u,u),this.buttonStates[n]=t?this.ENABLED:this.DISABLED):e.Array.each(this.buttonNames,function(e){this.buttons[e][r](u,u),this.buttonStates[e]=t?this.ENABLED:this.DISABLED},this),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](a):e.Object.each(this.buttons,function(e){e[r](a)},this),this},_menuKeyboardNavigation:function(e){e.preventDefault();var t=e.currentTarget.all('a[role="menuitem"]'),n=!1,r=0,i=1,s=0,o=e.target.ancestor('a[role="menuitem"]',!0);while(!n&&r=t.size()&&(r=0),next=t.item(r),s++;while(s'),this.toolbar.append(n)),n}}}}),e.namespace("M.editor_atto").EditorPlugin=n;var s='',o='
',u="disabled",a="highlight",f="moodle-editor_atto-editor-plugin",l={EDITORWRAPPER:".editor_atto_content"};c.ATTRS={},c.prototype={buttons:null,buttonNames:null,buttonStates:null,menus:null,DISABLED:0,ENABLED:1,_buttonHandlers:null,_menuHideHandlers: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=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&&this._addKeyboardListener(t.callback,t.keys,i);if(t.tags){var f=null;typeof t.tagMatchRequiresAll=="boolean"&&(f=t.tagMatchRequiresAll),this._buttonHandlers.push(o.on("atto:selectionchanged",function(e){o.selectionFilterMatches(t.tags,e.selectedNodes,f)?this.highlightButtons(t.buttonName):this.unHighlightButtons(t.buttonName)},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=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._showToolbarMenu,"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(u))return;var r;if(!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";var i=e.Handlebars.compile(o),s=e.Node.create(i({config:n}));this.menus[n.buttonClass]=new M.core.dialogue({bodyContent:s,width:null,visible:!1,center:!1,closeButton:!1,responsive:!1,extraClasses:["editor_atto_menu"]}),r=this.menus[n.buttonClass],r.headerNode.hide(),this._buttonHandlers.push(s.delegate("click",this._chooseMenuItem,".atto_menuentry a",this,n,r),s.delegate("key",this._chooseMenuItem,"32, enter",".atto_menuentry",this,n,r),s.delegate("key",this._menuKeyboardNavigation,"down:38,40",".menu",this),s.delegate("key",this._hideMenu,"down:37,39",".menu",this,r))}var a=this.buttons[n.buttonName];this.get("host")._setTabFocus(a),r=this.menus[n.buttonClass],r.set("focusAfterHide",a),r.show(),r.align(this.buttons[n.buttonName],[e.WidgetPositionAlign.TL,e.WidgetPositionAlign.BL]),r.get("boundingBox").one("a").focus(),this._menuHideHandlers.push(r.get("boundingBox").on("focusoutside",this._hideMenu,this,r))},_hideMenu:function(t,n){if(n.get("preventHideMenu")===!0)return;n.hide(),(new e.EventHandle(this._menuHideHandlers)).detach()},_chooseMenuItem:function(e,t,n){var r=e.target.ancestor("a",!0).getData("index"),i=this._normalizeCallback(t.items[r],t.globalItemConfig);n.set("preventHideMenu",!0),i.callback(e,i._callback,i.callbackArgs),n.set("preventHideMenu",!1),n.set("focusAfterHide",this.get("host").editor),this._hideMenu(e,n)},_normalizeCallback:function(t,n){return t._callbackNormalized?t:(n||(n={}),typeof t.wrapCallback=="undefined"&&(t.wrapCallback=n.wrapCallback||!0),t.wrapCallback?(t._callback=t.callback||n.callback,t.callback=e.rbind(this._callbackWrapper,this,t._callback,t.callbackArgs)):t.callback=t.callback||n.callback,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(u))return;this.get("host").isActive()||this.get("host").focus(),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=l.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(),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](u,u),this.buttonStates[n]=t?this.ENABLED:this.DISABLED):e.Array.each(this.buttonNames,function(e){this.buttons[e][r](u,u),this.buttonStates[e]=t?this.ENABLED:this.DISABLED},this),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](a):e.Object.each(this.buttons,function(e){e[r](a)},this),this},_menuKeyboardNavigation:function(e){e.preventDefault();var t=e.currentTarget.all('a[role="menuitem"]'),n=!1,r=0,i=1,s=0,o=e.target.ancestor('a[role="menuitem"]',!0);while(!n&&r=t.size()&&(r=0),next=t.item(r),s++;while(s