|
1 |
| - |
2 | 1 | let resolveStep = () => {}
|
3 | 2 |
|
4 | 3 | let logger = () => {}
|
@@ -40,8 +39,8 @@ export function morph(from, toHtml, options) {
|
40 | 39 | // hook to change. For example, when it was `shouldSkip()` the signature was `updating: (el, toEl, childrenOnly, skip)`. But if
|
41 | 40 | // we append `skipChildren()`, it would make the signature `updating: (el, toEl, childrenOnly, skipChildren, skip)`. This is
|
42 | 41 | // 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()` |
45 | 44 | // function as an earlier parameter and then append it to the `updating` hook signature manually. The signature of `updating`
|
46 | 45 | // hook is now `updating: (el, toEl, childrenOnly, skip, skipChildren)`.
|
47 | 46 | if (shouldSkipChildren(updating, () => skipChildren = true, from, to, () => updateChildrenOnly = true)) return
|
@@ -399,7 +398,7 @@ function shouldSkip(hook, ...args) {
|
399 | 398 |
|
400 | 399 | // Due to the structure of the `shouldSkip()` function, we can't pass in the `skipChildren`
|
401 | 400 | // 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 |
403 | 402 | // `skipChildren` function in as an earlier argument and then append it to the end
|
404 | 403 | // of the hook signature manually.
|
405 | 404 | function shouldSkipChildren(hook, skipChildren, ...args) {
|
@@ -526,3 +525,28 @@ function seedingMatchingId(to, from) {
|
526 | 525 | to.setAttribute('id', fromId)
|
527 | 526 | to.id = fromId
|
528 | 527 | }
|
| 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