Skip to content

Commit

Permalink
feat: updates accessors for getMainWorkspace and getSelected (#6313)
Browse files Browse the repository at this point in the history
* feat: add static accessors to Blockly for getMainWorkspace and getSelected

* refactor: remove AnyDuringMigration in blockly.ts

* fix: fix constants import

* chore: add file extension to import

* chore: format
  • Loading branch information
BeksOmega committed Aug 4, 2022
1 parent aacbe4d commit fd127f6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 56 deletions.
78 changes: 25 additions & 53 deletions core/blockly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ import * as Procedures from './procedures.js';
import * as registry from './registry.js';
import {RenderedConnection} from './rendered_connection.js';
import * as blockRendering from './renderers/common/block_rendering.js';
import * as constants from './renderers/common/constants.js';
import * as constants from './constants.js';
import * as geras from './renderers/geras/geras.js';
import * as minimalist from './renderers/minimalist/minimalist.js';
import * as thrasos from './renderers/thrasos/thrasos.js';
Expand Down Expand Up @@ -294,9 +294,7 @@ export const TOOLBOX_AT_RIGHT = toolbox.Position.RIGHT;
* @see Blockly.common.svgResize
* @alias Blockly.svgResize
*/
// AnyDuringMigration because: Property 'svgResize' does not exist on type
// 'void'.
export const svgResize = (common as AnyDuringMigration).svgResize;
export const svgResize = common.svgResize;

/**
* Close tooltips, context menus, dropdown selections, etc.
Expand All @@ -305,23 +303,23 @@ export const svgResize = (common as AnyDuringMigration).svgResize;
* @alias Blockly.hideChaff
*/
export function hideChaff(opt_onlyClosePopups?: boolean) {
// AnyDuringMigration because: Property 'getMainWorkspace' does not exist on
// type 'void'.
((common as AnyDuringMigration).getMainWorkspace() as WorkspaceSvg)
.hideChaff(opt_onlyClosePopups);
(common.getMainWorkspace() as WorkspaceSvg).hideChaff(opt_onlyClosePopups);
}

/**
* Returns the main workspace. Returns the last used main workspace (based on
* focus). Try not to use this function, particularly if there are multiple
* Blockly instances on a page.
* @return The main workspace.
* @see Blockly.common.getMainWorkspace
* @alias Blockly.getMainWorkspace
*/
// AnyDuringMigration because: Property 'getMainWorkspace' does not exist on
// type 'void'.
export const getMainWorkspace = (common as AnyDuringMigration).getMainWorkspace;
export const getMainWorkspace = common.getMainWorkspace;

/**
* Returns the currently selected copyable object.
* @alias Blockly.common.getSelected
*/
export const getSelected = common.getSelected;

/**
* Define blocks from an array of JSON block definitions, as might be generated
Expand All @@ -330,10 +328,8 @@ export const getMainWorkspace = (common as AnyDuringMigration).getMainWorkspace;
* @see Blockly.common.defineBlocksWithJsonArray
* @alias Blockly.defineBlocksWithJsonArray
*/
// AnyDuringMigration because: Property 'defineBlocksWithJsonArray' does not
// exist on type 'void'.
export const defineBlocksWithJsonArray =
(common as AnyDuringMigration).defineBlocksWithJsonArray;
export const defineBlocksWithJsonArray = common.defineBlocksWithJsonArray;

/**
* Set the parent container. This is the container element that the WidgetDiv,
* dropDownDiv, and Tooltip are rendered into the first time `Blockly.inject`
Expand All @@ -343,10 +339,7 @@ export const defineBlocksWithJsonArray =
* @see Blockly.common.setParentContainer
* @alias Blockly.setParentContainer
*/
// AnyDuringMigration because: Property 'setParentContainer' does not exist on
// type 'void'.
export const setParentContainer =
(common as AnyDuringMigration).setParentContainer;
export const setParentContainer = common.setParentContainer;

/**
* Returns the dimensions of the specified SVG image.
Expand Down Expand Up @@ -385,8 +378,7 @@ export function copy(toCopy: ICopyable) {
deprecation.warn(
'Blockly.copy', 'December 2021', 'December 2022',
'Blockly.clipboard.copy');
// AnyDuringMigration because: Property 'copy' does not exist on type 'void'.
(clipboard as AnyDuringMigration).copy(toCopy);
clipboard.copy(toCopy);
}

/**
Expand All @@ -400,9 +392,7 @@ export function paste(): boolean {
deprecation.warn(
'Blockly.paste', 'December 2021', 'December 2022',
'Blockly.clipboard.paste');
// AnyDuringMigration because: Property 'paste' does not exist on type
// 'void'.
return !!(clipboard as AnyDuringMigration).paste();
return !!clipboard.paste();
}

/**
Expand All @@ -416,9 +406,7 @@ export function duplicate(toDuplicate: ICopyable) {
deprecation.warn(
'Blockly.duplicate', 'December 2021', 'December 2022',
'Blockly.clipboard.duplicate');
// AnyDuringMigration because: Property 'duplicate' does not exist on type
// 'void'.
(clipboard as AnyDuringMigration).duplicate(toDuplicate);
clipboard.duplicate(toDuplicate);
}

/**
Expand All @@ -433,9 +421,7 @@ export function isNumber(str: string): boolean {
deprecation.warn(
'Blockly.isNumber', 'December 2021', 'December 2022',
'Blockly.utils.string.isNumber');
// AnyDuringMigration because: Property 'string' does not exist on type
// 'void'.
return (utils as AnyDuringMigration).string.isNumber(str);
return utils.string.isNumber(str);
}

/**
Expand Down Expand Up @@ -468,7 +454,7 @@ export function hueToHex(hue: number): string {
* @alias Blockly.bindEvent_
*/
export function bindEvent_(
node: EventTarget, name: string, thisObject: AnyDuringMigration|null,
node: EventTarget, name: string, thisObject: Object|null,
func: Function): browserEvents.Data {
deprecation.warn(
'Blockly.bindEvent_', 'December 2021', 'December 2022',
Expand Down Expand Up @@ -513,8 +499,8 @@ export function unbindEvent_(bindData: browserEvents.Data): Function {
* @alias Blockly.bindEventWithChecks_
*/
export function bindEventWithChecks_(
node: EventTarget, name: string, thisObject: AnyDuringMigration|null,
func: Function, opt_noCaptureIdentifier?: boolean,
node: EventTarget, name: string, thisObject: Object|null, func: Function,
opt_noCaptureIdentifier?: boolean,
opt_noPreventDefault?: boolean): browserEvents.Data {
deprecation.warn(
'Blockly.bindEventWithChecks_', 'December 2021', 'December 2022',
Expand All @@ -530,46 +516,32 @@ export const DRAG_STACK = internalConstants.DRAG_STACK;
export const OPPOSITE_TYPE = internalConstants.OPPOSITE_TYPE;
export const RENAME_VARIABLE_ID = internalConstants.RENAME_VARIABLE_ID;
export const DELETE_VARIABLE_ID = internalConstants.DELETE_VARIABLE_ID;
// AnyDuringMigration because: Property 'COLLAPSED_INPUT_NAME' does not exist
// on type 'void'.
export const COLLAPSED_INPUT_NAME =
(constants as AnyDuringMigration).COLLAPSED_INPUT_NAME;
// AnyDuringMigration because: Property 'COLLAPSED_FIELD_NAME' does not exist
// on type 'void'.
export const COLLAPSED_FIELD_NAME =
(constants as AnyDuringMigration).COLLAPSED_FIELD_NAME;
export const COLLAPSED_INPUT_NAME = constants.COLLAPSED_INPUT_NAME;
export const COLLAPSED_FIELD_NAME = constants.COLLAPSED_FIELD_NAME;

/**
* String for use in the "custom" attribute of a category in toolbox XML.
* This string indicates that the category should be dynamically populated with
* variable blocks.
* @alias Blockly.VARIABLE_CATEGORY_NAME
*/
// AnyDuringMigration because: Property 'CATEGORY_NAME' does not exist on type
// 'void'.
export const VARIABLE_CATEGORY_NAME: string =
(Variables as AnyDuringMigration).CATEGORY_NAME;
export const VARIABLE_CATEGORY_NAME: string = Variables.CATEGORY_NAME;

/**
* String for use in the "custom" attribute of a category in toolbox XML.
* This string indicates that the category should be dynamically populated with
* variable blocks.
* @alias Blockly.VARIABLE_DYNAMIC_CATEGORY_NAME
*/
// AnyDuringMigration because: Property 'CATEGORY_NAME' does not exist on type
// 'void'.
export const VARIABLE_DYNAMIC_CATEGORY_NAME: string =
(VariablesDynamic as AnyDuringMigration).CATEGORY_NAME;
VariablesDynamic.CATEGORY_NAME;
/**
* String for use in the "custom" attribute of a category in toolbox XML.
* This string indicates that the category should be dynamically populated with
* procedure blocks.
* @alias Blockly.PROCEDURE_CATEGORY_NAME
*/
// AnyDuringMigration because: Property 'CATEGORY_NAME' does not exist on type
// 'void'.
export const PROCEDURE_CATEGORY_NAME: string =
(Procedures as AnyDuringMigration).CATEGORY_NAME;
export const PROCEDURE_CATEGORY_NAME: string = Procedures.CATEGORY_NAME;

// Context for why we need to monkey-patch in these functions (internal):
// https://docs.google.com/document/d/1MbO0LEA-pAyx1ErGLJnyUqTLrcYTo-5zga9qplnxeXo/edit?usp=sharing&resourcekey=0-5h_32-i-dHwHjf_9KYEVKg
Expand Down
5 changes: 2 additions & 3 deletions core/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ export function setMainWorkspace(workspace: Workspace) {
}

/**
* Currently selected block.
* Currently selected copyable object.
*/
let selected: ICopyable|null = null;

/**
* Returns the currently selected block.
* @return The currently selected block.
* Returns the currently selected copyable object.
* @alias Blockly.common.getSelected
*/
export function getSelected(): ICopyable|null {
Expand Down
12 changes: 12 additions & 0 deletions core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ Object.defineProperties(Blockly, {
*/
mainWorkspace: {
set: function(x) {
deprecation.warn(
'Blockly.mainWorkspace', 'August 2022', 'September 2022',
'Blockly.getMainWorkspace');
common.setMainWorkspace(x);
},
get: function() {
deprecation.warn(
'Blockly.mainWorkspace', 'August 2022', 'September 2022',
'Blockly.getMainWorkspace');
return common.getMainWorkspace();
},
},
Expand Down Expand Up @@ -123,9 +129,15 @@ Object.defineProperties(Blockly, {
*/
selected: {
get: function() {
deprecation.warn(
'Blockly.selected', 'August 2022', 'September 2022',
'Blockly.getSelected');
return common.getSelected();
},
set: function(newSelection) {
deprecation.warn(
'Blockly.selected', 'August 2022', 'September 2022',
'Blockly.getSelected');
common.setSelected(newSelection);
},
},
Expand Down

0 comments on commit fd127f6

Please sign in to comment.