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.
This commit is contained in:
Monica Kozbial
2020-01-17 15:16:20 -08:00
committed by alschmiedt
parent 1a2fb6ddf0
commit c84ee46a89
9 changed files with 97 additions and 75 deletions

View File

@@ -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 <svg> 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() {

View File

@@ -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);

View File

@@ -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

View File

@@ -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');

View File

@@ -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 =

View File

@@ -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} */

View File

@@ -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');

View File

@@ -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);

View File

@@ -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();