Skip to content

Commit 75df4ff

Browse files
committed
fix rendering of multiple iterable child elements
1 parent 39bff54 commit 75df4ff

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

datex-bindings/dom-utils.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,28 +219,39 @@ export class DOMUtils {
219219

220220
const Comment = this.context.Comment;
221221

222+
const getAbsoluteOffset = (k: number) => {
223+
const startOffset = Array.from(parent.childNodes).indexOf(startAnchor);
224+
return startOffset + 1 + k;
225+
};
222226
const iterableHandler = new IterableHandler<appendableContent, Node>(children as appendableContent[], {
223227
map: (v,k) => {
224228
const {node: el} = this.valueToDOMNode(v);
225229
return el;
226230
},
227-
onEntryRemoved: (v,k) => {
231+
onEntryRemoved: (v, relative_k) => {
228232
// remove kth child
229-
const node = parent.childNodes[k+1];
233+
const k = getAbsoluteOffset(relative_k);
234+
const node = parent.childNodes[k];
230235
// out of bounds
231236
if (!node || node === endAnchor || node === startAnchor){
232237
return;
233238
}
234239
node.remove();
235240
},
236-
onNewEntry(v,k,p) {
241+
onNewEntry(v, relative_k, p) {
237242
// if kth child exists, replace, otherwise append at end
238-
const current = parent.childNodes[k+1];
243+
const k = getAbsoluteOffset(relative_k);
244+
const current = parent.childNodes[k];
239245
if (current && current != endAnchor) parent.replaceChild(v, current);
240246
else {
241247
// fill gap if k is larger than current children
242-
if (k > parent.childNodes.length-2) {
243-
for (let i = parent.childNodes.length-2; i < k; i++) {
248+
const childrenArray = Array.from(parent.childNodes);
249+
const startAnchorIndex = childrenArray.indexOf(startAnchor);
250+
const endAnchorIndex = childrenArray.indexOf(endAnchor);
251+
const totalParentSectionLength = endAnchorIndex - startAnchorIndex - 1;
252+
253+
if (relative_k > totalParentSectionLength) {
254+
for (let i = totalParentSectionLength; i < relative_k; i++) {
244255
parent.insertBefore(new Comment("empty"), endAnchor);
245256
}
246257
}

0 commit comments

Comments
 (0)