forked from hotwired/turbo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turbo Streams: Preserve permanent elements
Closes hotwired#623 Refactor the `Snapshot` implementation to make the permanent element finding code re-usable outside that module. Then, introduce the `StreamMessageRenderer` class, and re-use that code. The `StreamMessageRenderer` class also implements `BardoDelegate`, and relies on `Bardo.preservingPermanentElements` to manage elements across their `<turbo-stream>` rendering lifespan.
- Loading branch information
1 parent
495de2e
commit 4a86bbe
Showing
4 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { StreamMessage } from "./stream_message" | ||
import { StreamElement } from "../../elements/stream_element" | ||
import { Bardo, BardoDelegate } from "../bardo" | ||
import { PermanentElementMap, getPermanentElementById, queryPermanentElementsAll } from "../snapshot" | ||
|
||
export class StreamMessageRenderer implements BardoDelegate { | ||
render({ fragment }: StreamMessage) { | ||
Bardo.preservingPermanentElements(this, getPermanentElementMapForFragment(fragment), () => | ||
document.documentElement.appendChild(fragment) | ||
) | ||
} | ||
|
||
enteringBardo(currentPermanentElement: Element, newPermanentElement: Element) { | ||
newPermanentElement.replaceWith(currentPermanentElement.cloneNode(true)) | ||
} | ||
|
||
leavingBardo() {} | ||
} | ||
|
||
function getPermanentElementMapForFragment(fragment: DocumentFragment): PermanentElementMap { | ||
const permanentElementsInDocument = queryPermanentElementsAll(document.documentElement) | ||
const permanentElementMap: PermanentElementMap = {} | ||
for (const permanentElementInDocument of permanentElementsInDocument) { | ||
const { id } = permanentElementInDocument | ||
|
||
for (const streamElement of fragment.querySelectorAll<StreamElement>("turbo-stream")) { | ||
const elementInStream = getPermanentElementById(streamElement.templateElement.content, id) | ||
|
||
if (elementInStream) { | ||
permanentElementMap[id] = [permanentElementInDocument, elementInStream] | ||
} | ||
} | ||
} | ||
|
||
return permanentElementMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters