-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
roam.ts
166 lines (137 loc) · 4.94 KB
/
roam.ts
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
import {Selectors} from 'src/core/roam/selectors'
import {assumeExists} from 'src/core/common/assert'
import {RoamNode, Selection} from './roam-node'
import {getActiveEditElement, getFirstTopLevelBlock, getInputEvent, getLastTopLevelBlock} from '../common/dom'
import {Keyboard} from '../common/keyboard'
import {Mouse} from '../common/mouse'
import {delay} from 'src/core/common/async'
export const Roam = {
async save(roamNode: RoamNode): Promise<void> {
const roamElement = this.getRoamBlockInput()
if (roamElement) {
console.log(`Saving`, roamNode)
roamElement.value = roamNode.text
roamElement.dispatchEvent(getInputEvent())
// Need to select afterwards, otherwise the input event resets the cursor
await delay(1)
roamElement.setSelectionRange(roamNode.selection.start, roamNode.selection.end)
}
},
getRoamBlockInput(): HTMLTextAreaElement | null {
const element = getActiveEditElement()
if (element?.tagName.toLocaleLowerCase() !== 'textarea') {
return null
}
return element as HTMLTextAreaElement
},
getActiveRoamNode(): RoamNode | null {
const element = this.getRoamBlockInput()
if (!element) return null
return new RoamNode(element.value, new Selection(element.selectionStart, element.selectionEnd))
},
async applyToCurrent(action: (node: RoamNode) => RoamNode) {
const node = this.getActiveRoamNode()
if (!node) return
await this.save(action(node))
},
async highlight(element?: HTMLElement) {
if (element) {
await this.activateBlock(element)
}
if (this.getRoamBlockInput()) {
return Keyboard.pressEsc()
} else {
return Promise.reject("We're not inside a block")
}
},
async activateBlock(element: HTMLElement) {
if (element.classList.contains('roam-block')) {
await Mouse.leftClick(element)
}
return this.getRoamBlockInput()
},
async deleteBlock() {
return this.highlight().then(() => Keyboard.pressBackspace())
},
async copyBlock() {
await this.highlight()
document.execCommand('copy')
},
async duplicateBlock() {
await this.copyBlock()
await Keyboard.pressEnter()
await Keyboard.pressEnter()
document.execCommand('paste')
},
async moveCursorToStart() {
await this.applyToCurrent(node => node.withCursorAtTheStart())
},
async moveCursorToEnd() {
await this.applyToCurrent(node => node.withCursorAtTheEnd())
},
async moveCursorToSearchTerm(searchTerm: string) {
await this.applyToCurrent(node => node.withCursorAtSearchTerm(searchTerm))
},
writeText(text: string) {
this.applyToCurrent(node => new RoamNode(text, node.selection))
return this.getActiveRoamNode()?.text === text
},
appendText(text: string) {
const existingText = this.getActiveRoamNode()?.text || ''
return this.writeText(existingText + text)
},
async createSiblingAbove() {
await this.moveCursorToStart()
const isEmpty = !this.getActiveRoamNode()?.text
await Keyboard.pressEnter()
if (isEmpty) {
await Keyboard.simulateKey(Keyboard.UP_ARROW)
}
},
async createBlockBelow() {
await this.moveCursorToEnd()
await Keyboard.pressEnter()
},
async createSiblingBelow() {
await this.createBlockBelow()
await Keyboard.pressShiftTab(Keyboard.BASE_DELAY)
},
async createFirstChild() {
await this.moveCursorToEnd()
await Keyboard.pressEnter()
await Keyboard.pressTab()
},
async createLastChild() {
await this.createSiblingBelow()
await Keyboard.pressTab()
},
async createDeepestLastDescendant() {
await this.highlight()
await Keyboard.simulateKey(Keyboard.RIGHT_ARROW)
await Keyboard.pressEnter()
},
async createBlockAtTop(forceCreation: boolean = false) {
await this.activateBlock(getFirstTopLevelBlock())
if (this.getActiveRoamNode()?.text || forceCreation) {
await this.createSiblingAbove()
}
},
async createBlockAtBottom(forceCreation: boolean = false) {
await this.activateBlock(getLastTopLevelBlock())
if (this.getActiveRoamNode()?.text || forceCreation) {
await this.createSiblingBelow()
}
},
async toggleFoldBlock(block: HTMLElement) {
const foldButton = nearestFoldButton(block)
await Mouse.hover(foldButton)
await Mouse.leftClick(foldButton)
},
}
const nearestFoldButton = (element: HTMLElement): HTMLElement => {
const foldButton = element.querySelector(Selectors.foldButton) as HTMLElement
if (foldButton) {
return foldButton
}
return nearestFoldButton(assumeExists(element.parentElement))
}