-
Couldn't load subscription status.
- Fork 472
Consolidate morphing functions into core/morphing
#1438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seanpdoyle
wants to merge
1
commit into
main
Choose a base branch
from
consolidate-morphing-logic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−72
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,32 +1,12 @@ | ||
| import { FrameRenderer } from "./frame_renderer" | ||
| import { morphChildren, shouldRefreshFrameWithMorphing, closestFrameReloadableWithMorphing } from "../morphing" | ||
| import { dispatch } from "../../util" | ||
| import { morphTurboFrameElements } from "../morphing" | ||
|
|
||
| export class MorphingFrameRenderer extends FrameRenderer { | ||
| static renderElement(currentElement, newElement) { | ||
| dispatch("turbo:before-frame-morph", { | ||
| target: currentElement, | ||
| detail: { currentElement, newElement } | ||
| }) | ||
|
|
||
| morphChildren(currentElement, newElement, { | ||
| callbacks: { | ||
| beforeNodeMorphed: (node, newNode) => { | ||
| if ( | ||
| shouldRefreshFrameWithMorphing(node, newNode) && | ||
| closestFrameReloadableWithMorphing(node) === currentElement | ||
| ) { | ||
| node.reload() | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
| } | ||
| }) | ||
| static render(currentFrame, newFrame) { | ||
| morphTurboFrameElements(currentFrame, newFrame) | ||
| } | ||
|
|
||
| async preservingPermanentElements(callback) { | ||
| return await callback() | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,6 +33,65 @@ export function morphChildren(currentElement, newElement, options = {}) { | |
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Morph the state of the currentBody based on the attributes and contents of | ||
| * the newBody. Morphing body elements may dispatch turbo:morph, | ||
| * turbo:before-morph-element, turbo:before-morph-attribute, and | ||
| * turbo:morph-element events. | ||
| * | ||
| * @param currentBody HTMLBodyElement destination of morphing changes | ||
| * @param newBody HTMLBodyElement source of morphing changes | ||
| */ | ||
| export function morphBodyElements(currentElement, newElement) { | ||
| morphElements(currentElement, newElement, { | ||
| callbacks: { | ||
| beforeNodeMorphed: (node, newNode) => { | ||
| if ( | ||
| shouldRefreshFrameWithMorphing(node, newNode) && | ||
| !closestFrameReloadableWithMorphing(node) | ||
| ) { | ||
| node.reload() | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| dispatch("turbo:morph", { detail: { currentElement, newElement } }) | ||
| } | ||
|
|
||
| /** | ||
| * Morph the child elements of the currentFrame based on the child elements of | ||
| * the newFrame. Morphing turbo-frame elements may dispatch turbo:before-frame-morph, | ||
| * turbo:before-morph-element, turbo:before-morph-attribute, and | ||
| * turbo:morph-element events. | ||
| * | ||
| * @param currentFrame FrameElement destination of morphing children changes | ||
| * @param newFrame FrameElement source of morphing children changes | ||
| */ | ||
| export function morphTurboFrameElements(currentElement, newElement) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about adding |
||
| dispatch("turbo:before-frame-morph", { | ||
| target: currentElement, | ||
| detail: { currentElement, newElement } | ||
| }) | ||
|
|
||
| morphChildren(currentElement, newElement, { | ||
| callbacks: { | ||
| beforeNodeMorphed: (node, newNode) => { | ||
| if ( | ||
| shouldRefreshFrameWithMorphing(node, newNode) && | ||
| closestFrameReloadableWithMorphing(node) === currentElement | ||
| ) { | ||
| node.reload() | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| export function shouldRefreshFrameWithMorphing(currentFrame, newFrame) { | ||
| return currentFrame instanceof FrameElement && | ||
| // newFrame cannot yet be an instance of FrameElement because custom | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% convinced about this name. You might want to use this method in something like
turbo:before-renderto replace something likediv#appinstead of body. But it's true that this event also gives younewBodyas a parameter. I feel like the difference betweenmorphBodyElementsandmorphElementsis very subtle. One is simply lower level than the other, but we expose them at the same level.What do you think if we rename this to just
morph, so it becomesTurbo.morph(currentElement, newElement, { action: "replace|update", refreshFrames: true|false }), and leavemorphElementsas private? It'd be something similar toTurbo.visit(url, { frame: "id" }). Also, that way, we'd always dispatch theturbo:morphevent. Then themorphmethod of the Turbo stream actions could also use that method. Perhaps the names of the option params could be improved, but they come pretty close to the idea of Turbo stream actions.