Skip to content
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

feat: Paste block in viewport / near cursor #7521

Merged
merged 7 commits into from
Sep 22, 2023
Merged
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
10 changes: 10 additions & 0 deletions core/interfaces/i_movable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {Coordinate} from '../utils/coordinate.js';

// Former goog.module ID: Blockly.IMovable

/**
Expand All @@ -16,4 +18,12 @@ export interface IMovable {
* @returns True if movable.
*/
isMovable(): boolean;

/**
* Return the coordinates of the top-left corner of this movable object
bsampada7 marked this conversation as resolved.
Show resolved Hide resolved
* relative to the drawing surface's origin (0,0), in workspace units.
*
* @returns Object with .x and .y properties.
*/
getRelativeToSurfaceXY(): Coordinate;
}
33 changes: 31 additions & 2 deletions core/shortcut_items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as common from './common.js';
import {Gesture} from './gesture.js';
import {ICopyData, isCopyable} from './interfaces/i_copyable.js';
import {KeyboardShortcut, ShortcutRegistry} from './shortcut_registry.js';
import {Coordinate} from './utils/coordinate.js';
import {KeyCodes} from './utils/keycodes.js';
import type {WorkspaceSvg} from './workspace_svg.js';

Expand Down Expand Up @@ -82,6 +83,7 @@ export function registerDelete() {

let copyData: ICopyData | null = null;
let copyWorkspace: WorkspaceSvg | null = null;
let copyCoords: Coordinate | null = null;

/**
* Keyboard shortcut to copy a block on ctrl+c, cmd+c, or alt+c.
Expand Down Expand Up @@ -118,6 +120,7 @@ export function registerCopy() {
const selected = common.getSelected();
if (!selected || !isCopyable(selected)) return false;
copyData = selected.toCopyData();
copyCoords = selected.getRelativeToSurfaceXY();
copyWorkspace = workspace;
return !!copyData;
},
Expand Down Expand Up @@ -158,6 +161,7 @@ export function registerCut() {
const selected = common.getSelected();
if (!selected || !isCopyable(selected)) return false;
copyData = selected.toCopyData();
copyCoords = selected.getRelativeToSurfaceXY();
copyWorkspace = workspace;
(selected as BlockSvg).checkAndDelete();
return true;
Expand Down Expand Up @@ -188,8 +192,33 @@ export function registerPaste() {
return !workspace.options.readOnly && !Gesture.inProgress();
},
callback() {
if (!copyData || !copyWorkspace) return false;
return !!clipboard.paste(copyData, copyWorkspace);
if (!copyData || !copyCoords || !copyWorkspace) return false;
const {
bsampada7 marked this conversation as resolved.
Show resolved Hide resolved
left: viewportLeft,
top: viewportTop,
width: viewportWidth,
height: viewportHeight,
} = copyWorkspace.getMetricsManager().getViewMetrics(true);
const selected = common.getSelected() as BlockSvg;

// Pass the default copy coordinates when
// default location is inside viewport.
if (
bsampada7 marked this conversation as resolved.
Show resolved Hide resolved
copyCoords.x >= viewportLeft &&
copyCoords.x + selected.width <= viewportLeft + viewportWidth &&
copyCoords.y >= viewportTop &&
copyCoords.y + selected.height <= viewportTop + viewportHeight
) {
return !!clipboard.paste(copyData, copyWorkspace, copyCoords);
} else {
// Pass the center of the new viewport
// to paste the copied block
const centerCoords = new Coordinate(
viewportLeft + Math.trunc(viewportWidth / 2 - selected.width / 2),
viewportTop + Math.trunc(viewportHeight / 2 - selected.height / 2),
);
return !!clipboard.paste(copyData, copyWorkspace, centerCoords);
}
},
keyCodes: [ctrlV, altV, metaV],
};
Expand Down