Skip to content

Commit

Permalink
fix: sublbocks
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Apr 25, 2023
1 parent 2b5aaf2 commit 129b2a0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface EditAttribute {
}

interface EditText {
type: 'text';
type: 'child';
path: number[];
index: number;
hole: any;
Expand Down Expand Up @@ -74,7 +74,7 @@ export const render = (
const child = vnode.children[i];
if (child instanceof Hole) {
edits.push({
type: 'text',
type: 'child',
path,
index: i,
hole: child.key,
Expand All @@ -90,6 +90,7 @@ export const render = (

interface BlockInstance {
props: Props;
edits: Edit[];
mount: (parent: HTMLElement) => void;
patch: (newBlock: BlockInstance) => void;
}
Expand Down Expand Up @@ -126,7 +127,12 @@ export const block = (fn: (props: Props) => VNode) => {

if (edit.type === 'attribute') {
currentEl[edit.propName] = value;
} else if (edit.type === 'text') {
} else if (edit.type === 'child') {
if (value.mount && typeof value.mount === 'function') {
value.mount(currentEl);
continue;
}

const textNode = document.createTextNode(value);
currentEl.insertBefore(textNode, currentEl.childNodes[edit.index]);
}
Expand All @@ -145,13 +151,18 @@ export const block = (fn: (props: Props) => VNode) => {

if (edit.type === 'attribute') {
currentEl[edit.propName] = newValue;
} else if (edit.type === 'text') {
} else if (edit.type === 'child') {
if (value.patch && typeof value.patch === 'function') {
// patch cooresponding child blocks
value.patch(newBlock.edits[i].hole);
continue;
}
currentEl.childNodes[edit.index].textContent = newValue;
}
}
};

return { mount, patch, props };
return { mount, patch, props, edits };
};

return Block;
Expand Down

0 comments on commit 129b2a0

Please sign in to comment.