Skip to content

Commit 49b474c

Browse files
committed
wip
1 parent e363181 commit 49b474c

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

packages/morph/src/morph.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
let resolveStep = () => {}
32

43
let logger = () => {}
@@ -40,8 +39,8 @@ export function morph(from, toHtml, options) {
4039
// hook to change. For example, when it was `shouldSkip()` the signature was `updating: (el, toEl, childrenOnly, skip)`. But if
4140
// we append `skipChildren()`, it would make the signature `updating: (el, toEl, childrenOnly, skipChildren, skip)`. This is
4241
// a breaking change due to how the `shouldSkip()` function is structured.
43-
//
44-
// So we're using `shouldSkipChildren()` instead which doesn't have this problem as it allows us to pass in the `skipChildren()`
42+
//
43+
// So we're using `shouldSkipChildren()` instead which doesn't have this problem as it allows us to pass in the `skipChildren()`
4544
// function as an earlier parameter and then append it to the `updating` hook signature manually. The signature of `updating`
4645
// hook is now `updating: (el, toEl, childrenOnly, skip, skipChildren)`.
4746
if (shouldSkipChildren(updating, () => skipChildren = true, from, to, () => updateChildrenOnly = true)) return
@@ -399,7 +398,7 @@ function shouldSkip(hook, ...args) {
399398

400399
// Due to the structure of the `shouldSkip()` function, we can't pass in the `skipChildren`
401400
// function as an argument as it would change the signature of the existing hooks. So we
402-
// are using this function instead which doesn't have this problem as we can pass the
401+
// are using this function instead which doesn't have this problem as we can pass the
403402
// `skipChildren` function in as an earlier argument and then append it to the end
404403
// of the hook signature manually.
405404
function shouldSkipChildren(hook, skipChildren, ...args) {
@@ -526,3 +525,28 @@ function seedingMatchingId(to, from) {
526525
to.setAttribute('id', fromId)
527526
to.id = fromId
528527
}
528+
529+
export function morphBetween(startMarker, endMarker, toHtml, options = {}) {
530+
// Create a temporary container for the new content
531+
let tempContainer = document.createElement('div')
532+
tempContainer.innerHTML = toHtml
533+
534+
// Create a wrapper element to hold the current DOM content between markers
535+
let fromWrapper = document.createElement('div')
536+
537+
// Move all nodes between markers into the wrapper (not clones, actual nodes)
538+
let current = startMarker.nextSibling
539+
while (current && current !== endMarker) {
540+
let next = current.nextSibling
541+
fromWrapper.appendChild(current)
542+
current = next
543+
}
544+
545+
// Morph the wrapper with the new content
546+
morph(fromWrapper, tempContainer, options)
547+
548+
// Move the morphed content back between the markers
549+
while (fromWrapper.firstChild) {
550+
endMarker.parentNode.insertBefore(fromWrapper.firstChild, endMarker)
551+
}
552+
}

0 commit comments

Comments
 (0)