forked from sjas/vimflowy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcursorMovement.js
187 lines (150 loc) · 5.14 KB
/
cursorMovement.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const closest = (node, selector) => {
let element = node
while(element && !element.matches(selector)) {
element = element.parentNode
}
return element
}
const NODE_TYPES = {
ELEMENT: 1,
TEXT: 3
}
const getContentAbstraction = node => {
const contentElement = closest(node, '.content')
const getNodes = element => [...element.childNodes].reduce((accu, current) => {
if (current.nodeType === NODE_TYPES.TEXT) {
return [...accu, current]
}
if (current.nodeType === NODE_TYPES.ELEMENT) {
return [...accu, ...getNodes(current)]
}
console.log(`I did not expect this nodetype: ${current.nodeType} in this element`, current)
}, [])
const nodes = getNodes(contentElement)
return {
get length() { return nodes.reduce((accu, current) => accu + current.length, 0) },
setCursorAt: function (offset) {
for(let i = 0; i < nodes.length; ++i) {
const node = nodes[i]
if (offset < node.length) {
const range = document.createRange()
range.setStart(node, offset)
range.collapse(true)
const selection = document.getSelection()
selection.removeAllRanges()
selection.addRange(range)
selection.modify('extend', 'right', 'character')
contentElement.focus()
return
}
offset -= node.length
}
}
}
}
const projectAncestor = project => closest(project, `.project:not([projectid='${project.getAttribute('projectid')}'])`)
const moveAboveFold = element => {
const rect = element.getBoundingClientRect()
const fold = window.innerHeight
const scrollPosition = window.scrollY
const beyondFold = rect.top >= fold || (rect.top < fold && rect.bottom > fold)
const floatingHeaderHeight = 30
const aboveViewport = rect.top < floatingHeaderHeight
if (!beyondFold && !aboveViewport) {
return
}
element.scrollIntoView()
if (aboveViewport) {
window.scrollBy(0, -floatingHeaderHeight)
}
}
const setCursorAfterVerticalMove = (calculateOffset, cursorTargetProject) => {
const cursorTarget = cursorTargetProject.querySelector('.name>.content')
if (!cursorTarget.childNodes.length) {
cursorTarget.append(' ')
}
const abstraction = getContentAbstraction(cursorTarget)
const offset = calculateOffset(abstraction, o => o)
abstraction.setCursorAt(offset)
moveAboveFold(cursorTarget)
}
const moveCursorDown = startElement => {
const project = projectAncestor(startElement)
if (project.className.includes('open')) {
return project.querySelector('.project')
}
let cursorTargetProject = project
while(!(cursorTargetProject.nextElementSibling && cursorTargetProject.nextElementSibling.className.includes('project'))) {
const ancestor = projectAncestor(cursorTargetProject)
if (ancestor.className.includes('mainTreeRoot')) {
return project
}
cursorTargetProject = ancestor
}
return cursorTargetProject.nextElementSibling
}
const moveCursorUp = t => {
const project = projectAncestor(t)
let cursorTarget = null
if (project.previousElementSibling) {
cursorTarget = project.previousElementSibling
if (cursorTarget.className.includes('open')) {
const textContainers = cursorTarget.querySelectorAll('.project')
cursorTarget = textContainers[textContainers.length - 1]
}
}
if (!cursorTarget) {
cursorTarget = projectAncestor(project)
}
return cursorTarget.className.includes('mainTreeRoot')
? project
: cursorTarget
}
const setCursorAt = (offset) => {
const selection = document.getSelection()
const {anchorOffset, baseNode} = selection
let effectiveOffset = offset
if (typeof offset === 'function') {
effectiveOffset = offset(anchorOffset, baseNode)
}
effectiveOffset = Math.min(effectiveOffset, baseNode.length - 1)
effectiveOffset = Math.max(effectiveOffset, 0)
state.set(_ => ({
anchorOffset: effectiveOffset
}))
const range = document.createRange()
range.setStart(baseNode, effectiveOffset)
range.collapse(true)
selection.removeAllRanges()
selection.addRange(range)
selection.modify('extend', 'right', 'character')
baseNode.parentElement.focus()
}
const moveCursorToStart = (target, calculateOffset) => {
const contentAbstraction = getContentAbstraction(target)
const offset = calculateOffset(contentAbstraction, () => 0)
contentAbstraction.setCursorAt(0)
}
const moveCursorToEnd = (target, calculateOffset) => {
const contentAbstraction = getContentAbstraction(target)
const offset = calculateOffset(contentAbstraction, () => contentAbstraction.length - 1)
contentAbstraction.setCursorAt(offset)
}
const moveCursorLeft = (target, calculateOffset) => {
const contentAbstraction = getContentAbstraction(target)
const offset = calculateOffset(contentAbstraction, o => o - 1)
contentAbstraction.setCursorAt(offset)
}
const moveCursorRight = (target, calculateOffset) => {
const contentAbstraction = getContentAbstraction(target)
const offset = calculateOffset(contentAbstraction, o => o + 1)
contentAbstraction.setCursorAt(offset)
}
if (typeof module !== 'undefined') {
module.exports = {
moveCursorLeft,
moveCursorRight,
moveCursorDown,
moveCursorUp
}
}