44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7- import * as Constants from '../constants ' ;
7+ import type { BlockSvg , IDragger , IDragStrategy } from 'blockly ' ;
88import {
99 ASTNode ,
10- Connection ,
11- ContextMenuRegistry ,
12- ShortcutRegistry ,
13- WorkspaceSvg ,
1410 common ,
11+ Connection ,
1512 registry ,
1613 utils ,
14+ WorkspaceSvg ,
1715} from 'blockly' ;
18- import type { BlockSvg , IDragger , IDragStrategy } from 'blockly' ;
19- import { Navigation } from '../navigation' ;
20- import { KeyboardDragStrategy } from '../keyboard_drag_strategy' ;
16+ import * as Constants from '../constants' ;
2117import { Direction , getXYFromDirection } from '../drag_direction' ;
22-
23- const KeyCodes = utils . KeyCodes ;
24- const createSerializedKey = ShortcutRegistry . registry . createSerializedKey . bind (
25- ShortcutRegistry . registry ,
26- ) ;
18+ import { KeyboardDragStrategy } from '../keyboard_drag_strategy' ;
19+ import { Navigation } from '../navigation' ;
2720
2821/**
2922 * The distance to move an item, in workspace coordinates, when
@@ -32,7 +25,7 @@ const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind(
3225const UNCONSTRAINED_MOVE_DISTANCE = 20 ;
3326
3427/**
35- * Actions for moving blocks with keyboard shortcuts.
28+ * Low-level code for moving blocks with keyboard shortcuts.
3629 */
3730export class Mover {
3831 /**
@@ -58,145 +51,6 @@ export class Mover {
5851
5952 constructor ( protected navigation : Navigation ) { }
6053
61- private shortcuts : ShortcutRegistry . KeyboardShortcut [ ] = [
62- // Begin and end move.
63- {
64- name : 'Start move' ,
65- preconditionFn : ( workspace ) => this . canMove ( workspace ) ,
66- callback : ( workspace ) => this . startMove ( workspace ) ,
67- keyCodes : [ KeyCodes . M ] ,
68- } ,
69- {
70- name : 'Finish move' ,
71- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
72- callback : ( workspace ) => this . finishMove ( workspace ) ,
73- keyCodes : [ KeyCodes . ENTER ] ,
74- allowCollision : true ,
75- } ,
76- {
77- name : 'Abort move' ,
78- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
79- callback : ( workspace ) => this . abortMove ( workspace ) ,
80- keyCodes : [ KeyCodes . ESC ] ,
81- allowCollision : true ,
82- } ,
83-
84- // Constrained moves.
85- {
86- name : 'Move left, constrained' ,
87- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
88- callback : ( workspace ) => this . moveConstrained ( workspace , Direction . Left ) ,
89- keyCodes : [ KeyCodes . LEFT ] ,
90- allowCollision : true ,
91- } ,
92- {
93- name : 'Move right unconstrained' ,
94- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
95- callback : ( workspace ) => this . moveConstrained ( workspace , Direction . Right ) ,
96- keyCodes : [ KeyCodes . RIGHT ] ,
97- allowCollision : true ,
98- } ,
99- {
100- name : 'Move up, constrained' ,
101- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
102- callback : ( workspace ) => this . moveConstrained ( workspace , Direction . Up ) ,
103- keyCodes : [ KeyCodes . UP ] ,
104- allowCollision : true ,
105- } ,
106- {
107- name : 'Move down constrained' ,
108- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
109- callback : ( workspace ) => this . moveConstrained ( workspace , Direction . Down ) ,
110- keyCodes : [ KeyCodes . DOWN ] ,
111- allowCollision : true ,
112- } ,
113-
114- // Unconstrained moves.
115- {
116- name : 'Move left, unconstrained' ,
117- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
118- callback : ( workspace ) =>
119- this . moveUnconstrained ( workspace , Direction . Left ) ,
120- keyCodes : [
121- createSerializedKey ( KeyCodes . LEFT , [ KeyCodes . ALT ] ) ,
122- createSerializedKey ( KeyCodes . LEFT , [ KeyCodes . CTRL ] ) ,
123- ] ,
124- } ,
125- {
126- name : 'Move right, unconstrained' ,
127- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
128- callback : ( workspace ) =>
129- this . moveUnconstrained ( workspace , Direction . Right ) ,
130- keyCodes : [
131- createSerializedKey ( KeyCodes . RIGHT , [ KeyCodes . ALT ] ) ,
132- createSerializedKey ( KeyCodes . RIGHT , [ KeyCodes . CTRL ] ) ,
133- ] ,
134- } ,
135- {
136- name : 'Move up unconstrained' ,
137- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
138- callback : ( workspace ) => this . moveUnconstrained ( workspace , Direction . Up ) ,
139- keyCodes : [
140- createSerializedKey ( KeyCodes . UP , [ KeyCodes . ALT ] ) ,
141- createSerializedKey ( KeyCodes . UP , [ KeyCodes . CTRL ] ) ,
142- ] ,
143- } ,
144- {
145- name : 'Move down, unconstrained' ,
146- preconditionFn : ( workspace ) => this . isMoving ( workspace ) ,
147- callback : ( workspace ) =>
148- this . moveUnconstrained ( workspace , Direction . Down ) ,
149- keyCodes : [
150- createSerializedKey ( KeyCodes . DOWN , [ KeyCodes . ALT ] ) ,
151- createSerializedKey ( KeyCodes . DOWN , [ KeyCodes . CTRL ] ) ,
152- ] ,
153- } ,
154- ] ;
155-
156- menuItems : ContextMenuRegistry . RegistryItem [ ] = [
157- {
158- displayText : 'Move Block (M)' ,
159- preconditionFn : ( scope ) => {
160- const workspace = scope . block ?. workspace as WorkspaceSvg | null ;
161- if ( ! workspace ) return 'hidden' ;
162- return this . canMove ( workspace ) ? 'enabled' : 'disabled' ;
163- } ,
164- callback : ( scope ) => {
165- const workspace = scope . block ?. workspace as WorkspaceSvg | null ;
166- if ( ! workspace ) return false ;
167- this . startMove ( workspace ) ;
168- } ,
169- scopeType : ContextMenuRegistry . ScopeType . BLOCK ,
170- id : 'move' ,
171- weight : 8.5 ,
172- } ,
173- ] ;
174-
175- /**
176- * Install the actions as both keyboard shortcuts and (where
177- * applicable) context menu items.
178- */
179- install ( ) {
180- for ( const shortcut of this . shortcuts ) {
181- ShortcutRegistry . registry . register ( shortcut ) ;
182- }
183- for ( const menuItem of this . menuItems ) {
184- ContextMenuRegistry . registry . register ( menuItem ) ;
185- }
186- }
187-
188- /**
189- * Uninstall these actions.
190- */
191- uninstall ( ) {
192- for ( const shortcut of this . shortcuts ) {
193- ShortcutRegistry . registry . unregister ( shortcut . name ) ;
194- }
195- for ( const menuItem of this . menuItems ) {
196- ContextMenuRegistry . registry . unregister ( menuItem . id ) ;
197- }
198- }
199-
20054 /**
20155 * Returns true iff we are able to begin moving the block which
20256 * currently has focus on the given workspace.
0 commit comments