1- import { EditPage } from "./components/Editor.js" ;
1+ import { EditPage } from "./components/Editor.js"
22
3- const $content = document . getElementById ( "content" ) ;
3+ const $content = document . getElementById ( "content" )
44
55// 라우팅 처리 함수
66function matchRoute ( path ) {
7- if ( path === "/" ) return routes [ "/" ] ;
8- if ( path === "/documents" ) return routes [ "/documents" ] ;
7+ if ( path === "/" ) return routes [ "/" ]
8+ if ( path === "/documents" ) return routes [ "/documents" ]
99
10- // 동적 경로: /documents/:id
11- if ( path . startsWith ( "/documents/" ) ) {
12- const id = path . split ( "/" ) [ 2 ] ; // "123" 부분 추출
13- return ( ) => routes [ "/documents/:id" ] ( id ) ;
14- }
10+ // 동적 경로: /documents/:id
11+ if ( path . startsWith ( "/documents/" ) ) {
12+ const id = path . split ( "/" ) [ 2 ] // "123" 부분 추출
13+ return ( ) => routes [ "/documents/:id" ] ( id )
14+ }
1515
16- return routes [ "/404" ] ;
16+ return routes [ "/404" ]
1717}
1818
1919// 라우팅 테이블
2020const routes = {
21- "/" : ( ) => {
22- $content . innerHTML = "" ;
23- } ,
24- "/documents" : ( ) => {
25- $content . innerHTML = "<h1>문서 목록</h1>" ;
26- // 예시: 목록을 클릭하면 특정 id 문서로 이동
27- const link = document . createElement ( "a" ) ;
28- link . href = "/documents/123" ;
29- link . textContent = "문서 123 보기" ;
30- link . addEventListener ( "click" , ( e ) => {
31- e . preventDefault ( ) ;
32- navigateTo ( "/documents/123" ) ;
33- } ) ;
34- $content . appendChild ( link ) ;
35- } ,
36- "/documents/:id" : ( id ) => {
37- $content . innerHTML = "" ;
38- // id를 EditPage에 전달 (실제라면 API로 문서 로드)
39- EditPage ( $content , id ) ;
40- } ,
41- "/404" : ( ) => {
42- $content . innerHTML = "<p>404 - 페이지를 찾을 수 없습니다.</p>" ;
43- } ,
44- } ;
21+ "/" : ( ) => {
22+ $content . innerHTML = ""
23+ } ,
24+ "/documents" : ( ) => {
25+ $content . innerHTML = "<h1>문서 목록</h1>"
26+ // 예시: 목록을 클릭하면 특정 id 문서로 이동
27+ const link = document . createElement ( "a" )
28+ link . href = "/documents/123"
29+ link . textContent = "문서 123 보기"
30+ link . addEventListener ( "click" , ( e ) => {
31+ e . preventDefault ( )
32+ navigateTo ( "/documents/123" )
33+ } )
34+ $content . appendChild ( link )
35+ } ,
36+ "/documents/:id" : ( id ) => {
37+ $content . innerHTML = ""
38+ // id를 EditPage에 전달 (실제라면 API로 문서 로드)
39+ EditPage ( $content , id )
40+ } ,
41+ "/404" : ( ) => {
42+ $content . innerHTML = "<p>404 - 페이지를 찾을 수 없습니다.</p>"
43+ } ,
44+ }
4545
4646export function navigateTo ( path ) {
47- history . pushState ( { path } , "" , path ) ;
48- loadContent ( path ) ;
47+ history . pushState ( { path } , "" , path )
48+ loadContent ( path )
4949}
5050
5151export function loadContent ( path ) {
52- const handler = matchRoute ( path ) ;
53- handler ( ) ;
52+ const handler = matchRoute ( path )
53+ handler ( )
5454}
5555
5656// popstate
5757window . addEventListener ( "popstate" , ( e ) => {
58- const path = e . state ?. path || "/" ;
59- loadContent ( path ) ;
60- } ) ;
58+ const path = e . state ?. path || "/"
59+ loadContent ( path )
60+ } )
6161
6262// 첫 로딩
6363window . addEventListener ( "DOMContentLoaded" , ( ) => {
64- loadContent ( location . pathname ) ;
65- } ) ;
64+ loadContent ( location . pathname )
65+ } )
0 commit comments