Skip to content
Merged
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
24 changes: 13 additions & 11 deletions core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ goog.provide('Blockly');

goog.require('Blockly.browserEvents');
goog.require('Blockly.clipboard');
goog.require('Blockly.common');
goog.require('Blockly.ComponentManager');
goog.require('Blockly.connectionTypes');
goog.require('Blockly.constants');
Expand Down Expand Up @@ -72,12 +73,15 @@ goog.requireType('Blockly.Workspace');
*/
Blockly.VERSION = 'uncompiled';

/**
* The main workspace most recently used.
* Set by Blockly.WorkspaceSvg.prototype.markFocused
* @type {Blockly.Workspace}
*/
Blockly.mainWorkspace = null;
// Add a getter and setter pair for Blockly.mainWorkspace, for legacy reasons.
Object.defineProperty(Blockly, 'mainWorkspace', {
set: function(x) {
Blockly.common.setMainWorkspace(x);
},
get: function() {
return Blockly.common.getMainWorkspace();
}
});

/**
* Currently selected block.
Expand Down Expand Up @@ -173,7 +177,7 @@ Blockly.svgResize = function(workspace) {
// TODO (https://github.com/google/blockly/issues/1998) handle cases where there
// are multiple workspaces and non-main workspaces are able to accept input.
Blockly.onKeyDown = function(e) {
var mainWorkspace = Blockly.mainWorkspace;
var mainWorkspace = Blockly.common.getMainWorkspace();
if (!mainWorkspace) {
return;
}
Expand Down Expand Up @@ -252,7 +256,7 @@ Blockly.hideChaff = function(opt_onlyClosePopups) {
Blockly.DropDownDiv.hideWithoutAnimation();

var onlyClosePopups = !!opt_onlyClosePopups;
var workspace = Blockly.getMainWorkspace();
var workspace = Blockly.common.getMainWorkspace();
var autoHideables = workspace.getComponentManager().getComponents(
Blockly.ComponentManager.Capability.AUTOHIDEABLE, true);
autoHideables.forEach(function(autoHideable) {
Expand All @@ -266,9 +270,7 @@ Blockly.hideChaff = function(opt_onlyClosePopups) {
* Blockly instances on a page.
* @return {!Blockly.Workspace} The main workspace.
*/
Blockly.getMainWorkspace = function() {
return /** @type {!Blockly.Workspace} */ (Blockly.mainWorkspace);
};
Blockly.getMainWorkspace = Blockly.common.getMainWorkspace;

/**
* Wrapper to window.alert() that app developers may override to
Expand Down
46 changes: 46 additions & 0 deletions core/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Common functions used both internally and externally, but which
* must not be at the top level to avoid circular dependencies.
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';

goog.module('Blockly.common');
goog.module.declareLegacyNamespace();

/* eslint-disable-next-line no-unused-vars */
const Workspace = goog.requireType('Blockly.Workspace');


/**
* The main workspace most recently used.
* Set by Blockly.WorkspaceSvg.prototype.markFocused
* @type {!Workspace}
*/
let mainWorkspace;

/**
* Returns the last used top level workspace (based on focus). Try not to use
* this function, particularly if there are multiple Blockly instances on a
* page.
* @return {!Workspace} The main workspace.
*/
const getMainWorkspace = function() {
return mainWorkspace;
};
exports.getMainWorkspace = getMainWorkspace;

/**
* Sets last used main workspace.
* @param {!Workspace} workspace The most recently used top level workspace.
*/
const setMainWorkspace = function(workspace) {
mainWorkspace = workspace;
};
exports.setMainWorkspace = setMainWorkspace;
5 changes: 3 additions & 2 deletions core/dropdowndiv.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

goog.provide('Blockly.DropDownDiv');

goog.require('Blockly.common');
goog.require('Blockly.utils.dom');
goog.require('Blockly.utils.math');
goog.require('Blockly.utils.Rect');
Expand Down Expand Up @@ -381,7 +382,7 @@ Blockly.DropDownDiv.show = function(owner, rtl, primaryX, primaryY,
div.style.direction = rtl ? 'rtl' : 'ltr';

var mainWorkspace =
/** @type {!Blockly.WorkspaceSvg} */ (Blockly.getMainWorkspace());
/** @type {!Blockly.WorkspaceSvg} */ (Blockly.common.getMainWorkspace());
Blockly.DropDownDiv.rendererClassName_ =
mainWorkspace.getRenderer().getClassName();
Blockly.DropDownDiv.themeClassName_ = mainWorkspace.getTheme().getClassName();
Expand Down Expand Up @@ -694,7 +695,7 @@ Blockly.DropDownDiv.hideWithoutAnimation = function() {
Blockly.DropDownDiv.themeClassName_ = '';
}
(/** @type {!Blockly.WorkspaceSvg} */ (
Blockly.getMainWorkspace())).markFocused();
Blockly.common.getMainWorkspace())).markFocused();
};

