-
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
[7팀 김영우] [Chapter 1-2] 프레임워크 없이 SPA 만들기 #22
Open
ywkim95
wants to merge
13
commits into
hanghae-plus:main
Choose a base branch
from
ywkim95: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
13 commits
Select commit
Hold shift + click to select a range
41bc9e3
FEAT: createElement 함수 내용 추가
ywkim95 93c3c5e
FEAT: createVNode 함수 내용 추가
ywkim95 ad4bfdd
FEAT: normalizeVNode 함수 내용 추가
ywkim95 4a3bb82
FEAT: renderElement 함수 내용 추가
ywkim95 235a9a8
FEAT: eventManager 함수 내용 추가
ywkim95 8598740
FEAT: 조건에 맞게 업데이트
ywkim95 77844be
FEAT: 이벤트 전역 변수 타입 변경 및 안정성추가
ywkim95 192d4be
REFACTOR: 사용하지 않는 파일 제거
ywkim95 426bd9f
FEAT: oldNode의 값을 가지고있기위한 WeakMap 추가
ywkim95 a1e80c8
FEAT: updateElement 로직 작성
ywkim95 9f532ce
FEAT: 로그인 여부를 통한 PostForm 표기 여부
ywkim95 154dc15
FEAT: 게시물 작성 로직 추가
ywkim95 7983a16
FEAT: 좋아요 토글 로직 추가
ywkim95 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
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. post.jsx 와 마찬가지로 globalStore.js에 액션을 정리하면 일관성있을 것 같아요!! |
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,49 @@ | ||
import { addEvent } from "./eventManager"; | ||
|
||
export function createElement(vNode) {} | ||
export function createElement(vNode) { | ||
if ( | ||
vNode === null || | ||
typeof vNode === "undefined" || | ||
typeof vNode === "boolean" | ||
) { | ||
return document.createTextNode(""); | ||
} | ||
if (typeof vNode === "string" || typeof vNode === "number") { | ||
return document.createTextNode(`${vNode}`); | ||
} | ||
|
||
function updateAttributes($el, props) {} | ||
if (typeof vNode.type === "function" && typeof vNode === "object") { | ||
throw new Error(); | ||
} | ||
|
||
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); | ||
if (Array.isArray(vNode.children)) { | ||
vNode.children.forEach((child) => { | ||
$el.appendChild(createElement(child)); | ||
}); | ||
} | ||
return $el; | ||
} | ||
|
||
function updateAttributes($el, props) { | ||
Object.entries(props || {}).forEach(([key, value]) => { | ||
if (key.startsWith("on")) { | ||
const eventType = key.toLowerCase().substring(2); | ||
addEvent($el, eventType, value); | ||
} else if (key.toLowerCase() === "classname") { | ||
// console.log($el, key, value); | ||
$el.setAttribute("class", value); | ||
} else { | ||
$el.setAttribute(key, 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,12 @@ | ||
export function createVNode(type, props, ...children) { | ||
return {}; | ||
// TODO: 0을 제외한 Falsy 값은 필터링 | ||
const filteredChildren = children.flat(Infinity).filter((child) => { | ||
return !( | ||
typeof child === "boolean" || | ||
typeof child === "undefined" || | ||
child === null | ||
); | ||
}); | ||
|
||
return { type, props, children: filteredChildren }; | ||
} |
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,69 @@ | ||
export function setupEventListeners(root) {} | ||
const events = new Map(); | ||
let $root = null; | ||
|
||
export function addEvent(element, eventType, handler) {} | ||
function handleEvent(event) { | ||
const element = event.target; | ||
const elementMap = events.get(element); | ||
if (!elementMap) return; | ||
|
||
export function removeEvent(element, eventType, handler) {} | ||
const handlers = elementMap.get(event.type); | ||
if (!handlers) return; | ||
|
||
handlers.forEach((handler) => handler(event)); | ||
} | ||
|
||
export function setupEventListeners(root) { | ||
// TODO: 이벤트 함수를 가져와서 한 번에 root에 이벤트를 등록한다. | ||
if ($root && $root !== root) { | ||
events.forEach((value) => { | ||
$root.removeEventListener(value.eventType, value.handler); | ||
}); | ||
} | ||
|
||
if ($root !== root) { | ||
$root = root; | ||
events.forEach((value, key) => { | ||
const elementMap = events.get(key); | ||
elementMap.forEach((handlers, eventType) => { | ||
$root.addEventListener(eventType, handleEvent); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export function addEvent(element, eventType, handler) { | ||
if (events.has(element)) return; | ||
|
||
events.set(element, new Map()); | ||
|
||
if ($root) { | ||
$root.addEventListener(eventType, handleEvent); | ||
} | ||
|
||
const elementMap = events.get(element); | ||
if (!elementMap.has(eventType)) { | ||
elementMap.set(eventType, new Set()); | ||
} | ||
elementMap.get(eventType).add(handler); | ||
} | ||
|
||
export function removeEvent(element, eventType, handler) { | ||
const elementMap = events.get(element); | ||
if (!elementMap) return; | ||
|
||
const handlers = elementMap.get(eventType); | ||
if (!handlers) return; | ||
|
||
handlers.delete(handler); | ||
|
||
if (handlers.size === 0) { | ||
elementMap.delete(eventType); | ||
} | ||
|
||
if (elementMap.size === 0) { | ||
events.delete(element); | ||
if ($root) { | ||
$root.removeEventListener(eventType, handleEvent); | ||
} | ||
} | ||
} |
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 ( | ||
vNode === null || | ||
typeof vNode === "boolean" || | ||
typeof vNode === "undefined" | ||
) { | ||
return ""; | ||
} | ||
|
||
if (typeof vNode === "string" || typeof vNode === "number") { | ||
return `${vNode}`; | ||
} | ||
|
||
if (typeof vNode.type === "function") { | ||
const component = vNode.type({ | ||
...(vNode.props || {}), | ||
children: vNode.children, | ||
}); | ||
return normalizeVNode(component); | ||
} | ||
|
||
if (Array.isArray(vNode)) { | ||
return vNode | ||
.map((child) => normalizeVNode(child)) | ||
.filter((child) => child || child === 0); | ||
} | ||
|
||
return { | ||
...vNode, | ||
children: vNode.children | ||
.map((child) => normalizeVNode(child)) | ||
.filter((child) => child || child === 0), | ||
}; | ||
} |
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,92 @@ | ||
import { addEvent, removeEvent } from "./eventManager"; | ||
import { createElement } from "./createElement.js"; | ||
|
||
function updateAttributes(target, originNewProps, originOldProps) {} | ||
function removeStaleEvents(target, newProps, oldProps) { | ||
Object.keys(oldProps).forEach((key) => { | ||
if (key.startsWith("on") && typeof oldProps[key] === "function") { | ||
const eventType = key.toLowerCase().substring(2); | ||
const oldHandler = oldProps[key]; | ||
const newHandler = newProps[key]; | ||
|
||
export function updateElement(parentElement, newNode, oldNode, index = 0) {} | ||
if (!newHandler || newHandler !== oldHandler) { | ||
removeEvent(target, eventType, oldHandler); | ||
} | ||
} else if (!newProps[key]) { | ||
target?.removeAttribute(key); | ||
} | ||
}); | ||
} | ||
|
||
function addNewEvents(target, newProps, oldProps) { | ||
Object.keys(newProps).forEach((key) => { | ||
if (key.startsWith("on")) { | ||
const eventType = key.toLowerCase().substring(2); | ||
const newHandler = newProps[key]; | ||
const oldHandler = oldProps[key]; | ||
|
||
if (!oldHandler || newHandler !== oldHandler) { | ||
addEvent(target, eventType, newHandler); | ||
} | ||
} else if (key.toLowerCase() === "classname") { | ||
target?.setAttribute("class", newProps[key] ?? ""); | ||
} else { | ||
target?.setAttribute(key, newProps[key]); | ||
} | ||
}); | ||
} | ||
|
||
function updateAttributes(target, newProps, oldProps) { | ||
removeStaleEvents(target, newProps, oldProps); | ||
addNewEvents(target, newProps, oldProps); | ||
} | ||
|
||
export function updateElement(parentElement, newNode, oldNode, index = 0) { | ||
if (!newNode && oldNode) { | ||
parentElement.removeChild(parentElement.childNodes[index]); | ||
return; | ||
} | ||
|
||
if (newNode && !oldNode) { | ||
parentElement.appendChild(createElement(newNode)); | ||
return; | ||
} | ||
|
||
if (typeof newNode === "string" && typeof oldNode === "string") { | ||
if (newNode !== oldNode) { | ||
parentElement.replaceChild( | ||
createElement(newNode), | ||
parentElement.childNodes[index], | ||
); | ||
return; | ||
} | ||
return; | ||
} | ||
|
||
if (newNode.type !== oldNode.type) { | ||
const newVNode = createElement(newNode); | ||
const oldVNode = parentElement?.childNodes[index]; | ||
if (oldVNode) { | ||
parentElement.replaceChild(newVNode, oldVNode); | ||
return; | ||
} | ||
parentElement.appendChild(newVNode); | ||
return; | ||
} | ||
|
||
updateAttributes( | ||
parentElement?.childNodes[index], | ||
newNode.props || {}, | ||
oldNode.props || {}, | ||
); | ||
|
||
const max = Math.max(newNode.children.length, oldNode.children.length); | ||
|
||
for (let i = 0; i < max; i++) { | ||
updateElement( | ||
parentElement?.childNodes[index], | ||
newNode.children[i], | ||
oldNode.children[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.
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.
어떤 액션이 일어나는지 직관적으로 알 수 있어 좋았지만 action에 관련된 내용이 globalStore.js에 들어가면 일관성있어 설계상 더 좋을 것 같습니당!
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.
감사합니다 지금보니 확실히 globalState로 넣어서 처리하는게 더 깔끔해보이네요 피드백 감사드려요!