This repository was archived by the owner on May 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathcodeMirrorTouchCursor.ts
More file actions
125 lines (103 loc) · 2.87 KB
/
codeMirrorTouchCursor.ts
File metadata and controls
125 lines (103 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const longPressDelay = 350
const movementTolerance = 8
interface TouchCursorState {
active: boolean
startX: number
startY: number
timer: number
}
const getTouchDistance = (touch: Touch, state: TouchCursorState) => {
const x = touch.clientX - state.startX
const y = touch.clientY - state.startY
return Math.sqrt(x * x + y * y)
}
export function attachTouchCursorDrag(editor: CodeMirror.Editor) {
const wrapperElement = editor.getWrapperElement()
let state: TouchCursorState | null = null
const clearState = () => {
if (state != null) {
window.clearTimeout(state.timer)
state = null
}
wrapperElement.classList.remove('CodeMirror-touch-cursor-dragging')
}
const moveCursorToTouch = (touch: Touch) => {
const pos = editor.coordsChar({
left: touch.pageX,
top: touch.pageY,
})
editor.focus()
editor.setCursor(pos)
editor.scrollIntoView(pos, 30)
}
const onTouchStart = (event: TouchEvent) => {
clearState()
if (event.touches.length !== 1) {
return
}
const touch = event.touches[0]
state = {
active: false,
startX: touch.clientX,
startY: touch.clientY,
timer: window.setTimeout(() => {
if (state == null) {
return
}
state.active = true
wrapperElement.classList.add('CodeMirror-touch-cursor-dragging')
moveCursorToTouch(touch)
}, longPressDelay),
}
}
const onTouchMove = (event: TouchEvent) => {
if (state == null || event.touches.length !== 1) {
clearState()
return
}
const touch = event.touches[0]
if (!state.active) {
if (getTouchDistance(touch, state) > movementTolerance) {
clearState()
}
return
}
event.preventDefault()
event.stopPropagation()
moveCursorToTouch(touch)
}
const onTouchEnd = (event: TouchEvent) => {
if (state?.active) {
event.preventDefault()
event.stopPropagation()
}
clearState()
}
const onContextMenu = (event: MouseEvent) => {
if (state?.active) {
event.preventDefault()
event.stopPropagation()
}
}
wrapperElement.addEventListener('touchstart', onTouchStart, {
passive: true,
})
wrapperElement.addEventListener('touchmove', onTouchMove, {
passive: false,
})
wrapperElement.addEventListener('touchend', onTouchEnd, {
passive: false,
})
wrapperElement.addEventListener('touchcancel', onTouchEnd, {
passive: false,
})
wrapperElement.addEventListener('contextmenu', onContextMenu)
return () => {
clearState()
wrapperElement.removeEventListener('touchstart', onTouchStart)
wrapperElement.removeEventListener('touchmove', onTouchMove)
wrapperElement.removeEventListener('touchend', onTouchEnd)
wrapperElement.removeEventListener('touchcancel', onTouchEnd)
wrapperElement.removeEventListener('contextmenu', onContextMenu)
}
}