|
1 | 1 | import { VNode } from "../v-dom";
|
2 |
| -import { createElement, updateElement } from "./elements"; |
| 2 | +import { camelToDash } from "../utils/others"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Create a dom node according to the tag name of the vnode. |
| 6 | + * |
| 7 | + * 1. Create a dom node according to the tag name of the vnode. |
| 8 | + * 2. Set the attributes of the dom node. |
| 9 | + * 3. Create the child nodes of the dom node. |
| 10 | + * |
| 11 | + * @param node the vnode |
| 12 | + */ |
| 13 | +export function createElement(node: VNode): Node { |
| 14 | + // create a dom node according to the tag name of the vnode |
| 15 | + const el = |
| 16 | + node.tagName === "Fragment" |
| 17 | + ? document.createDocumentFragment() |
| 18 | + : document.createElement(node.tagName); |
| 19 | + |
| 20 | + // set the attributes of the dom node |
| 21 | + const attributes = node.attributes; |
| 22 | + for (const key in attributes) { |
| 23 | + if (key.startsWith("on")) { |
| 24 | + const eventName = key.slice(2).toLowerCase(); |
| 25 | + el.addEventListener(eventName, attributes[key]); |
| 26 | + } else { |
| 27 | + node.tagName === "Fragment" |
| 28 | + ? null |
| 29 | + : // @ts-ignore |
| 30 | + el.setAttribute(camelToDash(key), attributes[key]); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // create the child nodes of the dom node |
| 35 | + if (node.children) { |
| 36 | + for (const child of node.children) { |
| 37 | + if (typeof child === "string") { |
| 38 | + el.appendChild(document.createTextNode(child)); |
| 39 | + continue; |
| 40 | + } |
| 41 | + el.appendChild(createElement(child)); |
| 42 | + } |
| 43 | + } |
| 44 | + return el; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Update the attributes of the dom node. |
| 49 | + * |
| 50 | + * 1. Get the dom node of the vnode. |
| 51 | + * 2. Get the attributes of the vnode. |
| 52 | + * 3. Update the attributes of the dom node. |
| 53 | + * 4. Update the child nodes of the dom node. |
| 54 | + * |
| 55 | + * @param node the vnode |
| 56 | + */ |
| 57 | +export function updateElement(oldNode: VNode, newNode: VNode) { |
| 58 | + // get the dom node of the vnode |
| 59 | + const el = newNode.el! as HTMLElement; |
| 60 | + |
| 61 | + // get the attributes of the vnode |
| 62 | + const attributes = newNode.attributes; |
| 63 | + |
| 64 | + // update the attributes of the dom node |
| 65 | + for (const key in attributes) { |
| 66 | + if (key.startsWith("on")) { |
| 67 | + if (typeof oldNode?.attributes[key] === "function") { |
| 68 | + const eventName = key.slice(2).toLowerCase(); |
| 69 | + el.removeEventListener(eventName, oldNode.attributes[key]); |
| 70 | + el.addEventListener(eventName, attributes[key]); |
| 71 | + } |
| 72 | + } else { |
| 73 | + el.setAttribute(camelToDash(key), attributes[key]); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + for (const child of newNode.children) { |
| 78 | + if (typeof child === "string") { |
| 79 | + el.appendChild(document.createTextNode(child)); |
| 80 | + continue; |
| 81 | + } |
| 82 | + el.appendChild(createElement(child)); |
| 83 | + } |
| 84 | + |
| 85 | + // update the child nodes of the dom node |
| 86 | + if (newNode.children) { |
| 87 | + for (const child of Object.values(newNode.children)) { |
| 88 | + updateDOM(undefined, child); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return el; |
| 93 | +} |
3 | 94 |
|
4 | 95 | /**
|
5 | 96 | * Update the dom tree.
|
|
0 commit comments