Skip to content

Commit

Permalink
fix: patch up instance of diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Feb 10, 2023
1 parent 09ca3d3 commit 6fb9440
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 45 deletions.
1 change: 1 addition & 0 deletions client/dist/engine/dom.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default class Dom {
static diff(newNode: string, oldNode: HTMLElement): void;
static diffElements(newNode: HTMLElement, oldNode: HTMLElement): void;
static getBody(html: string, removeScripts?: boolean): HTMLElement;
static flattenDomIntoArray(node: HTMLElement): HTMLCollection;
static compareNodesAndReturnChanges(newNode: HTMLElement, oldNode: HTMLElement): Record<string, Element | null>[];
Expand Down
34 changes: 21 additions & 13 deletions client/dist/ui.cjs.development.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/dist/ui.cjs.development.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/ui.cjs.production.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/ui.cjs.production.min.js.map

Large diffs are not rendered by default.

34 changes: 21 additions & 13 deletions client/dist/ui.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/dist/ui.esm.js.map

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion client/dist/utils/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export declare enum DIRECTIVE_SHORTHANDS {
export declare function arraysMatch(a: any[], b: any[]): boolean;
declare global {
interface Window {
leafUI: Component;
leafUI: {
rootEl?: HTMLElement;
component: Component;
};
_leafUIConfig: LeafUIConfig;
}
interface HTMLElement {
Expand Down
5 changes: 5 additions & 0 deletions client/src/core/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export default class Component {
this.render();
rootEl['component'] = this;

window.leafUI = {
rootEl,
component: this
};

return this;
}

Expand Down
42 changes: 29 additions & 13 deletions client/src/engine/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { DIRECTIVE_SHORTHANDS, arraysMatch } from './../utils/data';

export default class Dom {
static diff(newNode: string, oldNode: HTMLElement): void {
const newDomBody = Dom.getBody(newNode, true);
const newNodes = Array.prototype.slice.call(newDomBody.children);
Dom.diffElements(Dom.getBody(newNode, false), oldNode);
}

static diffElements(newNode: HTMLElement, oldNode: HTMLElement): void {
const newNodes = Array.prototype.slice.call(newNode.children);
const oldNodes = Array.prototype.slice.call(oldNode.children);

/**
* Get the type for a node
* @param {Node} node The node
* @return {String} The type
* @return {String} The type
*/
const getNodeType = (node: HTMLElement) => {
if (node.nodeType === 3) return 'text';
Expand All @@ -31,7 +34,7 @@ export default class Dom {
// const diff = Dom.compareNodesAndReturnChanges(newDomBody, oldNode);

// If extra elements in DOM, remove them
var count = oldNodes.length - newNodes.length;
let count = oldNodes.length - newNodes.length;
if (count > 0) {
for (; count > 0; count--) {
oldNodes[oldNodes.length - count].parentNode.removeChild(
Expand Down Expand Up @@ -78,31 +81,45 @@ export default class Dom {

if (hasDirectivePrefix || hasDirectiveShorthandPrefix) {
oldNodes[index].innerHTML = node.innerHTML;

for (let j = 0; j < node.attributes.length; j++) {
const attr = node.attributes[j];

if (
attr.name.startsWith('ui-') ||
Object.keys(DIRECTIVE_SHORTHANDS).some(shorthand =>
Object.values(oldNodes[index].attributes)
.map((attr: any) => attr.name.startsWith(shorthand))
.map((attr: any) =>
attr.name.startsWith(shorthand)
)
.includes(true)
)
) {
if (
oldNodes[index].getAttribute(attr.name) !==
attr.value
) {
oldNodes[index].replaceWith(node);
initComponent(node);
const newNodeClone = node.cloneNode(true);
oldNodes[index].parentNode.replaceChild(
newNodeClone,
oldNodes[index]
);
initComponent(newNodeClone);
}

continue;
}
oldNodes[index].setAttribute(attr.name, attr.value);

const newNodeClone = node.cloneNode(true);
oldNodes[index].parentNode.replaceChild(
newNodeClone,
oldNodes[index]
);
initComponent(newNodeClone);
}
continue;
}


// If element is not the same type, replace it with new element
if (getNodeType(node) !== getNodeType(oldNodes[index])) {
const newNodeClone = node.cloneNode(true);
Expand Down Expand Up @@ -138,13 +155,12 @@ export default class Dom {
const fragment = document.createDocumentFragment();
Dom.diff(node, fragment as any);
oldNodes[index].appendChild(fragment);
initComponent(node);
continue;
}

if (node.children.length > 0) {
Dom.diff(node, oldNodes[index]);
initComponent(node);
console.log('diffing children', newNode, oldNode);
Dom.diffElements(node, oldNodes[index]);
}
}
}
Expand All @@ -153,7 +169,7 @@ export default class Dom {
const parser = new DOMParser();
const dom = parser.parseFromString(html, 'text/html');

if (removeScripts) {
if (removeScripts === true) {
const scripts = dom.body.getElementsByTagName('script');

for (let i = 0; i < scripts.length; i++) {
Expand Down
5 changes: 4 additions & 1 deletion client/src/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export function arraysMatch(a: any[], b: any[]) {

declare global {
interface Window {
leafUI: Component;
leafUI: {
rootEl?: HTMLElement;
component: Component;
};
_leafUIConfig: LeafUIConfig;
}

Expand Down

0 comments on commit 6fb9440

Please sign in to comment.