Skip to content

Commit 602745e

Browse files
authored
fix paste location for blocks (microsoft#10442)
1 parent 673712e commit 602745e

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

webapp/src/blocks.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,29 +1957,23 @@ export class Editor extends toolboxeditor.ToolboxEditor {
19571957
}
19581958
}
19591959

1960-
protected pasteCallback = () => {
1960+
protected pasteCallback = (workspace: Blockly.Workspace, ev: Event) => {
19611961
const data = getCopyData();
19621962
if (!data?.data || !this.editor || !this.canPasteData(data)) return false;
19631963

1964-
this.pasteAsync(data);
1964+
this.pasteAsync(data, ev.type === "pointerdown" ? ev as PointerEvent : undefined);
19651965
return true;
19661966
}
19671967

1968-
protected async pasteAsync(data: CopyDataEntry) {
1968+
protected async pasteAsync(data: CopyDataEntry, ev?: PointerEvent) {
19691969
const copyData = data.data;
19701970
const copyWorkspace = this.editor;
19711971
const copyCoords = copyWorkspace.id === data.workspaceId ? data.coord : undefined;
19721972

19731973
// this pasting code is adapted from Blockly/core/shortcut_items.ts
19741974
const doPaste = () => {
1975-
if (!copyCoords) {
1976-
// If we don't have location data about the original copyable, let the
1977-
// paster determine position.
1978-
return !!Blockly.clipboard.paste(copyData, copyWorkspace);
1979-
}
1980-
1981-
const { left, top, width, height } = copyWorkspace
1982-
.getMetricsManager()
1975+
const metricsManager = copyWorkspace.getMetricsManager();
1976+
const { left, top, width, height } = metricsManager
19831977
.getViewMetrics(true);
19841978
const viewportRect = new Blockly.utils.Rect(
19851979
top,
@@ -1988,7 +1982,21 @@ export class Editor extends toolboxeditor.ToolboxEditor {
19881982
left + width
19891983
);
19901984

1991-
if (viewportRect.contains(copyCoords.x, copyCoords.y)) {
1985+
if (ev) {
1986+
// if we have a pointer event, then paste at that location
1987+
const injectionDivBBox = copyWorkspace.getInjectionDiv().getBoundingClientRect();
1988+
const pixelViewport = metricsManager.getViewMetrics();
1989+
const workspaceSvgOffset = metricsManager.getAbsoluteMetrics();
1990+
1991+
const offsetX = ((ev.clientX - injectionDivBBox.left - workspaceSvgOffset.left) / pixelViewport.width);
1992+
const offsetY = ((ev.clientY - injectionDivBBox.top - workspaceSvgOffset.top) / pixelViewport.height);
1993+
1994+
const contextMenuCoords = new Blockly.utils.Coordinate(left + offsetX * width, top + offsetY * height);
1995+
1996+
return !!Blockly.clipboard.paste(copyData, copyWorkspace, contextMenuCoords);
1997+
}
1998+
1999+
if (copyCoords && viewportRect.contains(copyCoords.x, copyCoords.y)) {
19922000
// If the original copyable is inside the viewport, let the paster
19932001
// determine position.
19942002
return !!Blockly.clipboard.paste(copyData, copyWorkspace);

0 commit comments

Comments
 (0)