-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1234 from seanpdoyle/extract-morphing
Extract and re-use element morphing logic
- Loading branch information
Showing
7 changed files
with
138 additions
and
193 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
import { FrameElement } from "../../elements/frame_element" | ||
import { MorphingFrameRenderer } from "../frames/morphing_frame_renderer" | ||
import { PageRenderer } from "./page_renderer" | ||
import { dispatch } from "../../util" | ||
import { morphElements } from "../morphing" | ||
|
||
export class MorphingPageRenderer extends PageRenderer { | ||
static renderElement(currentElement, newElement) { | ||
morphElements(currentElement, newElement, { | ||
callbacks: { | ||
beforeNodeMorphed: element => !canRefreshFrame(element) | ||
} | ||
}) | ||
|
||
for (const frame of currentElement.querySelectorAll("turbo-frame")) { | ||
if (canRefreshFrame(frame)) refreshFrame(frame) | ||
} | ||
|
||
dispatch("turbo:morph", { detail: { currentElement, newElement } }) | ||
} | ||
|
||
async preservingPermanentElements(callback) { | ||
return await callback() | ||
} | ||
|
||
get renderMethod() { | ||
return "morph" | ||
} | ||
|
||
get shouldAutofocus() { | ||
return false | ||
} | ||
} | ||
|
||
function canRefreshFrame(frame) { | ||
return frame instanceof FrameElement && | ||
frame.src && | ||
frame.refresh === "morph" && | ||
!frame.closest("[data-turbo-permanent]") | ||
} | ||
|
||
function refreshFrame(frame) { | ||
frame.addEventListener("turbo:before-frame-render", ({ detail }) => { | ||
detail.render = MorphingFrameRenderer.renderElement | ||
}, { once: true }) | ||
|
||
frame.reload() | ||
} |
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,14 @@ | ||
import { FrameRenderer } from "./frame_renderer" | ||
import { morphChildren } from "../morphing" | ||
import { dispatch } from "../../util" | ||
|
||
export class MorphingFrameRenderer extends FrameRenderer { | ||
static renderElement(currentElement, newElement) { | ||
dispatch("turbo:before-frame-morph", { | ||
target: currentElement, | ||
detail: { currentElement, newElement } | ||
}) | ||
|
||
morphChildren(currentElement, newElement) | ||
} | ||
} |
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,66 @@ | ||
import { Idiomorph } from "idiomorph/dist/idiomorph.esm.js" | ||
import { dispatch } from "../util" | ||
|
||
export function morphElements(currentElement, newElement, { callbacks, ...options } = {}) { | ||
Idiomorph.morph(currentElement, newElement, { | ||
...options, | ||
callbacks: new DefaultIdiomorphCallbacks(callbacks) | ||
}) | ||
} | ||
|
||
export function morphChildren(currentElement, newElement) { | ||
morphElements(currentElement, newElement.children, { | ||
morphStyle: "innerHTML" | ||
}) | ||
} | ||
|
||
class DefaultIdiomorphCallbacks { | ||
#beforeNodeMorphed | ||
|
||
constructor({ beforeNodeMorphed } = {}) { | ||
this.#beforeNodeMorphed = beforeNodeMorphed || (() => true) | ||
} | ||
|
||
beforeNodeAdded = (node) => { | ||
return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id)) | ||
} | ||
|
||
beforeNodeMorphed = (currentElement, newElement) => { | ||
if (currentElement instanceof Element) { | ||
if (!currentElement.hasAttribute("data-turbo-permanent") && this.#beforeNodeMorphed(currentElement, newElement)) { | ||
const event = dispatch("turbo:before-morph-element", { | ||
cancelable: true, | ||
target: currentElement, | ||
detail: { currentElement, newElement } | ||
}) | ||
|
||
return !event.defaultPrevented | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
beforeAttributeUpdated = (attributeName, target, mutationType) => { | ||
const event = dispatch("turbo:before-morph-attribute", { | ||
cancelable: true, | ||
target, | ||
detail: { attributeName, mutationType } | ||
}) | ||
|
||
return !event.defaultPrevented | ||
} | ||
|
||
beforeNodeRemoved = (node) => { | ||
return this.beforeNodeMorphed(node) | ||
} | ||
|
||
afterNodeMorphed = (currentElement, newElement) => { | ||
if (currentElement instanceof Element) { | ||
dispatch("turbo:morph-element", { | ||
target: currentElement, | ||
detail: { currentElement, newElement } | ||
}) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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