Skip to content

Commit e5a187c

Browse files
committed
fix last bugs
1 parent 3bce29e commit e5a187c

File tree

2 files changed

+87
-55
lines changed

2 files changed

+87
-55
lines changed

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 71 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof win
592592
let insertionPoint = oldParent.firstChild;
593593
let newChild;
594594

595+
newParent.children;
596+
oldParent.children;
597+
595598
// run through all the new content
596599
while (nextNewChild) {
597600

@@ -1205,68 +1208,82 @@ function executeMorphdom(rootFromElement, rootToElement, modifiedFieldElements,
12051208
childComponents.forEach((childComponent) => {
12061209
childComponentMap.set(childComponent.element, childComponent);
12071210
});
1208-
Idiomorph.morph(rootFromElement, rootToElement, {
1209-
beforeNodeMorphed: (fromEl, toEl) => {
1210-
if (!(fromEl instanceof Element) || !(toEl instanceof Element)) {
1211-
return true;
1212-
}
1213-
if (fromEl === rootFromElement) {
1214-
return true;
1215-
}
1216-
if (fromEl.hasAttribute('data-live-id')) {
1217-
if (fromEl.getAttribute('data-live-id') !== toEl.getAttribute('data-live-id')) {
1218-
for (const child of fromEl.children) {
1219-
child.setAttribute('parent-live-id-changed', '');
1211+
Idiomorph.morph(rootFromElement, rootToElement, { callbacks: {
1212+
beforeNodeMorphed: (fromEl, toEl) => {
1213+
if (!(fromEl instanceof Element) || !(toEl instanceof Element)) {
1214+
return true;
1215+
}
1216+
if (fromEl === rootFromElement) {
1217+
return true;
1218+
}
1219+
let idChanged = false;
1220+
if (fromEl.hasAttribute('data-live-id')) {
1221+
if (fromEl.getAttribute('data-live-id') !== toEl.getAttribute('data-live-id')) {
1222+
for (const child of fromEl.children) {
1223+
child.setAttribute('parent-live-id-changed', '');
1224+
}
1225+
idChanged = true;
12201226
}
12211227
}
1222-
}
1223-
if (fromEl instanceof HTMLElement && toEl instanceof HTMLElement) {
1224-
if (typeof fromEl.__x !== 'undefined') {
1225-
if (!window.Alpine) {
1226-
throw new Error('Unable to access Alpine.js though the global window.Alpine variable. Please make sure Alpine.js is loaded before Symfony UX LiveComponent.');
1228+
if (fromEl instanceof HTMLElement && toEl instanceof HTMLElement) {
1229+
if (typeof fromEl.__x !== 'undefined') {
1230+
if (!window.Alpine) {
1231+
throw new Error('Unable to access Alpine.js though the global window.Alpine variable. Please make sure Alpine.js is loaded before Symfony UX LiveComponent.');
1232+
}
1233+
if (typeof window.Alpine.morph !== 'function') {
1234+
throw new Error('Unable to access Alpine.js morph function. Please make sure the Alpine.js Morph plugin is installed and loaded, see https://alpinejs.dev/plugins/morph for more information.');
1235+
}
1236+
window.Alpine.morph(fromEl.__x, toEl);
12271237
}
1228-
if (typeof window.Alpine.morph !== 'function') {
1229-
throw new Error('Unable to access Alpine.js morph function. Please make sure the Alpine.js Morph plugin is installed and loaded, see https://alpinejs.dev/plugins/morph for more information.');
1238+
if (childComponentMap.has(fromEl)) {
1239+
const childComponent = childComponentMap.get(fromEl);
1240+
return !childComponent.updateFromNewElementFromParentRender(toEl) && idChanged;
1241+
}
1242+
if (externalMutationTracker.wasElementAdded(fromEl)) {
1243+
fromEl.insertAdjacentElement('afterend', toEl);
1244+
return false;
1245+
}
1246+
if (modifiedFieldElements.includes(fromEl)) {
1247+
setValueOnElement(toEl, getElementValue(fromEl));
1248+
}
1249+
const elementChanges = externalMutationTracker.getChangedElement(fromEl);
1250+
if (elementChanges) {
1251+
elementChanges.applyToElement(toEl);
1252+
}
1253+
if (fromEl.nodeName.toUpperCase() !== 'OPTION' && fromEl.isEqualNode(toEl)) {
1254+
const normalizedFromEl = cloneHTMLElement(fromEl);
1255+
normalizeAttributesForComparison(normalizedFromEl);
1256+
const normalizedToEl = cloneHTMLElement(toEl);
1257+
normalizeAttributesForComparison(normalizedToEl);
1258+
if (normalizedFromEl.isEqualNode(normalizedToEl)) {
1259+
return false;
1260+
}
12301261
}
1231-
window.Alpine.morph(fromEl.__x, toEl);
1232-
}
1233-
if (childComponentMap.has(fromEl)) {
1234-
const childComponent = childComponentMap.get(fromEl);
1235-
childComponent.updateFromNewElementFromParentRender(toEl);
1236-
return false;
12371262
}
1238-
if (modifiedFieldElements.includes(fromEl)) {
1239-
setValueOnElement(toEl, getElementValue(fromEl));
1263+
if (fromEl.hasAttribute('parent-live-id-changed')) {
1264+
fromEl.removeAttribute('parent-live-id-changed');
1265+
return true;
12401266
}
1241-
const elementChanges = externalMutationTracker.getChangedElement(fromEl);
1242-
if (elementChanges) {
1243-
elementChanges.applyToElement(toEl);
1267+
return !fromEl.hasAttribute('data-live-ignore');
1268+
},
1269+
beforeNodeRemoved(node) {
1270+
if (!(node instanceof HTMLElement)) {
1271+
return true;
12441272
}
1245-
if (fromEl.nodeName.toUpperCase() !== 'OPTION' && fromEl.isEqualNode(toEl)) {
1246-
const normalizedFromEl = cloneHTMLElement(fromEl);
1247-
normalizeAttributesForComparison(normalizedFromEl);
1248-
const normalizedToEl = cloneHTMLElement(toEl);
1249-
normalizeAttributesForComparison(normalizedToEl);
1250-
if (normalizedFromEl.isEqualNode(normalizedToEl)) {
1251-
return false;
1252-
}
1273+
if (externalMutationTracker.wasElementAdded(node)) {
1274+
return false;
12531275
}
1254-
}
1255-
if (fromEl.hasAttribute('parent-live-id-changed')) {
1256-
fromEl.removeAttribute('parent-live-id-changed');
1257-
return true;
1258-
}
1259-
return !fromEl.hasAttribute('data-live-ignore');
1260-
},
1261-
beforeNodeRemoved(node) {
1262-
if (!(node instanceof HTMLElement)) {
1263-
return true;
1264-
}
1265-
if (externalMutationTracker.wasElementAdded(node)) {
1266-
return false;
1267-
}
1268-
return !node.hasAttribute('data-live-ignore');
1269-
},
1276+
return !node.hasAttribute('data-live-ignore');
1277+
},
1278+
} });
1279+
childComponentMap.forEach((childComponent, element) => {
1280+
var _a;
1281+
const childComponentInResult = findChildComponent((_a = childComponent.id) !== null && _a !== void 0 ? _a : '', rootFromElement);
1282+
if (null === childComponentInResult || element === childComponentInResult) {
1283+
return;
1284+
}
1285+
childComponentInResult === null || childComponentInResult === void 0 ? void 0 : childComponentInResult.replaceWith(element);
1286+
childComponent.updateFromNewElementFromParentRender(childComponentInResult);
12701287
});
12711288
}
12721289

src/LiveComponent/assets/src/morphdom.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ export function executeMorphdom(
3030
return true;
3131
}
3232

33+
let idChanged = false;
3334
// Track children if data-live-id changed
3435
if (fromEl.hasAttribute('data-live-id')) {
3536
if (fromEl.getAttribute('data-live-id') !== toEl.getAttribute('data-live-id')) {
3637
for (const child of fromEl.children) {
3738
child.setAttribute('parent-live-id-changed', '')
3839
}
40+
idChanged = true;
3941
}
4042
}
4143

@@ -63,8 +65,11 @@ export function executeMorphdom(
6365
if (childComponentMap.has(fromEl)) {
6466
const childComponent = childComponentMap.get(fromEl) as Component;
6567

66-
childComponent.updateFromNewElementFromParentRender(toEl);
68+
return !childComponent.updateFromNewElementFromParentRender(toEl) && idChanged;
69+
}
6770

71+
if (externalMutationTracker.wasElementAdded(fromEl)) {
72+
fromEl.insertAdjacentElement('afterend', toEl);
6873
return false;
6974
}
7075

@@ -124,4 +129,14 @@ export function executeMorphdom(
124129
return !node.hasAttribute('data-live-ignore');
125130
},
126131
}});
132+
133+
childComponentMap.forEach((childComponent, element) => {
134+
const childComponentInResult = findChildComponent(childComponent.id ?? '', rootFromElement);
135+
if (null === childComponentInResult || element === childComponentInResult) {
136+
return;
137+
}
138+
139+
childComponentInResult?.replaceWith(element);
140+
childComponent.updateFromNewElementFromParentRender(childComponentInResult);
141+
});
127142
}

0 commit comments

Comments
 (0)