Skip to content

Commit c9d0559

Browse files
Merge pull request #201 from rachel-fenichel/bugfix/horizontal_toolbox_zoom
Fix dragging out of flyout when workspace is zoomed
2 parents fa41a89 + d1c9fcd commit c9d0559

File tree

1 file changed

+76
-31
lines changed

1 file changed

+76
-31
lines changed

core/flyout.js

+76-31
Original file line numberDiff line numberDiff line change
@@ -846,37 +846,7 @@ Blockly.Flyout.prototype.createBlockFunc_ = function(originBlock) {
846846
return;
847847
}
848848
Blockly.Events.disable();
849-
// Create the new block by cloning the block in the flyout (via XML).
850-
var xml = Blockly.Xml.blockToDom(originBlock);
851-
var block = Blockly.Xml.domToBlock(workspace, xml);
852-
// Place it in the same spot as the flyout copy.
853-
var svgRootOld = originBlock.getSvgRoot();
854-
if (!svgRootOld) {
855-
throw 'originBlock is not rendered.';
856-
}
857-
var xyOld = Blockly.getSvgXY_(svgRootOld, workspace);
858-
// Scale the scroll (getSvgXY_ did not do this).
859-
if (flyout.toolboxPosition_ == Blockly.TOOLBOX_AT_RIGHT) {
860-
var width = workspace.getMetrics().viewWidth - flyout.width_;
861-
xyOld.x += width / workspace.scale - width;
862-
} else {
863-
xyOld.x += flyout.workspace_.scrollX / flyout.workspace_.scale -
864-
flyout.workspace_.scrollX;
865-
}
866-
xyOld.y += flyout.workspace_.scrollY / flyout.workspace_.scale -
867-
flyout.workspace_.scrollY;
868-
var svgRootNew = block.getSvgRoot();
869-
if (!svgRootNew) {
870-
throw 'block is not rendered.';
871-
}
872-
var xyNew = Blockly.getSvgXY_(svgRootNew, workspace);
873-
// Scale the scroll (getSvgXY_ did not do this).
874-
xyNew.x += workspace.scrollX / workspace.scale - workspace.scrollX;
875-
xyNew.y += workspace.scrollY / workspace.scale - workspace.scrollY;
876-
if (workspace.toolbox_ && !workspace.scrollbar) {
877-
xyNew.x += workspace.toolbox_.width / workspace.scale;
878-
}
879-
block.moveBy(xyOld.x - xyNew.x, xyOld.y - xyNew.y);
849+
var block = Blockly.Flyout.placeNewBlock_(originBlock, workspace, flyout);
880850
Blockly.Events.enable();
881851
if (Blockly.Events.isEnabled()) {
882852
Blockly.Events.setGroup(true);
@@ -895,6 +865,81 @@ Blockly.Flyout.prototype.createBlockFunc_ = function(originBlock) {
895865
};
896866
};
897867

868+
/**
869+
* Copy a block from the flyout to the workspace and position it correctly.
870+
* @param {!Blockly.Block} originBlock The flyout block to copy.
871+
* @param {!Blockly.Workspace} workspace The main workspace.
872+
* @param {!Blockly.Flyout} flyout The flyout where originBlock originates.
873+
* @return {!Blockly.Block} The new block in the main workspace.
874+
* @private
875+
*/
876+
Blockly.Flyout.placeNewBlock_ = function(originBlock, workspace,
877+
flyout) {
878+
var blockSvgOld = originBlock.getSvgRoot();
879+
if (!blockSvgOld) {
880+
throw 'originBlock is not rendered.';
881+
}
882+
// Figure out where the original block is on the screen, relative to the upper
883+
// left corner of the main workspace.
884+
var xyOld = Blockly.getSvgXY_(blockSvgOld, workspace);
885+
// Take into account that the flyout might have been scrolled horizontally
886+
// (separately from the main workspace).
887+
// Generally a no-op in vertical mode but likely to happen in horizontal
888+
// mode.
889+
var scrollX = flyout.workspace_.scrollX;
890+
var scale = flyout.workspace_.scale;
891+
xyOld.x += scrollX / scale - scrollX;
892+
// If the flyout is on the right side, (0, 0) in the flyout is offset to
893+
// the right of (0, 0) in the main workspace. Offset to take that into
894+
// account.
895+
if (flyout.toolboxPosition_ == Blockly.TOOLBOX_AT_RIGHT) {
896+
scrollX = workspace.getMetrics().viewWidth - flyout.width_;
897+
scale = workspace.scale;
898+
// Scale the scroll (getSvgXY_ did not do this).
899+
xyOld.x += scrollX / scale - scrollX;
900+
}
901+
902+
// Take into account that the flyout might have been scrolled vertically
903+
// (separately from the main workspace).
904+
// Generally a no-op in horizontal mode but likely to happen in vertical
905+
// mode.
906+
var scrollY = flyout.workspace_.scrollY;
907+
scale = flyout.workspace_.scale;
908+
xyOld.y += scrollY / scale - scrollY;
909+
// If the flyout is on the bottom, (0, 0) in the flyout is offset to be below
910+
// (0, 0) in the main workspace. Offset to take that into account.
911+
if (flyout.toolboxPosition_ == Blockly.TOOLBOX_AT_BOTTOM) {
912+
scrollY = workspace.getMetrics().viewHeight - flyout.height_;
913+
scale = workspace.scale;
914+
xyOld.y += scrollY / scale - scrollY;
915+
}
916+
917+
// Create the new block by cloning the block in the flyout (via XML).
918+
var xml = Blockly.Xml.blockToDom(originBlock);
919+
var block = Blockly.Xml.domToBlock(workspace, xml);
920+
var blockSvgNew = block.getSvgRoot();
921+
if (!blockSvgNew) {
922+
throw 'block is not rendered.';
923+
}
924+
// Figure out where the new block got placed on the screen, relative to the
925+
// upper left corner of the workspace. This may not be the same as the
926+
// original block because the flyout's origin may not be the same as the
927+
// main workspace's origin.
928+
var xyNew = Blockly.getSvgXY_(blockSvgNew, workspace);
929+
// Scale the scroll (getSvgXY_ did not do this).
930+
xyNew.x += workspace.scrollX / workspace.scale - workspace.scrollX;
931+
xyNew.y += workspace.scrollY / workspace.scale - workspace.scrollY;
932+
// If the flyout is collapsible and the workspace can't be scrolled.
933+
if (workspace.toolbox_ && !workspace.scrollbar) {
934+
xyNew.x += workspace.toolbox_.width / workspace.scale;
935+
xyNew.y += workspace.toolbox_.height / workspace.scale;
936+
}
937+
938+
// Move the new block to where the old block is.
939+
block.moveBy(xyOld.x - xyNew.x, xyOld.y - xyNew.y);
940+
return block;
941+
};
942+
898943
/**
899944
* Filter the blocks on the flyout to disable the ones that are above the
900945
* capacity limit.

0 commit comments

Comments
 (0)