-
Notifications
You must be signed in to change notification settings - Fork 433
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
Extract Morph
class, then use it in FrameRenderer
#1029
Closed
seanpdoyle
wants to merge
5
commits into
hotwired:morph-refreshes
from
seanpdoyle:morph-refreshes-morph-renderer
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f5edb5e
Page refreshes
afcapel 3f45254
Introduce refresh attribute for frame element
afcapel a653d12
Use dispatch util function in MorphRenderer
afcapel 2fb0190
[IGNORE]: extracted in https://github.com/hotwired/turbo/pull/1028
seanpdoyle 2fd95cf
Extract `Morph` class, then use it in `FrameRenderer`
seanpdoyle 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 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,15 @@ | ||
export class LimitedSet extends Set { | ||
constructor(maxSize) { | ||
super() | ||
this.maxSize = maxSize | ||
} | ||
|
||
add(value) { | ||
if (this.size >= this.maxSize) { | ||
const iterator = this.values() | ||
const oldestValue = iterator.next().value | ||
this.delete(oldestValue) | ||
} | ||
super.add(value) | ||
} | ||
} |
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,20 @@ | ||
import { Renderer } from "../renderer" | ||
import { Morph } from "../morph" | ||
|
||
export class MorphRenderer extends Renderer { | ||
static renderElement(currentElement, newElement) { | ||
MorphRenderer.morph(currentElement, newElement) | ||
} | ||
|
||
static morph(currentElement, newElement) { | ||
Morph.render(currentElement, newElement) | ||
} | ||
|
||
async render() { | ||
if (this.willRender) this.renderElement(this.currentElement, this.newElement) | ||
} | ||
|
||
get renderMethod() { | ||
return "morph" | ||
} | ||
} |
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
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
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,54 @@ | ||
import "idiomorph" | ||
import { nextAnimationFrame } from "../util" | ||
|
||
export class Morph { | ||
static render(currentElement, newElement, morphStyle) { | ||
const morph = new this(currentElement, newElement) | ||
|
||
morph.render(morphStyle) | ||
} | ||
|
||
constructor(currentElement, newElement) { | ||
this.currentElement = currentElement | ||
this.newElement = newElement | ||
} | ||
|
||
render(morphStyle = "outerHTML") { | ||
window.Idiomorph.morph(this.currentElement, this.newElement, { | ||
ignoreActiveValue: true, | ||
morphStyle: morphStyle, | ||
callbacks: { | ||
beforeNodeMorphed: shouldMorphElement, | ||
beforeNodeRemoved: shouldRemoveElement, | ||
afterNodeMorphed: reloadStimulusControllers | ||
} | ||
}) | ||
|
||
this.#remoteFrames.forEach((frame) => frame.reload()) | ||
} | ||
|
||
get #remoteFrames() { | ||
return this.currentElement.querySelectorAll("turbo-frame[src]") | ||
} | ||
} | ||
|
||
function shouldRemoveElement(node) { | ||
return shouldMorphElement(node) | ||
} | ||
|
||
function shouldMorphElement(node) { | ||
if (node instanceof HTMLElement) { | ||
return !node.hasAttribute("data-turbo-permanent") | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
async function reloadStimulusControllers(node) { | ||
if (node instanceof HTMLElement && node.hasAttribute("data-controller")) { | ||
const originalAttribute = node.getAttribute("data-controller") | ||
node.removeAttribute("data-controller") | ||
await nextAnimationFrame() | ||
node.setAttribute("data-controller", originalAttribute) | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
The ignoreActiveValue: option was introduced in
0.0.9
, and feels important enough to include as a default for the sake of focus preservation and to minimize disruptions to the element with focus.Progressing this branch forward hinges on bigskysoftware/idiomorph#12 being merged. Without it, the package needs to depend on
window.Idiomorph
.