-
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
[4팀 장철희] [Chapter 1-2] 프레임워크 없이 SPA 만들기 #32
Open
jch1223
wants to merge
32
commits into
hanghae-plus:main
Choose a base branch
from
jch1223: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
32 commits
Select commit
Hold shift + click to select a range
a228163
createVNode 로직 구현
jch1223 7ac8e38
파일 별 @jsx 주석 제거
jch1223 86775ad
Merge pull request #2 from jch1223/create-vNode
jch1223 baa46a0
normalizedVNode 로직 작성
jch1223 72ba36b
create v node의 children filter 로직 수정
jch1223 26c29e6
Merge pull request #3 from jch1223/normalize-v-node
jch1223 710bc78
text node 반환 로직
jch1223 a6e1d2f
파라비터가 배열일 경우 로직
jch1223 1ba06e8
파라미터가 컴포넌트 일 경우 로직
jch1223 a098614
attributes추가 로직을 updateAttributes 함수로 분리
jch1223 1e874d2
Merge pull request #4 from jch1223/create-element
jch1223 7704286
이벤트 메니저 로직 작성
jch1223 c48930a
Merge pull request #5 from jch1223/event-manager
jch1223 3703590
updateAttributes 함수에서 props의 이벤트 등록 로직 추가
jch1223 dfa63d6
renderElement 로직
jch1223 99ddcc7
Merge pull request #6 from jch1223/render-element
jch1223 12d2a6e
Revert "파일 별 @jsx 주석 제거"
jch1223 a67ca62
normalizeVnode children 적용안되는 버그 수정
jch1223 671205c
Merge pull request #7 from jch1223/render-element
jch1223 211a948
updateElement함수 실행 분기 처리
jch1223 8cbbbf8
같은 타입의 노드 업데이트
jch1223 ef12d56
노드 타입이 다를 경우 기존 노드를 새로운 노드로 치환
jch1223 9514446
updateAttributes 기능 구현
jch1223 5a042cf
Merge pull request #8 from jch1223/update-element
jch1223 87a7b19
handler가 없을 때 예외 처리
jch1223 2cc83cd
비사용자 기능 구현
jch1223 c3075c3
포스트 추가 기능
jch1223 a2e5fe8
노드를 업데이트 할 때, 연속된 문자열 일 경우 마지막 문자열로 덮어씌워지는 문제 수정
jch1223 4e21f54
노드가 제거 되었을 때 로직 추가
jch1223 ccbc185
좋아요 토글 기능
jch1223 c3cadba
Merge pull request #9 from jch1223/post-page
jch1223 bfdeee5
getstate 위치 수정
jch1223 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,56 @@ | ||
import { addEvent } from "./eventManager"; | ||
|
||
export function createElement(vNode) {} | ||
export function createElement(vNode) { | ||
if (typeof vNode === "function") { | ||
throw new Error("invalid vNode"); | ||
} | ||
|
||
function updateAttributes($el, props) {} | ||
if ( | ||
typeof vNode === "boolean" || | ||
typeof vNode === "undefined" || | ||
vNode === null | ||
) { | ||
return new Text(""); | ||
} | ||
|
||
if (typeof vNode === "number" || typeof vNode === "string") { | ||
return new Text(String(vNode)); | ||
} | ||
|
||
if (Array.isArray(vNode)) { | ||
const fragment = new DocumentFragment(); | ||
|
||
vNode.forEach((node) => fragment.append(createElement(node))); | ||
|
||
return fragment; | ||
} | ||
|
||
if (!Array.isArray(vNode) && typeof vNode === "object") { | ||
const container = document.createElement(vNode.type); | ||
|
||
updateAttributes(container, vNode?.props); | ||
container.append(createElement(vNode.children)); | ||
|
||
return container; | ||
} | ||
|
||
return vNode; | ||
} | ||
|
||
function updateAttributes($el, props = {}) { | ||
if (props === null) return; | ||
|
||
Object.entries(props).forEach(([attr, value]) => { | ||
if (attr === "className") { | ||
$el.setAttribute("class", value); | ||
return; | ||
} | ||
|
||
if (attr.startsWith("on")) { | ||
addEvent($el, attr.slice(2).toLowerCase(), value); | ||
return; | ||
} | ||
|
||
$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((item) => { | ||
return item === 0 || item; | ||
}), | ||
}; | ||
} |
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,83 @@ | ||
export function setupEventListeners(root) {} | ||
/** | ||
* DOM 이벤트 리스너들을 관리하는 중첩된 Map 구조입니다. | ||
* | ||
* @type {Map<HTMLElement, Map<string, Map<Function, Function>>>} | ||
* | ||
* @description | ||
* 3중 중첩 Map 구조로 되어있습니다: | ||
* - 첫 번째 레벨: DOM 엘리먼트를 키로 가지는 Map | ||
* - 두 번째 레벨: 이벤트 타입(예: 'click', 'mouseover')을 키로 가지는 Map | ||
* - 세 번째 레벨: 원본 핸들러 함수를 키로 가지고, 래핑된 핸들러 함수를 값으로 가지는 Map | ||
* | ||
* @example | ||
* // Map 구조 예시 | ||
* Map(HTMLElement) => { | ||
* Map(eventType: string) => { | ||
* Map(originalHandler: Function => wrappedHandler: Function) | ||
* } | ||
* } | ||
*/ | ||
const eventListeners = new Map(); | ||
let root = null; | ||
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. 처음에는 단순하게 eventListeners를 배열로 만들고, addEvent할 때 event 등록에 필요한 값들을 push해서 관리하려고 했습니다. |
||
|
||
export function addEvent(element, eventType, handler) {} | ||
export function setupEventListeners(_root) { | ||
if (!_root) { | ||
throw new Error("이벤트 리스너 설정을 위해 root 엘리먼트가 필요합니다."); | ||
} | ||
|
||
export function removeEvent(element, eventType, handler) {} | ||
root = _root; | ||
|
||
eventListeners.forEach((eventTypeMap) => { | ||
eventTypeMap?.forEach((handlers, eventType) => { | ||
handlers?.forEach((handler) => { | ||
root.addEventListener(eventType, handler); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
export function addEvent(element, eventType, handler) { | ||
if (!eventListeners.has(element)) { | ||
eventListeners.set(element, new Map()); | ||
} | ||
|
||
const elementEvents = eventListeners.get(element); | ||
|
||
if (!elementEvents.has(eventType)) { | ||
elementEvents.set(eventType, new Map()); | ||
} | ||
|
||
const wrappedHandler = | ||
typeof handler === "function" | ||
? (e) => { | ||
if (e.target === element) { | ||
handler(e); | ||
} | ||
} | ||
: undefined; | ||
|
||
elementEvents.get(eventType).set(handler, wrappedHandler); | ||
} | ||
|
||
export function removeEvent(element, eventType, handler) { | ||
if (!eventListeners.has(element)) return; | ||
|
||
const eventsTypes = eventListeners.get(element); | ||
if (!eventsTypes.has(eventType)) return; | ||
|
||
const handlers = eventsTypes.get(eventType); | ||
if (!handlers.has(handler)) return; | ||
|
||
// dom에 등록된 이벤트 제거 | ||
const registeredHandler = handlers.get(handler); | ||
root.removeEventListener(eventType, registeredHandler); | ||
|
||
// eventListeners에 등록된 이벤트 제거 | ||
handlers.delete(handler); | ||
if (handlers.size === 0) { | ||
eventsTypes.delete(eventType); | ||
} | ||
if (eventsTypes.size === 0) { | ||
eventListeners.delete(element); | ||
} | ||
} |
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,28 @@ | ||
export function normalizeVNode(vNode) { | ||
if ( | ||
typeof vNode === "boolean" || | ||
typeof vNode === "undefined" || | ||
vNode === null | ||
) { | ||
return ""; | ||
} | ||
|
||
if (typeof vNode === "number" || typeof vNode === "string") { | ||
return String(vNode); | ||
} | ||
|
||
if (typeof vNode.type === "function") { | ||
return normalizeVNode( | ||
vNode.type({ | ||
...vNode.props, | ||
children: vNode.children, | ||
}), | ||
); | ||
} | ||
|
||
if (Array.isArray(vNode.children)) { | ||
vNode.children = vNode.children.filter(normalizeVNode).map(normalizeVNode); | ||
} | ||
|
||
return vNode; | ||
} |
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.
전역 상태관리를 하고 있고, click에 대한 로직이 페이지에 있는 것보다 post 컴포넌트에 있는 것이 관심사가 맞다고 생각하여
로직에 필요한 값들(loggedIn, currentUser, posts)을 pops로 받지않고 스토어로 가지고 왔습니다.
props받지 않는 이유에 대한 적절한 논리가 될 수 있을까요?