amateras 0.7.4 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (257) hide show
  1. package/README.md +76 -85
  2. package/package.json +29 -33
  3. package/tsconfig.json +4 -3
  4. package/packages/core/package.json +0 -32
  5. package/packages/core/src/env.browser.ts +0 -21
  6. package/packages/core/src/env.node.ts +0 -21
  7. package/packages/core/src/global.ts +0 -18
  8. package/packages/core/src/index.ts +0 -96
  9. package/packages/core/src/lib/assignNodeProperties.ts +0 -11
  10. package/packages/core/src/lib/assignProperties.ts +0 -57
  11. package/packages/core/src/lib/chain.ts +0 -17
  12. package/packages/core/src/lib/dom.ts +0 -20
  13. package/packages/core/src/main.ts +0 -4
  14. package/packages/core/src/node/$Element.ts +0 -366
  15. package/packages/core/src/node/$EventTarget.ts +0 -48
  16. package/packages/core/src/node/$HTMLElement.ts +0 -99
  17. package/packages/core/src/node/$Node.ts +0 -201
  18. package/packages/core/src/node/$Virtual.ts +0 -65
  19. package/packages/css/README.md +0 -128
  20. package/packages/css/package.json +0 -17
  21. package/packages/css/src/ext/colors/amber.ts +0 -25
  22. package/packages/css/src/ext/colors/blackwhite.ts +0 -13
  23. package/packages/css/src/ext/colors/blue.ts +0 -25
  24. package/packages/css/src/ext/colors/cyan.ts +0 -25
  25. package/packages/css/src/ext/colors/emerald.ts +0 -25
  26. package/packages/css/src/ext/colors/fuchsia.ts +0 -25
  27. package/packages/css/src/ext/colors/gray.ts +0 -25
  28. package/packages/css/src/ext/colors/green.ts +0 -25
  29. package/packages/css/src/ext/colors/indigo.ts +0 -25
  30. package/packages/css/src/ext/colors/lime.ts +0 -25
  31. package/packages/css/src/ext/colors/neutral.ts +0 -25
  32. package/packages/css/src/ext/colors/orange.ts +0 -25
  33. package/packages/css/src/ext/colors/pink.ts +0 -25
  34. package/packages/css/src/ext/colors/purple.ts +0 -25
  35. package/packages/css/src/ext/colors/red.ts +0 -25
  36. package/packages/css/src/ext/colors/rose.ts +0 -25
  37. package/packages/css/src/ext/colors/sky.ts +0 -25
  38. package/packages/css/src/ext/colors/slate.ts +0 -25
  39. package/packages/css/src/ext/colors/stone.ts +0 -25
  40. package/packages/css/src/ext/colors/teal.ts +0 -25
  41. package/packages/css/src/ext/colors/violet.ts +0 -25
  42. package/packages/css/src/ext/colors/yellow.ts +0 -25
  43. package/packages/css/src/ext/colors/zinc.ts +0 -25
  44. package/packages/css/src/ext/colors.ts +0 -23
  45. package/packages/css/src/ext/container.ts +0 -32
  46. package/packages/css/src/ext/keyframes.ts +0 -54
  47. package/packages/css/src/ext/media.ts +0 -32
  48. package/packages/css/src/ext/property.ts +0 -48
  49. package/packages/css/src/ext/variable.ts +0 -51
  50. package/packages/css/src/index.ts +0 -436
  51. package/packages/css/src/lib/colorAssign.ts +0 -6
  52. package/packages/css/src/lib/utils.ts +0 -11
  53. package/packages/css/src/structure/$CSSContainerRule.ts +0 -13
  54. package/packages/css/src/structure/$CSSDeclaration.ts +0 -16
  55. package/packages/css/src/structure/$CSSKeyframesRule.ts +0 -13
  56. package/packages/css/src/structure/$CSSMediaRule.ts +0 -10
  57. package/packages/css/src/structure/$CSSProperty.ts +0 -19
  58. package/packages/css/src/structure/$CSSRule.ts +0 -13
  59. package/packages/css/src/structure/$CSSStyleRule.ts +0 -14
  60. package/packages/css/src/structure/$CSSVariable.ts +0 -30
  61. package/packages/dom/package.json +0 -20
  62. package/packages/dom/src/lib/HTMLElementMap.ts +0 -213
  63. package/packages/dom/src/lib/assignAttributes.ts +0 -16
  64. package/packages/dom/src/structure/CSS.ts +0 -7
  65. package/packages/dom/src/structure/CSSStyleSheet.ts +0 -10
  66. package/packages/dom/src/structure/DOMTokenList.ts +0 -19
  67. package/packages/dom/src/structure/Document.ts +0 -36
  68. package/packages/dom/src/structure/Element.ts +0 -106
  69. package/packages/dom/src/structure/HTMLElement.ts +0 -34
  70. package/packages/dom/src/structure/History.ts +0 -11
  71. package/packages/dom/src/structure/Location.ts +0 -9
  72. package/packages/dom/src/structure/Node.ts +0 -51
  73. package/packages/dom/src/structure/NodeList.ts +0 -10
  74. package/packages/dom/src/structure/Storage.ts +0 -8
  75. package/packages/dom/src/structure/Text.ts +0 -20
  76. package/packages/dom/src/structure/Window.ts +0 -14
  77. package/packages/dom/src/structure/html/HTMLAbbrElement.ts +0 -16
  78. package/packages/dom/src/structure/html/HTMLAddressElement.ts +0 -16
  79. package/packages/dom/src/structure/html/HTMLAnchorElement.ts +0 -25
  80. package/packages/dom/src/structure/html/HTMLAreaElement.ts +0 -26
  81. package/packages/dom/src/structure/html/HTMLArticleElement.ts +0 -16
  82. package/packages/dom/src/structure/html/HTMLAsideElement.ts +0 -16
  83. package/packages/dom/src/structure/html/HTMLAudioElement.ts +0 -16
  84. package/packages/dom/src/structure/html/HTMLBDIElement.ts +0 -16
  85. package/packages/dom/src/structure/html/HTMLBDOElement.ts +0 -16
  86. package/packages/dom/src/structure/html/HTMLBElement.ts +0 -16
  87. package/packages/dom/src/structure/html/HTMLBRElement.ts +0 -17
  88. package/packages/dom/src/structure/html/HTMLBaseElement.ts +0 -18
  89. package/packages/dom/src/structure/html/HTMLBodyElement.ts +0 -22
  90. package/packages/dom/src/structure/html/HTMLButtonElement.ts +0 -26
  91. package/packages/dom/src/structure/html/HTMLCanvasElement.ts +0 -18
  92. package/packages/dom/src/structure/html/HTMLCiteElement.ts +0 -16
  93. package/packages/dom/src/structure/html/HTMLCodeElement.ts +0 -16
  94. package/packages/dom/src/structure/html/HTMLDDElement.ts +0 -16
  95. package/packages/dom/src/structure/html/HTMLDFNElement.ts +0 -16
  96. package/packages/dom/src/structure/html/HTMLDListElement.ts +0 -17
  97. package/packages/dom/src/structure/html/HTMLDTElement.ts +0 -16
  98. package/packages/dom/src/structure/html/HTMLDataElement.ts +0 -17
  99. package/packages/dom/src/structure/html/HTMLDataListElement.ts +0 -16
  100. package/packages/dom/src/structure/html/HTMLDetailsElement.ts +0 -17
  101. package/packages/dom/src/structure/html/HTMLDialogElement.ts +0 -18
  102. package/packages/dom/src/structure/html/HTMLDivElement.ts +0 -17
  103. package/packages/dom/src/structure/html/HTMLEMElement.ts +0 -16
  104. package/packages/dom/src/structure/html/HTMLEmbedElement.ts +0 -20
  105. package/packages/dom/src/structure/html/HTMLFieldSetElement.ts +0 -19
  106. package/packages/dom/src/structure/html/HTMLFigCaptionElement.ts +0 -16
  107. package/packages/dom/src/structure/html/HTMLFigureElement.ts +0 -16
  108. package/packages/dom/src/structure/html/HTMLFooterElement.ts +0 -16
  109. package/packages/dom/src/structure/html/HTMLFormElement.ts +0 -24
  110. package/packages/dom/src/structure/html/HTMLHGroupElement.ts +0 -16
  111. package/packages/dom/src/structure/html/HTMLHRElement.ts +0 -21
  112. package/packages/dom/src/structure/html/HTMLHeadElement.ts +0 -16
  113. package/packages/dom/src/structure/html/HTMLHeaderElement.ts +0 -16
  114. package/packages/dom/src/structure/html/HTMLHeadingElement.ts +0 -17
  115. package/packages/dom/src/structure/html/HTMLHtmlElement.ts +0 -18
  116. package/packages/dom/src/structure/html/HTMLIElement.ts +0 -16
  117. package/packages/dom/src/structure/html/HTMLIFrameElement.ts +0 -31
  118. package/packages/dom/src/structure/html/HTMLImageElement.ts +0 -38
  119. package/packages/dom/src/structure/html/HTMLInputElement.ts +0 -55
  120. package/packages/dom/src/structure/html/HTMLKBDElement.ts +0 -16
  121. package/packages/dom/src/structure/html/HTMLLIElement.ts +0 -18
  122. package/packages/dom/src/structure/html/HTMLLabelElement.ts +0 -18
  123. package/packages/dom/src/structure/html/HTMLLegendElement.ts +0 -17
  124. package/packages/dom/src/structure/html/HTMLLinkElement.ts +0 -31
  125. package/packages/dom/src/structure/html/HTMLMainElement.ts +0 -16
  126. package/packages/dom/src/structure/html/HTMLMapElement.ts +0 -17
  127. package/packages/dom/src/structure/html/HTMLMarkElement.ts +0 -16
  128. package/packages/dom/src/structure/html/HTMLMediaElement.ts +0 -48
  129. package/packages/dom/src/structure/html/HTMLMenuElement.ts +0 -18
  130. package/packages/dom/src/structure/html/HTMLMetaElement.ts +0 -22
  131. package/packages/dom/src/structure/html/HTMLMeterElement.ts +0 -23
  132. package/packages/dom/src/structure/html/HTMLModElement.ts +0 -18
  133. package/packages/dom/src/structure/html/HTMLNavElement.ts +0 -16
  134. package/packages/dom/src/structure/html/HTMLNoscriptElement.ts +0 -16
  135. package/packages/dom/src/structure/html/HTMLOListElement.ts +0 -20
  136. package/packages/dom/src/structure/html/HTMLObjectElement.ts +0 -34
  137. package/packages/dom/src/structure/html/HTMLOptGroupElement.ts +0 -18
  138. package/packages/dom/src/structure/html/HTMLOptionElement.ts +0 -20
  139. package/packages/dom/src/structure/html/HTMLOutputElement.ts +0 -20
  140. package/packages/dom/src/structure/html/HTMLParagraphElement.ts +0 -17
  141. package/packages/dom/src/structure/html/HTMLPictureElement.ts +0 -16
  142. package/packages/dom/src/structure/html/HTMLPreElement.ts +0 -17
  143. package/packages/dom/src/structure/html/HTMLProgressElement.ts +0 -19
  144. package/packages/dom/src/structure/html/HTMLQuoteElement.ts +0 -17
  145. package/packages/dom/src/structure/html/HTMLRPElement.ts +0 -16
  146. package/packages/dom/src/structure/html/HTMLRTElement.ts +0 -16
  147. package/packages/dom/src/structure/html/HTMLRubyElement.ts +0 -16
  148. package/packages/dom/src/structure/html/HTMLSElement.ts +0 -16
  149. package/packages/dom/src/structure/html/HTMLSampElement.ts +0 -16
  150. package/packages/dom/src/structure/html/HTMLScriptElement.ts +0 -27
  151. package/packages/dom/src/structure/html/HTMLSectionElement.ts +0 -16
  152. package/packages/dom/src/structure/html/HTMLSelectElement.ts +0 -27
  153. package/packages/dom/src/structure/html/HTMLSlotElement.ts +0 -17
  154. package/packages/dom/src/structure/html/HTMLSmallElement.ts +0 -16
  155. package/packages/dom/src/structure/html/HTMLSourceElement.ts +0 -21
  156. package/packages/dom/src/structure/html/HTMLSpanElement.ts +0 -16
  157. package/packages/dom/src/structure/html/HTMLStrongElement.ts +0 -16
  158. package/packages/dom/src/structure/html/HTMLStyleElement.ts +0 -18
  159. package/packages/dom/src/structure/html/HTMLSubElement.ts +0 -16
  160. package/packages/dom/src/structure/html/HTMLSummaryElement.ts +0 -16
  161. package/packages/dom/src/structure/html/HTMLSupElement.ts +0 -16
  162. package/packages/dom/src/structure/html/HTMLTableCaptionElement.ts +0 -17
  163. package/packages/dom/src/structure/html/HTMLTableCellElement.ts +0 -31
  164. package/packages/dom/src/structure/html/HTMLTableColElement.ts +0 -23
  165. package/packages/dom/src/structure/html/HTMLTableElement.ts +0 -26
  166. package/packages/dom/src/structure/html/HTMLTableRowElement.ts +0 -23
  167. package/packages/dom/src/structure/html/HTMLTableSectionElement.ts +0 -20
  168. package/packages/dom/src/structure/html/HTMLTemplateElement.ts +0 -17
  169. package/packages/dom/src/structure/html/HTMLTextAreaElement.ts +0 -33
  170. package/packages/dom/src/structure/html/HTMLTimeElement.ts +0 -17
  171. package/packages/dom/src/structure/html/HTMLTitleElement.ts +0 -17
  172. package/packages/dom/src/structure/html/HTMLTrackElement.ts +0 -21
  173. package/packages/dom/src/structure/html/HTMLUElement.ts +0 -16
  174. package/packages/dom/src/structure/html/HTMLUListElement.ts +0 -18
  175. package/packages/dom/src/structure/html/HTMLVarElement.ts +0 -16
  176. package/packages/dom/src/structure/html/HTMLVideoElement.ts +0 -22
  177. package/packages/dom/src/structure/html/HTMLWBRElement.ts +0 -16
  178. package/packages/html/package.json +0 -18
  179. package/packages/html/src/index.ts +0 -13
  180. package/packages/html/src/node/$Anchor.ts +0 -49
  181. package/packages/html/src/node/$Canvas.ts +0 -38
  182. package/packages/html/src/node/$Dialog.ts +0 -16
  183. package/packages/html/src/node/$Form.ts +0 -16
  184. package/packages/html/src/node/$Image.ts +0 -72
  185. package/packages/html/src/node/$Input.ts +0 -193
  186. package/packages/html/src/node/$Label.ts +0 -25
  187. package/packages/html/src/node/$Media.ts +0 -16
  188. package/packages/html/src/node/$OptGroup.ts +0 -23
  189. package/packages/html/src/node/$Option.ts +0 -40
  190. package/packages/html/src/node/$Select.ts +0 -76
  191. package/packages/html/src/node/$TextArea.ts +0 -16
  192. package/packages/i18n/README.md +0 -73
  193. package/packages/i18n/package.json +0 -19
  194. package/packages/i18n/src/index.ts +0 -140
  195. package/packages/i18n/src/structure/I18n.ts +0 -44
  196. package/packages/i18n/src/structure/I18nDictionary.ts +0 -31
  197. package/packages/i18n/src/structure/I18nTranslation.ts +0 -41
  198. package/packages/idb/README.md +0 -127
  199. package/packages/idb/package.json +0 -19
  200. package/packages/idb/src/core.ts +0 -6
  201. package/packages/idb/src/index.ts +0 -17
  202. package/packages/idb/src/lib/$IDBRequest.ts +0 -8
  203. package/packages/idb/src/structure/$IDB.ts +0 -63
  204. package/packages/idb/src/structure/$IDBCursor.ts +0 -34
  205. package/packages/idb/src/structure/$IDBIndex.ts +0 -48
  206. package/packages/idb/src/structure/$IDBStore.ts +0 -103
  207. package/packages/idb/src/structure/$IDBStoreBase.ts +0 -30
  208. package/packages/idb/src/structure/$IDBTransaction.ts +0 -38
  209. package/packages/idb/src/structure/builder/$IDBBuilder.ts +0 -229
  210. package/packages/idb/src/structure/builder/$IDBStoreBuilder.ts +0 -100
  211. package/packages/markdown/README.md +0 -53
  212. package/packages/markdown/package.json +0 -19
  213. package/packages/markdown/src/index.ts +0 -3
  214. package/packages/markdown/src/lib/type.ts +0 -26
  215. package/packages/markdown/src/lib/util.ts +0 -21
  216. package/packages/markdown/src/structure/Markdown.ts +0 -54
  217. package/packages/markdown/src/structure/MarkdownLexer.ts +0 -111
  218. package/packages/markdown/src/structure/MarkdownParser.ts +0 -34
  219. package/packages/markdown/src/syntax/alert.ts +0 -46
  220. package/packages/markdown/src/syntax/blockquote.ts +0 -35
  221. package/packages/markdown/src/syntax/bold.ts +0 -11
  222. package/packages/markdown/src/syntax/code.ts +0 -11
  223. package/packages/markdown/src/syntax/codeblock.ts +0 -44
  224. package/packages/markdown/src/syntax/heading.ts +0 -14
  225. package/packages/markdown/src/syntax/horizontalRule.ts +0 -11
  226. package/packages/markdown/src/syntax/image.ts +0 -23
  227. package/packages/markdown/src/syntax/italic.ts +0 -11
  228. package/packages/markdown/src/syntax/link.ts +0 -46
  229. package/packages/markdown/src/syntax/list.ts +0 -121
  230. package/packages/markdown/src/syntax/table.ts +0 -67
  231. package/packages/markdown/src/syntax/text.ts +0 -19
  232. package/packages/router/README.md +0 -175
  233. package/packages/router/package.json +0 -19
  234. package/packages/router/src/index.ts +0 -68
  235. package/packages/router/src/node/Page.ts +0 -38
  236. package/packages/router/src/node/Router.ts +0 -212
  237. package/packages/router/src/node/RouterAnchor.ts +0 -24
  238. package/packages/router/src/structure/PageBuilder.ts +0 -24
  239. package/packages/router/src/structure/Route.ts +0 -105
  240. package/packages/signal/README.md +0 -93
  241. package/packages/signal/package.json +0 -18
  242. package/packages/signal/src/index.ts +0 -221
  243. package/packages/signal/src/structure/Signal.ts +0 -38
  244. package/packages/ui/lib/VirtualScroll.ts +0 -25
  245. package/packages/ui/node/Accordian.ts +0 -97
  246. package/packages/ui/node/Carousel.ts +0 -20
  247. package/packages/ui/node/Form.ts +0 -54
  248. package/packages/ui/node/Grid.ts +0 -0
  249. package/packages/ui/node/Modal.ts +0 -45
  250. package/packages/ui/node/Table.ts +0 -43
  251. package/packages/ui/node/Tabs.ts +0 -129
  252. package/packages/ui/node/Toast.ts +0 -16
  253. package/packages/ui/node/Waterfall.ts +0 -94
  254. package/packages/ui/package.json +0 -21
  255. package/packages/utils/package.json +0 -17
  256. package/packages/utils/src/global.ts +0 -25
  257. package/packages/utils/src/index.ts +0 -90
