Skip to content

Commit

Permalink
fix: pasting with nothing selected (#7561)
Browse files Browse the repository at this point in the history
* fix: pasting with nothing selected

* fix: import

* chore: format
  • Loading branch information
BeksOmega authored Sep 29, 2023
1 parent 19e9115 commit 48fc400
Showing 1 changed file with 9 additions and 21 deletions.
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

0 comments on commit 48fc400

Please sign in to comment.