Debian Codegraph @

src/svg.draggable.js

raw
1 import { Box, Element, G, extend, off, on } from '@svgdotjs/svg.js'
2
3 const getCoordsFromEvent = (ev) => {
4 if (ev.changedTouches) {
5 ev = ev.changedTouches[0]
6 }
7 return { x: ev.clientX, y: ev.clientY }
8 }
9
10 // Creates handler, saves it
11 class DragHandler {
12 constructor(el) {
13 el.remember('_draggable', this)
14 this.el = el
15
16 this.drag = this.drag.bind(this)
17 this.startDrag = this.startDrag.bind(this)
18 this.endDrag = this.endDrag.bind(this)
19 }
20
21 // Enables or disabled drag based on input
22 init(enabled) {
23 if (enabled) {
24 this.el.on('mousedown.drag', this.startDrag)
25 this.el.on('touchstart.drag', this.startDrag, { passive: false })
26 } else {
27 this.el.off('mousedown.drag')
28 this.el.off('touchstart.drag')
29 }
30 }
31
32 // Start dragging
33 startDrag(ev) {
34 const isMouse = !ev.type.indexOf('mouse')
35
36 // Check for left button
37 if (isMouse && ev.which !== 1 && ev.buttons !== 0) {
38 return
39 }
40
41 // Fire beforedrag event
42 if (
43 this.el.dispatch('beforedrag', { event: ev, handler: this })
44 .defaultPrevented
45 ) {
46 return
47 }
48
49 // Prevent browser drag behavior as soon as possible
50 ev.preventDefault()
51
52 // Prevent propagation to a parent that might also have dragging enabled
53 ev.stopPropagation()
54
55 // Make sure that start events are unbound so that one element
56 // is only dragged by one input only
57 this.init(false)
58
59 this.box = this.el.bbox()
60 this.lastClick = this.el.point(getCoordsFromEvent(ev))
61
62 const eventMove = (isMouse ? 'mousemove' : 'touchmove') + '.drag'
63 const eventEnd = (isMouse ? 'mouseup' : 'touchend') + '.drag'
64
65 // Bind drag and end events to window
66 on(window, eventMove, this.drag, this, { passive: false })
67 on(window, eventEnd, this.endDrag, this, { passive: false })
68
69 // Fire dragstart event
70 this.el.fire('dragstart', { event: ev, handler: this, box: this.box })
71 }
72
73 // While dragging
74 drag(ev) {
75 const { box, lastClick } = this
76
77 const currentClick = this.el.point(getCoordsFromEvent(ev))
78 const dx = currentClick.x - lastClick.x
79 const dy = currentClick.y - lastClick.y
80
81 if (!dx && !dy) return box
82
83 const x = box.x + dx
84 const y = box.y + dy
85 this.box = new Box(x, y, box.w, box.h)
86 this.lastClick = currentClick
87
88 if (
89 this.el.dispatch('dragmove', {
90 event: ev,
91 handler: this,
92 box: this.box,
93 dx,
94 dy,
95 }).defaultPrevented
96 ) {
97 return
98 }
99
100 this.move(x, y)
101 }
102
103 move(x, y) {
104 // Svg elements bbox depends on their content even though they have
105 // x, y, width and height - strange!
106 // Thats why we handle them the same as groups
107 if (this.el.type === 'svg') {
108 G.prototype.move.call(this.el, x, y)
109 } else {
110 this.el.move(x, y)
111 }
112 }
113
114 endDrag(ev) {
115 // final drag
116 this.drag(ev)
117
118 // fire dragend event
119 this.el.fire('dragend', { event: ev, handler: this, box: this.box })
120
121 // unbind events
122 off(window, 'mousemove.drag')
123 off(window, 'touchmove.drag')
124 off(window, 'mouseup.drag')
125 off(window, 'touchend.drag')
126
127 // Rebind initial Events
128 this.init(true)
129 }
130 }
131
132 extend(Element, {
133 draggable(enable = true) {
134 const dragHandler = this.remember('_draggable') || new DragHandler(this)
135 dragHandler.init(enable)
136 return this
137 },
138 })