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

fix: pasting with nothing selected #7561

Merged
merged 3 commits into from
Sep 29, 2023
Merged
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
30 changes: 9 additions & 21 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 {Rect} from './utils/rect.js';
import {Coordinate} from './utils/coordinate.js';
import {KeyCodes} from './utils/keycodes.js';
import type {WorkspaceSvg} from './workspace_svg.js';
Expand Down Expand Up @@ -190,30 +191,17 @@ export function registerPaste() {
},
callback() {
if (!copyData || !copyCoords || !copyWorkspace) return false;
const {
left: viewportLeft,
top: viewportTop,
width: viewportWidth,
height: viewportHeight,
} = copyWorkspace.getMetricsManager().getViewMetrics(true);
const selected = common.getSelected() as BlockSvg;
const {left, top, width, height} = copyWorkspace
.getMetricsManager()
.getViewMetrics(true);
const viewportRect = new Rect(top, top + height, left, left + width);

// Pass the default copy coordinates when
// default location is inside viewport.
if (
copyCoords.x >= viewportLeft &&
copyCoords.x + selected.width <= viewportLeft + viewportWidth &&
copyCoords.y >= viewportTop &&
copyCoords.y + selected.height <= viewportTop + viewportHeight
) {
if (viewportRect.contains(copyCoords.x, copyCoords.y)) {
// Pass the default copy coordinates when they are inside the viewport.
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),
);
// Otherwise paste in the middle of the viewport.
const centerCoords = new Coordinate(left + width / 2, top + height / 2);
return !!clipboard.paste(copyData, copyWorkspace, centerCoords);
}
},
Expand Down