aptechka 0.1.1 → 0.1.3

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 (300) hide show
  1. package/lib/Damped-DdPj4Sho.cjs +1 -0
  2. package/lib/Damped-H3PuZwKV.js +152 -0
  3. package/lib/SourceManager-tmQlXkSD.cjs +1 -0
  4. package/lib/SourceManager-vXInLlh_.js +108 -0
  5. package/lib/Store-qq7IjRLE.js +136 -0
  6. package/lib/Store-txkR3wXU.cjs +1 -0
  7. package/lib/WheelControls-9ZQKSYYq.js +83 -0
  8. package/lib/WheelControls-TYhGU6oA.cjs +1 -0
  9. package/lib/accordion/AccordionElement.d.ts +7 -0
  10. package/lib/accordion/index.d.ts +1 -0
  11. package/lib/animation/Animated.d.ts +41 -0
  12. package/lib/animation/Damped.d.ts +11 -0
  13. package/lib/animation/Tweened.d.ts +16 -0
  14. package/lib/animation/index.cjs +1 -0
  15. package/lib/animation/index.d.ts +3 -0
  16. package/lib/animation/index.js +56 -0
  17. package/lib/attribute/index.cjs +1 -0
  18. package/lib/attribute/index.d.ts +8 -0
  19. package/lib/attribute/index.js +47 -0
  20. package/lib/attributes-_2U65GDl.cjs +1 -0
  21. package/lib/attributes-w0u-KiIb.js +10 -0
  22. package/lib/browser-QbF6EW-w.cjs +1 -0
  23. package/lib/browser-S4eq8AeN.js +4 -0
  24. package/lib/canvas-2d/index.cjs +5 -0
  25. package/lib/canvas-2d/index.d.ts +30 -0
  26. package/lib/canvas-2d/index.js +90 -0
  27. package/lib/controls/Controls.d.ts +9 -0
  28. package/lib/controls/KeyboardControls.d.ts +10 -0
  29. package/lib/controls/LinearControls.d.ts +11 -0
  30. package/lib/controls/WheelControls.d.ts +17 -0
  31. package/lib/controls/index.cjs +1 -0
  32. package/lib/controls/index.d.ts +4 -0
  33. package/lib/controls/index.js +42 -0
  34. package/lib/css-unit-parser/index.cjs +8 -0
  35. package/lib/css-unit-parser/index.d.ts +6 -0
  36. package/lib/css-unit-parser/index.js +36 -0
  37. package/lib/custom-element/index.cjs +1 -0
  38. package/lib/custom-element/index.d.ts +8 -0
  39. package/lib/custom-element/index.js +16 -0
  40. package/lib/device/Device.d.ts +17 -0
  41. package/lib/device/Viewport.d.ts +27 -0
  42. package/lib/device/index.cjs +1 -0
  43. package/lib/device/index.d.ts +2 -0
  44. package/lib/device/index.js +123 -0
  45. package/lib/dom-QhWsdQS2.cjs +1 -0
  46. package/lib/dom-bHEwc_xV.js +24 -0
  47. package/lib/easings-_wFs9ZWc.cjs +1 -0
  48. package/lib/easings-uF-tgahf.js +19 -0
  49. package/lib/element-constructor/ElementConstructor.d.ts +76 -0
  50. package/lib/element-constructor/htmlTags.d.ts +113 -0
  51. package/lib/element-constructor/index.cjs +1 -0
  52. package/lib/element-constructor/index.d.ts +4 -0
  53. package/lib/element-constructor/index.js +1390 -0
  54. package/lib/element-constructor/specialObjects.d.ts +2 -0
  55. package/lib/element-constructor/svgTags.d.ts +64 -0
  56. package/lib/en3/attachments/En3SourceManager.d.ts +30 -0
  57. package/lib/en3/core/en3.d.ts +55 -0
  58. package/lib/en3/index.cjs +28 -0
  59. package/lib/en3/index.d.ts +18 -0
  60. package/lib/en3/index.js +3249 -0
  61. package/lib/en3/instances/en3LazyLoader.d.ts +7 -0
  62. package/lib/en3/libs/MeshoptDecoder.d.ts +1 -0
  63. package/lib/en3/loaders/en3GLTFLoader.d.ts +16 -0
  64. package/lib/en3/loaders/en3TextureLoader.d.ts +2 -0
  65. package/lib/en3/objects/En3Clip.d.ts +10 -0
  66. package/lib/en3/objects/En3ClipHelpers.d.ts +5 -0
  67. package/lib/en3/objects/En3GLTF.d.ts +9 -0
  68. package/lib/en3/objects/En3Image.d.ts +7 -0
  69. package/lib/en3/objects/En3ImageLike.d.ts +22 -0
  70. package/{src/packages/en3/objects/En3SourceConsumer.ts → lib/en3/objects/En3SourceConsumer.d.ts} +2 -3
  71. package/lib/en3/objects/En3Video.d.ts +12 -0
  72. package/lib/en3/test/En3HTML.d.ts +9 -0
  73. package/lib/en3/test/En3ModifiedMaterial.d.ts +32 -0
  74. package/lib/en3/test/En3Raycaster.d.ts +19 -0
  75. package/lib/en3/utils/coverTexture.d.ts +6 -0
  76. package/lib/en3/utils/dispose.d.ts +2 -0
  77. package/lib/en3/utils/traverseMaterials.d.ts +2 -0
  78. package/lib/en3/utils/traverseMeshes.d.ts +2 -0
  79. package/lib/function-QP7IreXR.cjs +1 -0
  80. package/lib/function-zwSFehNd.js +17 -0
  81. package/lib/image/index.cjs +1 -0
  82. package/lib/image/index.d.ts +10 -0
  83. package/lib/image/index.js +30 -0
  84. package/lib/intersector/index.cjs +1 -0
  85. package/lib/intersector/index.d.ts +8 -0
  86. package/lib/intersector/index.js +55 -0
  87. package/lib/ladder/index.cjs +1 -0
  88. package/lib/ladder/index.d.ts +21 -0
  89. package/lib/ladder/index.js +97 -0
  90. package/lib/layout-5SJlcXTY.js +12 -0
  91. package/lib/layout-box/index.cjs +1 -0
  92. package/lib/layout-box/index.d.ts +78 -0
  93. package/lib/layout-box/index.js +235 -0
  94. package/lib/layout-rZzbsLPd.cjs +1 -0
  95. package/lib/loading/index.cjs +1 -0
  96. package/lib/loading/index.d.ts +38 -0
  97. package/lib/loading/index.js +99 -0
  98. package/lib/math-FNUBmQPK.cjs +1 -0
  99. package/lib/math-_a3IpKOc.js +45 -0
  100. package/lib/measurer/CumulativeOffsetLeft.d.ts +4 -0
  101. package/lib/measurer/CumulativeOffsetTop.d.ts +4 -0
  102. package/lib/measurer/Meaurer.d.ts +8 -0
  103. package/lib/measurer/index.cjs +1 -0
  104. package/lib/measurer/index.d.ts +3 -0
  105. package/lib/measurer/index.js +48 -0
  106. package/lib/media/index.cjs +1 -0
  107. package/lib/media/index.d.ts +11 -0
  108. package/lib/media/index.js +33 -0
  109. package/lib/morph/Link.d.ts +6 -0
  110. package/lib/morph/Morph.d.ts +26 -0
  111. package/lib/morph/index.cjs +1 -0
  112. package/lib/morph/index.d.ts +1 -0
  113. package/lib/morph/index.js +182 -0
  114. package/lib/notifier/index.cjs +1 -0
  115. package/lib/notifier/index.d.ts +8 -0
  116. package/lib/notifier/index.js +38 -0
  117. package/lib/number-2bR0KBcX.cjs +1 -0
  118. package/lib/number-bCHB2GAD.js +31 -0
  119. package/lib/order/index.cjs +1 -0
  120. package/lib/order/index.d.ts +13 -0
  121. package/lib/order/index.js +5 -0
  122. package/lib/resizer/index.cjs +1 -0
  123. package/lib/resizer/index.d.ts +10 -0
  124. package/lib/resizer/index.js +45 -0
  125. package/lib/router/Link.d.ts +6 -0
  126. package/lib/router/Route.d.ts +15 -0
  127. package/lib/router/RouteElement.d.ts +14 -0
  128. package/lib/router/Router.d.ts +33 -0
  129. package/lib/router/index.cjs +1 -0
  130. package/lib/router/index.d.ts +3 -0
  131. package/lib/router/index.js +266 -0
  132. package/lib/scroll/ScrollElement.d.ts +40 -0
  133. package/lib/scroll/ScrollUserElement.d.ts +7 -0
  134. package/lib/scroll/ScrollbarElement.d.ts +13 -0
  135. package/lib/scroll/index.cjs +71 -0
  136. package/lib/scroll/index.d.ts +2 -0
  137. package/lib/scroll/index.js +444 -0
  138. package/lib/scroll-entries/index.cjs +1 -0
  139. package/lib/scroll-entries/index.d.ts +17 -0
  140. package/lib/scroll-entries/index.js +56 -0
  141. package/lib/source/SourceClass.d.ts +11 -0
  142. package/lib/source/SourceElement.d.ts +16 -0
  143. package/lib/source/SourceManager.d.ts +13 -0
  144. package/lib/source/SourceSet.d.ts +8 -0
  145. package/lib/source/index.cjs +5 -0
  146. package/lib/source/index.d.ts +4 -0
  147. package/lib/source/index.js +95 -0
  148. package/lib/store/Composed.d.ts +7 -0
  149. package/lib/store/CumulativeResource.d.ts +12 -0
  150. package/lib/store/Derived.d.ts +7 -0
  151. package/lib/store/DerivedArray.d.ts +7 -0
  152. package/lib/store/Resource.d.ts +14 -0
  153. package/lib/store/Store.d.ts +33 -0
  154. package/lib/store/StoreRegistry.d.ts +22 -0
  155. package/lib/store/index.cjs +1 -0
  156. package/lib/store/index.d.ts +6 -0
  157. package/lib/store/index.js +99 -0
  158. package/lib/string-GmxZA5Nq.js +31 -0
  159. package/lib/string-WmNOGq16.cjs +1 -0
  160. package/lib/studio/StudioElement.d.ts +9 -0
  161. package/lib/studio/index.d.ts +1 -0
  162. package/lib/ticker/index.cjs +1 -0
  163. package/lib/ticker/index.d.ts +19 -0
  164. package/lib/ticker/index.js +107 -0
  165. package/lib/utils/array.d.ts +1 -0
  166. package/lib/utils/attributes.d.ts +2 -0
  167. package/lib/utils/browser.d.ts +1 -0
  168. package/lib/utils/canvas.d.ts +6 -0
  169. package/lib/utils/collisions.d.ts +3 -0
  170. package/lib/utils/coordinates.d.ts +13 -0
  171. package/lib/utils/decoding.d.ts +2 -0
  172. package/lib/utils/dev.d.ts +3 -0
  173. package/lib/utils/dom.d.ts +4 -0
  174. package/lib/utils/easings.d.ts +17 -0
  175. package/lib/utils/file.d.ts +1 -0
  176. package/lib/utils/function.d.ts +2 -0
  177. package/lib/utils/index.cjs +1 -0
  178. package/lib/utils/index.d.ts +19 -0
  179. package/lib/utils/index.js +203 -0
  180. package/lib/utils/layout.d.ts +3 -0
  181. package/lib/utils/math.d.ts +11 -0
  182. package/lib/utils/number.d.ts +3 -0
  183. package/lib/utils/object.d.ts +7 -0
  184. package/lib/utils/string.d.ts +5 -0
  185. package/lib/utils/ts-shape.d.ts +19 -0
  186. package/lib/utils/ts-utility.d.ts +19 -0
  187. package/lib/video/index.cjs +1 -0
  188. package/lib/video/index.d.ts +11 -0
  189. package/lib/video/index.js +37 -0
  190. package/package.json +7 -7
  191. package/.github/workflows/publish.yml +0 -20
  192. package/.prettierignore +0 -16
  193. package/.prettierrc +0 -9
  194. package/index.html +0 -32
  195. package/public/vite.svg +0 -1
  196. package/src/packages/animation/Animated.ts +0 -189
  197. package/src/packages/animation/Damped.ts +0 -39
  198. package/src/packages/animation/Tweened.ts +0 -51
  199. package/src/packages/animation/index.ts +0 -10
  200. package/src/packages/attribute/index.ts +0 -59
  201. package/src/packages/canvas-2d/index.ts +0 -137
  202. package/src/packages/controls/Controls.ts +0 -15
  203. package/src/packages/controls/KeyboardControls.ts +0 -63
  204. package/src/packages/controls/LinearControls.ts +0 -27
  205. package/src/packages/controls/User.ts +0 -20
  206. package/src/packages/controls/WheelControls.ts +0 -92
  207. package/src/packages/controls/index.ts +0 -5
  208. package/src/packages/css-unit-parser/index.ts +0 -32
  209. package/src/packages/custom-element/index.ts +0 -19
  210. package/src/packages/device/Device.ts +0 -113
  211. package/src/packages/device/Viewport.ts +0 -67
  212. package/src/packages/device/index.ts +0 -2
  213. package/src/packages/element-constructor/ElementConstructor.ts +0 -577
  214. package/src/packages/element-constructor/htmlTags.ts +0 -679
  215. package/src/packages/element-constructor/index.ts +0 -4
  216. package/src/packages/element-constructor/specialObjects.ts +0 -8
  217. package/src/packages/element-constructor/svgTags.ts +0 -588
  218. package/src/packages/en3/attachments/En3SourceManager.ts +0 -116
  219. package/src/packages/en3/core/en3.ts +0 -306
  220. package/src/packages/en3/index.ts +0 -52
  221. package/src/packages/en3/instances/en3LazyLoader.ts +0 -22
  222. package/src/packages/en3/libs/MeshoptDecoder.js +0 -138
  223. package/src/packages/en3/loaders/en3GLTFLoader.ts +0 -54
  224. package/src/packages/en3/loaders/en3TextureLoader.ts +0 -3
  225. package/src/packages/en3/objects/En3Clip.ts +0 -53
  226. package/src/packages/en3/objects/En3ClipHelpers.ts +0 -12
  227. package/src/packages/en3/objects/En3GLTF.ts +0 -35
  228. package/src/packages/en3/objects/En3Image.ts +0 -18
  229. package/src/packages/en3/objects/En3ImageLike.ts +0 -101
  230. package/src/packages/en3/objects/En3Video.ts +0 -88
  231. package/src/packages/en3/test/En3HTML.ts +0 -55
  232. package/src/packages/en3/test/En3ModifiedMaterial.ts +0 -221
  233. package/src/packages/en3/test/En3Raycaster.ts +0 -187
  234. package/src/packages/en3/utils/coverTexture.ts +0 -29
  235. package/src/packages/en3/utils/dispose.ts +0 -27
  236. package/src/packages/en3/utils/traverseMaterials.ts +0 -10
  237. package/src/packages/en3/utils/traverseMeshes.ts +0 -9
  238. package/src/packages/image/index.ts +0 -19
  239. package/src/packages/intersector/index.ts +0 -83
  240. package/src/packages/ladder/index.ts +0 -112
  241. package/src/packages/layout-box/index.ts +0 -417
  242. package/src/packages/loading/index.ts +0 -131
  243. package/src/packages/measurer/CumulativeOffsetLeft.ts +0 -8
  244. package/src/packages/measurer/CumulativeOffsetTop.ts +0 -8
  245. package/src/packages/measurer/Meaurer.ts +0 -38
  246. package/src/packages/measurer/index.ts +0 -3
  247. package/src/packages/media/index.ts +0 -38
  248. package/src/packages/morph/Link.ts +0 -32
  249. package/src/packages/morph/Morph.ts +0 -246
  250. package/src/packages/morph/index.ts +0 -10
  251. package/src/packages/notifier/index.ts +0 -41
  252. package/src/packages/order/index.ts +0 -14
  253. package/src/packages/resizer/index.ts +0 -55
  254. package/src/packages/router/Link.ts +0 -33
  255. package/src/packages/router/Route.ts +0 -152
  256. package/src/packages/router/RouteElement.ts +0 -34
  257. package/src/packages/router/Router.ts +0 -190
  258. package/src/packages/router/index.ts +0 -13
  259. package/src/packages/scroll/ScrollElement.ts +0 -618
  260. package/src/packages/scroll/ScrollUserElement.ts +0 -21
  261. package/src/packages/scroll/ScrollbarElement.ts +0 -170
  262. package/src/packages/scroll/index.ts +0 -2
  263. package/src/packages/scroll-entries/index.ts +0 -74
  264. package/src/packages/source/SourceClass.ts +0 -77
  265. package/src/packages/source/SourceElement.ts +0 -177
  266. package/src/packages/source/SourceManager.ts +0 -61
  267. package/src/packages/source/SourceSet.ts +0 -52
  268. package/src/packages/source/index.ts +0 -8
  269. package/src/packages/store/Composed.ts +0 -33
  270. package/src/packages/store/Derived.ts +0 -24
  271. package/src/packages/store/DerivedArray.ts +0 -36
  272. package/src/packages/store/Resource.ts +0 -38
  273. package/src/packages/store/Store.ts +0 -144
  274. package/src/packages/store/StoreRegistry.ts +0 -105
  275. package/src/packages/store/index.ts +0 -23
  276. package/src/packages/ticker/index.ts +0 -173
  277. package/src/packages/utils/array.ts +0 -3
  278. package/src/packages/utils/attributes.ts +0 -19
  279. package/src/packages/utils/browser.ts +0 -2
  280. package/src/packages/utils/canvas.ts +0 -46
  281. package/src/packages/utils/collisions.ts +0 -12
  282. package/src/packages/utils/coordinates.ts +0 -40
  283. package/src/packages/utils/decoding.ts +0 -11
  284. package/src/packages/utils/dev.ts +0 -5
  285. package/src/packages/utils/dom.ts +0 -48
  286. package/src/packages/utils/easings.ts +0 -69
  287. package/src/packages/utils/file.ts +0 -17
  288. package/src/packages/utils/function.ts +0 -29
  289. package/src/packages/utils/index.ts +0 -61
  290. package/src/packages/utils/layout.ts +0 -22
  291. package/src/packages/utils/math.ts +0 -74
  292. package/src/packages/utils/number.ts +0 -26
  293. package/src/packages/utils/object.ts +0 -108
  294. package/src/packages/utils/string.ts +0 -49
  295. package/src/packages/utils/ts-shape.ts +0 -25
  296. package/src/packages/utils/ts-utility.ts +0 -47
  297. package/src/packages/video/index.ts +0 -39
  298. package/src/playground/index.ts +0 -0
  299. package/tsconfig.json +0 -31
  300. package/vite.config.ts +0 -78
