-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlivePreview.tsx
282 lines (246 loc) · 8.3 KB
/
livePreview.tsx
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
import {Feature, Settings} from '../settings'
import {Navigation} from '../roam/navigation'
import {createPopper, Instance} from '@popperjs/core'
import {delay} from '../common/async'
import {Selectors} from '../roam/selectors'
import {Browser} from '../common/browser'
export const config: Feature = {
id: 'live_preview',
name: 'Live Preview',
warning: 'Experimental feature; Large databases might see performance issues.',
enabledByDefault: false,
}
const checkSettingsAndSetupIframeToggle = () => {
Settings.isActive('live_preview').then(active => {
toggleIframe(active)
})
}
checkSettingsAndSetupIframeToggle()
Browser.addMessageListener(async message => {
if (message === 'settings-updated') {
checkSettingsAndSetupIframeToggle()
}
})
let iframeInstance: PreviewIframe | null = null
const toggleIframe = (active: boolean) => {
if (active) {
if (!iframeInstance) {
iframeInstance = new PreviewIframe()
iframeInstance.activate()
}
} else {
if (iframeInstance) {
iframeInstance.destroy()
iframeInstance = null
}
}
}
class PreviewIframe {
iframeId = 'roam-toolkit-iframe-preview'
iframe: HTMLIFrameElement
popupTimeout: ReturnType<typeof setTimeout> | null = null
hoveredElement: HTMLElement | null = null
popper: Instance | null = null
popupTimeoutDuration = 300
constructor() {
this.iframe = document.createElement('iframe')
}
activate() {
this.initPreviewIframe()
}
destroy() {
this.removeIframe()
this.clearPopupTimeout()
this.destroyPopper()
this.destroyMouseListeners()
}
private clearPopupTimeout() {
if (this.popupTimeout) {
clearTimeout(this.popupTimeout)
this.popupTimeout = null
}
}
private removeIframe() {
const isCurrentIframePresent = document.body.contains(this.iframe)
if (!isCurrentIframePresent) {
return
}
document.body.removeChild(this.iframe)
}
private initPreviewIframe() {
const url = Navigation.getDailyNotesUrl()
this.setupHiddenIframe(url)
this.addIframeToBody()
this.scrollToTopOnPageLoad()
this.attachMouseListeners()
}
private addIframeToBody() {
document.body.appendChild(this.iframe)
}
private attachMouseListeners() {
document.addEventListener('mouseover', this.mouseOverListener)
document.addEventListener('mouseout', this.mouseOutListener)
}
private destroyMouseListeners() {
document.removeEventListener('mouseover', this.mouseOverListener)
document.removeEventListener('mouseout', this.mouseOutListener)
}
private mouseOverListener = (e: Event) => {
const target = e.target as HTMLElement
const isPageRef = this.isTargetPageRef(target)
if (isPageRef) {
const text = this.getTargetInnerText(target)
this.hoveredElement = target
const url = Navigation.getPageUrlByName(text)
if (url) {
this.prepIframeForDisplay(url)
this.setTimerForPopup(target)
}
}
}
private mouseOutListener = (e: MouseEvent) => {
const nextTarget = e.relatedTarget as HTMLElement
if (this.shouldRemoveOnMouseOut(nextTarget)) {
this.hidePreview()
}
}
private hidePreview() {
this.hoveredElement = null
this.clearPopupTimeout()
this.resetIframeForNextHover()
this.destroyPopper()
}
private shouldRemoveOnMouseOut(nextTarget: HTMLElement) {
const isNotHoveringOverTarget = nextTarget !== this.hoveredElement
const isNotHoveringOverIframe = nextTarget !== this.iframe
return isNotHoveringOverIframe && isNotHoveringOverTarget
}
private setTimerForPopup(target: HTMLElement) {
if (!this.popupTimeout) {
this.popupTimeout = window.setTimeout(() => {
this.showPreview()
this.makePopper(target)
}, this.popupTimeoutDuration)
}
}
private getTargetInnerText(target: HTMLElement) {
// remove '#' for page tags
const isPageRefTag = this.isTargetPageRefTag(target)
return isPageRefTag ? target.innerText.slice(1) : target.innerText
}
private isTargetPageRefTag(target: HTMLElement) {
return target.classList.contains('rm-page-ref-tag')
}
private isTargetPageRef(target: HTMLElement) {
return target.classList.contains('rm-page-ref')
}
private destroyPopper() {
if (this.popper) {
this.popper.destroy()
this.popper = null
}
}
private resetIframeForNextHover() {
this.scrollIframeToTopOnMouseOut()
this.iframe.style.pointerEvents = 'none'
this.iframe.style.opacity = '0'
this.iframe.style.height = '0'
this.iframe.style.width = '0'
}
private showPreview() {
this.iframe.style.opacity = '1'
this.iframe.style.pointerEvents = 'all'
}
private async prepIframeForDisplay(url: string) {
if (!(await this.stillHoveringOverSameObjectAfterDelay())) return
// this pre-loads the iframe, (which is shown after a delay)
this.iframe.src = url
this.iframe.style.height = '500px'
this.iframe.style.width = '500px'
this.iframe.style.pointerEvents = 'none'
}
private async stillHoveringOverSameObjectAfterDelay(millis: number = 100) {
const hoverTargetAtCallTime = this.hoveredElement
await delay(millis)
return hoverTargetAtCallTime === this.hoveredElement
}
private makePopper(target: HTMLElement) {
this.popper = createPopper(target, this.iframe, {
placement: 'right',
modifiers: [
{
name: 'preventOverflow',
options: {
padding: {top: 48},
},
},
{
name: 'flip',
options: {
boundary: document.querySelector('#app'),
},
},
],
})
}
private setupHiddenIframe = (url: string) => {
this.iframe.src = url
this.iframe.style.position = 'absolute'
this.iframe.style.left = '0'
this.iframe.style.top = '0'
this.iframe.style.opacity = '0'
this.iframe.style.pointerEvents = 'none'
this.iframe.style.height = '0'
this.iframe.style.width = '0'
this.iframe.style.border = '0'
this.iframe.style.boxShadow = '0 0 4px 5px rgba(0, 0, 0, 0.2)'
this.iframe.style.borderRadius = '4px'
this.iframe.id = this.iframeId
this.appendStylesToIFrameOnLoad()
}
private appendStylesToIFrameOnLoad = () => {
const styleNode = document.createElement('style')
styleNode.innerHTML = `
.roam-topbar {
display: none !important;
}
${Selectors.mainBody} {
top: 0px !important;
}
#buffer {
display: none !important;
}
iframe {
display: none !important;
}
`
this.iframe.onload = (event: Event) => {
;(event.target as HTMLIFrameElement).contentDocument?.body.appendChild(styleNode)
}
}
/**
* Sets the iframe's scrollTop to 0,
* so the next popup is in the correct scroll position
*/
private scrollIframeToTopOnMouseOut() {
if (this.iframe.contentDocument) {
const scrollContainer = this.iframe.contentDocument.querySelector('.roam-center > div')
this.scrollToTopForElement(scrollContainer)
}
}
/**
* HACK: to reset scroll after adding iframe to DOM.
* Since the `overflow` is not set to `hidden` for the HTML tag,
* on adding iframe, the body scrolls down,
* causing the loader to not be centerd.
* This fixes it by setting the scrollTop to 0
*/
private scrollToTopOnPageLoad = () => {
this.scrollToTopForElement(document.querySelector('html'))
}
private scrollToTopForElement = (element: Element | null) => {
if (element) {
element.scrollTop = 0
}
}
}