| 42 | type GuideCandidates = { vertical: number[]; horizontal: number[] }; |
| 43 | |
| 44 | export class DragElement extends Interaction implements IInteraction { |
| 45 | name = 'drag-element'; |
| 46 | |
| 47 | private pointerId?: number; |
| 48 | private startPoint?: DOMPoint; |
| 49 | private startTarget?: Selection[number]; |
| 50 | private selectionForDrag: Selection = []; |
| 51 | private willReplaceSelection = false; |
| 52 | private exclusiveStarted = false; |
| 53 | private dragItems: DragItem[] = []; |
| 54 | private dragging = false; |
| 55 | private dragThreshold = 4; |
| 56 | private completeInteraction?: () => void; |
| 57 | private guideCandidates?: GuideCandidates; |
| 58 | private startBounds?: Bounds; |
| 59 | private guideVertical?: SVGLineElement; |
| 60 | private guideHorizontal?: SVGLineElement; |
| 61 | |
| 62 | init(options: InteractionInitOptions) { |
| 63 | super.init(options); |
| 64 | this.editor.getDocument().addEventListener('pointerdown', this.handleStart); |
| 65 | } |
| 66 | |
| 67 | destroy() { |
| 68 | this.detachPointerListeners(); |
| 69 | this.editor |
| 70 | .getDocument() |
| 71 | .removeEventListener('pointerdown', this.handleStart); |
| 72 | } |
| 73 | |
| 74 | private handleStart = (event: PointerEvent) => { |
| 75 | if (!this.interaction.isActive()) return; |
| 76 | if (event.pointerType === 'mouse' && event.button !== 0) return; |
| 77 | |
| 78 | const target = getEventTarget(event.target as SVGElement); |
| 79 | if (!target) return; |
| 80 | if (isEditingText(target)) return; |
| 81 | |
| 82 | const svg = this.editor.getDocument(); |
| 83 | this.pointerId = event.pointerId; |
| 84 | this.startPoint = clientToViewport(svg, event.clientX, event.clientY); |
| 85 | this.dragging = false; |
| 86 | this.startTarget = target; |
| 87 | const isSelected = this.interaction.isSelected(target); |
| 88 | this.selectionForDrag = isSelected |
| 89 | ? this.interaction.getSelection() |
| 90 | : [target]; |
| 91 | this.willReplaceSelection = !isSelected; |
| 92 | this.exclusiveStarted = false; |
| 93 | this.startBounds = this.getSelectionBounds(this.selectionForDrag); |
| 94 | this.guideCandidates = this.collectGuideCandidates(this.selectionForDrag); |
| 95 | |
| 96 | window.addEventListener('pointermove', this.handleMove); |
| 97 | window.addEventListener('pointerup', this.handleEnd); |
| 98 | window.addEventListener('pointercancel', this.handleEnd); |
| 99 | }; |
| 100 | |
| 101 | private handleMove = (event: PointerEvent) => { |
nothing calls this directly
no test coverage detected