Skip to content

Commit

Permalink
feat: have block use drag strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Mar 28, 2024
1 parent 8b6e9d8 commit 41f9a94
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
35 changes: 34 additions & 1 deletion core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import type {Input} from './inputs/input.js';
import type {IASTNodeLocationSvg} from './interfaces/i_ast_node_location_svg.js';
import type {IBoundedElement} from './interfaces/i_bounded_element.js';
import type {ICopyable} from './interfaces/i_copyable.js';
import type {IDraggable} from './interfaces/i_draggable.old.js';
import type {IDragStrategy, IDraggable} from './interfaces/i_draggable.js';
import {IIcon} from './interfaces/i_icon.js';
import * as internalConstants from './internal_constants.js';
import {ASTNode} from './keyboard_nav/ast_node.js';
Expand All @@ -60,6 +60,7 @@ import type {WorkspaceSvg} from './workspace_svg.js';
import * as renderManagement from './render_management.js';
import {IconType} from './icons/icon_types.js';
import {BlockCopyData, BlockPaster} from './clipboard/block_paster.js';
import {BlockDragStrategy} from './dragging/block_drag_strategy.js';

/**
* Class for a block's SVG representation.
Expand Down Expand Up @@ -154,6 +155,8 @@ export class BlockSvg
*/
relativeCoords = new Coordinate(0, 0);

private dragStrategy: IDragStrategy = new BlockDragStrategy(this);

/**
* @param workspace The block's workspace.
* @param prototypeName Name of the language object containing type-specific
Expand Down Expand Up @@ -1622,4 +1625,34 @@ export class BlockSvg
add,
);
}

/** Sets the drag strategy for this block. */
setDragStrategy(dragStrategy: IDragStrategy) {
this.dragStrategy = dragStrategy;
}

/** Returns whether this block is movable or not. */
override isMovable(): boolean {
return this.dragStrategy.isMovable();
}

/** Starts a drag on the block. */
startDrag(e?: PointerEvent): void {
this.dragStrategy.startDrag(e);
}

/** Drags the block to the given location. */
drag(newLoc: Coordinate, e?: PointerEvent): void {
this.dragStrategy.drag(newLoc, e);
}

/** Ends the drag on the block. */
endDrag(e?: PointerEvent): void {
this.dragStrategy.endDrag(e);
}

/** Moves the block back to where it was at the start of a drag. */
revertDrag(): void {
this.dragStrategy.revertDrag();
}
}
3 changes: 3 additions & 0 deletions core/dragging/block_drag_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ export class BlockDragStrategy implements IDragStrategy {
);
}

this.startChildConn = null;
this.startParentConn = null;

this.connectionPreviewer!.hidePreview();
this.connectionCandidate = null;

Expand Down

0 comments on commit 41f9a94

Please sign in to comment.