Skip to content

Commit 5031327

Browse files
feat: make it possible to copy a block from the flyout and paste it into the workspace (#120)
* feat: make it possible to copy a block from the flyout and paste it into the workspace * chore: apply suggested fix from review
1 parent 2ed7b7d commit 5031327

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/navigation_controller.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -533,22 +533,36 @@ export class NavigationController {
533533
name: Constants.SHORTCUT_NAMES.COPY,
534534
preconditionFn: (workspace) => {
535535
if (this.canCurrentlyEdit(workspace)) {
536-
const curNode = workspace.getCursor()?.getCurNode();
537-
if (curNode && curNode.getSourceBlock()) {
538-
const sourceBlock = curNode.getSourceBlock();
539-
return !!(
540-
!Blockly.Gesture.inProgress() &&
541-
sourceBlock &&
542-
sourceBlock.isDeletable() &&
543-
sourceBlock.isMovable()
544-
);
536+
switch (this.navigation.getState(workspace)) {
537+
case Constants.STATE.WORKSPACE:
538+
const curNode = workspace?.getCursor()?.getCurNode();
539+
const source = curNode?.getSourceBlock();
540+
return !!(
541+
source?.isDeletable() &&
542+
source?.isMovable() &&
543+
!Blockly.Gesture.inProgress()
544+
);
545+
case Constants.STATE.FLYOUT:
546+
const flyoutWorkspace = workspace.getFlyout()?.getWorkspace();
547+
const sourceBlock = flyoutWorkspace
548+
?.getCursor()
549+
?.getCurNode()
550+
?.getSourceBlock();
551+
return !!(sourceBlock && !Blockly.Gesture.inProgress());
552+
default:
553+
return false;
545554
}
546555
}
547556
return false;
548557
},
549558
callback: (workspace) => {
550-
const sourceBlock = workspace
551-
.getCursor()
559+
const navigationState = this.navigation.getState(workspace);
560+
let activeWorkspace: Blockly.WorkspaceSvg | undefined = workspace;
561+
if (navigationState == Constants.STATE.FLYOUT) {
562+
activeWorkspace = workspace.getFlyout()?.getWorkspace();
563+
}
564+
const sourceBlock = activeWorkspace
565+
?.getCursor()
552566
?.getCurNode()
553567
.getSourceBlock() as BlockSvg;
554568
workspace.hideChaff();
@@ -572,9 +586,12 @@ export class NavigationController {
572586
name: Constants.SHORTCUT_NAMES.PASTE,
573587
preconditionFn: (workspace) =>
574588
this.canCurrentlyEdit(workspace) && !Blockly.Gesture.inProgress(),
575-
callback: () => {
589+
callback: (workspace) => {
576590
if (!this.copyData || !this.copyWorkspace) return false;
577-
return this.navigation.paste(this.copyData, this.copyWorkspace);
591+
const pasteWorkspace = this.copyWorkspace.isFlyout
592+
? workspace
593+
: this.copyWorkspace;
594+
return this.navigation.paste(this.copyData, pasteWorkspace);
578595
},
579596
keyCodes: [
580597
createSerializedKey(KeyCodes.V, [KeyCodes.CTRL]),

0 commit comments

Comments
 (0)