-
Notifications
You must be signed in to change notification settings - Fork 65
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
[6팀 소수지] [Chapter 1-2] 프레임워크 없이 SPA 만들기 #47
Open
devsuzy
wants to merge
5
commits into
hanghae-plus:main
Choose a base branch
from
devsuzy:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,5 +1,43 @@ | ||
import { addEvent } from "./eventManager"; | ||
|
||
export function createElement(vNode) {} | ||
export function createElement(vNode) { | ||
if ( | ||
typeof vNode === "undefined" || | ||
vNode === null || | ||
typeof vNode === "boolean" | ||
) { | ||
return document.createTextNode(""); | ||
} | ||
|
||
function updateAttributes($el, props) {} | ||
if (typeof vNode === "string" || typeof vNode === "number") { | ||
return document.createTextNode(vNode); | ||
} | ||
|
||
if (Array.isArray(vNode)) { | ||
const fragment = document.createDocumentFragment(); | ||
vNode.forEach((child) => fragment.appendChild(createElement(child))); | ||
|
||
return fragment; | ||
} | ||
|
||
const $el = document.createElement(vNode.type); | ||
|
||
updateAttributes($el, vNode.props ?? {}); | ||
|
||
$el.append(...vNode.children.map(createElement)); | ||
|
||
return $el; | ||
} | ||
|
||
function updateAttributes($el, props) { | ||
Object.entries(props).forEach(([attr, value]) => { | ||
if (attr.startsWith("on") && typeof value === "function") { | ||
const eventType = attr.toLowerCase().slice(2); | ||
addEvent($el, eventType, value); | ||
} else if (attr === "className") { | ||
$el.setAttribute("class", value); | ||
} else { | ||
$el.setAttribute(attr, value); | ||
} | ||
}); | ||
} |
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,3 +1,9 @@ | ||
export function createVNode(type, props, ...children) { | ||
return {}; | ||
return { | ||
type, | ||
props, | ||
children: children | ||
.flat(Infinity) | ||
.filter((value) => value === 0 || Boolean(value)), | ||
}; | ||
} |
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,5 +1,59 @@ | ||
export function setupEventListeners(root) {} | ||
const eventListeners = new Map(); | ||
let $root = null; | ||
|
||
export function addEvent(element, eventType, handler) {} | ||
function handleEvent(e) { | ||
let target = e.target; | ||
|
||
export function removeEvent(element, eventType, handler) {} | ||
while (target && target !== $root) { | ||
const handlers = eventListeners.get(e.type)?.get(target); | ||
if (handlers) { | ||
handlers.forEach((handler) => handler(e)); | ||
} | ||
target = target.parentElement; | ||
} | ||
} | ||
|
||
export function setupEventListeners(root) { | ||
if (!root) return; | ||
$root = root; | ||
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. 오호! early return! |
||
|
||
eventListeners.forEach((handlers, eventType) => { | ||
$root.removeEventListener(eventType, handleEvent); | ||
$root.addEventListener(eventType, handleEvent); | ||
}); | ||
} | ||
|
||
export function addEvent(element, eventType, handler) { | ||
if (!eventListeners.has(eventType)) { | ||
eventListeners.set(eventType, new WeakMap()); | ||
} | ||
|
||
const handlers = eventListeners.get(eventType); | ||
if (!handlers.has(element)) { | ||
handlers.set(element, new Set()); | ||
} | ||
|
||
handlers.get(element).add(handler); | ||
} | ||
|
||
export function removeEvent(element, eventType, handler) { | ||
const handlersMap = eventListeners.get(eventType); | ||
if (!handlersMap) return; | ||
|
||
const handlers = handlersMap.get(element); | ||
if (!handlers) return; | ||
|
||
if (handler) { | ||
handlers.delete(handler); | ||
|
||
if (handlers.size === 0) { | ||
handlersMap.delete(element); | ||
} | ||
} else { | ||
handlersMap.delete(element); | ||
} | ||
|
||
if (handlersMap.size === 0) { | ||
eventListeners.delete(eventType); | ||
} | ||
} |
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,3 +1,34 @@ | ||
export function normalizeVNode(vNode) { | ||
return vNode; | ||
if ( | ||
typeof vNode === "undefined" || | ||
vNode === null || | ||
typeof vNode === "boolean" | ||
) { | ||
return ""; | ||
} | ||
|
||
if (typeof vNode === "string" || typeof vNode === "number") { | ||
return String(vNode); | ||
} | ||
|
||
if (typeof vNode.type === "function") { | ||
return normalizeVNode( | ||
vNode.type({ ...vNode.props, children: vNode.children }), | ||
); | ||
} | ||
|
||
// if (Array.isArray(vNode)) { | ||
// return vNode.map(normalizeVNode).join(""); | ||
// } | ||
|
||
const children = Array.isArray(vNode.children) | ||
? vNode.children | ||
: vNode.children | ||
? [vNode.children] | ||
: []; | ||
|
||
return { | ||
...vNode, | ||
children: children.map(normalizeVNode).filter(Boolean), | ||
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. filter(Boolean)은 JavaScript에서 truthy한 값만 남기기 위한 필터링 방법이라고 하네요! (처음 알았어요) 깔끔한 처리인 것 같습니다. |
||
}; | ||
} |
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,6 +1,77 @@ | ||
import { addEvent, removeEvent } from "./eventManager"; | ||
import { createElement } from "./createElement.js"; | ||
|
||
function updateAttributes(target, originNewProps, originOldProps) {} | ||
function updateAttributes(target, originNewProps, originOldProps) { | ||
const oldProps = originOldProps || {}; | ||
const newProps = originNewProps || {}; | ||
|
||
export function updateElement(parentElement, newNode, oldNode, index = 0) {} | ||
Object.entries(oldProps).forEach(([attr, value]) => { | ||
if (!newProps[attr]) { | ||
if (attr.startsWith("on")) { | ||
const eventType = attr.slice(2).toLowerCase(); | ||
removeEvent(target, eventType, value); | ||
} else { | ||
target.removeAttribute(attr); | ||
} | ||
} | ||
}); | ||
|
||
Object.entries(newProps).forEach(([attr, value]) => { | ||
if (oldProps[attr] !== value) { | ||
if (attr.startsWith("on")) { | ||
const eventType = attr.slice(2).toLowerCase(); | ||
if (typeof oldProps[attr] === "function") { | ||
removeEvent(target, eventType, oldProps[attr]); | ||
} | ||
addEvent(target, eventType, value); | ||
} else if (attr === "className") { | ||
target.setAttribute("class", value); | ||
} else { | ||
target.setAttribute(attr, value); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
export function updateElement(parentElement, newNode, oldNode, index = 0) { | ||
const existingNode = parentElement?.childNodes[index]; | ||
|
||
// oldNode만 있는 경우: oldNode를 parentElement에서 제거한다. | ||
if (!newNode && oldNode) { | ||
parentElement.removeChild(existingNode); | ||
return; | ||
} | ||
|
||
// newNode만 있는 경우: newNode를 parentElement에 추가한다. | ||
if (!oldNode && newNode) { | ||
parentElement.appendChild(createElement(newNode)); | ||
return; | ||
} | ||
|
||
// oldNode와 newNode 모두 string인 경우: oldNode와 newNode 내용이 다르다면, newNode 내용으로 교체한다. | ||
if (typeof oldNode === "string" || typeof newNode === "string") { | ||
if (newNode !== oldNode) { | ||
parentElement.replaceChild(createElement(newNode), existingNode); | ||
} | ||
return; | ||
} | ||
|
||
// oldeNode와 newNode의 태그 이름(type)이 다를 경우: oldNode를 제거하고 해당 위치에 newNode를 추가한다. | ||
if (oldNode.type !== newNode.type) { | ||
parentElement.replaceChild(createElement(newNode), existingNode); | ||
return; | ||
} | ||
|
||
// oldNode와 newNode의 태그 이름(type)이 같을 경우: newNode와 oldNode의 속성을 비교하여 변경된 부분만 반영한다. | ||
updateAttributes(existingNode, newNode.props || {}, oldNode.props || {}); | ||
|
||
// oldNode와 newNode를 순회하며, 앞에 조건식을 반복한다. | ||
const newChildren = newNode.children || []; | ||
const oldChildren = oldNode.children || []; | ||
|
||
const maxLength = Math.max(newChildren.length, oldChildren.length); | ||
|
||
for (let i = 0; i < maxLength; i++) { | ||
updateElement(existingNode, newChildren[i], oldChildren[i], i); | ||
} | ||
} |
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
Oops, something went wrong.
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.
크게 중요하지는 않을 것 같지만 리뷰 남겨 봅니다...!
현재 taillwind를 사용해서 css를 다루다 보니 각 element에 className이 더 자주 사용되지 않을까 라는 생각이 듭니다!
그래서 조건문 비교를 attr === 'className'을 가장 처음에 비교하는게 더 좋지 않을까? 라는 생각을 해봤습니다 ㅎㅎ
이번주 과제도 고생 많으셨습니다 :)
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.
앗 근데 생각 해보니 의미가 없을것 같네요....ㅎㅎㅎ