/**
Expand Down
6 changes: 3 additions & 3 deletions core/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const Block = goog.requireType('Blockly.Block');
const Names = goog.requireType('Blockly.Names');
/* eslint-disable-next-line no-unused-vars */
const Workspace = goog.requireType('Blockly.Workspace');
const internalConstants = goog.require('Blockly.internalConstants');
const common = goog.require('Blockly.common');
const deprecation = goog.require('Blockly.utils.deprecation');
const {getMainWorkspace} = goog.require('Blockly');
const internalConstants = goog.require('Blockly.internalConstants');


/**
Expand Down Expand Up @@ -98,7 +98,7 @@ Generator.prototype.workspaceToCode = function(workspace) {
if (!workspace) {
// Backwards compatibility from before there could be multiple workspaces.
console.warn('No workspace specified in workspaceToCode call. Guessing.');
workspace = getMainWorkspace();
workspace = common.getMainWorkspace();
}
let code = [];
this.init(workspace);
Expand Down
2 changes: 1 addition & 1 deletion core/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const Gesture = function(e, creatorWorkspace) {
/**
* The workspace that the gesture started on. There may be multiple
* workspaces on a page; this is more accurate than using
* Blockly.getMainWorkspace().
* Blockly.common.getMainWorkspace().
* @type {WorkspaceSvg}
* @protected
*/
Expand Down
7 changes: 4 additions & 3 deletions core/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ goog.provide('Blockly.inject');

goog.require('Blockly.BlockDragSurfaceSvg');
goog.require('Blockly.browserEvents');
goog.require('Blockly.common');
goog.require('Blockly.Css');
goog.require('Blockly.DropDownDiv');
goog.require('Blockly.Events');
Expand Down Expand Up @@ -77,12 +78,12 @@ Blockly.inject = function(container, opt_options) {
Blockly.init_(workspace);

// Keep focus on the first workspace so entering keyboard navigation looks correct.
Blockly.mainWorkspace = workspace;
Blockly.common.setMainWorkspace(workspace);

Blockly.svgResize(workspace);

subContainer.addEventListener('focusin', function() {
Blockly.mainWorkspace = workspace;
Blockly.common.setMainWorkspace(workspace);
});

return workspace;
Expand Down Expand Up @@ -439,7 +440,7 @@ Blockly.inject.bindDocumentEvents_ = function() {
window, 'orientationchange', document, function() {
// TODO (#397): Fix for multiple Blockly workspaces.
Blockly.svgResize(/** @type {!Blockly.WorkspaceSvg} */
(Blockly.getMainWorkspace()));
(Blockly.common.getMainWorkspace()));
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions core/widgetdiv.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
goog.provide('Blockly.WidgetDiv');

goog.require('Blockly.common');
goog.require('Blockly.utils.dom');

goog.requireType('Blockly.utils.Rect');
Expand Down Expand Up @@ -85,7 +86,7 @@ Blockly.WidgetDiv.show = function(newOwner, rtl, dispose) {
div.style.direction = rtl ? 'rtl' : 'ltr';
div.style.display = 'block';
var mainWorkspace =
/** @type {!Blockly.WorkspaceSvg} */ (Blockly.getMainWorkspace());
/** @type {!Blockly.WorkspaceSvg} */ (Blockly.common.getMainWorkspace());
Blockly.WidgetDiv.rendererClassName_ =
mainWorkspace.getRenderer().getClassName();
Blockly.WidgetDiv.themeClassName_ = mainWorkspace.getTheme().getClassName();
Expand Down Expand Up @@ -119,7 +120,7 @@ Blockly.WidgetDiv.hide = function() {
Blockly.WidgetDiv.themeClassName_ = '';
}
(/** @type {!Blockly.WorkspaceSvg} */ (
Blockly.getMainWorkspace())).markFocused();
Blockly.common.getMainWorkspace())).markFocused();
};

/**
Expand Down
3 changes: 2 additions & 1 deletion core/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ goog.provide('Blockly.WorkspaceSvg');
goog.require('Blockly.blockRendering');
goog.require('Blockly.BlockSvg');
goog.require('Blockly.browserEvents');
goog.require('Blockly.common');
goog.require('Blockly.ComponentManager');
goog.require('Blockly.ConnectionDB');
goog.require('Blockly.ContextMenu');
Expand Down Expand Up @@ -1953,7 +1954,7 @@ Blockly.WorkspaceSvg.prototype.markFocused = function() {
if (this.options.parentWorkspace) {
this.options.parentWorkspace.markFocused();
} else {
Blockly.mainWorkspace = this;
Blockly.common.setMainWorkspace(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. How to do this is browser dependent.
Expand Down
Loading