@@ -1,152 +0,0 @@
1
- import { isBrowser } from '$packages/utils'
2
- import { RouteElement, RouteParameters } from './RouteElement'
3
-
4
- export type RouteModule = () => Promise<any>
5
-
6
- export class Route {
7
- #pattern: string
8
- #module: RouteModule
9
- #urlPattern: URLPattern
10
- #elementConstructor: typeof RouteElement | null
11
- #element: RouteElement | null
12
- #isActive: boolean
13
- #outlet: HTMLElement | ShadowRoot | null
14
- #mutationObserver: MutationObserver = null!
15
- #permanentHeadNodes: Array<Node> = []
16
- #temporalHeadNodes: Array<Node> = []
17
-
18
- constructor(pattern: string, module: RouteModule) {
19
- this.#pattern = pattern
20
- this.#module = module
21
- this.#urlPattern = new URLPattern({ pathname: this.#pattern })
22
- this.#elementConstructor = null
23
- this.#element = null
24
- this.#isActive = false
25
- this.#outlet = null
26
-
27
- if (isBrowser) {
28
- this.#mutationObserver = new MutationObserver((mutations) => {
29
- const mutation = mutations[0]
30
-
31
- mutation.addedNodes.forEach((addedNode) => {
32
- if (!this.#elementConstructor) {
33
- this.#permanentHeadNodes.push(addedNode)
34
- } else {
35
- this.#temporalHeadNodes.push(addedNode)
36
- }
37
- })
38
- })
39
- }
40
- }
41
-
42
- public get pattern() {
43
- return this.#pattern
44
- }
45
-
46
- public get urlPattern() {
47
- return this.#urlPattern
48
- }
49
-
50
- public get isActive() {
51
- return this.#isActive
52
- }
53
-
54
- public get element() {
55
- return this.#element
56
- }
57
-
58
- public get outlet() {
59
- return this.#outlet
60
- }
61
-
62
- public testPathname(pathname: string) {
63
- return this.urlPattern.test({ pathname: pathname })
64
- }
65
-
66
- public async render(containerElement: HTMLElement | ShadowRoot, pathname: string) {
67
- this.#mutationObserver.observe(document.head, { childList: true, subtree: true })
68
-
69
- if (!this.#elementConstructor) {
70
- const content = await this.#module()
71
-
72
- this.#temporalHeadNodes = [...this.#permanentHeadNodes]
73
-
74
- if (typeof content.default === 'function') {
75
- this.#elementConstructor = content.default
76
- customElements.define('e-' + this.#elementConstructor?.name.toLowerCase(), content.default)
77
- }
78
- } else {
79
- this.#permanentHeadNodes.forEach((node) => {
80
- document.head.appendChild(node)
81
- })
82
- }
83
-
84
- await this.#waitHeadNodesLoad()
85
-
86
- if (this.#elementConstructor) {
87
- const v = this.#urlPattern.exec({ pathname })
88
- const pathnameParams = v?.pathname.groups || {}
89
- const searchParams = Object.fromEntries(new URLSearchParams(location.search))
90
-
91
- const routeParameters: RouteParameters<any, any> = {
92
- pathnameParams,
93
- searchParams,
94
- }
95
-
96
- this.#element = new this.#elementConstructor(routeParameters)
97
-
98
- containerElement.appendChild(this.#element)
99
-
100
- this.#outlet =
101
- this.#element.querySelector<HTMLElement>('[data-outlet]') ||
102
- this.#element.shadowRoot?.querySelector<HTMLElement>('[data-outlet]') ||
103
- this.#element.shadowRoot ||
104
- this.#element
105
-
106
- this.#isActive = true
107
- }
108
-
109
- this.#mutationObserver.disconnect()
110
- }
111
-
112
- public close() {
113
- this.#mutationObserver.disconnect()
114
- this.#element?.remove()
115
- this.#isActive = false
116
-
117
- this.#temporalHeadNodes.forEach((node) => document.head.removeChild(node))
118
- this.#temporalHeadNodes = []
119
- }
120
-
121
- public getAnchorElements() {
122
- let links: Array<HTMLAnchorElement> = []
123
-
124
- if (this.#element) {
125
- links = [...this.#element.querySelectorAll<HTMLAnchorElement>('a')]
126
- }
127
-
128
- if (this.#element?.shadowRoot) {
129
- links = [...links, ...this.#element.shadowRoot.querySelectorAll<HTMLAnchorElement>('a')]
130
- }
131
-
132
- return links
133
- }
134
-
135
- async #waitHeadNodesLoad() {
136
- const nodes = this.#permanentHeadNodes.filter((node) => {
137
- if (node instanceof HTMLElement) {
138
- return node.tagName === 'STYLE' || node.tagName === 'SCRIPT' || node.tagName === 'LINK'
139
- }
140
-
141
- return false
142
- }) as Array<HTMLStyleElement | HTMLScriptElement | HTMLLinkElement>
143
-
144
- for await (const node of nodes) {
145
- await new Promise<void>((res) => {
146
- node.onload = () => {
147
- res()
148
- }
149
- })
150
- }
151
- }
152
- }
@@ -1,34 +0,0 @@
1
- import { CustomElement } from '$packages/custom-element'
2
-
3
- export type RouteURLParams<T extends string = string> = Partial<{ [key in T]: string }>
4
-
5
- export type RouteParameters<
6
- PathnameParams extends string = string,
7
- SearchParams extends string = string
8
- > = {
9
- pathnameParams: RouteURLParams<PathnameParams>
10
- searchParams: RouteURLParams<SearchParams>
11
- }
12
-
13
- export class RouteElement<
14
- PathnameParams extends string = string,
15
- SearchParams extends string = string
16
- > extends CustomElement {
17
- #pathnameParams: RouteURLParams<PathnameParams>
18
- #searchParams: RouteURLParams<SearchParams>
19
-
20
- constructor(parameters: RouteParameters<PathnameParams, SearchParams>) {
21
- super()
22
-
23
- this.#pathnameParams = parameters.pathnameParams
24
- this.#searchParams = parameters.searchParams
25
- }
26
-
27
- public get pathnameParams() {
28
- return this.#pathnameParams
29
- }
30
-
31
- public get searchParams() {
32
- return this.#searchParams
33
- }
34
- }
@@ -1,190 +0,0 @@
1
- import { URLPattern } from 'urlpattern-polyfill'
2
- import { Notifier } from '$packages/notifier'
3
- import { isBrowser, debounce } from '$packages/utils'
4
- import { Route, RouteModule } from './Route'
5
- import { Link } from './Link'
6
-
7
- // @ts-ignore
8
- globalThis.URLPattern = URLPattern
9
-
10
- export interface RouterPreprocessorEntry {
11
- pathname: string
12
- resolve: () => void
13
- reject: () => void
14
- }
15
-
16
- export type RouterPreprocessor = (entry: RouterPreprocessorEntry) => void
17
-
18
- export interface RouterPostprocessorEntry {
19
- pathname: string
20
- }
21
-
22
- export type RouterPostprocessor = (entry: RouterPostprocessorEntry) => void
23
-
24
- export interface RouterAfterNavigationEntry {
25
- pathname: string
26
- }
27
-
28
- export type RouterAfterNavigationCallback = (entry: RouterAfterNavigationEntry) => void
29
-
30
- export type RouterHistoryAction = 'push' | 'replace' | 'none'
31
-
32
- export interface RouterParameters {
33
- rootElement?: HTMLElement
34
- base?: string
35
- }
36
-
37
- export class Router {
38
- public static active: Router
39
-
40
- #rootElement: HTMLElement = null!
41
- #base: string
42
- #routes: Array<Route> = []
43
- #lastRoute: Route | null = null
44
- #links: Array<Link> = []
45
- #candidatePathname: string | undefined
46
- #currentPathname: string = null!
47
-
48
- #navigationEvent = new Notifier<RouterAfterNavigationCallback>()
49
-
50
- public preprocessor?: RouterPreprocessor
51
- public postprocessor?: RouterPostprocessor
52
-
53
- constructor(parameters?: RouterParameters) {
54
- this.#base = parameters?.base || ''
55
-
56
- Router.active = this
57
-
58
- if (isBrowser) {
59
- this.#rootElement = parameters?.rootElement || document.body
60
- addEventListener('popstate', this.#popStateListener)
61
- }
62
- }
63
-
64
- public get currentPathname() {
65
- return this.#currentPathname
66
- }
67
-
68
- public get candidatePathname() {
69
- return this.#candidatePathname
70
- }
71
-
72
- public get routes() {
73
- return this.#routes
74
- }
75
-
76
- public navigationEvent(callback: RouterAfterNavigationCallback) {
77
- return this.#navigationEvent.subscribe(callback)
78
- }
79
-
80
- public defineRoute(pattern: string, module: RouteModule) {
81
- const route = new Route(pattern, module)
82
- this.#routes.push(route)
83
- this.#start()
84
- }
85
-
86
- public async navigate(pathname: string, action: RouterHistoryAction = 'push') {
87
- if (this.#candidatePathname === pathname || this.#currentPathname === pathname) {
88
- return
89
- }
90
-
91
- Router.active = this
92
-
93
- this.#candidatePathname = pathname
94
-
95
- const activeRoutes = this.#routes.filter((r) => r.isActive)
96
-
97
- const newRoutes = this.#routes.filter(
98
- (r) => !activeRoutes.includes(r) && r.testPathname(pathname)
99
- )
100
-
101
- const oldRoutes = activeRoutes.filter((r) => !r.testPathname(pathname))
102
- const keepRoutes = activeRoutes.filter((r) => r.testPathname(pathname))
103
-
104
- let isOkToSwitch = true
105
-
106
- if (this.preprocessor) {
107
- try {
108
- await new Promise<void>((resolve, reject) => {
109
- this.preprocessor?.({ pathname, resolve, reject })
110
- })
111
- } catch (e: any) {
112
- if (e) {
113
- console.error(e)
114
- } else {
115
- console.log('Route change canceled')
116
- }
117
- isOkToSwitch = false
118
- }
119
- }
120
-
121
- if (isOkToSwitch && this.#candidatePathname === pathname) {
122
- this.#lastRoute = keepRoutes[keepRoutes.length - 1]
123
-
124
- oldRoutes.forEach((r) => {
125
- r.close()
126
- })
127
-
128
- this.#currentPathname = pathname
129
-
130
- for await (const route of newRoutes) {
131
- await route.render(this.#lastRoute?.outlet || this.#rootElement, pathname)
132
- this.#lastRoute = route
133
- }
134
-
135
- const fullPathname = this.#base + pathname + location.search
136
-
137
- if (action === 'push') {
138
- history.pushState(fullPathname, '', fullPathname)
139
- } else if (action === 'replace') {
140
- history.replaceState(fullPathname, '', fullPathname)
141
- }
142
-
143
- this.#updateLinks()
144
-
145
- this.postprocessor?.({ pathname })
146
- this.#navigationEvent.notify({ pathname })
147
- }
148
- }
149
-
150
- #start = debounce(() => {
151
- const slashesLength = (pathname: string) => {
152
- return pathname.split('/').length
153
- }
154
-
155
- this.#routes = this.#routes.sort((a, b) => {
156
- return slashesLength(a.pattern) - slashesLength(b.pattern)
157
- })
158
-
159
- this.navigate(location.pathname.replace(this.#base, ''))
160
- }, 0)
161
-
162
- #updateLinks() {
163
- const activeRoutes = this.#routes.filter((r) => r.isActive)
164
-
165
- const anchorElements = Array.from(
166
- new Set(
167
- [
168
- ...this.#rootElement.querySelectorAll('a'),
169
- ...activeRoutes.map((r) => r.getAnchorElements()).flat(),
170
- ].filter((a) => a.getAttribute('href')?.startsWith('/'))
171
- )
172
- )
173
-
174
- this.#links.forEach((link) => {
175
- link.destroy()
176
- })
177
-
178
- this.#links = anchorElements.map((element) => {
179
- const link = new Link(this, element)
180
-
181
- return link
182
- })
183
- }
184
-
185
- #popStateListener = (event: PopStateEvent) => {
186
- if (event.state) {
187
- this.navigate(event.state, 'none')
188
- }
189
- }
190
- }
@@ -1,13 +0,0 @@
1
- export {
2
- Router,
3
- type RouterPreprocessorEntry,
4
- type RouterPreprocessor,
5
- type RouterPostprocessor,
6
- type RouterAfterNavigationCallback,
7
- type RouterHistoryAction,
8
- type RouterParameters,
9
- } from './Router'
10
-
11
- export { RouteElement, type RouteParameters, type RouteURLParams } from './RouteElement'
12
-
13
- export { type RouteModule } from './Route'