-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathstream_message_renderer.js
98 lines (76 loc) · 3.08 KB
/
stream_message_renderer.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
import { Bardo } from "../bardo"
import { getPermanentElementById, queryPermanentElementsAll } from "../snapshot"
import { around, elementIsFocusable, nextRepaint, queryAutofocusableElement, uuid } from "../../util"
export class StreamMessageRenderer {
render({ fragment }) {
Bardo.preservingPermanentElements(this, getPermanentElementMapForFragment(fragment), () => {
withAutofocusFromFragment(fragment, () => {
withPreservedFocus(() => {
document.documentElement.appendChild(fragment)
})
})
})
}
// Bardo delegate
enteringBardo(currentPermanentElement, newPermanentElement) {
newPermanentElement.replaceWith(currentPermanentElement.cloneNode(true))
}
leavingBardo() {}
}
function getPermanentElementMapForFragment(fragment) {
const permanentElementsInDocument = queryPermanentElementsAll(document.documentElement)
const permanentElementMap = {}
for (const permanentElementInDocument of permanentElementsInDocument) {
const { id } = permanentElementInDocument
for (const streamElement of fragment.querySelectorAll("turbo-stream")) {
const elementInStream = getPermanentElementById(streamElement.templateElement.content, id)
if (elementInStream) {
permanentElementMap[id] = [permanentElementInDocument, elementInStream]
}
}
}
return permanentElementMap
}
async function withAutofocusFromFragment(fragment, callback) {
const generatedID = `turbo-stream-autofocus-${uuid()}`
const turboStreams = fragment.querySelectorAll("turbo-stream")
const elementWithAutofocus = firstAutofocusableElementInStreams(turboStreams)
let willAutofocusId = null
if (elementWithAutofocus) {
if (elementWithAutofocus.id) {
willAutofocusId = elementWithAutofocus.id
} else {
willAutofocusId = generatedID
}
elementWithAutofocus.id = willAutofocusId
}
callback()
await nextRepaint()
const hasNoActiveElement = document.activeElement == null || document.activeElement == document.body
if (hasNoActiveElement && willAutofocusId) {
const elementToAutofocus = document.getElementById(willAutofocusId)
if (elementIsFocusable(elementToAutofocus)) {
elementToAutofocus.focus()
}
if (elementToAutofocus && elementToAutofocus.id == generatedID) {
elementToAutofocus.removeAttribute("id")
}
}
}
async function withPreservedFocus(callback) {
const [activeElementBeforeRender, activeElementAfterRender] = await around(callback, () => document.activeElement)
const restoreFocusTo = activeElementBeforeRender && activeElementBeforeRender.id
if (restoreFocusTo) {
const elementToFocus = document.getElementById(restoreFocusTo)
if (elementIsFocusable(elementToFocus) && elementToFocus != activeElementAfterRender) {
elementToFocus.focus()
}
}
}
function firstAutofocusableElementInStreams(nodeListOfStreamElements) {
for (const streamElement of nodeListOfStreamElements) {
const elementWithAutofocus = queryAutofocusableElement(streamElement.templateElement.content)
if (elementWithAutofocus) return elementWithAutofocus
}
return null
}