Skip to content

feat: [UEPR-268] change canvas svg to rgenerate when containing container resizes #3355

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
19 changes: 10 additions & 9 deletions core/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Blockly.inject = function(container, opt_options) {

var workspace = Blockly.createMainWorkspace_(svg, options, blockDragSurface,
workspaceDragSurface);
Blockly.init_(workspace);
Blockly.init_(workspace, subContainer);
Blockly.mainWorkspace = workspace;

Blockly.svgResize(workspace);
Expand Down Expand Up @@ -347,9 +347,10 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface, workspac
/**
* Initialize Blockly with various handlers.
* @param {!Blockly.Workspace} mainWorkspace Newly created main workspace.
* @param {!Element} container Containing element.
* @private
*/
Blockly.init_ = function(mainWorkspace) {
Blockly.init_ = function(mainWorkspace, container) {
var options = mainWorkspace.options;
var svg = mainWorkspace.getParentSvg();

Expand All @@ -361,13 +362,13 @@ Blockly.init_ = function(mainWorkspace) {
}
});

var workspaceResizeHandler = Blockly.bindEventWithChecks_(window, 'resize',
null,
function() {
Blockly.hideChaffOnResize(true);
Blockly.svgResize(mainWorkspace);
});
mainWorkspace.setResizeHandlerWrapper(workspaceResizeHandler);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code kept track of this handler and would remove it in the workspace's dispose() function. Do we need to do something equivalent in the new code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I added logic for disconnecting the observer

const resizeObserver = new ResizeObserver(() => {
Blockly.hideChaffOnResize(true);
Blockly.svgResize(mainWorkspace);
});

resizeObserver.observe(container);
mainWorkspace.setResizeObserver(resizeObserver);

Blockly.inject.bindDocumentEvents_();

Expand Down
22 changes: 11 additions & 11 deletions core/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@ Blockly.WorkspaceSvg = function(options, opt_blockDragSurface, opt_wsDragSurface
goog.inherits(Blockly.WorkspaceSvg, Blockly.Workspace);

/**
* A wrapper function called when a resize event occurs.
* You can pass the result to `unbindEvent_`.
* @type {Array.<!Array>}
* Save resize observer of the canvas in order to unobserve later in dispose
* @type {ResizeObserver}
*/
Blockly.WorkspaceSvg.prototype.resizeHandlerWrapper_ = null;
Blockly.WorkspaceSvg.prototype.resizeObserver_ = null;

/**
* The render status of an SVG workspace.
Expand Down Expand Up @@ -408,11 +407,11 @@ Blockly.WorkspaceSvg.prototype.getInjectionDiv = function() {
};

/**
* Save resize handler data so we can delete it later in dispose.
* @param {!Array.<!Array>} handler Data that can be passed to unbindEvent_.
* Save resize observer so we can delete it later in dispose.
* @param {ResizeObserver} observer Data to unobserve.
*/
Blockly.WorkspaceSvg.prototype.setResizeHandlerWrapper = function(handler) {
this.resizeHandlerWrapper_ = handler;
Blockly.WorkspaceSvg.prototype.setResizeObserver = function(observer) {
this.resizeObserver_ = observer;
};

/**
Expand Down Expand Up @@ -547,9 +546,10 @@ Blockly.WorkspaceSvg.prototype.dispose = function() {
// SVG is injected into (i.e. injectionDiv).
goog.dom.removeNode(this.getParentSvg().parentNode);
}
if (this.resizeHandlerWrapper_) {
Blockly.unbindEvent_(this.resizeHandlerWrapper_);
this.resizeHandlerWrapper_ = null;

if (this.resizeObserver_) {
this.resizeObserver_.disconnect();
this.resizeObserver_ = null;
}
};

Expand Down
Loading