-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from zigsong/main
[Virtual DOM 만들기] 지그(송지은) 미션 제출합니다.
- Loading branch information
Showing
11 changed files
with
236 additions
and
24 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.cache/ | ||
dist/ | ||
node_modules/ |
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "javascript-own-ui-library", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"repository": "https://github.com/zigsong/javascript-own-ui-library.git", | ||
"author": "zigsong <wldms5764@gmail.com>", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "parcel build index.html" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Zig from '../lib/zig-react'; | ||
|
||
const Counter = () => { | ||
const [getCount, setCount] = Zig.useState(0); | ||
|
||
const decrease = () => { | ||
setCount(getCount() - 1); | ||
}; | ||
|
||
const increase = () => { | ||
setCount(getCount() + 1); | ||
}; | ||
|
||
const reset = () => { | ||
setCount(0); | ||
}; | ||
|
||
return () => | ||
Zig.createElement( | ||
'div', | ||
{ className: 'container' }, | ||
Zig.createElement('span', { className: 'count' }, getCount()), | ||
Zig.createElement( | ||
'div', | ||
{ className: 'btn-group' }, | ||
Zig.createElement('button', { id: 'decrement', onclick: decrease }, Zig.createElement('strong', {}, '-')), | ||
Zig.createElement('button', { id: 'reset', onclick: reset }, Zig.createElement('strong', {}, 'RESET')), | ||
Zig.createElement('button', { id: 'increment', onclick: increase }, Zig.createElement('strong', {}, '+')) | ||
), | ||
'이러다 다 죽어~! 🔫' | ||
); | ||
}; | ||
|
||
export default Counter; |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import ZigDom from './lib/zig-react-dom'; | ||
import Counter from './components/Counter'; | ||
|
||
ZigDom.render(Counter(), document.getElementById('root')); |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
let isMarked = false; | ||
|
||
const isPrimitive = (test) => test !== Object(test); | ||
|
||
const deepEqual = (prevDom, newDom) => { | ||
if (isPrimitive(prevDom) && isPrimitive(newDom)) { | ||
return prevDom === newDom; | ||
} | ||
|
||
if (prevDom.type !== newDom.type) { | ||
return false; | ||
} | ||
|
||
if (Object.keys(prevDom)?.length != Object.keys(newDom)?.length) { | ||
return false; | ||
} | ||
|
||
for (const key in prevDom) { | ||
if (newDom.hasOwnProperty(key)) { | ||
if (!deepEqual(prevDom[key], newDom[key])) { | ||
// element type이라면 | ||
if (newDom.type && !isMarked) { | ||
newDom['marked'] = true; | ||
isMarked = true; | ||
} | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const reconciliate = (prevDom, newDom) => { | ||
isMarked = false; | ||
|
||
const markedDom = { ...newDom }; | ||
const isEqual = deepEqual(prevDom, markedDom); | ||
|
||
return { isEqual, markedDom }; | ||
}; | ||
|
||
export default reconciliate; |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import reconciliate from './reconciliate'; | ||
|
||
const ZigDom = (function () { | ||
let currDom; | ||
let domFactory; | ||
let root; | ||
|
||
const createDom = (element, container) => { | ||
const { type, props } = element; | ||
|
||
const fragment = document.createDocumentFragment(); | ||
const initialNode = type === 'text' ? document.createTextNode('') : document.createElement(type); | ||
|
||
const vDom = Object.entries(props).reduce((totalNode, [key, value]) => { | ||
if (key !== 'children' && key !== 'marked') { | ||
totalNode[key] = value; | ||
} | ||
|
||
return totalNode; | ||
}, initialNode); | ||
|
||
props.children.forEach((child) => createDom(child, vDom)); | ||
|
||
(container || fragment).appendChild(vDom); | ||
|
||
return container || fragment; | ||
}; | ||
|
||
const _render = (element, container) => { | ||
const vDom = createDom(element); | ||
|
||
container.appendChild(vDom); | ||
}; | ||
|
||
const render = (component, container) => { | ||
domFactory = domFactory || component; | ||
root = root || container; | ||
|
||
if (typeof domFactory !== 'function') { | ||
console.error('component should return function'); | ||
|
||
return; | ||
} | ||
|
||
const vDom = domFactory(); | ||
currDom = vDom; | ||
|
||
_render(vDom, root); | ||
}; | ||
|
||
const rerender = () => { | ||
const newDom = domFactory(); | ||
|
||
const { isEqual, markedDom } = reconciliate(currDom, newDom); | ||
|
||
currDom = newDom; | ||
|
||
// (쉬운 ver.)diff하지 않고 전부 리렌더링 | ||
// root.innerHTML = ''; | ||
// _render(newDom, root); | ||
|
||
if (!isEqual) { | ||
repaint(markedDom, root.firstChild); | ||
} | ||
}; | ||
|
||
const repaint = (element, originDom) => { | ||
if (element.marked) { | ||
originDom.replaceWith(createDom(element)); | ||
} | ||
|
||
element.props.children.forEach((child, idx) => { | ||
repaint(child, originDom.childNodes[idx]); | ||
}); | ||
}; | ||
|
||
return { render, rerender }; | ||
})(); | ||
|
||
export default ZigDom; |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import ZigDom from './zig-react-dom'; | ||
|
||
const Zig = (function () { | ||
const createElement = (type, props, ...children) => { | ||
return { | ||
type, | ||
props: { | ||
...props, | ||
children: children.map((child) => { | ||
if (typeof child === 'string' || typeof child === 'number') { | ||
return createTextElement(child); | ||
} else { | ||
return child; | ||
} | ||
}), | ||
}, | ||
}; | ||
}; | ||
|
||
const createTextElement = (textValue) => { | ||
return createElement('text', { nodeValue: textValue }); | ||
}; | ||
|
||
let hooks = []; | ||
let idx = 0; | ||
|
||
const useState = (initialValue) => { | ||
const _idx = idx; | ||
hooks[_idx] = initialValue; | ||
|
||
const getState = () => hooks[_idx] || initialValue; | ||
|
||
const setState = (newValue) => { | ||
hooks[_idx] = newValue; | ||
|
||
ZigDom.rerender(); | ||
}; | ||
|
||
idx++; | ||
|
||
return [getState, setState]; | ||
}; | ||
|
||
return { createElement, useState }; | ||
})(); | ||
|
||
export default Zig; |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|