@@ -1,19 +0,0 @@
1
- import { EMPTY_LINE, TEXT, TEXT_LINE } from "#lib/type";
2
- import { htmltag, setProcessor } from "#lib/util";
3
- import type { MarkdownParser } from "#structure/MarkdownParser";
4
-
5
- export const textProcessor = (parser: MarkdownParser) => setProcessor(parser, TEXT, token => token.text!);
6
-
7
- export const textLineProcessor = (parser: MarkdownParser) => setProcessor(parser, TEXT_LINE, (_, tokens) => {
8
- let html = '';
9
- let i = 0;
10
- for (const token of tokens) {
11
- if (token.type === EMPTY_LINE) break;
12
- html += parser.parse(token.content!);
13
- i++;
14
- }
15
- return {
16
- html: htmltag('p', html),
17
- skipTokens: i
18
- };
19
- })
@@ -1,175 +0,0 @@
1
- # amateras/router
2
-
3
- ## Usage
4
- ```ts
5
- import 'amateras';
6
- import 'amateras/router';
7
- ```
8
-
9
- ## Create Route Map
10
- ```ts
11
- // create home page route
12
- const HomePage = $.route(page => page
13
- .pageTitle('Home | My Site') // set window title
14
- .content([
15
- $('h1').content('Home')
16
- ])
17
- )
18
- // append router and mapping home page route into router
19
- $(document.body).content([
20
- $('router')
21
- .route('/', HomePage)
22
- .route('/hello', page => 'Hello!')
23
- .listen() // start to listen path change
24
- ])
25
- ```
26
-
27
- > [!NOTE]
28
- > Don't forget to `.listen()` the path change!
29
-
30
- ## Router Anchor
31
- Use `RouterAnchor` to prevent load page when open link by default `HTMLAnchorElement` element.
32
- ```ts
33
- $('ra').content('Contact').href('/contact');
34
- ```
35
-
36
- ## Common Methods
37
- - `$.open(url)`: Open page without load page.
38
- - `$.replace(url)`: Replace history state with url and open page.
39
- - `$.forward()`: Forward page.
40
- - `$.back()`: Back page.
41
-
42
- ## Async Route
43
- ```ts
44
- // ./page/home_page.ts
45
- export default $.route(page => {
46
- return page
47
- .content([
48
- $('h1').content('Home Page')
49
- ])
50
- })
51
-
52
- // ./router.ts
53
- $('router')
54
- .route('/about', () => import('./page/home_page'))
55
- ```
56
-
57
- ## Path Parameter
58
- TypeScript will parse the parameter in the path, parameter always start with `:`, after the colon comes the name of the parameter. You can access theses parameter using `Page.params`.
59
-
60
- ```ts
61
- $('router')
62
- .route('/user/@:username', page => {
63
- console.log(page.params.username);
64
- return page;
65
- })
66
- .listen()
67
- // simulate page open
68
- .resolve('/user/@amateras') // 'amateras'
69
- ```
70
-
71
- If you want separate router and route builder to different file, use generic parameter to define parameter name on `$.route` method.
72
-
73
- ```ts
74
- // greating_page.ts
75
- export default $.route<['name']>(page => {
76
- return page
77
- .content(`Hello, ${name}`)
78
- })
79
-
80
- // router.ts
81
- $('router')
82
- .route('/greating', () => import('./greating_page'))
83
- // ^ typescript wiil report an error, the route builder required 'name' parameter
84
-
85
- .route('/greating/:name', () => import('./greating_page'))
86
- // ^ pass
87
- ```
88
-
89
- ### Optional Parameter
90
- Sometime we parameter can be optional, you can define the optional parameter by add `?` sign after the name of the parameter.
91
-
92
- ```ts
93
- const userPage = $.route<'photoId', 'postId?'>(page => {
94
- return page
95
- .content([
96
- `Photo ID: ${page.params.photoId}`, // photoId: string
97
- `Post ID: ${page.params.postId}` // postId: string | undefined
98
- ])
99
- })
100
-
101
- $('router')
102
- .route('/photos/:photoId', userPage)
103
- // ^ pass
104
- .route('/posts/:postId/photos/:photoId', userPage)
105
- // ^ pass
106
- ```
107
-
108
- ## Nesting Route
109
- `Router` element is the container of `Page` element, we can archieve nesting route by create `Router` and append it inside `Page`.
110
- ```ts
111
- const ContactPage = $.route(page => page
112
- .pageTitle('Home')
113
- .content([
114
- $('h1').content('Contact'),
115
- $('router', page)
116
- // here is the magic happened,
117
- // pass the Page into router arguments
118
- ])
119
- )
120
- ```
121
-
122
- Then, we need to declare the router map like this:
123
-
124
- ```ts
125
- $('router')
126
- .route('/', HomePage)
127
- .route('/contact', ContactPage, route => route
128
- // we can define more child routes inside this '/contact' route!
129
- .route('/', () => 'My name is Amateras.')
130
- .route('/phone', () => '0123456789')
131
- )
132
- ```
133
-
134
- ### Alias Path
135
-
136
- Sometime, the page doesn't have just one path. We can declare the alias paths to one route!
137
-
138
- ```ts
139
- $('router')
140
- .route('/', HomePage, route => route
141
- .alias('/home')
142
- .alias('/the/another/way/to/home')
143
- // ... more alias path
144
- )
145
- ```
146
-
147
- What if the main path included parameters? Here is how to deal with it:
148
-
149
- ```ts
150
- $('router')
151
- .route('/users/:username', UserPage, route = route
152
- .alias('/u/:username')
153
- .alias('/profile')
154
- // ^ typescript will report an error
155
-
156
- .alias('/profile', { username: 'amateras' })
157
- // ^ pass, the params required is fulfilled
158
-
159
- .alias('/profile', () => { return { username: getUsername() } })
160
- // ^ even you can pass an arrow function!
161
- )
162
- ```
163
-
164
- ### Group Path
165
-
166
- There have a lot of paths got same prefix? We provide the solution:
167
-
168
- ```ts
169
- $('router')
170
- .route('/', HomePage)
171
- .group('/search', route => route
172
- .route('/', SearchPage)
173
- .route('/users', SearchUserPage)
174
- )
175
- ```
@@ -1,19 +0,0 @@
1
- {
2
- "name": "@amateras/router",
3
- "peerDependencies": {
4
- "@amateras/core": "workspace:*",
5
- "@amateras/html": "workspace:*",
6
- "@amateras/utils": "workspace:*"
7
- },
8
- "imports": {
9
- "#structure/*": "./src/structure/*.ts",
10
- "#lib/*": "./src/lib/*.ts",
11
- "#node/*": "./src/node/*.ts"
12
- },
13
- "exports": {
14
- ".": "./src/index.ts",
15
- "./structure/*": "./src/structure/*.ts",
16
- "./lib/*": "./src/lib/*.ts",
17
- "./node/*": "./src/node/*.ts"
18
- }
19
- }
@@ -1,68 +0,0 @@
1
- import type { Page } from "#node/Page";
2
- import { Router } from "#node/Router";
3
- import { PageBuilder } from "#structure/PageBuilder";
4
- import { Route, type RouteBuilder, type RouteParams } from "#structure/Route";
5
- import { _Object_assign, _bind, forEach } from "@amateras/utils";
6
- import type { AnchorTarget } from "../../html/src/node/$Anchor";
7
-
8
- declare module '@amateras/core' {
9
- export namespace $ {
10
- export function route<Params extends RouteParams = []>(builder: (page: Page<Params>) => OrPromise<Page<Params>>): PageBuilder<Params>;
11
- export function open(url: string | URL | Nullish, target?: AnchorTarget): typeof Router;
12
- export function replace(url: string | URL | Nullish): typeof Router;
13
- export function back(): typeof Router;
14
- export function forward(): typeof Router;
15
- export interface $NodeMap {
16
- 'router': typeof Router;
17
- }
18
- }
19
- }
20
-
21
- declare global {
22
- interface GlobalEventHandlersEventMap {
23
- 'routeopen': Event;
24
- }
25
- }
26
-
27
- let prototype = {
28
- route(this: { routes: Map<string, Route> }, path: string, builder: RouteBuilder, handle?: (route: Route) => Route) {
29
- const route = new Route<any>(path, builder);
30
- handle?.(route);
31
- this.routes.set(path, route);
32
- return this;
33
- },
34
-
35
- group(this: { routes: Map<string, Route> }, path: string, handle: (route: Route) => Route) {
36
- this.routes.set(path, handle(new Route<any>(path)))
37
- return this;
38
- },
39
-
40
- notFound(this: { routes: Map<string, Route> }, builder: RouteBuilder) {
41
- this.routes.set('notfound', new Route('notfound', builder));
42
- return this;
43
- }
44
- }
45
-
46
- // assign methods
47
- _Object_assign(Router.prototype, prototype)
48
- _Object_assign(Route.prototype, prototype)
49
- _Object_assign($, {
50
- route: (builder: (page: Page) => Page) => new PageBuilder(builder),
51
- open: _bind(Router.open, Router),
52
- replace: _bind(Router.replace, Router),
53
- back: _bind(Router.back, Router),
54
- forward: _bind(Router.forward, Router)
55
- })
56
- // assign node
57
- $.assign(['router', Router])
58
- // use style
59
- forEach([
60
- `router{display:block}`,
61
- `page{display:block}`
62
- ], $.style);
63
-
64
- export * from '#node/Page';
65
- export * from '#node/Router';
66
- export * from '#node/RouterAnchor';
67
- export * from '#structure/PageBuilder';
68
- export * from '#structure/Route';
@@ -1,38 +0,0 @@
1
- import type { RouteParams } from "#structure/Route";
2
- import { chain } from "@amateras/core/lib/chain";
3
- import { $HTMLElement } from "@amateras/core/node/$HTMLElement";
4
- import { _null } from "@amateras/utils";
5
- import type { Router } from "./Router";
6
-
7
-
8
- export class Page<Params extends RouteParams = []> extends $HTMLElement {
9
- params: PageParamsResolver<Params>;
10
- router: null | Router = _null
11
- #pageTitle: string | null = _null;
12
- built = false;
13
- pathId: string;
14
- constructor(pathId: string, params: PageParamsResolver<Params>) {
15
- super('page');
16
- this.params = params;
17
- this.pathId = pathId;
18
- }
19
-
20
- pageTitle(): string | null;
21
- pageTitle(title: string | null): this;
22
- pageTitle(title?: string | null) {
23
- return chain(this, arguments, () => this.#pageTitle, title, title => this.#pageTitle = title)
24
- }
25
-
26
- }
27
-
28
- export type PageParams = { [key: string]: string }
29
- export type PageParamsResolver<Params extends string[]> =
30
- Prettify<
31
- Params extends [`${infer String}`, ...infer Rest]
32
- ? Rest extends string[]
33
- ? String extends `${infer Key}?`
34
- ? { [key in Key]?: string } & PageParamsResolver<Rest>
35
- : { [key in String]: string } & PageParamsResolver<Rest>
36
- : never
37
- : {}
38
- >
@@ -1,212 +0,0 @@
1
- import { Route, type RouteBuilder, type RoutePath, type RouteParamsResolver, type RouteParams, type RouteParamsStrings, type AsyncPageBuilder } from "../structure/Route";
2
- import type { AnchorTarget } from "../../../html/src/node/$Anchor";
3
- import { Page, type PageParams } from "./Page";
4
- import type { PageBuilder, PageBuilderFunction } from "#structure/PageBuilder";
5
- import { _document } from "@amateras/core/env";
6
- import { $HTMLElement } from "@amateras/core/node/$HTMLElement";
7
- import { _instanceof, startsWith, _JSON_parse, forEach, _Object_entries, _JSON_stringify, isFunction } from "@amateras/utils";
8
- // history index
9
- let index = 0;
10
- const _addEventListener = addEventListener;
11
- const _location = location;
12
- const {origin} = _location;
13
- const _history = history;
14
- const _sessionStorage = sessionStorage;
15
- const documentElement = _document.documentElement;
16
- const [PUSH, REPLACE] = [1, 2] as const;
17
- const [FORWARD, BACK] = ['forward', 'back'] as const;
18
- const scrollStorageKey = '__scroll__';
19
- /** convert path string to URL object */
20
- const toURL = (path: string | URL) =>
21
- _instanceof(path, URL) ? path : startsWith(path, 'http') ? new URL(path) : new URL(startsWith(path, origin) ? path : origin + path);
22
-
23
- type ScrollData = {[key: number]: {x: number, y: number}};
24
- const scrollRecord = (e?: Event) => {
25
- const data = _JSON_parse(_sessionStorage.getItem(scrollStorageKey) ?? '{}') as ScrollData;
26
- data[index] = { x: documentElement.scrollLeft, y: documentElement.scrollTop };
27
- // e is Event when called from scroll or beforeload
28
- if (!e) forEach(_Object_entries(data), ([i]) => +i > index && delete data[+i])
29
- _sessionStorage.setItem(scrollStorageKey, _JSON_stringify(data));
30
- }
31
- /** handle history state with push and replace state. */
32
- const historyHandler = async (path: string | URL | Nullish, mode: 1 | 2, target?: AnchorTarget) => {
33
- if (!path) return;
34
- const url = toURL(path);
35
- if (url.href === _location.href) return;
36
- if (target && target !== '_self') return open(url, target);
37
- if (url.origin !== origin) return open(url, target);
38
- scrollRecord();
39
- if (mode === PUSH) index += 1;
40
- Router.direction = FORWARD;
41
- Router.prevUrl = toURL(_location.href);
42
- _history[mode === PUSH ? 'pushState' : 'replaceState']({index}, '' , url);
43
- Router.url = url;
44
- forEach(Router.routers, router => router.resolve(path))
45
- }
46
- // disable browser scroll restoration
47
- _history.scrollRestoration = 'manual';
48
-
49
- type RouteData = { route: Route, params: PageParams, pathId: string }
50
- export class Router extends $HTMLElement {
51
- static direction: 'back' | 'forward' = FORWARD;
52
- static routers = new Set<Router>();
53
- static url: URL = toURL(_location.href);
54
- static prevUrl: URL | null = null;
55
- routes = new Map<RoutePath, Route>();
56
- pages = new Map<string, Page>();
57
- constructor(page?: Page) {
58
- super('router');
59
- if (page) page.router = this;
60
- else Router.routers.add(this);
61
- }
62
-
63
- static open(path: string | URL | Nullish, target?: AnchorTarget) {
64
- historyHandler(path, PUSH, target);
65
- return this;
66
- }
67
-
68
- static back() {
69
- _history.back();
70
- return this;
71
- }
72
-
73
- static forward() {
74
- _history.forward();
75
- return this;
76
- }
77
-
78
- static replace(path: string | URL | Nullish) {
79
- historyHandler(path, REPLACE);
80
- return this;
81
- }
82
-
83
- static get scroll(): ScrollData[number] {
84
- return _JSON_parse(_sessionStorage.getItem(scrollStorageKey) ?? '{}')[index] ?? {x: 0, y: 0}
85
- }
86
-
87
- listen() {
88
- const resolve = () => {
89
- const stateIndex = _history.state?.index ?? 0;
90
- if (index > stateIndex) Router.direction = BACK;
91
- if (index < stateIndex) Router.direction = FORWARD;
92
- index = stateIndex;
93
- Router.prevUrl = Router.url;
94
- Router.url = toURL(_location.href);
95
- this.resolve(_location.href);
96
- }
97
- _addEventListener('popstate', resolve);
98
- _addEventListener('beforeunload', scrollRecord);
99
- _addEventListener('scroll', scrollRecord, false);
100
- resolve();
101
- return this;
102
- }
103
-
104
- async resolve(path: string | URL, force = false): Promise<this> {
105
- const {pathname, href} = toURL(path);
106
- const routes = this.getRoutes(this.routes, pathname);
107
- const routerPageMap = new Map<Router, Page>();
108
- let prevRouter: Router | null = this;
109
- let pathIdStr = '';
110
- let paramsData = {}
111
- await forEach(routes, async ({route, params, pathId}) => {
112
- pathIdStr += pathId;
113
- paramsData = {...paramsData, ...params}
114
- // skip route group
115
- const builderResolver = route.builder;
116
- if (!builderResolver) return;
117
- // get page from cache or create new page
118
- const page = route.pages.get(pathId) ?? new Page(pathIdStr, paramsData);
119
- // resolve builder
120
- if (!page.built) await builderResolver.build(page);
121
- page.built = true;
122
- // set router to page map
123
- if (prevRouter) routerPageMap.set(prevRouter, page)
124
- // set cache
125
- if (!force) route.pages.set(pathId, page);
126
- prevRouter = page.router;
127
- })
128
-
129
- forEach(routerPageMap, ([router, page]) => {
130
- // set title
131
- _document && (_document.title = page.pageTitle() ?? _document.title ?? '');
132
- // check location is still same, page parent is not router before insert page
133
- if (force || (href === _location.href && page.parentNode !== router.node)) router.content(page);
134
- })
135
- // handle scroll restoration
136
- let { x, y } = Router.scroll ?? {x: 0, y: 0};
137
- scrollTo(x, y);
138
- // event
139
- this.dispatchEvent(new Event('routeopen', {bubbles: true}));
140
- return this;
141
- }
142
-
143
- protected getRoutes(routes: typeof this.routes, targetPath: string): RouteData[] {
144
- const split = (p: string) => p.replaceAll(/\/+/g, '/').replace(/^\//, '').split('/').map(path => `/${path}`);
145
- let targetPathSplit = split(targetPath);
146
- if (!routes.size) return [];
147
- // check each route
148
- for (const [_, route] of routes) {
149
- // check each path pass
150
- routePathLoop: for (const [path, paramsHandle] of route.paths) {
151
- let routePathSplit = split(path);
152
- let targetPathNodePosition = 0;
153
- let params: { [key: string]: string } = isFunction(paramsHandle) ? paramsHandle() : paramsHandle ?? {};
154
- let pathId = '';
155
- // check each path node
156
- pathNodeLoop: for (let i = 0; i < routePathSplit.length; i++) {
157
- // reset target path node position
158
- targetPathNodePosition = i;
159
- const routeNode = routePathSplit[i];
160
- const targetNode = targetPathSplit[i];
161
- // path node undefined, break path loop
162
- if (!routeNode || !targetNode) continue routePathLoop;
163
- // path node is params node
164
- if (routeNode.includes(':')) {
165
- // target not matched
166
- if (targetNode === '/') continue routePathLoop;
167
- const [prefix, paramName] = routeNode.split(':') as [string, string];
168
- if (!startsWith(targetNode, prefix)) continue routePathLoop;
169
- params[paramName] = targetNode.replace(`${prefix}`, '');
170
- pathId += targetNode;
171
- continue pathNodeLoop;
172
- }
173
- // path node not matched, next path
174
- if (routeNode !== targetNode) continue routePathLoop;
175
- pathId += targetNode;
176
- }
177
- // target path node longer than route, next route
178
- if (targetPathSplit[targetPathNodePosition + 1] && !route.routes.size) continue routePathLoop;
179
- // all path node passed, route found
180
- return [{route, params, pathId}, ...this.getRoutes(route.routes, targetPathSplit.slice(targetPathNodePosition + 1).join('/'))]
181
- }
182
- }
183
- // no route passed
184
- const notfound = routes.get('notfound');
185
- if (notfound) return [{route: notfound, params: {}, pathId: 'notfound'}]
186
- return [];
187
- }
188
-
189
- protected getPage() {
190
-
191
- }
192
- }
193
-
194
- export interface Router {
195
- route<
196
- P extends RoutePath,
197
- B extends PageBuilder
198
- >(path: P, builder: B, handle?: (route: Route<P, B['params']>) => Route<P>): Router
199
- route<
200
- K extends RoutePath,
201
- P extends RouteParamsStrings<K>,
202
- F extends PageBuilderFunction<P>
203
- >(path: K, builder: F, handle?: (route: Route<K, P>) => Route<K, P>): this
204
- route<
205
- K extends RoutePath,
206
- P extends RouteParamsStrings<K>,
207
- F extends AsyncPageBuilder<P>
208
- >(path: K, builder: F, handle?: (route: Route<K, P>) => Route<K, P>): this
209
- route<P extends RoutePath>(path: P, builder: RouteBuilder<RouteParamsResolver<P>>, handle?: (route: Route<P>) => Route<P>): Router
210
- group<P extends RoutePath>(path: P, handle: <R extends Route<P>>(route: R) => R): this;
211
- notFound(builder: RouteBuilder<RouteParamsResolver<RoutePath>>): this;
212
- }
@@ -1,24 +0,0 @@
1
- import { $Anchor } from "@amateras/html/node/$Anchor";
2
-
3
- export class RouterAnchor extends $Anchor {
4
- constructor() {
5
- super();
6
- this.on('click', e => {
7
- if (e.shiftKey || e.ctrlKey) return;
8
- e.preventDefault();
9
- this.target() === '_replace'
10
- ? $.replace(this.href())
11
- : $.open(this.href(), this.target())
12
- })
13
- }
14
- }
15
-
16
- declare module '@amateras/core' {
17
- export namespace $ {
18
- export interface $NodeMap {
19
- 'ra': typeof RouterAnchor;
20
- }
21
- }
22
- }
23
-
24
- $.assign(['ra', RouterAnchor]);
@@ -1,24 +0,0 @@
1
- import { Page } from "#node/Page";
2
- import type { $NodeContentResolver } from "@amateras/core/node/$Node";
3
- import { _instanceof, _Promise } from "@amateras/utils";
4
- import type { AsyncPageBuilder, RouteParams } from "./Route";
5
-
6
- export class PageBuilder<Params extends RouteParams = any> {
7
- params!: Params
8
- #builder: PageBuilderFunction<Params> | AsyncPageBuilder<Params>;
9
- constructor(builder: PageBuilderFunction<Params>) {
10
- this.#builder = builder;
11
- }
12
-
13
- async build(page: Page<Params>): Promise<Page<Params>> {
14
- const resolver = this.#builder(page)
15
- const handle = async (result: any) => {
16
- if (_instanceof(result, Page)) return result;
17
- else if (result[Symbol.toStringTag] === 'Module') return await result.default.build(page);
18
- else return page.content(result);
19
- }
20
- return handle(_instanceof(resolver, _Promise) ? await resolver : resolver);
21
- }
22
- }
23
-
24
- export type PageBuilderFunction<Params extends RouteParams> = (page: Page<Params>) => OrPromise<Page<Params> | $NodeContentResolver<Page<Params>>>