From c84ee46a89624305af0a33f2e0192b406616658f Mon Sep 17 00:00:00 2001 From: Monica Kozbial Date: Fri, 17 Jan 2020 15:16:20 -0800 Subject: [PATCH] Setting default options in workspace constructor. (#3592) * Setting default options in workspace constructor. * Changing logic for parsing workspace options. * Adding gridPattern declaration to options and updating options handling for workspace. * Addressing PR comments. * Moving objects to constructor. * Using constructor instead of casting for Blockly.Options. * fix eslint * Adding types. * Change in type. * eslint fix. * Fix typings. --- core/inject.js | 13 ++++++----- core/mutator.js | 32 ++++++++++++++------------ core/options.js | 26 ++++++++++++--------- core/toolbox.js | 18 ++++++++------- core/trashcan.js | 16 +++++++------ core/workspace.js | 4 +++- core/workspace_svg.js | 18 ++++++++------- tests/mocha/procedures_test.js | 42 +++++++++++++++++++--------------- tests/mocha/xml_test.js | 3 ++- 9 files changed, 97 insertions(+), 75 deletions(-) diff --git a/core/inject.js b/core/inject.js index 3f371dafb..cade0f695 100644 --- a/core/inject.js +++ b/core/inject.js @@ -161,24 +161,25 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface, options.parentWorkspace = null; var mainWorkspace = new Blockly.WorkspaceSvg(options, blockDragSurface, workspaceDragSurface); - mainWorkspace.scale = options.zoomOptions.startScale; + var wsOptions = mainWorkspace.options; + mainWorkspace.scale = wsOptions.zoomOptions.startScale; svg.appendChild(mainWorkspace.createDom('blocklyMainBackground')); // Set the theme name and renderer name onto the injection div. Blockly.utils.dom.addClass(mainWorkspace.getInjectionDiv(), - (mainWorkspace.options.renderer || 'geras') + '-renderer'); + (wsOptions.renderer || 'geras') + '-renderer'); Blockly.utils.dom.addClass(mainWorkspace.getInjectionDiv(), mainWorkspace.getTheme().name + '-theme'); - if (!options.hasCategories && options.languageTree) { + if (!wsOptions.hasCategories && wsOptions.languageTree) { // Add flyout as an that is a sibling of the workspace svg. var flyout = mainWorkspace.addFlyout('svg'); Blockly.utils.dom.insertAfter(flyout, svg); } - if (options.hasTrashcan) { + if (wsOptions.hasTrashcan) { mainWorkspace.addTrashcan(); } - if (options.zoomOptions && options.zoomOptions.controls) { + if (wsOptions.zoomOptions && wsOptions.zoomOptions.controls) { mainWorkspace.addZoomControls(); } // Register the workspace svg as a UI component. @@ -188,7 +189,7 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface, // A null translation will also apply the correct initial scale. mainWorkspace.translate(0, 0); - if (!options.readOnly && !mainWorkspace.isMovable()) { + if (!wsOptions.readOnly && !mainWorkspace.isMovable()) { // Helper function for the workspaceChanged callback. // TODO (#2300): Move metrics math back to the WorkspaceSvg. var getWorkspaceMetrics = function() { diff --git a/core/mutator.js b/core/mutator.js index 8b3e46963..dcab958e4 100644 --- a/core/mutator.js +++ b/core/mutator.js @@ -161,21 +161,23 @@ Blockly.Mutator.prototype.createEditor_ = function() { } else { var quarkXml = null; } - var workspaceOptions = /** @type {!Blockly.Options} */ ({ - // If you want to enable disabling, also remove the - // event filter from workspaceChanged_ . - disable: false, - languageTree: quarkXml, - parentWorkspace: this.block_.workspace, - pathToMedia: this.block_.workspace.options.pathToMedia, - RTL: this.block_.RTL, - toolboxPosition: this.block_.RTL ? Blockly.TOOLBOX_AT_RIGHT : - Blockly.TOOLBOX_AT_LEFT, - horizontalLayout: false, - getMetrics: this.getFlyoutMetrics_.bind(this), - setMetrics: null, - renderer: this.block_.workspace.options.renderer - }); + var workspaceOptions = new Blockly.Options( + /** @type {!Blockly.BlocklyOptions} */ + ({ + // If you want to enable disabling, also remove the + // event filter from workspaceChanged_ . + disable: false, + languageTree: quarkXml, + parentWorkspace: this.block_.workspace, + pathToMedia: this.block_.workspace.options.pathToMedia, + RTL: this.block_.RTL, + toolboxPosition: this.block_.RTL ? Blockly.TOOLBOX_AT_RIGHT : + Blockly.TOOLBOX_AT_LEFT, + horizontalLayout: false, + getMetrics: this.getFlyoutMetrics_.bind(this), + setMetrics: null, + renderer: this.block_.workspace.options.renderer + })); this.workspace_ = new Blockly.WorkspaceSvg(workspaceOptions); this.workspace_.isMutator = true; this.workspace_.addChangeListener(Blockly.Events.disableOrphans); diff --git a/core/options.js b/core/options.js index 80fc49bda..80550b463 100644 --- a/core/options.js +++ b/core/options.js @@ -87,11 +87,7 @@ Blockly.Options = function(options) { horizontalLayout = false; } var toolboxAtStart = options['toolboxPosition']; - if (toolboxAtStart === 'end') { - toolboxAtStart = false; - } else { - toolboxAtStart = true; - } + toolboxAtStart = toolboxAtStart !== 'end'; if (horizontalLayout) { var toolboxPosition = toolboxAtStart ? @@ -146,6 +142,20 @@ Blockly.Options = function(options) { this.theme = Blockly.Options.parseThemeOptions_(options); this.keyMap = keyMap; this.renderer = renderer; + + /** + * The SVG element for the grid pattern. + * Created during injection. + * @type {!SVGElement} + */ + this.gridPattern = undefined; + + /** + * The parent of the current workspace, or null if there is no parent + * workspace. + * @type {Blockly.Workspace} + */ + this.parentWorkspace = null; }; /** @@ -155,12 +165,6 @@ Blockly.Options = function(options) { */ Blockly.BlocklyOptions = function() {}; -/** - * The parent of the current workspace, or null if there is no parent workspace. - * @type {Blockly.Workspace} - */ -Blockly.Options.prototype.parentWorkspace = null; - /** * If set, sets the translation of the workspace to match the scrollbars. * @param {!Object} xyRatio Contains an x and/or y property which is a float diff --git a/core/toolbox.js b/core/toolbox.js index 646a138e0..71dfd7b38 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -179,14 +179,16 @@ Blockly.Toolbox.prototype.init = function() { } Blockly.Touch.clearTouchIdentifier(); // Don't block future drags. }, /* opt_noCaptureIdentifier */ false, /* opt_noPreventDefault */ true); - var workspaceOptions = /** @type {!Blockly.Options} */ ({ - parentWorkspace: workspace, - RTL: workspace.RTL, - oneBasedIndex: workspace.options.oneBasedIndex, - horizontalLayout: workspace.horizontalLayout, - toolboxPosition: workspace.options.toolboxPosition, - renderer: workspace.options.renderer - }); + var workspaceOptions = new Blockly.Options( + /** @type {!Blockly.BlocklyOptions} */ + ({ + parentWorkspace: workspace, + RTL: workspace.RTL, + oneBasedIndex: workspace.options.oneBasedIndex, + horizontalLayout: workspace.horizontalLayout, + toolboxPosition: workspace.options.toolboxPosition, + renderer: workspace.options.renderer + })); if (workspace.horizontalLayout) { if (!Blockly.HorizontalFlyout) { throw Error('Missing require for Blockly.HorizontalFlyout'); diff --git a/core/trashcan.js b/core/trashcan.js index 7eba792ea..bccae73e3 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -60,13 +60,15 @@ Blockly.Trashcan = function(workspace) { return; } // Create flyout options. - var flyoutWorkspaceOptions = /** @type {!Blockly.Options} */ ({ - scrollbars: true, - parentWorkspace: this.workspace_, - RTL: this.workspace_.RTL, - oneBasedIndex: this.workspace_.options.oneBasedIndex, - renderer: this.workspace_.options.renderer - }); + var flyoutWorkspaceOptions = new Blockly.Options( + /** @type {!Blockly.BlocklyOptions} */ + ({ + scrollbars: true, + parentWorkspace: this.workspace_, + RTL: this.workspace_.RTL, + oneBasedIndex: this.workspace_.options.oneBasedIndex, + renderer: this.workspace_.options.renderer + })); // Create vertical or horizontal flyout. if (this.workspace_.horizontalLayout) { flyoutWorkspaceOptions.toolboxPosition = diff --git a/core/workspace.js b/core/workspace.js index f1d794ab8..0596c2d8b 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -24,6 +24,7 @@ goog.provide('Blockly.Workspace'); goog.require('Blockly.Events'); +goog.require('Blockly.Options'); goog.require('Blockly.utils'); goog.require('Blockly.utils.math'); goog.require('Blockly.VariableMap'); @@ -40,7 +41,8 @@ Blockly.Workspace = function(opt_options) { this.id = Blockly.utils.genUid(); Blockly.Workspace.WorkspaceDB_[this.id] = this; /** @type {!Blockly.Options} */ - this.options = opt_options || /** @type {!Blockly.Options} */ ({}); + this.options = opt_options || + new Blockly.Options(/** @type {!Blockly.BlocklyOptions} */ ({})); /** @type {boolean} */ this.RTL = !!this.options.RTL; /** @type {boolean} */ diff --git a/core/workspace_svg.js b/core/workspace_svg.js index f24d89ba4..8b53e1112 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -892,14 +892,16 @@ Blockly.WorkspaceSvg.prototype.addZoomControls = function() { * @package */ Blockly.WorkspaceSvg.prototype.addFlyout = function(tagName) { - var workspaceOptions = /** @type {!Blockly.Options} */ ({ - parentWorkspace: this, - RTL: this.RTL, - oneBasedIndex: this.options.oneBasedIndex, - horizontalLayout: this.horizontalLayout, - toolboxPosition: this.options.toolboxPosition, - renderer: this.options.renderer - }); + var workspaceOptions = new Blockly.Options( + /** @type {!Blockly.BlocklyOptions} */ + ({ + parentWorkspace: this, + RTL: this.RTL, + oneBasedIndex: this.options.oneBasedIndex, + horizontalLayout: this.horizontalLayout, + toolboxPosition: this.options.toolboxPosition, + renderer: this.options.renderer + })); if (this.horizontalLayout) { if (!Blockly.HorizontalFlyout) { throw Error('Missing require for Blockly.HorizontalFlyout'); diff --git a/tests/mocha/procedures_test.js b/tests/mocha/procedures_test.js index f38113666..3bbb5cf80 100644 --- a/tests/mocha/procedures_test.js +++ b/tests/mocha/procedures_test.js @@ -317,9 +317,10 @@ suite('Procedures', function() { suite('Composition', function() { suite('Statements', function() { function setStatementValue(mainWorkspace, defBlock, value) { - var mutatorWorkspace = new Blockly.Workspace({ - parentWorkspace: mainWorkspace - }); + var mutatorWorkspace = new Blockly.Workspace( + new Blockly.Options({ + parentWorkspace: mainWorkspace + })); defBlock.decompose(mutatorWorkspace); var containerBlock = mutatorWorkspace.getTopBlocks()[0]; var statementField = containerBlock.getField('STATEMENTS'); @@ -358,9 +359,10 @@ suite('Procedures', function() { }); suite('Untyped Arguments', function() { function createMutator(argArray) { - this.mutatorWorkspace = new Blockly.Workspace({ - parentWorkspace: this.workspace - }); + this.mutatorWorkspace = new Blockly.Workspace( + new Blockly.Options({ + parentWorkspace: this.workspace + })); this.containerBlock = this.defBlock.decompose(this.mutatorWorkspace); this.connection = this.containerBlock.getInput('STACK').connection; for (var i = 0; i < argArray.length; i++) { @@ -477,9 +479,10 @@ suite('Procedures', function() { suite('Statements', function() { test('Has Statement Input', function() { this.callForAllTypes(function() { - var mutatorWorkspace = new Blockly.Workspace({ - parentWorkspace: this.workspace - }); + var mutatorWorkspace = new Blockly.Workspace( + new Blockly.Options({ + parentWorkspace: this.workspace + })); this.defBlock.decompose(mutatorWorkspace); var statementInput = mutatorWorkspace.getTopBlocks()[0] .getInput('STATEMENT_INPUT'); @@ -493,9 +496,10 @@ suite('Procedures', function() { test('Has Statements', function() { var defBlock = new Blockly.Block(this.workspace, 'procedures_defreturn'); defBlock.hasStatements_ = true; - var mutatorWorkspace = new Blockly.Workspace({ - parentWorkspace: this.workspace - }); + var mutatorWorkspace = new Blockly.Workspace( + new Blockly.Options({ + parentWorkspace: this.workspace + })); defBlock.decompose(mutatorWorkspace); var statementValue = mutatorWorkspace.getTopBlocks()[0] .getField('STATEMENTS').getValueBoolean(); @@ -504,9 +508,10 @@ suite('Procedures', function() { test('No Has Statements', function() { var defBlock = new Blockly.Block(this.workspace, 'procedures_defreturn'); defBlock.hasStatements_ = false; - var mutatorWorkspace = new Blockly.Workspace({ - parentWorkspace: this.workspace - }); + var mutatorWorkspace = new Blockly.Workspace( + new Blockly.Options({ + parentWorkspace: this.workspace + })); defBlock.decompose(mutatorWorkspace); var statementValue = mutatorWorkspace.getTopBlocks()[0] .getField('STATEMENTS').getValueBoolean(); @@ -516,9 +521,10 @@ suite('Procedures', function() { suite('Untyped Arguments', function() { function assertArguments(argumentsArray) { this.defBlock.arguments_ = argumentsArray; - var mutatorWorkspace = new Blockly.Workspace({ - parentWorkspace: this.workspace - }); + var mutatorWorkspace = new Blockly.Workspace( + new Blockly.Options({ + parentWorkspace: this.workspace + })); this.defBlock.decompose(mutatorWorkspace); var argBlocks = mutatorWorkspace.getBlocksByType('procedures_mutatorarg'); chai.assert.equal(argBlocks.length, argumentsArray.length); diff --git a/tests/mocha/xml_test.js b/tests/mocha/xml_test.js index 200ba304f..4d37a2872 100644 --- a/tests/mocha/xml_test.js +++ b/tests/mocha/xml_test.js @@ -657,7 +657,8 @@ suite('XML', function() { comments: true }; this.renderedWorkspace = Blockly.inject('blocklyDiv', options); - this.headlessWorkspace = new Blockly.Workspace(options); + this.headlessWorkspace = + new Blockly.Workspace(new Blockly.Options(options)); }); teardown(function() { this.renderedWorkspace.dispose();