Skip to content

Commit 1f2e242

Browse files
BeksOmegajohnnesky
authored andcommitted
feat: support dragging comments in the gesture (RaspberryPiFoundation#7977)
* feat: add dragging of comments in gesture * chore: fix naming problems
1 parent 4d4b527 commit 1f2e242

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

core/comments/rendered_workspace_comment.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {IRenderedElement} from '../interfaces/i_rendered_element.js';
1515
import * as dom from '../utils/dom.js';
1616
import {IDraggable} from '../interfaces/i_draggable.js';
1717
import {CommentDragStrategy} from '../dragging/comment_drag_strategy.js';
18+
import * as browserEvents from '../browser_events.js';
1819

1920
export class RenderedWorkspaceComment
2021
extends WorkspaceComment
@@ -39,6 +40,13 @@ export class RenderedWorkspaceComment
3940
this.view.setEditable(this.isEditable());
4041

4142
this.addModelUpdateBindings();
43+
44+
browserEvents.conditionalBind(
45+
this.view.getSvgRoot(),
46+
'pointerdown',
47+
this,
48+
this.startGesture,
49+
);
4250
}
4351

4452
/**
@@ -144,6 +152,17 @@ export class RenderedWorkspaceComment
144152
super.dispose();
145153
}
146154

155+
/**
156+
* Starts a gesture because we detected a pointer down on the comment
157+
* (that wasn't otherwise gobbled up, e.g. by resizing).
158+
*/
159+
private startGesture(e: PointerEvent) {
160+
const gesture = this.workspace.getGesture(e);
161+
if (gesture) {
162+
gesture.handleCommentStart(e, this);
163+
}
164+
}
165+
147166
/** Returns whether this comment is movable or not. */
148167
isMovable(): boolean {
149168
return this.dragStrategy.isMovable();

core/gesture.ts

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import type {IIcon} from './interfaces/i_icon.js';
3737
import {IDragger} from './interfaces/i_dragger.js';
3838
import * as registry from './registry.js';
3939
import {IDraggable} from './interfaces/i_draggable.js';
40+
import {RenderedWorkspaceComment} from './comments.js';
4041

4142
/**
4243
* Note: In this file "start" refers to pointerdown
@@ -85,6 +86,12 @@ export class Gesture {
8586
*/
8687
private startBlock: BlockSvg | null = null;
8788

89+
/**
90+
* The comment that the gesture started on, or null if it did not start on a
91+
* comment.
92+
*/
93+
private startComment: RenderedWorkspaceComment | null = null;
94+
8895
/**
8996
* The block that this gesture targets. If the gesture started on a
9097
* shadow block, this is the first non-shadow parent of the block. If the
@@ -315,6 +322,23 @@ export class Gesture {
315322
return true;
316323
}
317324

325+
/**
326+
* Update this gesture to record whether a comment is being dragged.
327+
* This function should be called on a pointermove event the first time
328+
* the drag radius is exceeded. It should be called no more than once per
329+
* gesture.
330+
*
331+
* @returns True if a comment is being dragged.
332+
*/
333+
private updateIsDraggingComment(e: PointerEvent): boolean {
334+
if (!this.startComment) {
335+
return false;
336+
}
337+
338+
this.startDraggingComment(e);
339+
return true;
340+
}
341+
318342
/**
319343
* Check whether to start a block drag. If a block should be dragged, either
320344
* from the flyout or in the workspace, create the necessary BlockDragger and
@@ -394,19 +418,22 @@ export class Gesture {
394418
if (this.updateIsDraggingBlock(e)) {
395419
return;
396420
}
421+
if (this.updateIsDraggingComment(e)) {
422+
return;
423+
}
397424
// Then check if it's a workspace drag.
398425
this.updateIsDraggingWorkspace();
399426
}
400427

401-
/** Create a block dragger and start dragging the selected block. */
428+
/** Start dragging the selected block. */
402429
private startDraggingBlock(e: PointerEvent) {
403430
this.dragging = true;
404431
this.dragger = this.createDragger(this.targetBlock!, this.startWorkspace_!);
405432
this.dragger.onDragStart(e);
406433
this.dragger.onDrag(e, this.currentDragDeltaXY);
407434
}
408435

409-
/** Create a bubble dragger and start dragging the selected bubble. */
436+
/** Start dragging the selected bubble. */
410437
private startDraggingBubble(e: PointerEvent) {
411438
if (!this.startBubble) {
412439
throw new Error(
@@ -427,6 +454,27 @@ export class Gesture {
427454
this.dragger.onDrag(e, this.currentDragDeltaXY);
428455
}
429456

457+
/** Start dragging the selected comment. */
458+
private startDraggingComment(e: PointerEvent) {
459+
if (!this.startComment) {
460+
throw new Error(
461+
'Cannot update dragging the comment because the start ' +
462+
'comment is undefined',
463+
);
464+
}
465+
if (!this.startWorkspace_) {
466+
throw new Error(
467+
'Cannot update dragging the comment because the start ' +
468+
'workspace is undefined',
469+
);
470+
}
471+
472+
this.dragging = true;
473+
this.dragger = this.createDragger(this.startComment, this.startWorkspace_);
474+
this.dragger.onDragStart(e);
475+
this.dragger.onDrag(e, this.currentDragDeltaXY);
476+
}
477+
430478
private createDragger(
431479
draggable: IDraggable,
432480
workspace: WorkspaceSvg,
@@ -893,6 +941,24 @@ export class Gesture {
893941
this.mostRecentEvent = e;
894942
}
895943

944+
/**
945+
* Handle a pointerdown event on a workspace comment.
946+
*
947+
* @param e A pointerdown event.
948+
* @param comment The comment the event hit.
949+
* @internal
950+
*/
951+
handleCommentStart(e: PointerEvent, comment: RenderedWorkspaceComment) {
952+
if (this.gestureHasStarted) {
953+
throw Error(
954+
'Tried to call gesture.handleCommentStart, ' +
955+
'but the gesture had already been started.',
956+
);
957+
}
958+
this.setStartComment(comment);
959+
this.mostRecentEvent = e;
960+
}
961+
896962
/* Begin functions defining what actions to take to execute clicks on each
897963
* type of target. Any developer wanting to add behaviour on clicks should
898964
* modify only this code. */
@@ -1049,6 +1115,18 @@ export class Gesture {
10491115
}
10501116
}
10511117

1118+
/**
1119+
* Record the comment that a gesture started on
1120+
*
1121+
* @param comment The comment the gesture started on.
1122+
* @internal
1123+
*/
1124+
setStartComment(comment: RenderedWorkspaceComment) {
1125+
if (!this.startComment) {
1126+
this.startComment = comment;
1127+
}
1128+
}
1129+
10521130
/**
10531131
* Record the block that a gesture started on, and set the target block
10541132
* appropriately.

0 commit comments

Comments
 (0)