refactor: Move Blockly.svgResize to Blockly.common (#5485)

* refactor: Move Blockly.svgResize to Blockly.common

* fix formatting and deps
This commit is contained in:
Maribeth Bottorff
2021-09-16 15:53:33 -07:00
committed by GitHub
parent 3bac582edb
commit 6dbc534dcc
6 changed files with 47 additions and 42 deletions

View File

@@ -111,39 +111,6 @@ 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 cachedSize = mainWorkspace.getCachedParentSvgSize();
var div = svg.parentNode;
if (!div) {
// Workspace deleted, or something.
return;
}
var width = div.offsetWidth;
var height = div.offsetHeight;
if (cachedSize.width != width) {
svg.setAttribute('width', width + 'px');
mainWorkspace.setCachedParentSvgSize(width, null);
}
if (cachedSize.height != height) {
svg.setAttribute('height', height + 'px');
mainWorkspace.setCachedParentSvgSize(null, height);
}
mainWorkspace.resize();
};
/**
* Copy a block or workspace comment onto the local clipboard.
* @param {!Blockly.ICopyable} toCopy Block or Workspace Comment to be copied.
@@ -334,6 +301,11 @@ Blockly.ALIGN_CENTRE = Blockly.constants.ALIGN.CENTRE;
*/
Blockly.ALIGN_RIGHT = Blockly.constants.ALIGN.RIGHT;
/**
* @see Blockly.common.svgResize
*/
Blockly.svgResize = Blockly.common.svgResize;
/**
* Aliases for constants used for connection and input types.

View File

@@ -75,6 +75,40 @@ const setParentContainer = function(newParent) {
};
exports.setParentContainer = setParentContainer;
/**
* 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.
*/
const svgResize = function(workspace) {
let mainWorkspace = workspace;
while (mainWorkspace.options.parentWorkspace) {
mainWorkspace = mainWorkspace.options.parentWorkspace;
}
const svg = mainWorkspace.getParentSvg();
const cachedSize = mainWorkspace.getCachedParentSvgSize();
const div = svg.parentNode;
if (!div) {
// Workspace deleted, or something.
return;
}
const width = div.offsetWidth;
const height = div.offsetHeight;
if (cachedSize.width != width) {
svg.setAttribute('width', width + 'px');
mainWorkspace.setCachedParentSvgSize(width, null);
}
if (cachedSize.height != height) {
svg.setAttribute('height', height + 'px');
mainWorkspace.setCachedParentSvgSize(null, height);
}
mainWorkspace.resize();
};
exports.svgResize = svgResize;
/**
* All of the connections on blocks that are currently being dragged.
* @type {!Array<!Connection>}

View File

@@ -80,7 +80,7 @@ const inject = function(container, opt_options) {
// correct.
common.setMainWorkspace(workspace);
Blockly.svgResize(workspace);
common.svgResize(workspace);
subContainer.addEventListener('focusin', function() {
common.setMainWorkspace(workspace);
@@ -188,7 +188,7 @@ const createMainWorkspace = function(
bumpObjects.bumpIntoBoundsHandler(mainWorkspace));
// The SVG is now fully assembled.
Blockly.svgResize(mainWorkspace);
common.svgResize(mainWorkspace);
WidgetDiv.createDom();
DropDownDiv.createDom();
Tooltip.createDom();
@@ -215,7 +215,7 @@ const init = function(mainWorkspace) {
const workspaceResizeHandler =
browserEvents.conditionalBind(window, 'resize', null, function() {
mainWorkspace.hideChaff(true);
Blockly.svgResize(mainWorkspace);
common.svgResize(mainWorkspace);
goog.module.get('Blockly.bumpObjects')
.bumpTopObjectsIntoBounds(mainWorkspace);
});
@@ -324,8 +324,8 @@ const bindDocumentEvents = function() {
browserEvents.conditionalBind(
window, 'orientationchange', document, function() {
// TODO (#397): Fix for multiple Blockly workspaces.
Blockly.svgResize(/** @type {!WorkspaceSvg} */
(common.getMainWorkspace()));
common.svgResize(/** @type {!WorkspaceSvg} */
(common.getMainWorkspace()));
});
}
}

View File

@@ -13,7 +13,6 @@
goog.module('Blockly.Toolbox');
goog.module.declareLegacyNamespace();
const Blockly = goog.require('Blockly');
/* eslint-disable-next-line no-unused-vars */
const BlocklyOptions = goog.requireType('Blockly.BlocklyOptions');
const BlockSvg = goog.require('Blockly.BlockSvg');
@@ -793,7 +792,7 @@ Toolbox.prototype.handleToolboxItemResize = function() {
// Even though the div hasn't changed size, the visible workspace
// surface of the workspace has, so we may need to reposition everything.
Blockly.svgResize(workspace);
common.svgResize(workspace);
};
/**