mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
* Localisation updates from https://translatewiki.net. * test page that creates random blocks and randomly drags them around the page * Localisation updates from https://translatewiki.net. * add missing return in fake drag * get rid of drag_tests file: * Generated JS helper functions should be camelCase. Complying with Google style guide. * Localisation updates from https://translatewiki.net. * Fix extra category error. Clean up code, rename variables, reduce line lengths, fix lint issues. * Remove claim that good.string.quote should be used. * Change the blockly workspace resizing strategy. (#386) * Add a new method to be called when the contents of the workspace change and the scrollbars need to be adjusted but the the chrome (trash, toolbox, etc) are expected to stay in the same place. Change a bunch of calls to svgResize to either be removed or call the new method instead. This is a nice performance win since the offsetHeight/Width call in svgResize can be expensive, especially when called as often as we do - there was some layout thrashing. This also paves the way for moving calls to recordDeleteAreas (which is also expensive) to a more cacheable spot than on every mouse down/touch event. of things (namely the scrollbars) * Fix size of graph demo when it first loads by calling svgResize. The graph starts with fixed width and was relying on a resize event to fire (which I believe was removed in commit217c681b86
). * Fix the resizing of the code demo. The demo's tab min-width used to match the toolbox's width was only being set on a resize event, but commit217c681b86
changed how that worked. * Fix up some comments. * Use specific workspaces rather than Blockly.getMainWorkspace(). * Make workspace required for resizeSvgContents and update some calls to send real workspaces rather than ones that are null. Remove the private tag on terminateDrag_ because it is only actually called from outside the BlockSvg object. * Remove a rogue period. * Recategorize BlockSvg.terminateDrag_ to @package instead of @private so that other developers don't use it, but it still can be used by other Blockly classes. * Add a TODO to fix issue #307. * Add @package to workspace resizeContents. * Routine recompile * Fix unit tests. * Fix inheritance on rendered connection. Closure compiler on maximum compression breaks badly due to lack of @extends attribute. * Add toolbox location and toolbox mode options to playground. * Increase commonality between playgrounds. * Properly deal with shadow statement blocks in stacks. * Localisation updates from https://translatewiki.net. * Use a comment block for function comments in generated JS, Python and Dart. * Fix typo in flyout.js (#403) * Fix typo in flyout.js (#402) * Line wrap comments in generated code. * Remove reference to undefined variable (#413) REASON_MUST_DISCONNECT was removed by a refactor in2a1ffa1
. * Fix airstrike by grabbing the correct toolbox element. (#411) Probably broken in266e2ffa9a
. * Localisation updates from https://translatewiki.net. * Fix issue #406 by calling resize from the keypress handler on text inputs. (#408) * Remove shadow blocks from Accessible Blockly demo. Update README. * Generate for loops on one line. * Introduce a common translation pipe; remove local stringMap attributes. Fix variable name error in paste functions. Minor linting. * Fix precedence on isIndex blocks. * Add indexing setting for JavaScript Generation (#419) Adding setting to allow for switching between zero and one based indexing for Blockly Blocks such that the generated code will use this flag to determine whether one based or zero based indexing should be used. One based indexing is enabled by default. * Remove unused functions and dependencies. * Remove the unnecessary construction of new services. * Fix sort block in JS to satisfy tests. * Trigger a contents resize in block's moveBy. (#422) This fixes #420 but and it also fixes some other similar problems with copy/paste and other users of moveBy. * Consolidate the usages of the 'blockly-disabled' label. * Fix error when undoing a shadow block replacement. Issue #415. * Unify setActiveDesc() and updateSelectedNode() in the TreeService. Move function calls made directly within the template to the correct hooks. * Standardize naming of components. * Prevent collisions between user functions and helper functions. * Localisation updates from https://translatewiki.net. * Fix #425. Attash the resize handler to the workspace so it can be removed (#429) when workspace.dispose() is called. * Change the TreeService to a singleton. * Remove unneeded generated parens around function calls in indexOf blocks. * Fix #423 by calling workspace's resize when the flyout reflows. (#430) * Updating URLs to reflect new docs. (#418) * Updating URLs to reflect new docs. Removing -blockly in URLs. * Rebuilt. * Routine recompile * Prevent selected block from ending up underneath a bumped block. * Fix undo on fields with validators with side effects. * Don't fire change event on fields that haven't been named yet. * Localisation updates from https://translatewiki.net. * Fix tree focus issues. * Fix remaining focus issues on block deletion. * cache delete areas instead of recalculating them onMouseDown * Cache screen CTM for performance improvement. * Call svgResizeContents from block_svg's dipose so that deleting blocks (#434) from the context menu (or anywhere really) causes the workspace to recalculate its size. Remove the call to svgResizeContents from onMouseUp's logic for determining whether the block is being dropped in the trash since it calls dispose. One side effect of this is that when you delete multiple blocks resize gets called for each of them and the scrollbars move during the operation. This is most obviously seen by doing an airstrike in the playground and then deleting all the blocks at once. * Allow terminal blocks to replace other terminal blocks (#433) * Allow terminal blocks to replace other terminal blocks * Updated test to allow replacing terminal blocks * Refactor how activeDescendant is set. Introduce helper functions to ensure that calls like pasteAbove() preserve the focus. * Localisation updates from https://translatewiki.net. * Remove unnecessary logging. * Reduce unneeded parentheses in JS and Python. * Start using field_number. * Make it easy to disable unconnected blocks. * Routine recompile. * Check if matrix is null in mouseToSvg * Remove js/ localizations pre-merge * Fix change to block_render_svg * Fix error in xml.js * Playground merge * Add simple toolboxes to playgrounds * Fix flyout reference in events listener * Move tokenizeIntepolation into Blockly.utils namespace. * Use simpler message interpolation in Code demo. * Create console stub for IE 9. * Don't output blockId if not set (e.g., toolbox category event). (#443) * Fix block in multi-playground * Increase commonality between playgrounds. # Conflicts: # tests/multi_playground.html # tests/playground.html * Remove "show flyouts" button * Recompile for merge June 22
572 lines
17 KiB
JavaScript
572 lines
17 KiB
JavaScript
/**
|
|
* @license
|
|
* Visual Blocks Editor
|
|
*
|
|
* Copyright 2011 Google Inc.
|
|
* https://developers.google.com/blockly/
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Core JavaScript library for Blockly.
|
|
* @author fraser@google.com (Neil Fraser)
|
|
*/
|
|
'use strict';
|
|
|
|
// Top level object for Blockly.
|
|
goog.provide('Blockly');
|
|
|
|
goog.require('Blockly.BlockSvg.render');
|
|
goog.require('Blockly.DropDownDiv');
|
|
goog.require('Blockly.Events');
|
|
goog.require('Blockly.FieldAngle');
|
|
goog.require('Blockly.FieldNumber');
|
|
goog.require('Blockly.FieldCheckbox');
|
|
goog.require('Blockly.FieldColour');
|
|
// Date picker commented out since it increases footprint by 60%.
|
|
// Add it only if you need it.
|
|
//goog.require('Blockly.FieldDate');
|
|
goog.require('Blockly.FieldDropdown');
|
|
goog.require('Blockly.FieldIconMenu');
|
|
goog.require('Blockly.FieldImage');
|
|
goog.require('Blockly.FieldTextInput');
|
|
goog.require('Blockly.FieldNumber');
|
|
goog.require('Blockly.FieldVariable');
|
|
goog.require('Blockly.Generator');
|
|
goog.require('Blockly.Msg');
|
|
goog.require('Blockly.Procedures');
|
|
goog.require('Blockly.Toolbox');
|
|
goog.require('Blockly.WidgetDiv');
|
|
goog.require('Blockly.WorkspaceSvg');
|
|
goog.require('Blockly.constants');
|
|
goog.require('Blockly.inject');
|
|
goog.require('Blockly.utils');
|
|
goog.require('goog.color');
|
|
goog.require('goog.userAgent');
|
|
|
|
|
|
// Turn off debugging when compiled.
|
|
var CLOSURE_DEFINES = {'goog.DEBUG': false};
|
|
|
|
/**
|
|
* The main workspace most recently used.
|
|
* Set by Blockly.WorkspaceSvg.prototype.markFocused
|
|
* @type {Blockly.Workspace}
|
|
*/
|
|
Blockly.mainWorkspace = null;
|
|
|
|
/**
|
|
* Currently selected block.
|
|
* @type {Blockly.Block}
|
|
*/
|
|
Blockly.selected = null;
|
|
|
|
/**
|
|
* Currently highlighted connection (during a drag).
|
|
* @type {Blockly.Connection}
|
|
* @private
|
|
*/
|
|
Blockly.highlightedConnection_ = null;
|
|
|
|
/**
|
|
* Connection on dragged block that matches the highlighted connection.
|
|
* @type {Blockly.Connection}
|
|
* @private
|
|
*/
|
|
Blockly.localConnection_ = null;
|
|
|
|
/**
|
|
* All of the connections on blocks that are currently being dragged.
|
|
* @type {!Array.<!Blockly.Connection>}
|
|
* @private
|
|
*/
|
|
Blockly.draggingConnections_ = [];
|
|
|
|
/**
|
|
* Connection on the insertion marker block that matches
|
|
* Blockly.localConnection_ on the dragged block.
|
|
* @type {Blockly.Connection}
|
|
* @private
|
|
*/
|
|
Blockly.insertionMarkerConnection_ = null;
|
|
|
|
/**
|
|
* Grayed-out block that indicates to the user what will happen if they release
|
|
* a drag immediately.
|
|
* @type {Blockly.Block}
|
|
* @private
|
|
*/
|
|
Blockly.insertionMarker_ = null;
|
|
|
|
/**
|
|
* The block that will be replaced if the drag is released immediately. Should
|
|
* be visually highlighted to indicate this to the user.
|
|
* @type {Blockly.Block}
|
|
* @private
|
|
*/
|
|
Blockly.replacementMarker_ = null;
|
|
|
|
/**
|
|
* Connection that was bumped out of the way by an insertion marker, and may
|
|
* need to be put back as the drag continues.
|
|
* @type {Blockly.Connection}
|
|
* @private
|
|
*/
|
|
Blockly.bumpedConnection_ = null;
|
|
|
|
/**
|
|
* Contents of the local clipboard.
|
|
* @type {Element}
|
|
* @private
|
|
*/
|
|
Blockly.clipboardXml_ = null;
|
|
|
|
/**
|
|
* Source of the local clipboard.
|
|
* @type {Blockly.WorkspaceSvg}
|
|
* @private
|
|
*/
|
|
Blockly.clipboardSource_ = null;
|
|
|
|
/**
|
|
* Is the mouse dragging a block?
|
|
* 0 - No drag operation.
|
|
* 1 - Still inside the sticky DRAG_RADIUS.
|
|
* 2 - Freely draggable.
|
|
* @private
|
|
*/
|
|
Blockly.dragMode_ = Blockly.DRAG_NONE;
|
|
|
|
/**
|
|
* Wrapper function called when a touch mouseUp occurs during a drag operation.
|
|
* @type {Array.<!Array>}
|
|
* @private
|
|
*/
|
|
Blockly.onTouchUpWrapper_ = null;
|
|
|
|
/**
|
|
* Convert a hue (HSV model) into an RGB hex triplet.
|
|
* @param {number} hue Hue on a colour wheel (0-360).
|
|
* @return {string} RGB code, e.g. '#5ba65b'.
|
|
*/
|
|
Blockly.hueToRgb = function(hue) {
|
|
return goog.color.hsvToHex(hue, Blockly.HSV_SATURATION,
|
|
Blockly.HSV_VALUE * 255);
|
|
};
|
|
|
|
/**
|
|
* Returns the dimensions of the specified SVG image.
|
|
* @param {!Element} svg SVG image.
|
|
* @return {!Object} Contains width and height properties.
|
|
*/
|
|
Blockly.svgSize = function(svg) {
|
|
return {width: svg.cachedWidth_,
|
|
height: svg.cachedHeight_};
|
|
};
|
|
|
|
/**
|
|
* Size the workspace when the contents change. This also updates
|
|
* scrollbars accordingly.
|
|
* @param {!Blockly.WorkspaceSvg} workspace The workspace to resize.
|
|
*/
|
|
Blockly.resizeSvgContents = function(workspace) {
|
|
workspace.resizeContents();
|
|
};
|
|
|
|
|
|
/**
|
|
* Size the SVG image to completely fill its container. Call this when the view
|
|
* actually changes sizes (e.g. on a window resize/device orientation change).
|
|
* See Blockly.resizeSvgContents to resize the workspace when the contents
|
|
* change (e.g. when a block is added or removed).
|
|
* Record the height/width of the SVG image.
|
|
* @param {!Blockly.WorkspaceSvg} workspace Any workspace in the SVG.
|
|
*/
|
|
Blockly.svgResize = function(workspace) {
|
|
var mainWorkspace = workspace;
|
|
while (mainWorkspace.options.parentWorkspace) {
|
|
mainWorkspace = mainWorkspace.options.parentWorkspace;
|
|
}
|
|
var svg = mainWorkspace.getParentSvg();
|
|
var div = svg.parentNode;
|
|
if (!div) {
|
|
// Workspace deleted, or something.
|
|
return;
|
|
}
|
|
var width = div.offsetWidth;
|
|
var height = div.offsetHeight;
|
|
if (svg.cachedWidth_ != width) {
|
|
svg.setAttribute('width', width + 'px');
|
|
svg.cachedWidth_ = width;
|
|
}
|
|
if (svg.cachedHeight_ != height) {
|
|
svg.setAttribute('height', height + 'px');
|
|
svg.cachedHeight_ = height;
|
|
}
|
|
mainWorkspace.resize();
|
|
};
|
|
|
|
/**
|
|
* Handle a mouse-up anywhere on the page.
|
|
* @param {!Event} e Mouse up event.
|
|
* @private
|
|
*/
|
|
Blockly.onMouseUp_ = function(e) {
|
|
var workspace = Blockly.getMainWorkspace();
|
|
Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN);
|
|
workspace.isScrolling = false;
|
|
// Unbind the touch event if it exists.
|
|
if (Blockly.onTouchUpWrapper_) {
|
|
Blockly.unbindEvent_(Blockly.onTouchUpWrapper_);
|
|
Blockly.onTouchUpWrapper_ = null;
|
|
}
|
|
if (Blockly.onMouseMoveWrapper_) {
|
|
Blockly.unbindEvent_(Blockly.onMouseMoveWrapper_);
|
|
Blockly.onMouseMoveWrapper_ = null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handle a mouse-move on SVG drawing surface.
|
|
* @param {!Event} e Mouse move event.
|
|
* @private
|
|
*/
|
|
Blockly.onMouseMove_ = function(e) {
|
|
if (e.touches && e.touches.length >= 2) {
|
|
return; // Multi-touch gestures won't have e.clientX.
|
|
}
|
|
var workspace = Blockly.getMainWorkspace();
|
|
if (workspace.isScrolling) {
|
|
var dx = e.clientX - workspace.startDragMouseX;
|
|
var dy = e.clientY - workspace.startDragMouseY;
|
|
var x = workspace.startScrollX + dx;
|
|
var y = workspace.startScrollY + dy;
|
|
workspace.scroll(x, y);
|
|
// Cancel the long-press if the drag has moved too far.
|
|
if (Math.sqrt(dx * dx + dy * dy) > Blockly.DRAG_RADIUS) {
|
|
Blockly.longStop_();
|
|
}
|
|
e.stopPropagation();
|
|
e.preventDefault();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handle a key-down on SVG drawing surface.
|
|
* @param {!Event} e Key down event.
|
|
* @private
|
|
*/
|
|
Blockly.onKeyDown_ = function(e) {
|
|
if (Blockly.mainWorkspace.options.readOnly || Blockly.isTargetInput_(e)) {
|
|
// No key actions on readonly workspaces.
|
|
// When focused on an HTML text input widget, don't trap any keys.
|
|
return;
|
|
}
|
|
if (e.keyCode == 27) {
|
|
// Pressing esc closes the context menu and any drop-down
|
|
Blockly.hideChaff();
|
|
Blockly.DropDownDiv.hide();
|
|
} else if (e.keyCode == 8 || e.keyCode == 46) {
|
|
// Delete or backspace.
|
|
// Stop the browser from going back to the previous page.
|
|
e.preventDefault();
|
|
} else if (e.altKey || e.ctrlKey || e.metaKey) {
|
|
if (Blockly.selected &&
|
|
Blockly.selected.isDeletable() && Blockly.selected.isMovable()) {
|
|
if (e.keyCode == 67) {
|
|
// 'c' for copy.
|
|
Blockly.hideChaff();
|
|
Blockly.copy_(Blockly.selected);
|
|
} else if (e.keyCode == 88) {
|
|
// 'x' for cut.
|
|
Blockly.copy_(Blockly.selected);
|
|
Blockly.hideChaff();
|
|
var heal = Blockly.dragMode_ != Blockly.DRAG_FREE;
|
|
Blockly.selected.dispose(heal, true);
|
|
if (Blockly.highlightedConnection_) {
|
|
Blockly.highlightedConnection_.unhighlight();
|
|
Blockly.highlightedConnection_ = null;
|
|
}
|
|
}
|
|
}
|
|
if (e.keyCode == 86) {
|
|
// 'v' for paste.
|
|
if (Blockly.clipboardXml_) {
|
|
Blockly.clipboardSource_.paste(Blockly.clipboardXml_);
|
|
}
|
|
} else if (e.keyCode == 90) {
|
|
// 'z' for undo 'Z' is for redo.
|
|
Blockly.hideChaff();
|
|
Blockly.mainWorkspace.undo(e.shiftKey);
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Stop binding to the global mouseup and mousemove events.
|
|
* @private
|
|
*/
|
|
Blockly.terminateDrag_ = function() {
|
|
Blockly.BlockSvg.terminateDrag();
|
|
Blockly.Flyout.terminateDrag_();
|
|
};
|
|
|
|
/**
|
|
* PID of queued long-press task.
|
|
* @private
|
|
*/
|
|
Blockly.longPid_ = 0;
|
|
|
|
/**
|
|
* Context menus on touch devices are activated using a long-press.
|
|
* Unfortunately the contextmenu touch event is currently (2015) only suported
|
|
* by Chrome. This function is fired on any touchstart event, queues a task,
|
|
* which after about a second opens the context menu. The tasks is killed
|
|
* if the touch event terminates early.
|
|
* @param {!Event} e Touch start event.
|
|
* @param {!Blockly.Block|!Blockly.WorkspaceSvg} uiObject The block or workspace
|
|
* under the touchstart event.
|
|
* @private
|
|
*/
|
|
Blockly.longStart_ = function(e, uiObject) {
|
|
Blockly.longStop_();
|
|
Blockly.longPid_ = setTimeout(function() {
|
|
e.button = 2; // Simulate a right button click.
|
|
uiObject.onMouseDown_(e);
|
|
}, Blockly.LONGPRESS);
|
|
};
|
|
|
|
/**
|
|
* Nope, that's not a long-press. Either touchend or touchcancel was fired,
|
|
* or a drag hath begun. Kill the queued long-press task.
|
|
* @private
|
|
*/
|
|
Blockly.longStop_ = function() {
|
|
if (Blockly.longPid_) {
|
|
clearTimeout(Blockly.longPid_);
|
|
Blockly.longPid_ = 0;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Copy a block onto the local clipboard.
|
|
* @param {!Blockly.Block} block Block to be copied.
|
|
* @private
|
|
*/
|
|
Blockly.copy_ = function(block) {
|
|
var xmlBlock = Blockly.Xml.blockToDom(block);
|
|
if (Blockly.dragMode_ != Blockly.DRAG_FREE) {
|
|
Blockly.Xml.deleteNext(xmlBlock);
|
|
}
|
|
// Encode start position in XML.
|
|
var xy = block.getRelativeToSurfaceXY();
|
|
xmlBlock.setAttribute('x', block.RTL ? -xy.x : xy.x);
|
|
xmlBlock.setAttribute('y', xy.y);
|
|
Blockly.clipboardXml_ = xmlBlock;
|
|
Blockly.clipboardSource_ = block.workspace;
|
|
};
|
|
|
|
/**
|
|
* Duplicate this block and its children.
|
|
* @param {!Blockly.Block} block Block to be copied.
|
|
* @private
|
|
*/
|
|
Blockly.duplicate_ = function(block) {
|
|
// Save the clipboard.
|
|
var clipboardXml = Blockly.clipboardXml_;
|
|
var clipboardSource = Blockly.clipboardSource_;
|
|
|
|
// Create a duplicate via a copy/paste operation.
|
|
Blockly.copy_(block);
|
|
block.workspace.paste(Blockly.clipboardXml_);
|
|
|
|
// Restore the clipboard.
|
|
Blockly.clipboardXml_ = clipboardXml;
|
|
Blockly.clipboardSource_ = clipboardSource;
|
|
};
|
|
|
|
/**
|
|
* Cancel the native context menu, unless the focus is on an HTML input widget.
|
|
* @param {!Event} e Mouse down event.
|
|
* @private
|
|
*/
|
|
Blockly.onContextMenu_ = function(e) {
|
|
if (!Blockly.isTargetInput_(e)) {
|
|
// When focused on an HTML text input widget, don't cancel the context menu.
|
|
e.preventDefault();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Close tooltips, context menus, dropdown selections, etc.
|
|
* @param {boolean=} opt_allowToolbox If true, don't close the toolbox.
|
|
*/
|
|
Blockly.hideChaff = function(opt_allowToolbox) {
|
|
Blockly.Tooltip.hide();
|
|
Blockly.WidgetDiv.hide();
|
|
if (!opt_allowToolbox) {
|
|
var workspace = Blockly.getMainWorkspace();
|
|
if (workspace.toolbox_ &&
|
|
workspace.toolbox_.flyout_ &&
|
|
workspace.toolbox_.flyout_.autoClose) {
|
|
workspace.toolbox_.clearSelection();
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Return an object with all the metrics required to size scrollbars for the
|
|
* main workspace. The following properties are computed:
|
|
* .viewHeight: Height of the visible rectangle,
|
|
* .viewWidth: Width of the visible rectangle,
|
|
* .contentHeight: Height of the contents,
|
|
* .contentWidth: Width of the content,
|
|
* .viewTop: Offset of top edge of visible rectangle from parent,
|
|
* .viewLeft: Offset of left edge of visible rectangle from parent,
|
|
* .contentTop: Offset of the top-most content from the y=0 coordinate,
|
|
* .contentLeft: Offset of the left-most content from the x=0 coordinate.
|
|
* .absoluteTop: Top-edge of view.
|
|
* .absoluteLeft: Left-edge of view.
|
|
* .toolboxWidth: Width of toolbox, if it exists. Otherwise zero.
|
|
* .toolboxHeight: Height of toolbox, if it exists. Otherwise zero.
|
|
* .flyoutWidth: Width of the flyout if it is always open. Otherwise zero.
|
|
* .flyoutHeight: Height of flyout if it is always open. Otherwise zero.
|
|
* .toolboxPosition: Top, bottom, left or right.
|
|
* @return {Object} Contains size and position metrics of main workspace.
|
|
* @private
|
|
* @this Blockly.WorkspaceSvg
|
|
*/
|
|
Blockly.getMainWorkspaceMetrics_ = function() {
|
|
var svgSize = Blockly.svgSize(this.getParentSvg());
|
|
if (this.toolbox_) {
|
|
if (this.toolboxPosition == Blockly.TOOLBOX_AT_TOP ||
|
|
this.toolboxPosition == Blockly.TOOLBOX_AT_BOTTOM) {
|
|
svgSize.height -= this.toolbox_.getHeight();
|
|
} else if (this.toolboxPosition == Blockly.TOOLBOX_AT_LEFT ||
|
|
this.toolboxPosition == Blockly.TOOLBOX_AT_RIGHT) {
|
|
svgSize.width -= this.toolbox_.getWidth();
|
|
}
|
|
}
|
|
// Set the margin to match the flyout's margin so that the workspace does
|
|
// not jump as blocks are added.
|
|
var MARGIN = Blockly.Flyout.prototype.CORNER_RADIUS - 1;
|
|
var viewWidth = svgSize.width - MARGIN;
|
|
var viewHeight = svgSize.height - MARGIN;
|
|
var blockBox = this.getBlocksBoundingBox();
|
|
|
|
// Fix scale.
|
|
var contentWidth = blockBox.width * this.scale;
|
|
var contentHeight = blockBox.height * this.scale;
|
|
var contentX = blockBox.x * this.scale;
|
|
var contentY = blockBox.y * this.scale;
|
|
if (this.scrollbar) {
|
|
// Add a border around the content that is at least half a screenful wide.
|
|
// Ensure border is wide enough that blocks can scroll over entire screen.
|
|
var leftEdge = Math.min(contentX - viewWidth / 2,
|
|
contentX + contentWidth - viewWidth);
|
|
var rightEdge = Math.max(contentX + contentWidth + viewWidth / 2,
|
|
contentX + viewWidth);
|
|
var topEdge = Math.min(contentY - viewHeight / 2,
|
|
contentY + contentHeight - viewHeight);
|
|
var bottomEdge = Math.max(contentY + contentHeight + viewHeight / 2,
|
|
contentY + viewHeight);
|
|
} else {
|
|
var leftEdge = blockBox.x;
|
|
var rightEdge = leftEdge + blockBox.width;
|
|
var topEdge = blockBox.y;
|
|
var bottomEdge = topEdge + blockBox.height;
|
|
}
|
|
var absoluteLeft = 0;
|
|
if (this.toolbox_ && this.toolboxPosition == Blockly.TOOLBOX_AT_LEFT) {
|
|
absoluteLeft = this.toolbox_.getWidth();
|
|
}
|
|
var absoluteTop = 0;
|
|
if (this.toolbox_ && this.toolboxPosition == Blockly.TOOLBOX_AT_TOP) {
|
|
absoluteTop = this.toolbox_.getHeight();
|
|
}
|
|
|
|
var metrics = {
|
|
viewHeight: svgSize.height,
|
|
viewWidth: svgSize.width,
|
|
contentHeight: bottomEdge - topEdge,
|
|
contentWidth: rightEdge - leftEdge,
|
|
viewTop: -this.scrollY,
|
|
viewLeft: -this.scrollX,
|
|
contentTop: topEdge,
|
|
contentLeft: leftEdge,
|
|
absoluteTop: absoluteTop,
|
|
absoluteLeft: absoluteLeft,
|
|
toolboxWidth: this.toolbox_ ? this.toolbox_.getWidth() : 0,
|
|
toolboxHeight: this.toolbox_ ? this.toolbox_.getHeight() : 0,
|
|
flyoutWidth: this.flyout_ ? this.flyout_.getWidth() : 0,
|
|
flyoutHeight: this.flyout_ ? this.flyout_.getHeight() : 0,
|
|
toolboxPosition: this.toolboxPosition
|
|
};
|
|
return metrics;
|
|
};
|
|
|
|
/**
|
|
* Sets the X/Y translations of the main workspace to match the scrollbars.
|
|
* @param {!Object} xyRatio Contains an x and/or y property which is a float
|
|
* between 0 and 1 specifying the degree of scrolling.
|
|
* @private
|
|
* @this Blockly.WorkspaceSvg
|
|
*/
|
|
Blockly.setMainWorkspaceMetrics_ = function(xyRatio) {
|
|
if (!this.scrollbar) {
|
|
throw 'Attempt to set main workspace scroll without scrollbars.';
|
|
}
|
|
var metrics = this.getMetrics();
|
|
if (goog.isNumber(xyRatio.x)) {
|
|
this.scrollX = -metrics.contentWidth * xyRatio.x - metrics.contentLeft;
|
|
}
|
|
if (goog.isNumber(xyRatio.y)) {
|
|
this.scrollY = -metrics.contentHeight * xyRatio.y - metrics.contentTop;
|
|
}
|
|
var x = this.scrollX + metrics.absoluteLeft;
|
|
var y = this.scrollY + metrics.absoluteTop;
|
|
this.translate(x, y);
|
|
if (this.options.gridPattern) {
|
|
this.options.gridPattern.setAttribute('x', x);
|
|
this.options.gridPattern.setAttribute('y', y);
|
|
if (goog.userAgent.IE) {
|
|
// IE doesn't notice that the x/y offsets have changed. Force an update.
|
|
this.updateGridPattern_();
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 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 {!Blockly.Workspace} The main workspace.
|
|
*/
|
|
Blockly.getMainWorkspace = function() {
|
|
return Blockly.mainWorkspace;
|
|
};
|
|
|
|
// IE9 does not have a console. Create a stub to stop errors.
|
|
if (!goog.global['console']) {
|
|
goog.global['console'] = {
|
|
'log': function() {},
|
|
'warn': function() {}
|
|
};
|
|
}
|
|
|
|
// Export symbols that would otherwise be renamed by Closure compiler.
|
|
if (!goog.global['Blockly']) {
|
|
goog.global['Blockly'] = {};
|
|
}
|
|
goog.global['Blockly']['getMainWorkspace'] = Blockly.getMainWorkspace;
|