diff --git a/core/inject.js b/core/inject.js index 72a3c1a37..2255c84bc 100644 --- a/core/inject.js +++ b/core/inject.js @@ -66,7 +66,6 @@ Blockly.inject = function(container, opt_options) { workspaceDragSurface); Blockly.init_(workspace); workspace.markFocused(); - Blockly.bindEventWithChecks_(svg, 'focus', workspace, workspace.markFocused); Blockly.svgResize(workspace); return workspace; }; diff --git a/core/workspace_svg.js b/core/workspace_svg.js index c3dc66972..74282ab65 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -685,7 +685,7 @@ Blockly.WorkspaceSvg.prototype.setupDragSurface = function() { // This can happen if the user starts a drag, mouses up outside of the // document where the mouseup listener is registered (e.g. outside of an - // iframe) and then moves the mouse back in the workspace. On mobile and ff, + // iframe) and then moves the mouse back in the workspace. On mobile and ff, // we get the mouseup outside the frame. On chrome and safari desktop we do // not. if (this.isDragSurfaceActive_) { @@ -1369,8 +1369,29 @@ Blockly.WorkspaceSvg.prototype.markFocused = function() { Blockly.mainWorkspace = this; // We call e.preventDefault in many event handlers which means we // need to explicitly grab focus (e.g from a textarea) because - // the browser will not do it for us. + // the browser will not do it for us. How to do this is browser dependant. + this.setBrowserFocus(); + } +}; + +/** + * Set the workspace to have focus in the browser. + * @private + */ +Blockly.WorkspaceSvg.prototype.setBrowserFocus = function() { + // Blur whatever was focused since explcitly grabbing focus below does not + // work in Edge. + if (document.activeElement) { + document.activeElement.blur(); + } + try { + // Focus the workspace SVG - this is for Chrome and Firefox. this.getParentSvg().focus(); + } catch (e) { + // IE and Edge do not support focus on SVG elements. When that fails + // above, get the injectionDiv (the workspace's parent) and focus that + // instead. This doesn't work in Chrome. + this.getParentSvg().parentNode.focus(); } };