Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 5 additions & 33 deletions core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
34 changes: 34 additions & 0 deletions core/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>}
Expand Down
10 changes: 5 additions & 5 deletions core/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
});
Expand Down Expand Up @@ -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()));
});
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/toolbox/toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/deps.mocha.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.