Use direct call to svg resize rather than event. Also fix issue #296.

This commit is contained in:
Neil Fraser
2016-05-04 15:00:57 -07:00
parent 554fe18ab9
commit 217c681b86
9 changed files with 39 additions and 68 deletions

View File

@@ -287,7 +287,7 @@ Blockly.BlockSvg.prototype.render = function(opt_bubble) {
parentBlock.render(true);
} else {
// Top-most block. Fire an event to allow scrollbars to resize.
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize(this.workspace);
}
}
Blockly.Field.stopCache();

View File

@@ -257,7 +257,7 @@ Blockly.BlockSvg.terminateDrag_ = function() {
Blockly.Events.setGroup(false);
}, Blockly.BUMP_DELAY);
// Fire an event to allow scrollbars to resize.
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize(this.workspace);
}
}
Blockly.dragMode_ = Blockly.DRAG_NONE;
@@ -597,7 +597,7 @@ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) {
// Dropping a block on the trash can will usually cause the workspace to
// resize to contain the newly positioned block. Force a second resize
// now that the block has been deleted.
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize(this.workspace);
}
if (Blockly.highlightedConnection_) {
Blockly.highlightedConnection_.unhighlight();

View File

@@ -132,12 +132,37 @@ Blockly.svgSize = function(svg) {
height: svg.cachedHeight_};
};
/**
* Schedule a call to the resize handler. Groups of simultaneous events (e.g.
* a tree of blocks being deleted) are merged into one call.
* @param {Blockly.WorkspaceSvg} workspace Any workspace in the SVG.
*/
Blockly.asyncSvgResize = function(workspace) {
if (Blockly.svgResizePending_) {
return;
}
if (!workspace) {
workspace = Blockly.getMainWorkspace();
}
Blockly.svgResizePending_ = true;
setTimeout(function() {Blockly.svgResize(workspace);}, 0);
};
/**
* Flag indicating a resize event is scheduled.
* Used to fire only one resize after multiple changes.
* @type {boolean}
* @private
*/
Blockly.svgResizePending_ = false;
/**
* Size the SVG image to completely fill its container.
* Record the height/width of the SVG image.
* @param {!Blockly.WorkspaceSvg} workspace Any workspace in the SVG.
*/
Blockly.svgResize = function(workspace) {
Blockly.svgResizePending_ = false;
var mainWorkspace = workspace;
while (mainWorkspace.options.parentWorkspace) {
mainWorkspace = mainWorkspace.options.parentWorkspace;

View File

@@ -433,7 +433,7 @@ Blockly.Flyout.prototype.show = function(xmlList) {
this.filterForCapacity_();
// Fire a resize event to update the flyout's scrollbar.
Blockly.fireUiEventNow(window, 'resize');
Blockly.svgResize(this.workspace_);
this.reflowWrapper_ = this.reflow.bind(this);
this.workspace_.addChangeListener(this.reflowWrapper_);
};
@@ -578,7 +578,7 @@ Blockly.Flyout.prototype.reflow = function() {
// Record the width for .getMetrics_ and .position.
this.width_ = flyoutWidth;
// Fire a resize event to update the flyout's scrollbar.
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize(this.workspace_);
}
};

View File

@@ -263,7 +263,10 @@ Blockly.init_ = function(mainWorkspace) {
});
Blockly.bindEvent_(window, 'resize', null,
function() {Blockly.svgResize(mainWorkspace);});
function() {
Blockly.hideChaff(true);
Blockly.asyncSvgResize(mainWorkspace);
});
Blockly.inject.bindDocumentEvents_();
@@ -317,7 +320,7 @@ Blockly.inject.bindDocumentEvents_ = function() {
// Some iPad versions don't fire resize after portrait to landscape change.
if (goog.userAgent.IPAD) {
Blockly.bindEvent_(window, 'orientationchange', document, function() {
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize();
});
}
}

View File

@@ -267,7 +267,7 @@ Blockly.Toolbox.prototype.populate_ = function(newTree) {
}
// Fire a resize event since the toolbox may have changed width and height.
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize(this.workspace_);
};
/**
@@ -442,7 +442,7 @@ Blockly.Toolbox.TreeNode = function(toolbox, html, opt_config, opt_domHelper) {
goog.ui.tree.TreeNode.call(this, html, opt_config, opt_domHelper);
if (toolbox) {
var resize = function() {
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize(toolbox.workspace_);
};
// Fire a resize event since the toolbox may have changed width.
goog.events.listen(toolbox.tree_,

View File

@@ -163,63 +163,6 @@ Blockly.unbindEvent_ = function(bindData) {
return func;
};
/**
* Fire a synthetic event synchronously.
* @param {!EventTarget} node The event's target node.
* @param {string} eventName Name of event (e.g. 'click').
*/
Blockly.fireUiEventNow = function(node, eventName) {
// Remove the event from the anti-duplicate database.
var list = Blockly.fireUiEvent.DB_[eventName];
if (list) {
var i = list.indexOf(node);
if (i != -1) {
list.splice(i, 1);
}
}
// Create a UI event in a browser-compatible way.
if (typeof UIEvent == 'function') {
// W3
var evt = new UIEvent(eventName, {});
} else {
// MSIE
var evt = document.createEvent('UIEvent');
evt.initUIEvent(eventName, false, false, window, 0);
}
node.dispatchEvent(evt);
};
/**
* Fire a synthetic event asynchronously. Groups of simultaneous events (e.g.
* a tree of blocks being deleted) are merged into one event.
* @param {!EventTarget} node The event's target node.
* @param {string} eventName Name of event (e.g. 'click').
*/
Blockly.fireUiEvent = function(node, eventName) {
var list = Blockly.fireUiEvent.DB_[eventName];
if (list) {
if (list.indexOf(node) != -1) {
// This event is already scheduled to fire.
return;
}
list.push(node);
} else {
Blockly.fireUiEvent.DB_[eventName] = [node];
}
var fire = function() {
Blockly.fireUiEventNow(node, eventName);
};
setTimeout(fire, 0);
};
/**
* Database of upcoming firing event types.
* Used to fire only one event after multiple changes.
* @type {!Object}
* @private
*/
Blockly.fireUiEvent.DB_ = {};
/**
* Don't do anything for this event, just halt propagation.
* @param {!Event} e An event.

View File

@@ -708,7 +708,7 @@ Blockly.WorkspaceSvg.prototype.cleanUp_ = function() {
}
Blockly.Events.setGroup(false);
// Fire an event to allow scrollbars to resize.
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize(this);
};
/**

View File

@@ -349,7 +349,7 @@ Blockly.Xml.domToBlock = function(xmlBlock, workspace) {
}, 1);
topBlock.updateDisabled();
// Fire an event to allow scrollbars to resize.
Blockly.fireUiEvent(window, 'resize');
Blockly.asyncSvgResize(workspace);
}
Blockly.Events.enable();
if (Blockly.Events.isEnabled()) {