@hamur/adapter-angular 0.0.0-stage → 0.0.1
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.
- package/LICENSE +22 -0
- package/README.md +42 -2
- package/dist/chunks/browser-index-7h7x2xz2.js +131 -0
- package/dist/chunks/browser-index-nann848q.js +205 -0
- package/dist/chunks/browser-index-qn8q5fa2.js +580 -0
- package/dist/src/adapter.js +1073 -0
- package/dist/src/error-page.js +34 -0
- package/dist/src/index.js +1729 -0
- package/dist/src/keepalive.js +0 -0
- package/dist/src/runtime/client.js +21 -0
- package/dist/src/runtime/island.js +92 -0
- package/dist/src/runtime/server.js +175 -0
- package/dist/types/src/activation.d.ts +20 -0
- package/dist/types/src/adapter.d.ts +7 -0
- package/dist/types/src/app.d.ts +3 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/client-parts.d.ts +32 -0
- package/dist/types/src/compile.d.ts +97 -0
- package/dist/types/src/context.d.ts +74 -0
- package/dist/types/src/data.d.ts +65 -0
- package/dist/types/src/definition.d.ts +13 -0
- package/dist/types/src/error-page.d.ts +5 -0
- package/dist/types/src/error.d.ts +4 -0
- package/dist/types/src/form.d.ts +65 -0
- package/dist/types/src/head.d.ts +12 -0
- package/dist/types/src/hmr.d.ts +14 -0
- package/dist/types/src/hydration.d.ts +16 -0
- package/dist/types/src/index.d.ts +24 -0
- package/dist/types/src/indicators.d.ts +69 -0
- package/dist/types/src/island.d.ts +39 -0
- package/dist/types/src/keepalive.d.ts +1 -0
- package/dist/types/src/link.d.ts +65 -0
- package/dist/types/src/markdown-page.d.ts +10 -0
- package/dist/types/src/markdown.d.ts +37 -0
- package/dist/types/src/mask.d.ts +12 -0
- package/dist/types/src/navigate.d.ts +14 -0
- package/dist/types/src/outlet.d.ts +71 -0
- package/dist/types/src/request.d.ts +31 -0
- package/dist/types/src/routing.d.ts +47 -0
- package/dist/types/src/runtime/client.d.ts +29 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +8 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/server.d.ts +15 -0
- package/dist/types/src/server-island.d.ts +21 -0
- package/dist/types/src/state.d.ts +14 -0
- package/dist/types/src/svg.d.ts +15 -0
- package/dist/types/src/time.d.ts +37 -0
- package/dist/types/src/wrapper.d.ts +21 -0
- package/package.json +56 -4
- package/src/activation.ts +83 -0
- package/src/adapter.ts +107 -0
- package/src/app.ts +6 -0
- package/src/async-data.ts +105 -0
- package/src/client-parts.ts +127 -0
- package/src/compile.ts +959 -0
- package/src/context.ts +189 -0
- package/src/data.ts +389 -0
- package/src/definition.ts +144 -0
- package/src/error-page.ts +17 -0
- package/src/error.ts +15 -0
- package/src/form.ts +253 -0
- package/src/head.ts +63 -0
- package/src/hmr.ts +61 -0
- package/src/hydration.ts +120 -0
- package/src/index.ts +98 -0
- package/src/indicators.ts +251 -0
- package/src/island.ts +95 -0
- package/src/keepalive.ts +1 -0
- package/src/link.ts +251 -0
- package/src/markdown-page.ts +31 -0
- package/src/markdown.ts +170 -0
- package/src/mask.ts +36 -0
- package/src/navigate.ts +19 -0
- package/src/outlet.ts +344 -0
- package/src/request.ts +170 -0
- package/src/routing.ts +218 -0
- package/src/runtime/client.ts +140 -0
- package/src/runtime/contract.d.ts +4 -0
- package/src/runtime/hydration-dev.ts +18 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +109 -0
- package/src/runtime/server.ts +228 -0
- package/src/server-island.ts +54 -0
- package/src/state.ts +70 -0
- package/src/svg.ts +65 -0
- package/src/time.ts +108 -0
- package/src/wrapper.ts +187 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export { default as DefaultErrorPage } from "./error-page.ts"
|
|
2
|
+
export { Outlet } from "./outlet.ts"
|
|
3
|
+
export { Link, injectLink, injectLinkStatus } from "./link.ts"
|
|
4
|
+
export { Navigate } from "./navigate.ts"
|
|
5
|
+
export { injectError } from "./error.ts"
|
|
6
|
+
export { clearError, forbidden, navigateTo, notFound, permanentRedirect, showError, unauthorized } from "hamur/app"
|
|
7
|
+
export {
|
|
8
|
+
callOnce,
|
|
9
|
+
clearState,
|
|
10
|
+
injectAsyncData,
|
|
11
|
+
injectData,
|
|
12
|
+
injectFetch,
|
|
13
|
+
injectLazyAsyncData,
|
|
14
|
+
injectLazyFetch,
|
|
15
|
+
injectNuxtData,
|
|
16
|
+
} from "./data.ts"
|
|
17
|
+
export type { AsyncDataResource, InjectAsyncDataOptions, InjectFetchOptions } from "./data.ts"
|
|
18
|
+
export { injectHead, injectHeadSafe, injectSeoMeta, injectServerHead, injectServerSeoMeta } from "./head.ts"
|
|
19
|
+
export { injectCookie, injectState, injectSyncedState } from "./state.ts"
|
|
20
|
+
export { DevOnly, injectDeferred, injectHydrated, injectId, onPrehydrate, transitionName } from "./client-parts.ts"
|
|
21
|
+
export { onPageActivated, onPageDeactivated } from "./activation.ts"
|
|
22
|
+
export { Time } from "./time.ts"
|
|
23
|
+
export {
|
|
24
|
+
Announcer,
|
|
25
|
+
LoadingIndicator,
|
|
26
|
+
RouteAnnouncer,
|
|
27
|
+
injectAnnouncer,
|
|
28
|
+
injectLoadingIndicator,
|
|
29
|
+
injectRouteAnnouncer,
|
|
30
|
+
} from "./indicators.ts"
|
|
31
|
+
export { MarkdownRenderer } from "./markdown.ts"
|
|
32
|
+
export type { MarkdownRendererProps } from "./markdown.ts"
|
|
33
|
+
export { Island } from "./island.ts"
|
|
34
|
+
export { ServerIsland } from "./server-island.ts"
|
|
35
|
+
export { Form, injectAction, injectForm, injectFormStatus, injectOptimistic } from "./form.ts"
|
|
36
|
+
export type { InjectActionResult, InjectFormInstance, InjectFormResult } from "./form.ts"
|
|
37
|
+
export {
|
|
38
|
+
injectApp,
|
|
39
|
+
injectAppUpdate,
|
|
40
|
+
injectOnline,
|
|
41
|
+
injectPoll,
|
|
42
|
+
injectPreviewMode,
|
|
43
|
+
injectReportWebVitals,
|
|
44
|
+
injectRequestEvent,
|
|
45
|
+
injectRequestFetch,
|
|
46
|
+
injectRequestHeader,
|
|
47
|
+
injectRequestHeaders,
|
|
48
|
+
injectRequestURL,
|
|
49
|
+
injectResponseHeader,
|
|
50
|
+
injectRuntimeConfig,
|
|
51
|
+
injectRuntimeHook,
|
|
52
|
+
injectSiteConfig,
|
|
53
|
+
onAppReady,
|
|
54
|
+
setResponseStatus,
|
|
55
|
+
} from "./request.ts"
|
|
56
|
+
export {
|
|
57
|
+
injectBlocker,
|
|
58
|
+
injectLayout,
|
|
59
|
+
injectLayoutSegment,
|
|
60
|
+
injectLayoutSegments,
|
|
61
|
+
injectMatches,
|
|
62
|
+
injectNavigation,
|
|
63
|
+
injectOutletContext,
|
|
64
|
+
injectPreventNavigate,
|
|
65
|
+
injectResolvedPath,
|
|
66
|
+
injectRoute,
|
|
67
|
+
injectRouteData,
|
|
68
|
+
injectRouter,
|
|
69
|
+
injectSearch,
|
|
70
|
+
injectSnapshot,
|
|
71
|
+
onAfterNavigate,
|
|
72
|
+
onBeforeNavigate,
|
|
73
|
+
onBeforeRouteLeave,
|
|
74
|
+
onBeforeRouteUpdate,
|
|
75
|
+
onNavigate,
|
|
76
|
+
} from "./routing.ts"
|
|
77
|
+
export type { AngularSetup } from "./context.ts"
|
|
78
|
+
export type {
|
|
79
|
+
AnnouncerPoliteness,
|
|
80
|
+
Blocker,
|
|
81
|
+
BlockerArgs,
|
|
82
|
+
LinkDefaults,
|
|
83
|
+
LinkSlot,
|
|
84
|
+
LoadingIndicatorOptions,
|
|
85
|
+
LoadingIndicatorProps,
|
|
86
|
+
MatchedRoute,
|
|
87
|
+
NavigateOptions,
|
|
88
|
+
Navigation,
|
|
89
|
+
NavigationState,
|
|
90
|
+
RouteDataName,
|
|
91
|
+
RouteAnnouncerPoliteness,
|
|
92
|
+
RouteDataOf,
|
|
93
|
+
RouteValue,
|
|
94
|
+
RouterApi,
|
|
95
|
+
SnapshotOptions,
|
|
96
|
+
UseBlockerOptions,
|
|
97
|
+
UseLinkOptions,
|
|
98
|
+
} from "hamur/adapter"
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Component,
|
|
3
|
+
DestroyRef,
|
|
4
|
+
ElementRef,
|
|
5
|
+
HostAttributeToken,
|
|
6
|
+
Injector,
|
|
7
|
+
afterNextRender,
|
|
8
|
+
afterRenderEffect,
|
|
9
|
+
assertInInjectionContext,
|
|
10
|
+
computed,
|
|
11
|
+
effect,
|
|
12
|
+
inject,
|
|
13
|
+
input,
|
|
14
|
+
runInInjectionContext,
|
|
15
|
+
signal,
|
|
16
|
+
untracked,
|
|
17
|
+
viewChild,
|
|
18
|
+
type OnInit,
|
|
19
|
+
type Signal,
|
|
20
|
+
} from "@angular/core"
|
|
21
|
+
import {
|
|
22
|
+
ANNOUNCER_ASSERTIVE_ID,
|
|
23
|
+
ANNOUNCER_POLITE_ID,
|
|
24
|
+
ROUTE_ANNOUNCER_ID,
|
|
25
|
+
announcerActions,
|
|
26
|
+
announcerRole,
|
|
27
|
+
createAnnouncer,
|
|
28
|
+
createLoadingIndicator,
|
|
29
|
+
createRouteAnnouncer,
|
|
30
|
+
hideAnnouncer,
|
|
31
|
+
loadingIndicatorOptions,
|
|
32
|
+
paintLoadingIndicator,
|
|
33
|
+
} from "hamur/adapter"
|
|
34
|
+
import type {
|
|
35
|
+
AnnouncerActions,
|
|
36
|
+
AnnouncerPoliteness,
|
|
37
|
+
LoadingIndicatorActions,
|
|
38
|
+
LoadingIndicatorOptions,
|
|
39
|
+
RouteAnnouncerActions,
|
|
40
|
+
RouteAnnouncerPoliteness,
|
|
41
|
+
} from "hamur/adapter"
|
|
42
|
+
import { HAMUR_APP, HAMUR_NAVIGATION, required } from "./context.ts"
|
|
43
|
+
|
|
44
|
+
/** The bar's state as signals, and its `start`, `finish`, `set` and `clear`; it follows the navigation while a component that called it is alive. */
|
|
45
|
+
export function injectLoadingIndicator(options?: LoadingIndicatorOptions): {
|
|
46
|
+
progress: Signal<number>
|
|
47
|
+
isLoading: Signal<boolean>
|
|
48
|
+
error: Signal<boolean>
|
|
49
|
+
} & LoadingIndicatorActions {
|
|
50
|
+
assertInInjectionContext(injectLoadingIndicator)
|
|
51
|
+
const store = createLoadingIndicator(required(HAMUR_APP, "injectLoadingIndicator"), options)
|
|
52
|
+
const navigation = required(HAMUR_NAVIGATION, "injectLoadingIndicator")
|
|
53
|
+
const state = signal(store.get())
|
|
54
|
+
const injector = inject(Injector)
|
|
55
|
+
const destroy = inject(DestroyRef)
|
|
56
|
+
afterNextRender(() => {
|
|
57
|
+
state.set(store.get())
|
|
58
|
+
const stopState = store.subscribe(() => state.set(store.get()))
|
|
59
|
+
const listeners = new Set<() => void>()
|
|
60
|
+
const watcher = effect(
|
|
61
|
+
() => {
|
|
62
|
+
navigation()
|
|
63
|
+
untracked(() => {
|
|
64
|
+
for (const listener of [...listeners]) listener()
|
|
65
|
+
})
|
|
66
|
+
},
|
|
67
|
+
{ injector },
|
|
68
|
+
)
|
|
69
|
+
const stopFollow = store.follow({
|
|
70
|
+
get: () => untracked(navigation),
|
|
71
|
+
subscribe(listener) {
|
|
72
|
+
listeners.add(listener)
|
|
73
|
+
return () => void listeners.delete(listener)
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
destroy.onDestroy(() => {
|
|
77
|
+
stopState()
|
|
78
|
+
stopFollow()
|
|
79
|
+
watcher.destroy()
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
return {
|
|
83
|
+
progress: computed(() => state().progress),
|
|
84
|
+
isLoading: computed(() => state().isLoading),
|
|
85
|
+
error: computed(() => state().error),
|
|
86
|
+
start: store.start,
|
|
87
|
+
finish: store.finish,
|
|
88
|
+
set: store.set,
|
|
89
|
+
clear: store.clear,
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* `<hamur-loading-indicator />`, the bar at the top of the page that follows `injectNavigation().state`. Its look is
|
|
95
|
+
* written through `element.style` after the first render, so the server's element has no `style` attribute.
|
|
96
|
+
*/
|
|
97
|
+
@Component({
|
|
98
|
+
selector: "hamur-loading-indicator",
|
|
99
|
+
template: "",
|
|
100
|
+
host: { "data-hamur-loading-indicator": "", "aria-hidden": "true" },
|
|
101
|
+
})
|
|
102
|
+
export class LoadingIndicator implements OnInit {
|
|
103
|
+
readonly color = input<string>()
|
|
104
|
+
readonly errorColor = input<string>()
|
|
105
|
+
readonly height = input<number>()
|
|
106
|
+
readonly duration = input<number>()
|
|
107
|
+
readonly throttle = input<number>()
|
|
108
|
+
readonly estimatedProgress = input<(duration: number, elapsed: number) => number>()
|
|
109
|
+
private readonly injector = inject(Injector)
|
|
110
|
+
private readonly element = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement
|
|
111
|
+
|
|
112
|
+
ngOnInit(): void {
|
|
113
|
+
runInInjectionContext(this.injector, () => {
|
|
114
|
+
const { progress, isLoading, error } = injectLoadingIndicator(
|
|
115
|
+
loadingIndicatorOptions({
|
|
116
|
+
duration: this.duration(),
|
|
117
|
+
throttle: this.throttle(),
|
|
118
|
+
estimatedProgress: this.estimatedProgress(),
|
|
119
|
+
}),
|
|
120
|
+
)
|
|
121
|
+
afterRenderEffect(() =>
|
|
122
|
+
paintLoadingIndicator(
|
|
123
|
+
this.element,
|
|
124
|
+
{ progress: progress(), isLoading: isLoading(), error: error() },
|
|
125
|
+
{ color: this.color(), errorColor: this.errorColor(), height: this.height() },
|
|
126
|
+
),
|
|
127
|
+
)
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The page's route announcer as signals, and `set`, `polite` and `assertive`. */
|
|
133
|
+
export function injectRouteAnnouncer(): {
|
|
134
|
+
message: Signal<string>
|
|
135
|
+
politeness: Signal<RouteAnnouncerPoliteness>
|
|
136
|
+
} & RouteAnnouncerActions {
|
|
137
|
+
assertInInjectionContext(injectRouteAnnouncer)
|
|
138
|
+
const store = createRouteAnnouncer(required(HAMUR_APP, "injectRouteAnnouncer"))
|
|
139
|
+
const state = signal(store.get())
|
|
140
|
+
const destroy = inject(DestroyRef)
|
|
141
|
+
afterNextRender(() => {
|
|
142
|
+
state.set(store.get())
|
|
143
|
+
destroy.onDestroy(store.subscribe(() => state.set(store.get())))
|
|
144
|
+
})
|
|
145
|
+
return {
|
|
146
|
+
message: computed(() => state().message),
|
|
147
|
+
politeness: computed(() => state().politeness),
|
|
148
|
+
set: store.set,
|
|
149
|
+
polite: store.polite,
|
|
150
|
+
assertive: store.assertive,
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* `<hamur-route-announcer />` is the region that speaks each navigation; the first one mounted holds `#__hamur_announcer`
|
|
156
|
+
* and the navigator's own region goes away while it is. A `politeness` pins `aria-live` and `role`; a `class` or `style` on the
|
|
157
|
+
* element keeps it visible.
|
|
158
|
+
*/
|
|
159
|
+
@Component({
|
|
160
|
+
selector: "hamur-route-announcer",
|
|
161
|
+
template: "",
|
|
162
|
+
host: {
|
|
163
|
+
"[attr.id]": "owner() ? id : null",
|
|
164
|
+
"[attr.aria-live]": "owner() ? live() : 'off'",
|
|
165
|
+
"aria-atomic": "true",
|
|
166
|
+
"[attr.role]": "owner() ? role() : null",
|
|
167
|
+
"[textContent]": "owner() ? message() : ''",
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
export class RouteAnnouncer {
|
|
171
|
+
readonly politeness = input<RouteAnnouncerPoliteness>()
|
|
172
|
+
protected readonly id = ROUTE_ANNOUNCER_ID
|
|
173
|
+
private readonly store = createRouteAnnouncer(required(HAMUR_APP, "RouteAnnouncer"))
|
|
174
|
+
private readonly state = injectRouteAnnouncer()
|
|
175
|
+
private readonly element = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement
|
|
176
|
+
private readonly styled =
|
|
177
|
+
inject(new HostAttributeToken("class"), { optional: true }) !== null ||
|
|
178
|
+
inject(new HostAttributeToken("style"), { optional: true }) !== null
|
|
179
|
+
private readonly key = {}
|
|
180
|
+
private readonly tick = signal(0)
|
|
181
|
+
|
|
182
|
+
protected readonly message = this.state.message
|
|
183
|
+
protected readonly live = computed(() => this.politeness() ?? this.state.politeness())
|
|
184
|
+
protected readonly role = computed(() => announcerRole(this.live()))
|
|
185
|
+
protected readonly owner = computed(() => {
|
|
186
|
+
this.tick()
|
|
187
|
+
return this.store.claim(this.key, () => this.tick.update((count) => count + 1))
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
constructor() {
|
|
191
|
+
afterRenderEffect((onCleanup) => {
|
|
192
|
+
if (!this.owner()) return
|
|
193
|
+
onCleanup(this.store.mount())
|
|
194
|
+
if (!this.styled) hideAnnouncer(this.element)
|
|
195
|
+
})
|
|
196
|
+
inject(DestroyRef).onDestroy(() => this.store.release(this.key))
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The page's announcer as signals, and `set`, `polite` and `assertive` (`polite` unless `politeness` says otherwise). */
|
|
201
|
+
export function injectAnnouncer({ politeness = "polite" }: { politeness?: AnnouncerPoliteness } = {}): {
|
|
202
|
+
message: Signal<string>
|
|
203
|
+
politeness: Signal<AnnouncerPoliteness>
|
|
204
|
+
} & AnnouncerActions {
|
|
205
|
+
assertInInjectionContext(injectAnnouncer)
|
|
206
|
+
const store = createAnnouncer(required(HAMUR_APP, "injectAnnouncer"))
|
|
207
|
+
const state = signal(store.get())
|
|
208
|
+
const destroy = inject(DestroyRef)
|
|
209
|
+
afterNextRender(() => {
|
|
210
|
+
state.set(store.get())
|
|
211
|
+
destroy.onDestroy(store.subscribe(() => state.set(store.get())))
|
|
212
|
+
})
|
|
213
|
+
return {
|
|
214
|
+
message: computed(() => state().message),
|
|
215
|
+
politeness: computed(() => state().politeness),
|
|
216
|
+
...announcerActions(store, politeness),
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** `<hamur-announcer />` holds the two regions `injectAnnouncer()` writes to: `#__hamur_announcer_polite` and `#__hamur_announcer_assertive`. */
|
|
221
|
+
@Component({
|
|
222
|
+
selector: "hamur-announcer",
|
|
223
|
+
template: `<div #polite [attr.id]="politeId" aria-live="polite" aria-atomic="true" role="status">{{ regions().polite }}</div
|
|
224
|
+
><div #assertive [attr.id]="assertiveId" aria-live="assertive" aria-atomic="true" role="alert">{{
|
|
225
|
+
regions().assertive
|
|
226
|
+
}}</div>`,
|
|
227
|
+
})
|
|
228
|
+
export class Announcer {
|
|
229
|
+
protected readonly politeId = ANNOUNCER_POLITE_ID
|
|
230
|
+
protected readonly assertiveId = ANNOUNCER_ASSERTIVE_ID
|
|
231
|
+
private readonly store = createAnnouncer(required(HAMUR_APP, "Announcer"))
|
|
232
|
+
private readonly state = signal(this.store.get())
|
|
233
|
+
protected readonly regions = computed(() => this.state().regions)
|
|
234
|
+
private readonly polite = viewChild.required<ElementRef<HTMLElement>>("polite")
|
|
235
|
+
private readonly assertive = viewChild.required<ElementRef<HTMLElement>>("assertive")
|
|
236
|
+
|
|
237
|
+
constructor() {
|
|
238
|
+
const destroy = inject(DestroyRef)
|
|
239
|
+
afterNextRender(() => {
|
|
240
|
+
this.state.set(this.store.get())
|
|
241
|
+
hideAnnouncer(this.polite().nativeElement)
|
|
242
|
+
hideAnnouncer(this.assertive().nativeElement)
|
|
243
|
+
const stop = this.store.subscribe(() => this.state.set(this.store.get()))
|
|
244
|
+
const unmount = this.store.mount()
|
|
245
|
+
destroy.onDestroy(() => {
|
|
246
|
+
stop()
|
|
247
|
+
unmount()
|
|
248
|
+
})
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
}
|
package/src/island.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ApplicationRef,
|
|
3
|
+
Component,
|
|
4
|
+
DestroyRef,
|
|
5
|
+
ElementRef,
|
|
6
|
+
InjectionToken,
|
|
7
|
+
Injector,
|
|
8
|
+
PendingTasks,
|
|
9
|
+
Renderer2,
|
|
10
|
+
booleanAttribute,
|
|
11
|
+
inject,
|
|
12
|
+
input,
|
|
13
|
+
type OnInit,
|
|
14
|
+
type TemplateRef,
|
|
15
|
+
type Type,
|
|
16
|
+
} from "@angular/core"
|
|
17
|
+
import { ISLAND_ELEMENT, renderIsland } from "hamur/adapter"
|
|
18
|
+
import type { IslandOn } from "hamur/adapter"
|
|
19
|
+
import { injectId } from "./client-parts.ts"
|
|
20
|
+
import { HAMUR_APP, HAMUR_FAIL, onServer } from "./context.ts"
|
|
21
|
+
|
|
22
|
+
/** A component that writes its own nodes below its host: a template it renders there is a view the application checks. */
|
|
23
|
+
export abstract class HostWriter {
|
|
24
|
+
protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement
|
|
25
|
+
protected readonly renderer = inject(Renderer2)
|
|
26
|
+
private readonly appRef = inject(ApplicationRef)
|
|
27
|
+
private readonly injector = inject(Injector)
|
|
28
|
+
private readonly destroyRef = inject(DestroyRef)
|
|
29
|
+
|
|
30
|
+
protected append(template: TemplateRef<unknown>, into: Element = this.host): void {
|
|
31
|
+
const view = template.createEmbeddedView({}, this.injector)
|
|
32
|
+
this.appRef.attachView(view)
|
|
33
|
+
for (const node of view.rootNodes) this.renderer.appendChild(into, node)
|
|
34
|
+
this.destroyRef.onDestroy(() => view.destroy())
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The element the core's islands runtime reads, `<appId>-island` of the build; a token, so a test gives it another app id. */
|
|
39
|
+
export const HAMUR_ISLAND_ELEMENT = new InjectionToken<string>("hamur island element", {
|
|
40
|
+
providedIn: "root",
|
|
41
|
+
factory: () => ISLAND_ELEMENT,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* On an island page, `component` rendered on the server as an application of its own inside `<hamur-island>`,
|
|
46
|
+
* hydrated on its own in the browser; `fallback` is what the server renders under `on="only"`. Its template is empty: the island's
|
|
47
|
+
* HTML is written below its host on the server and left as it is in the browser. The host is the core's element when the app id is
|
|
48
|
+
* `hamur`; under another app id the host holds `<appId>-island`, which the runtime reads.
|
|
49
|
+
*/
|
|
50
|
+
@Component({ selector: "hamur-island", template: "" })
|
|
51
|
+
export class Island extends HostWriter implements OnInit {
|
|
52
|
+
/** The default export of a file under `app/islands/`. */
|
|
53
|
+
readonly component = input.required<Type<unknown>>()
|
|
54
|
+
readonly props = input<Record<string, unknown>>()
|
|
55
|
+
/** The island directive: when the island hydrates, `"only"` to mount it in the browser alone, or a directive a module adds; default `"load"`. */
|
|
56
|
+
readonly on = input<IslandOn>()
|
|
57
|
+
readonly fallback = input<TemplateRef<unknown>>()
|
|
58
|
+
/** The island's id across swaps; a swap to a page with the same id keeps its element and state. */
|
|
59
|
+
readonly persist = input<string>()
|
|
60
|
+
/** With `persist`, a swap keeps the island's old props. */
|
|
61
|
+
readonly persistProps = input(false, { transform: booleanAttribute })
|
|
62
|
+
private readonly app = inject(HAMUR_APP, { optional: true })
|
|
63
|
+
private readonly id = injectId()
|
|
64
|
+
private readonly tasks = inject(PendingTasks)
|
|
65
|
+
private readonly fail = inject(HAMUR_FAIL, { optional: true })
|
|
66
|
+
private readonly server = onServer()
|
|
67
|
+
private readonly element = inject(HAMUR_ISLAND_ELEMENT)
|
|
68
|
+
|
|
69
|
+
constructor() {
|
|
70
|
+
super()
|
|
71
|
+
if (!this.app) throw new Error("Island must be rendered inside a Hamur app")
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
ngOnInit(): void {
|
|
75
|
+
if (!this.server) return
|
|
76
|
+
const island = renderIsland(this.app!, this.id, this.component(), this.props() ?? {}, this.on(), undefined, {
|
|
77
|
+
persist: this.persist(),
|
|
78
|
+
persistProps: this.persistProps(),
|
|
79
|
+
})
|
|
80
|
+
const element: Element =
|
|
81
|
+
this.host.localName === this.element ? this.host : this.renderer.createElement(this.element)
|
|
82
|
+
if (element !== this.host) this.renderer.appendChild(this.host, element)
|
|
83
|
+
for (const [name, value] of Object.entries(island.attributes)) this.renderer.setAttribute(element, name, value)
|
|
84
|
+
if (!island.html) {
|
|
85
|
+
const fallback = this.fallback()
|
|
86
|
+
if (fallback) this.append(fallback, element)
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
const done = this.tasks.add()
|
|
90
|
+
island.html
|
|
91
|
+
.then((html) => this.renderer.setProperty(element, "innerHTML", html))
|
|
92
|
+
.catch((error: unknown) => this.fail?.(error))
|
|
93
|
+
.finally(done)
|
|
94
|
+
}
|
|
95
|
+
}
|
package/src/keepalive.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/src/link.ts
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DestroyRef,
|
|
3
|
+
Directive,
|
|
4
|
+
ElementRef,
|
|
5
|
+
HostAttributeToken,
|
|
6
|
+
InjectionToken,
|
|
7
|
+
assertInInjectionContext,
|
|
8
|
+
booleanAttribute,
|
|
9
|
+
computed,
|
|
10
|
+
effect,
|
|
11
|
+
forwardRef,
|
|
12
|
+
inject,
|
|
13
|
+
input,
|
|
14
|
+
numberAttribute,
|
|
15
|
+
output,
|
|
16
|
+
type Signal,
|
|
17
|
+
} from "@angular/core"
|
|
18
|
+
import { withoutBase } from "hamur/app"
|
|
19
|
+
import type { RouteLocation, ViewTransitionCall } from "hamur/app"
|
|
20
|
+
import {
|
|
21
|
+
createLinkSlot,
|
|
22
|
+
linkAttributes,
|
|
23
|
+
linkDefaultsOf,
|
|
24
|
+
linkPending,
|
|
25
|
+
linkPrefetchProps,
|
|
26
|
+
linkState,
|
|
27
|
+
relativeTarget,
|
|
28
|
+
resolveLinkTarget,
|
|
29
|
+
searchTarget,
|
|
30
|
+
setLinkState,
|
|
31
|
+
} from "hamur/adapter"
|
|
32
|
+
import type { CheckedTarget, LinkSlot, Navigation, PrefetchOn, UseLinkOptions } from "hamur/adapter"
|
|
33
|
+
import {
|
|
34
|
+
HAMUR_APP,
|
|
35
|
+
HAMUR_BASE_URL,
|
|
36
|
+
HAMUR_HREF,
|
|
37
|
+
HAMUR_NAVIGATION,
|
|
38
|
+
HAMUR_ROUTE,
|
|
39
|
+
HAMUR_ROUTER,
|
|
40
|
+
detachedHref,
|
|
41
|
+
} from "./context.ts"
|
|
42
|
+
|
|
43
|
+
type AriaCurrent = "page" | "step" | "location" | "date" | "time" | "true"
|
|
44
|
+
type Query = (prev: never) => Record<string, unknown>
|
|
45
|
+
|
|
46
|
+
/** What a `Link`'s content reads through `injectLinkStatus()`. */
|
|
47
|
+
const LINK_HREF = new InjectionToken<{ readonly href: Signal<string> }>("hamur link href")
|
|
48
|
+
|
|
49
|
+
const optionalBoolean = (value: unknown): boolean | undefined =>
|
|
50
|
+
value === undefined ? undefined : booleanAttribute(value)
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* `<a hamurLink="/path">`. The host bindings write what the other adapters' `Link` writes on its anchor; the
|
|
54
|
+
* core navigator takes the click. A `target`, `rel` or `aria-current` written on the element stays.
|
|
55
|
+
*/
|
|
56
|
+
@Directive({
|
|
57
|
+
selector: "a[hamurLink]",
|
|
58
|
+
providers: [{ provide: LINK_HREF, useExisting: forwardRef(() => Link) }],
|
|
59
|
+
host: {
|
|
60
|
+
"[attr.href]": "href()",
|
|
61
|
+
"[attr.aria-current]": "ariaCurrent()",
|
|
62
|
+
"[attr.rel]": "attr('rel')",
|
|
63
|
+
"[attr.data-hamur-prefetch]": "attr('data-hamur-prefetch')",
|
|
64
|
+
"[attr.data-hamur-replace]": "attr('data-hamur-replace')",
|
|
65
|
+
"[attr.data-hamur-view-transition]": "attr('data-hamur-view-transition')",
|
|
66
|
+
"[attr.data-hamur-pending-class]": "attr('data-hamur-pending-class')",
|
|
67
|
+
"[attr.data-hamur-trim]": "attr('data-hamur-trim')",
|
|
68
|
+
"[attr.data-hamur-mask]": "attr('data-hamur-mask')",
|
|
69
|
+
"[attr.data-hamur-unmask]": "attr('data-hamur-unmask')",
|
|
70
|
+
"[attr.data-hamur-prefetched-class]": "attr('data-hamur-prefetched-class')",
|
|
71
|
+
"[attr.data-hamur-scroll]": "attr('data-hamur-scroll')",
|
|
72
|
+
"[attr.data-hamur-prefetch-delay]": "attr('data-hamur-prefetch-delay')",
|
|
73
|
+
"[attr.data-hamur-keepfocus]": "keepFocusValue()",
|
|
74
|
+
"[class]": "classes()",
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
export class Link<T extends string = string> {
|
|
78
|
+
readonly to = input.required<CheckedTarget<T> | RouteLocation>({ alias: "hamurLink" })
|
|
79
|
+
readonly replace = input(false, { transform: booleanAttribute })
|
|
80
|
+
readonly prefetch = input<boolean | undefined, unknown>(undefined, { transform: optionalBoolean })
|
|
81
|
+
readonly prefetchOn = input<PrefetchOn>()
|
|
82
|
+
readonly prefetchDelay = input<number | undefined, unknown>(undefined, {
|
|
83
|
+
transform: (value: unknown) => (value === undefined ? undefined : numberAttribute(value)),
|
|
84
|
+
})
|
|
85
|
+
readonly viewTransition = input<ViewTransitionCall>()
|
|
86
|
+
readonly query = input<Query>()
|
|
87
|
+
readonly state = input<Record<string, unknown>>()
|
|
88
|
+
readonly relative = input<"route">()
|
|
89
|
+
readonly mask = input<string>()
|
|
90
|
+
readonly unmaskOnReload = input<boolean | undefined, unknown>(undefined, { transform: optionalBoolean })
|
|
91
|
+
readonly activeClass = input<string>()
|
|
92
|
+
readonly exactActiveClass = input<string>()
|
|
93
|
+
readonly pendingClass = input<string>()
|
|
94
|
+
readonly prefetchedClass = input<string>()
|
|
95
|
+
readonly ariaCurrentValue = input<AriaCurrent>("page")
|
|
96
|
+
readonly external = input(false, { transform: booleanAttribute })
|
|
97
|
+
readonly noRel = input(false, { transform: booleanAttribute })
|
|
98
|
+
readonly scroll = input<boolean | undefined, unknown>(undefined, { transform: optionalBoolean })
|
|
99
|
+
readonly keepFocus = input(false, { transform: booleanAttribute })
|
|
100
|
+
readonly beforeNavigate = output<Navigation>()
|
|
101
|
+
|
|
102
|
+
private readonly app = inject(HAMUR_APP, { optional: true })
|
|
103
|
+
private readonly route = inject(HAMUR_ROUTE, { optional: true })
|
|
104
|
+
private readonly resolve = inject(HAMUR_HREF, { optional: true }) ?? detachedHref
|
|
105
|
+
private readonly baseURL = inject(HAMUR_BASE_URL, { optional: true }) ?? "/"
|
|
106
|
+
private readonly element = inject<ElementRef<HTMLAnchorElement>>(ElementRef).nativeElement
|
|
107
|
+
private readonly rel = inject(new HostAttributeToken("rel"), { optional: true })
|
|
108
|
+
private readonly target = inject(new HostAttributeToken("target"), { optional: true })
|
|
109
|
+
private readonly given = inject(new HostAttributeToken("aria-current"), { optional: true })
|
|
110
|
+
|
|
111
|
+
private readonly defaults = computed(() => linkDefaultsOf(this.app))
|
|
112
|
+
private readonly classNames = computed(() => ({
|
|
113
|
+
active: this.activeClass() ?? this.defaults().activeClass,
|
|
114
|
+
exact: this.exactActiveClass() ?? this.defaults().exactActiveClass,
|
|
115
|
+
pending: this.pendingClass() ?? this.defaults().pendingClass,
|
|
116
|
+
prefetched: this.prefetchedClass() ?? this.defaults().prefetchedClass,
|
|
117
|
+
}))
|
|
118
|
+
private readonly linked = computed(() => {
|
|
119
|
+
const route = this.route?.()
|
|
120
|
+
const to = this.to() as string | RouteLocation
|
|
121
|
+
return resolveLinkTarget(
|
|
122
|
+
this.app,
|
|
123
|
+
route ? searchTarget(relativeTarget(to, route, this.relative()), route, this.query()) : to,
|
|
124
|
+
{
|
|
125
|
+
replace: this.replace(),
|
|
126
|
+
prefetch: this.prefetch(),
|
|
127
|
+
prefetchOn: this.prefetchOn(),
|
|
128
|
+
external: this.external(),
|
|
129
|
+
scroll: this.scroll(),
|
|
130
|
+
mask: this.mask(),
|
|
131
|
+
...(this.target === null ? {} : { target: this.target }),
|
|
132
|
+
},
|
|
133
|
+
this.resolve,
|
|
134
|
+
this.external(),
|
|
135
|
+
)
|
|
136
|
+
})
|
|
137
|
+
private readonly matched = computed(() => {
|
|
138
|
+
const route = this.route?.()
|
|
139
|
+
const target = this.linked().to
|
|
140
|
+
if (!route) return { active: false, exact: false }
|
|
141
|
+
return linkState(typeof target === "string" ? target : withoutBase(this.href(), this.baseURL), route)
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
/** The based `href` of the link. */
|
|
145
|
+
readonly href = computed(() => this.resolve(this.linked().to))
|
|
146
|
+
protected readonly ariaCurrent = computed(
|
|
147
|
+
() => this.given ?? (this.matched().exact ? this.ariaCurrentValue() : undefined),
|
|
148
|
+
)
|
|
149
|
+
protected readonly keepFocusValue = computed(() => (this.keepFocus() ? "" : undefined))
|
|
150
|
+
protected readonly classes = computed(() => {
|
|
151
|
+
const { active, exact } = this.matched()
|
|
152
|
+
return [active && this.classNames().active, exact && this.classNames().exact].filter(Boolean).join(" ")
|
|
153
|
+
})
|
|
154
|
+
private readonly attrs = computed(() => {
|
|
155
|
+
const defaults = this.defaults()
|
|
156
|
+
return linkAttributes({
|
|
157
|
+
href: this.href(),
|
|
158
|
+
external: this.external(),
|
|
159
|
+
...linkPrefetchProps({ prefetch: this.prefetch(), prefetchOn: this.prefetchOn() }, defaults),
|
|
160
|
+
replace: this.replace(),
|
|
161
|
+
viewTransition: this.viewTransition(),
|
|
162
|
+
pendingClass: this.classNames().pending,
|
|
163
|
+
trim: !!this.query(),
|
|
164
|
+
mask: this.mask() === undefined ? undefined : this.resolve(this.mask()!),
|
|
165
|
+
unmaskOnReload: this.unmaskOnReload(),
|
|
166
|
+
rel: this.rel ?? undefined,
|
|
167
|
+
target: this.target ?? undefined,
|
|
168
|
+
noRel: this.noRel(),
|
|
169
|
+
prefetchedClass: this.classNames().prefetched,
|
|
170
|
+
scroll: this.scroll(),
|
|
171
|
+
prefetchDelay: this.prefetchDelay() ?? defaults.prefetchDelay,
|
|
172
|
+
externalRelAttribute: defaults.externalRelAttribute,
|
|
173
|
+
})
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
constructor() {
|
|
177
|
+
effect(() => {
|
|
178
|
+
const state = this.state()
|
|
179
|
+
setLinkState(this.element, state, (navigation) => this.beforeNavigate.emit(navigation))
|
|
180
|
+
})
|
|
181
|
+
inject(DestroyRef).onDestroy(() => setLinkState(this.element, undefined))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
protected attr(name: string): string | undefined {
|
|
185
|
+
return this.attrs()[name] ?? (name === "rel" ? (this.rel ?? undefined) : undefined)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The `LinkSlot` of a link to `to`, for an element that renders its own `<a>`; `app.link` sets its defaults. */
|
|
190
|
+
export function injectLink<T extends string = string>(
|
|
191
|
+
to: CheckedTarget<T> | RouteLocation | (() => CheckedTarget<T> | RouteLocation),
|
|
192
|
+
options?: UseLinkOptions | (() => UseLinkOptions),
|
|
193
|
+
): Signal<LinkSlot> {
|
|
194
|
+
assertInInjectionContext(injectLink)
|
|
195
|
+
const app = inject(HAMUR_APP, { optional: true })
|
|
196
|
+
const route = inject(HAMUR_ROUTE, { optional: true })
|
|
197
|
+
const router = inject(HAMUR_ROUTER, { optional: true })
|
|
198
|
+
const navigation = inject(HAMUR_NAVIGATION, { optional: true })
|
|
199
|
+
const resolve = inject(HAMUR_HREF, { optional: true }) ?? detachedHref
|
|
200
|
+
const baseURL = inject(HAMUR_BASE_URL, { optional: true }) ?? "/"
|
|
201
|
+
return computed(() => {
|
|
202
|
+
const given = (typeof options === "function" ? options() : options) ?? {}
|
|
203
|
+
const defaults = linkDefaultsOf(app)
|
|
204
|
+
const base = (typeof to === "function" ? to() : to) as string | RouteLocation
|
|
205
|
+
const current = route?.()
|
|
206
|
+
const searched = current ? searchTarget(relativeTarget(base, current, given.relative), current, given.query) : base
|
|
207
|
+
const { to: target } = resolveLinkTarget(app, searched, given, resolve, given.external)
|
|
208
|
+
const href = resolve(target)
|
|
209
|
+
const state = current
|
|
210
|
+
? linkState(typeof target === "string" ? target : withoutBase(href, baseURL), current)
|
|
211
|
+
: { active: false, exact: false }
|
|
212
|
+
return createLinkSlot({
|
|
213
|
+
href,
|
|
214
|
+
to: target,
|
|
215
|
+
attrs: linkAttributes({
|
|
216
|
+
href,
|
|
217
|
+
external: given.external,
|
|
218
|
+
...linkPrefetchProps({}, defaults),
|
|
219
|
+
replace: given.replace,
|
|
220
|
+
viewTransition: given.viewTransition,
|
|
221
|
+
pendingClass: defaults.pendingClass,
|
|
222
|
+
trim: !!given.query,
|
|
223
|
+
ariaCurrent: state.exact ? "page" : undefined,
|
|
224
|
+
prefetchedClass: defaults.prefetchedClass,
|
|
225
|
+
scroll: given.scroll,
|
|
226
|
+
prefetchDelay: defaults.prefetchDelay,
|
|
227
|
+
externalRelAttribute: defaults.externalRelAttribute,
|
|
228
|
+
}),
|
|
229
|
+
active: state.active,
|
|
230
|
+
exact: state.exact,
|
|
231
|
+
pending: navigation ? linkPending(href, navigation(), baseURL) : false,
|
|
232
|
+
external: given.external,
|
|
233
|
+
options: {
|
|
234
|
+
replace: given.replace,
|
|
235
|
+
state: given.state,
|
|
236
|
+
viewTransition: given.viewTransition,
|
|
237
|
+
scroll: given.scroll === false ? false : undefined,
|
|
238
|
+
},
|
|
239
|
+
router,
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Inside a `Link`'s content, whether a navigation to that link is in flight; `false` anywhere else. */
|
|
245
|
+
export function injectLinkStatus(): { pending: Signal<boolean> } {
|
|
246
|
+
assertInInjectionContext(injectLinkStatus)
|
|
247
|
+
const link = inject(LINK_HREF, { optional: true })
|
|
248
|
+
const navigation = inject(HAMUR_NAVIGATION, { optional: true })
|
|
249
|
+
const baseURL = inject(HAMUR_BASE_URL, { optional: true }) ?? "/"
|
|
250
|
+
return { pending: computed(() => !!link && !!navigation && linkPending(link.href(), navigation(), baseURL)) }
|
|
251
|
+
}
|