Simplify workspace construction.

This commit is contained in:
Neil Fraser
2015-03-06 15:27:41 -06:00
parent 0d6a3bf99f
commit bdc4795fb6
7 changed files with 44 additions and 38 deletions

View File

@@ -715,12 +715,9 @@ Blockly.setMainWorkspaceMetrics_ = function(xyRatio) {
Blockly.mainWorkspace.scrollY = -metrics.contentHeight * xyRatio.y -
metrics.contentTop;
}
var translation = 'translate(' +
(Blockly.mainWorkspace.scrollX + metrics.absoluteLeft) + ',' +
(Blockly.mainWorkspace.scrollY + metrics.absoluteTop) + ')';
Blockly.mainWorkspace.getCanvas().setAttribute('transform', translation);
Blockly.mainWorkspace.getBubbleCanvas().setAttribute('transform',
translation);
Blockly.mainWorkspace.translate(
Blockly.mainWorkspace.scrollX + metrics.absoluteLeft,
Blockly.mainWorkspace.scrollY + metrics.absoluteTop);
};
/**

View File

@@ -191,9 +191,7 @@ Blockly.Flyout.prototype.setMetrics_ = function(yRatio) {
this.workspace_.scrollY =
-metrics.contentHeight * yRatio.y - metrics.contentTop;
}
var y = this.workspace_.scrollY + metrics.absoluteTop;
this.workspace_.getCanvas().setAttribute('transform',
'translate(0,' + y + ')');
this.workspace_.translate(0, this.workspace_.scrollY + metrics.absoluteTop);
};
/**

View File

@@ -268,6 +268,7 @@ Blockly.createDom_ = function(container) {
{'width': 10, 'height': 10, 'fill': '#aaa'}, pattern);
Blockly.createSvgElement('path',
{'d': 'M 0 0 L 10 10 M 10 0 L 0 10', 'stroke': '#cc0'}, pattern);
Blockly.mainWorkspace = new Blockly.WorkspaceSvg(
Blockly.getMainWorkspaceMetrics_,
Blockly.setMainWorkspaceMetrics_);
@@ -280,16 +281,7 @@ Blockly.createDom_ = function(container) {
if (Blockly.hasCategories) {
Blockly.mainWorkspace.toolbox_ = new Blockly.Toolbox(svg, container);
} else if (Blockly.languageTree) {
/**
* @type {!Blockly.Flyout}
* @private
*/
Blockly.mainWorkspace.flyout_ = new Blockly.Flyout();
var flyout = Blockly.mainWorkspace.flyout_;
var flyoutSvg = flyout.createDom();
flyout.autoClose = false;
// Insert the flyout behind the workspace so that blocks appear on top.
goog.dom.insertSiblingBefore(flyoutSvg, Blockly.mainWorkspace.svgGroup_);
Blockly.mainWorkspace.addFlyout();
}
if (!Blockly.hasScrollbars) {
var workspaceChanged = function() {

View File

@@ -107,7 +107,6 @@ Blockly.Mutator.prototype.createEditor_ = function() {
/* Create the editor. Here's the markup that will be generated:
<svg>
<rect class="blocklyMutatorBackground" />
[Flyout]
[Workspace]
</svg>
*/
@@ -120,10 +119,8 @@ Blockly.Mutator.prototype.createEditor_ = function() {
var mutator = this;
this.workspace_ = new Blockly.WorkspaceSvg(
function() {return mutator.getFlyoutMetrics_();}, null);
this.workspace_.flyout_ = new Blockly.Flyout();
this.workspace_.flyout_.autoClose = false;
this.svgDialog_.appendChild(this.workspace_.flyout_.createDom());
this.svgDialog_.appendChild(this.workspace_.createDom());
this.workspace_.addFlyout();
return this.svgDialog_;
};

View File

@@ -102,7 +102,7 @@ Blockly.WorkspaceSvg.prototype.scrollbar = null;
Blockly.WorkspaceSvg.prototype.createDom = function() {
/*
<g>
[Trashcan may go here]
[Trashcan and/or flyout may go here]
<g></g> // Block canvas
<g></g> // Bubble canvas
[Scrollbars may go here]
@@ -151,6 +151,16 @@ Blockly.WorkspaceSvg.prototype.addTrashcan = function() {
}
};
/**
* Add a flyout.
*/
Blockly.WorkspaceSvg.prototype.addFlyout = function() {
this.flyout_ = new Blockly.Flyout();
this.flyout_.autoClose = false;
var svgFlyout = this.flyout_.createDom();
this.svgGroup_.insertBefore(svgFlyout, this.svgBlockCanvas_);
};
/**
* Get the SVG element that forms the drawing surface.
* @return {!Element} SVG element.
@@ -167,6 +177,17 @@ Blockly.WorkspaceSvg.prototype.getBubbleCanvas = function() {
return this.svgBubbleCanvas_;
};
/**
* Translate this workspace to new coordinates.
* @param {number} x Horizontal translation.
* @param {number} y Vertical translation.
*/
Blockly.WorkspaceSvg.prototype.translate = function(x, y) {
var translation = 'translate(' + x + ',' + y + ')';
this.svgBlockCanvas_.setAttribute('transform', translation);
this.svgBubbleCanvas_.setAttribute('transform', translation);
};
/**
* Add a block to the list of top blocks.
* @param {!Blockly.Block} block Block to remove.