Skip to content

Commit 51540b7

Browse files
committed
feat: Save block starting location and connections
1 parent c17d7fc commit 51540b7

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/actions/mover.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
*/
66

77
import * as Constants from '../constants';
8-
import {ASTNode, ShortcutRegistry, utils as BlocklyUtils} from 'blockly';
9-
import type {Block, WorkspaceSvg} from 'blockly';
8+
import {ASTNode, Connection, ShortcutRegistry, common, utils} from 'blockly';
9+
import type {Block, BlockSvg, WorkspaceSvg} from 'blockly';
1010
import {Navigation} from '../navigation';
1111

12-
const KeyCodes = BlocklyUtils.KeyCodes;
12+
const KeyCodes = utils.KeyCodes;
1313
const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind(
1414
ShortcutRegistry.registry,
1515
);
@@ -148,11 +148,15 @@ export class Mover {
148148
* @returns True iff we can beign a move.
149149
*/
150150
canMove(workspace: WorkspaceSvg) {
151-
// TODO: also check if current block is movable.
152-
return (
151+
const cursor = workspace?.getCursor();
152+
const curNode = cursor?.getCurNode();
153+
const block = curNode?.getSourceBlock();
154+
155+
return !!(
153156
this.navigation.getState(workspace) === Constants.STATE.WORKSPACE &&
154157
this.canCurrentlyEdit(workspace) &&
155-
!this.moves.has(workspace)
158+
!this.moves.has(workspace) && // No move in progress.
159+
block?.isMovable()
156160
);
157161
}
158162

@@ -179,12 +183,17 @@ export class Mover {
179183
startMove(workspace: WorkspaceSvg) {
180184
const cursor = workspace?.getCursor();
181185
const curNode = cursor?.getCurNode();
182-
const block = curNode?.getSourceBlock();
183-
if (!block || !block.isMovable()) return false;
186+
const block = curNode?.getSourceBlock() as BlockSvg | null;
187+
if (!cursor || !block) throw new Error('precondition failure');
184188

189+
// Select and focus block.
190+
common.setSelected(block);
191+
cursor.setCurNode(ASTNode.createBlockNode(block)!);
192+
193+
// Additional implementation goes here.
185194
console.log('startMove');
186195

187-
this.moves.set(workspace, {});
196+
this.moves.set(workspace, new MoveInfo(block));
188197
return true;
189198
}
190199

@@ -198,7 +207,10 @@ export class Mover {
198207
*/
199208
finishMove(workspace: WorkspaceSvg) {
200209
if (!workspace) return false;
210+
const info = this.moves.get(workspace);
211+
if (!info) throw new Error('no move info for workspace');
201212

213+
// Additional implementation goes here.
202214
console.log('finishMove');
203215

204216
this.moves.delete(workspace);
@@ -215,7 +227,10 @@ export class Mover {
215227
*/
216228
abortMove(workspace: WorkspaceSvg) {
217229
if (!workspace) return false;
230+
const info = this.moves.get(workspace);
231+
if (!info) throw new Error('no move info for workspace');
218232

233+
// Additional implementation goes here.
219234
console.log('abortMove');
220235

221236
this.moves.delete(workspace);
@@ -262,4 +277,14 @@ export class Mover {
262277
* Information about the currently in-progress move for a given
263278
* Workspace.
264279
*/
265-
type MoveInfo = {};
280+
export class MoveInfo {
281+
public readonly parentNext: Connection | null;
282+
public readonly parentInput: Connection | null;
283+
public readonly startLocation: utils.Coordinate | null;
284+
285+
constructor(public readonly block: Block) {
286+
this.parentNext = block.previousConnection?.targetConnection ?? null;
287+
this.parentInput = block.outputConnection?.targetConnection ?? null;
288+
this.startLocation = block.getRelativeToSurfaceXY();
289+
}
290+
}

0 commit comments

Comments
 (0)