forked from tarnas14/vimflowy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcursorMovement.js
346 lines (280 loc) · 9.67 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
const closest = (node, selector) => {
let element = node
while(element
&& typeof element.matches !== 'undefined'
&& !element.matches(selector)) {
element = element.parentNode
}
return element
}
const offsetCalculator = state => (contentAbstraction, offset) =>
{
const maxOffset = contentAbstraction.length - 1;
const bound = o =>
{
let inBounds = o
inBounds = Math.min(maxOffset, inBounds)
inBounds = Math.max(0, inBounds)
return inBounds
}
const currentOffset = state.get().anchorOffset;
const effective = bound(offset(currentOffset))
state.set(_ => ({anchorOffset: effective}))
return effective
}
const NODE_TYPES = {
ELEMENT: 1,
TEXT: 3
}
const getNodes = element => [...element.childNodes].reduce((accu, current) =>
{
if(!accu)
return;
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 getContentAbstraction = node =>
{
const contentElement = closest(node, '.content')
const nodes = getNodes(contentElement)
if(!nodes)
return;
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 projectAncestor = project => typeof project.getAttribute !== 'undefined'
? closest(project, `.project:not([projectid='${project.getAttribute('projectid')}'])`)
: project */
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) =>
{
// @TODO: this should be removed once we refactor
// cursorMovement to use the workflowy API instead.
if(cursorTargetProject === null)
return;
const cursorTarget = cursorTargetProject.querySelector('.name>.content')
if(cursorTarget === null)
{
return;
}
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('selected'))
return project.querySelector('.project')
if (project.className.includes('open'))
{
const querySelectedProject = project.querySelector('.project');
// need to guard against the case of a sibling with hidden items in it
if(querySelectedProject)
{
return querySelectedProject;
}
}
let cursorTargetProject = project
// ensure that we walk past the upcoming SVG element by taking an additional step here.
if (cursorTargetProject.nextElementSibling != null && typeof(cursorTargetProject.nextElementSibling.className) !== 'string')
cursorTargetProject = cursorTargetProject.nextElementSibling;
// walk up the node tree, when reaching the end of the backlink feature list,
// in order to find the next valid backlink item list
while(cursorTargetProject.nextElementSibling == null)
{
cursorTargetProject = projectAncestor(cursorTargetProject);
// check if we are walking around in circles (end of current item list)
if (cursorTargetProject.className && cursorTargetProject.className.includes('selected'))
{
// We've probably reached the very last visbile item. No need to continue.
return project
}
// make sure that we walk past upcoming SVG elements when working our way up the tree
while (cursorTargetProject.nextElementSibling != null && typeof (cursorTargetProject.nextElementSibling.className) !== 'string')
{
cursorTargetProject = cursorTargetProject.nextElementSibling;
}
}
while(!(cursorTargetProject.nextElementSibling
&& typeof cursorTargetProject.nextElementSibling.className.includes !== 'undefined'
&& cursorTargetProject.nextElementSibling.className.includes('project')
))
{
const ancestor = projectAncestor(cursorTargetProject)
// @TODO: this legacy fix prevents one from navigating to the next column in boards...
// It triggers when reaching the Add item element button at the end of WF.CurrentItem list.
// That plus button is at the bottom of every board column as well - which is why
// the cursor just stops when reaching the end of column list in a board. Proper
// fix is to check if we've reached the final element in the WF.CurrentItem list instead.
if (ancestor.className
//&& typeof ancestor.className.includes !== 'undefined'
//&& ancestor.className.includes('mainTreeRoot'))
&& ancestor.className.includes('selected'))
{
return project
}
cursorTargetProject = ancestor
}
return cursorTargetProject.nextElementSibling
}
const moveCursorUp = t =>
{
const project = projectAncestor(t)
let cursorTarget = null
if (project.previousElementSibling)
{
cursorTarget = project.previousElementSibling
/* Walk past SVGs.
The HTML structure might look like this:
project1
project2 <--- We want to end up here
SVG
project3 (backlink) <--- We are here
Project4 (backlink)
*/
// (this works, but the while loop is not necessary?)
// while (typeof(cursorTarget.className) !== 'string')
// {
// if(cursorTarget.previousElementSibling == null)
// break;
// cursorTarget = cursorTarget.previousElementSibling;
// }
if (typeof(cursorTarget.className) !== 'string')
cursorTarget = cursorTarget.previousElementSibling;
if (cursorTarget != null && cursorTarget.className.includes('open'))
{
const textContainers = cursorTarget.querySelectorAll('.project')
// need to guard against the case of a sibling with hidden items in it
if(textContainers[textContainers.length - 1] !== undefined)
{
cursorTarget = textContainers[textContainers.length - 1]
}
}
}
if (!cursorTarget)
cursorTarget = projectAncestor(project)
/* return cursorTarget.className.includes('mainTreeRoot')
? project
: cursorTarget */
if(!cursorTarget.className
//|| typeof cursorTarget.className.includes === 'undefined'
//|| cursorTarget.className.includes('mainTreeRoot')
)
{
return project
}
else
{
return cursorTarget
}
}
const setCursorAt = (offset) =>
{
if(isNaN(offset))
return;
const selection = document.getSelection()
const {anchorOffset, baseNode} = selection
let effectiveOffset = offset
if (typeof offset === 'function') {
effectiveOffset = offset(anchorOffset, baseNode)
}
let baseNodeLen = baseNode.length !== undefined ? baseNode.length - 1 : -1;
effectiveOffset = Math.min(effectiveOffset, baseNodeLen);
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 moveCursorTo = (target, calculateOffset, desiredOffset) =>
{
if(isNaN(desiredOffset))
return;
const contentAbstraction = getContentAbstraction(target);
const offset = calculateOffset(contentAbstraction, () => desiredOffset);
contentAbstraction.setCursorAt(offset);
}
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)
if(contentAbstraction)
{
const offset = calculateOffset(contentAbstraction, o => o - 1)
contentAbstraction.setCursorAt(offset)
}
}
const moveCursorRight = (target, calculateOffset) => {
const contentAbstraction = getContentAbstraction(target)
if(contentAbstraction)
{
const offset = calculateOffset(contentAbstraction, o => o + 1)
contentAbstraction.setCursorAt(offset)
}
}
if (typeof module !== 'undefined') {
module.exports = {
moveCursorLeft,
moveCursorRight,
moveCursorDown,
moveCursorUp
}
}