-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: smart diffing #40
Closed
Closed
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
63cbe53
feat: print only dynamic node objects
ayoayco e63bdd1
update example templating
ayoayco fba4a86
feat: implement DOM watch list
ayoayco 6e4adc9
refactor: remove console.logs
ayoayco 09d4f59
feat: replace node
ayoayco e162003
initial work for multiple found
ayoayco 9bed501
remove obsolete example
ayoayco 1ff68b0
remove obsolete example
ayoayco 6484401
update toggle example
ayoayco 668f866
update templating example
ayoayco 8c9c4af
refactor: separate render function
ayoayco 685c01f
fix: wrong import
ayoayco a27515f
fix: typo in filename
ayoayco e8feef5
initial todo app example
ayoayco 6258460
initial handling of different watch list lengths
ayoayco 16b1f02
Merge branch 'main' into feat/smart-diffing
ayoayco ac34267
Merge branch 'main' of github.com:ayoayco/web-component-base into fea…
ayoayco 83432ca
delete unused prop #prevDom
ayoayco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
update toggle example
commit 64844010dfa4fb0171ff1b27ed31765b64f037c4
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,50 @@ | ||
import { serialize } from "./serialize.mjs"; | ||
export function createElement(tree, watchList = [], selector = []) { | ||
if (!tree) { | ||
export function createElement(tree, watchList = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require watchList and remove default |
||
if (!tree && tree !== 0) { | ||
console.log(tree); | ||
const div = document.createElement("div"); | ||
div.className = `__placeholder__`; | ||
selector.push(`div.${div.className}`); | ||
const watchObj = { | ||
selector: selector.join(" "), | ||
type: "replace", | ||
node: div, | ||
value: null, | ||
value: "__replace__", | ||
}; | ||
watchList.push(watchObj); | ||
return div; | ||
} | ||
if (!tree.type) { | ||
const value = tree.dynamic ? tree.value : tree; | ||
|
||
if (Array.isArray(value)) { | ||
if (Array.isArray(tree)) { | ||
const frag = document.createDocumentFragment(); | ||
frag.replaceChildren( | ||
...value.map((leaf) => | ||
leaf.dynamic | ||
? createElement(leaf.value, watchList) | ||
: createElement(leaf, watchList) | ||
) | ||
...tree.map((leaf, index) => { | ||
return createElement(leaf, watchList, undefined, index); | ||
}) | ||
); | ||
|
||
return frag; | ||
} | ||
|
||
const node = document.createTextNode(value); | ||
const node = document.createTextNode(tree); | ||
const watchObj = { | ||
selector: selector.join(" "), | ||
type: "textContent", | ||
node, | ||
value, | ||
value: tree, | ||
}; | ||
watchList.push(watchObj); | ||
|
||
return node; | ||
} else { | ||
const el = document.createElement(tree.type); | ||
const elSelector = tree.type; | ||
const selectorIndex = selector.length; | ||
selector.push(elSelector); | ||
|
||
/** | ||
* handle props | ||
*/ | ||
if (tree.props) { | ||
Object.entries(tree.props).forEach(([prop, value]) => { | ||
if (prop === "id") { | ||
selector[selectorIndex] += `#${value}`; | ||
} | ||
if (prop === "class") { | ||
selector[selectorIndex] += `.${value}`; | ||
} | ||
handleProp(prop, value, el); | ||
const watchObj2 = { | ||
selector: selector.join(" "), | ||
type: "prop", | ||
key: prop, | ||
value: value, | ||
value, | ||
node: el, | ||
}; | ||
watchList.push(watchObj2); | ||
|
@@ -72,7 +55,7 @@ export function createElement(tree, watchList = [], selector = []) { | |
* handle children | ||
*/ | ||
tree.children?.forEach((child) => { | ||
const childEl = createElement(child, watchList, selector); | ||
const childEl = createElement(child, watchList); | ||
if (childEl instanceof Node) { | ||
el.appendChild(childEl); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add JSDoc