@hamur/adapter-svelte 0.0.1 → 0.0.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.
- package/dist/src/index.js +1 -1
- package/dist/types/src/Announcer.svelte +26 -0
- package/dist/types/src/Await.svelte +43 -0
- package/dist/types/src/AwaitAsync.svelte +42 -0
- package/dist/types/src/BodyAttrs.svelte +7 -0
- package/dist/types/src/Caught.svelte +41 -0
- package/dist/types/src/ClientComponent.svelte +35 -0
- package/dist/types/src/ClientFallback.svelte +46 -0
- package/dist/types/src/ClientOnly.svelte +22 -0
- package/dist/types/src/DefaultErrorPage.svelte +15 -0
- package/dist/types/src/DevOnly.svelte +12 -0
- package/dist/types/src/ErrorBoundary.svelte +14 -0
- package/dist/types/src/FallbackEffect.svelte +9 -0
- package/dist/types/src/Form.svelte +96 -0
- package/dist/types/src/HeadLink.svelte +7 -0
- package/dist/types/src/HeadMeta.svelte +7 -0
- package/dist/types/src/HeadScript.svelte +7 -0
- package/dist/types/src/HeadStyle.svelte +7 -0
- package/dist/types/src/HeadTitle.svelte +6 -0
- package/dist/types/src/HtmlAttrs.svelte +7 -0
- package/dist/types/src/HttpHeader.svelte +7 -0
- package/dist/types/src/HttpStatusCode.svelte +7 -0
- package/dist/types/src/Island.svelte +59 -0
- package/dist/types/src/IslandChildren.svelte +7 -0
- package/dist/types/src/KeptOutlet.svelte +46 -0
- package/dist/types/src/KeptPage.svelte +55 -0
- package/dist/types/src/LazyHydration.svelte +54 -0
- package/dist/types/src/Link.svelte +209 -0
- package/dist/types/src/LoadingIndicator.svelte +18 -0
- package/dist/types/src/MarkdownNodes.svelte +36 -0
- package/dist/types/src/MarkdownRenderer.svelte +20 -0
- package/dist/types/src/MarkdownSlots.svelte +33 -0
- package/dist/types/src/Navigate.svelte +10 -0
- package/dist/types/src/Nest.svelte +83 -0
- package/dist/types/src/Outlet.svelte +31 -0
- package/dist/types/src/Rethrow.svelte +7 -0
- package/dist/types/src/Root.svelte +29 -0
- package/dist/types/src/RouteAnnouncer.svelte +47 -0
- package/dist/types/src/Scoped.svelte +11 -0
- package/dist/types/src/ServerComponent.svelte +19 -0
- package/dist/types/src/ServerComponentClient.svelte +64 -0
- package/dist/types/src/ServerIsland.svelte +26 -0
- package/dist/types/src/Slotted.svelte +11 -0
- package/dist/types/src/Thrown.svelte +9 -0
- package/dist/types/src/Time.svelte +23 -0
- package/dist/types/src/index.d.ts +7 -7
- package/package.json +22 -7
- package/src/index.ts +8 -7
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { KeptUnit } from "hamur/adapter"
|
|
3
|
+
import type { AppState, routeValue } from "./context.ts"
|
|
4
|
+
|
|
5
|
+
const keptStates = new WeakMap<KeptUnit, AppState>()
|
|
6
|
+
|
|
7
|
+
/** The app state a kept unit renders with while hidden: the route, payload and tree it was last shown with. */
|
|
8
|
+
function keptState(route: typeof routeValue, state: AppState, unit: KeptUnit): AppState {
|
|
9
|
+
let own = keptStates.get(unit)
|
|
10
|
+
if (!own) {
|
|
11
|
+
const { data = {}, handlerState } = unit.payload as { data?: Record<string, unknown>; handlerState?: unknown }
|
|
12
|
+
keptStates.set(
|
|
13
|
+
unit,
|
|
14
|
+
(own = {
|
|
15
|
+
...state,
|
|
16
|
+
route: route({ route: unit.route, payload: unit.payload, tree: unit.tree, app: state.app }),
|
|
17
|
+
components: unit.tree.components,
|
|
18
|
+
data,
|
|
19
|
+
handlerState,
|
|
20
|
+
tree: unit.tree,
|
|
21
|
+
}),
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
return own
|
|
25
|
+
}
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<script lang="ts">
|
|
29
|
+
import { getContext, onDestroy, setContext, untrack } from "svelte"
|
|
30
|
+
import type { Snippet } from "svelte"
|
|
31
|
+
import type { Source } from "./store.ts"
|
|
32
|
+
import type { Activation } from "./activation.ts"
|
|
33
|
+
import type { KeepAliveBase } from "./keepalive.ts"
|
|
34
|
+
|
|
35
|
+
let { name, unit, base, children }: { name: string; unit?: KeptUnit; base: KeepAliveBase; children: Snippet } =
|
|
36
|
+
$props()
|
|
37
|
+
const { APP_KEY, ACTIVATION_KEY, routeValue, activation, detach, setHidden } = base
|
|
38
|
+
const parent = getContext<Source<AppState>>(APP_KEY)
|
|
39
|
+
setContext<Source<AppState>>(APP_KEY, {
|
|
40
|
+
get: () => (unit ? keptState(routeValue, parent.get(), unit) : parent.get()),
|
|
41
|
+
set: parent.set,
|
|
42
|
+
subscribe: parent.subscribe,
|
|
43
|
+
})
|
|
44
|
+
const own = activation(
|
|
45
|
+
getContext<Activation | undefined>(ACTIVATION_KEY),
|
|
46
|
+
untrack(() => unit !== undefined),
|
|
47
|
+
)
|
|
48
|
+
setContext(ACTIVATION_KEY, own)
|
|
49
|
+
$effect(() => setHidden(own, unit !== undefined))
|
|
50
|
+
onDestroy(() => detach(own))
|
|
51
|
+
</script>
|
|
52
|
+
|
|
53
|
+
<div data-hamur-keepalive={name} hidden={unit !== undefined} style={unit ? undefined : "display:contents"}
|
|
54
|
+
>{@render children()}</div
|
|
55
|
+
>
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import {
|
|
3
|
+
flushSync,
|
|
4
|
+
getAllContexts,
|
|
5
|
+
getContext,
|
|
6
|
+
hydrate,
|
|
7
|
+
mount,
|
|
8
|
+
onDestroy,
|
|
9
|
+
onMount,
|
|
10
|
+
setContext,
|
|
11
|
+
unmount,
|
|
12
|
+
} from "svelte"
|
|
13
|
+
import { replayInteraction, whenTriggered } from "hamur/adapter"
|
|
14
|
+
import { APP_KEY, IDS_KEY, nextId } from "./context.ts"
|
|
15
|
+
import type { AppState, Ids } from "./context.ts"
|
|
16
|
+
import type { Source } from "./store.ts"
|
|
17
|
+
import type { Lazy } from "./lazy-hydration.ts"
|
|
18
|
+
|
|
19
|
+
let { lazy, onHydrated, ...rest }: { lazy: Lazy; onHydrated?: () => void } & Record<string, unknown> = $props()
|
|
20
|
+
|
|
21
|
+
const parentIds = getContext<Ids | undefined>(IDS_KEY)
|
|
22
|
+
if (parentIds) setContext(IDS_KEY, { base: `${nextId(parentIds)}-`, next: 0 })
|
|
23
|
+
const contexts = getAllContexts()
|
|
24
|
+
const app = getContext<Source<AppState> | undefined>(APP_KEY)?.get().app
|
|
25
|
+
const html = lazy.html(rest, contexts, app)
|
|
26
|
+
const controller = new AbortController()
|
|
27
|
+
let wrapper: HTMLElement
|
|
28
|
+
let instance: Record<string, unknown> | undefined
|
|
29
|
+
|
|
30
|
+
onMount(() => {
|
|
31
|
+
const hydrating = wrapper.firstElementChild !== null || (wrapper.textContent ?? "") !== ""
|
|
32
|
+
const ready = hydrating
|
|
33
|
+
? whenTriggered(wrapper, lazy.on, { ...lazy.options, signal: controller.signal }).then(lazy.load)
|
|
34
|
+
: lazy.load()
|
|
35
|
+
void ready.then((component) => {
|
|
36
|
+
if (controller.signal.aborted) return
|
|
37
|
+
const mounting = { target: wrapper, props: rest, context: contexts }
|
|
38
|
+
instance = hydrating ? hydrate(component, mounting) : mount(component, mounting)
|
|
39
|
+
flushSync()
|
|
40
|
+
onHydrated?.()
|
|
41
|
+
replayInteraction(wrapper)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
onDestroy(() => {
|
|
46
|
+
controller.abort()
|
|
47
|
+
if (instance) void unmount(instance)
|
|
48
|
+
})
|
|
49
|
+
</script>
|
|
50
|
+
|
|
51
|
+
<hamur-hydrate {...lazy.attributes} bind:this={wrapper}>
|
|
52
|
+
<!-- svelte-ignore hydration_html_changed -->
|
|
53
|
+
{@html html}
|
|
54
|
+
</hamur-hydrate>
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
<script lang="ts" generics="T extends string = string">
|
|
2
|
+
import { getContext, setContext, type Snippet } from "svelte"
|
|
3
|
+
import type { HTMLAnchorAttributes } from "svelte/elements"
|
|
4
|
+
import { withoutBase, type HamurLinkProps, type RouteLocation } from "hamur/app"
|
|
5
|
+
import { createSubscriber } from "svelte/reactivity"
|
|
6
|
+
import {
|
|
7
|
+
LINK_DEFAULTS,
|
|
8
|
+
createLinkSlot,
|
|
9
|
+
linkAttributes,
|
|
10
|
+
linkDefaultsOf,
|
|
11
|
+
linkExtraAttributes,
|
|
12
|
+
linkPending,
|
|
13
|
+
linkPrefetchProps,
|
|
14
|
+
linkState,
|
|
15
|
+
prefetchAttribute,
|
|
16
|
+
viewTransitionAttribute,
|
|
17
|
+
relativeTarget,
|
|
18
|
+
resolveLinkTarget,
|
|
19
|
+
searchTarget,
|
|
20
|
+
setLinkState,
|
|
21
|
+
type CheckedTarget,
|
|
22
|
+
type LinkDefaults,
|
|
23
|
+
type LinkSlot,
|
|
24
|
+
type Navigation,
|
|
25
|
+
type PrefetchOn,
|
|
26
|
+
warnRemovedLinkProps,
|
|
27
|
+
} from "hamur/adapter"
|
|
28
|
+
import { APP_KEY, LEVEL_KEY, LINK_KEY, detachedHref, type AppState, type Level } from "./context.ts"
|
|
29
|
+
import { track, type Source } from "./store.ts"
|
|
30
|
+
|
|
31
|
+
interface Props extends Omit<HTMLAnchorAttributes, "href" | "children">, HamurLinkProps {
|
|
32
|
+
to: CheckedTarget<T> | RouteLocation
|
|
33
|
+
/** Removed: write `to`. */
|
|
34
|
+
href?: never
|
|
35
|
+
replace?: boolean
|
|
36
|
+
prefetch?: boolean
|
|
37
|
+
prefetchOn?: PrefetchOn
|
|
38
|
+
/** Removed: write `prefetch={false}`. */
|
|
39
|
+
noPrefetch?: never
|
|
40
|
+
viewTransition?: boolean | { types: readonly string[] }
|
|
41
|
+
query?: (prev: never) => Record<string, unknown>
|
|
42
|
+
state?: Record<string, unknown>
|
|
43
|
+
relative?: "route"
|
|
44
|
+
mask?: string
|
|
45
|
+
unmaskOnReload?: boolean
|
|
46
|
+
activeClass?: string
|
|
47
|
+
exactActiveClass?: string
|
|
48
|
+
pendingClass?: string
|
|
49
|
+
ariaCurrentValue?: "page" | "step" | "location" | "date" | "time" | "true"
|
|
50
|
+
external?: boolean
|
|
51
|
+
noRel?: boolean
|
|
52
|
+
prefetchedClass?: string
|
|
53
|
+
scroll?: boolean
|
|
54
|
+
prefetchDelay?: number
|
|
55
|
+
onBeforeNavigate?: (nav: Navigation) => void
|
|
56
|
+
/** Renders only the `children` snippet, given the `LinkSlot`, instead of an `<a>`. */
|
|
57
|
+
custom?: boolean
|
|
58
|
+
/** Set by `defineLink`; read over `app.link`. */
|
|
59
|
+
defaults?: LinkDefaults
|
|
60
|
+
children?: Snippet<[LinkSlot]>
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let {
|
|
64
|
+
to,
|
|
65
|
+
href: removedHref,
|
|
66
|
+
query,
|
|
67
|
+
state: entryState,
|
|
68
|
+
relative,
|
|
69
|
+
mask,
|
|
70
|
+
unmaskOnReload,
|
|
71
|
+
replace = false,
|
|
72
|
+
prefetch,
|
|
73
|
+
prefetchOn,
|
|
74
|
+
noPrefetch: removedNoPrefetch,
|
|
75
|
+
viewTransition,
|
|
76
|
+
activeClass,
|
|
77
|
+
exactActiveClass,
|
|
78
|
+
pendingClass,
|
|
79
|
+
ariaCurrentValue = "page",
|
|
80
|
+
external,
|
|
81
|
+
noRel,
|
|
82
|
+
prefetchedClass,
|
|
83
|
+
scroll,
|
|
84
|
+
prefetchDelay,
|
|
85
|
+
onBeforeNavigate,
|
|
86
|
+
custom = false,
|
|
87
|
+
defaults,
|
|
88
|
+
children,
|
|
89
|
+
...rest
|
|
90
|
+
}: Props = $props()
|
|
91
|
+
warnRemovedLinkProps({ href: removedHref, noPrefetch: removedNoPrefetch, prefetch })
|
|
92
|
+
const source = getContext<Source<AppState> | undefined>(APP_KEY)
|
|
93
|
+
const read = source && track(source)
|
|
94
|
+
const level = getContext<Level | undefined>(LEVEL_KEY)
|
|
95
|
+
const baseURL = source?.get().baseURL ?? "/"
|
|
96
|
+
const resolved = $derived(linkDefaultsOf(source?.get().app, defaults))
|
|
97
|
+
const classes = $derived({
|
|
98
|
+
active: activeClass ?? resolved.activeClass,
|
|
99
|
+
exact: exactActiveClass ?? resolved.exactActiveClass,
|
|
100
|
+
pending: pendingClass ?? resolved.pendingClass,
|
|
101
|
+
prefetched: prefetchedClass ?? resolved.prefetchedClass,
|
|
102
|
+
})
|
|
103
|
+
const mode = $derived(external ? "false" : prefetchAttribute(linkPrefetchProps({ prefetch, prefetchOn }, resolved)))
|
|
104
|
+
const given = $derived(to)
|
|
105
|
+
const linked = $derived(
|
|
106
|
+
resolveLinkTarget(
|
|
107
|
+
read?.().app,
|
|
108
|
+
read ? searchTarget(relativeTarget(given, level?.route ?? read().route, relative), read().route, query) : given,
|
|
109
|
+
rest,
|
|
110
|
+
read?.().href,
|
|
111
|
+
external,
|
|
112
|
+
),
|
|
113
|
+
)
|
|
114
|
+
const target = $derived(linked.to)
|
|
115
|
+
const attrs = $derived(linked.props)
|
|
116
|
+
const href = $derived(read ? read().href(target) : detachedHref(target))
|
|
117
|
+
setContext(LINK_KEY, () => href)
|
|
118
|
+
const current = $derived(
|
|
119
|
+
read
|
|
120
|
+
? linkState(typeof target === "string" ? target : withoutBase(href, baseURL), read().route)
|
|
121
|
+
: { active: false, exact: false },
|
|
122
|
+
)
|
|
123
|
+
let anchor: HTMLAnchorElement | undefined = $state()
|
|
124
|
+
$effect(() => {
|
|
125
|
+
const element = anchor
|
|
126
|
+
const before = onBeforeNavigate
|
|
127
|
+
if (!element || (entryState === undefined && !before)) return
|
|
128
|
+
setLinkState(element, entryState, before ? (nav) => onBeforeNavigate?.(nav) : undefined)
|
|
129
|
+
return () => setLinkState(element, undefined)
|
|
130
|
+
})
|
|
131
|
+
const extras = $derived(
|
|
132
|
+
linkExtraAttributes({
|
|
133
|
+
href,
|
|
134
|
+
external,
|
|
135
|
+
noRel,
|
|
136
|
+
rel: rest.rel,
|
|
137
|
+
target: rest.target,
|
|
138
|
+
prefetchedClass: classes.prefetched,
|
|
139
|
+
scroll,
|
|
140
|
+
prefetchDelay: prefetchDelay ?? resolved.prefetchDelay,
|
|
141
|
+
externalRelAttribute: resolved.externalRelAttribute,
|
|
142
|
+
}),
|
|
143
|
+
)
|
|
144
|
+
const subscribeNavigation = createSubscriber((update) => source?.get().navigation?.subscribe(update) ?? (() => {}))
|
|
145
|
+
const slot = $derived.by(() => {
|
|
146
|
+
subscribeNavigation()
|
|
147
|
+
const app = read?.()
|
|
148
|
+
return createLinkSlot({
|
|
149
|
+
href,
|
|
150
|
+
to: target,
|
|
151
|
+
attrs: linkAttributes({
|
|
152
|
+
href,
|
|
153
|
+
external,
|
|
154
|
+
...linkPrefetchProps({ prefetch, prefetchOn }, resolved),
|
|
155
|
+
replace,
|
|
156
|
+
viewTransition,
|
|
157
|
+
pendingClass: classes.pending,
|
|
158
|
+
trim: !!query,
|
|
159
|
+
mask: mask === undefined ? undefined : app ? app.href(mask) : detachedHref(mask),
|
|
160
|
+
unmaskOnReload,
|
|
161
|
+
ariaCurrent: current.exact ? ariaCurrentValue : undefined,
|
|
162
|
+
rel: rest.rel,
|
|
163
|
+
target: rest.target,
|
|
164
|
+
noRel,
|
|
165
|
+
prefetchedClass: classes.prefetched,
|
|
166
|
+
scroll,
|
|
167
|
+
prefetchDelay: prefetchDelay ?? resolved.prefetchDelay,
|
|
168
|
+
externalRelAttribute: resolved.externalRelAttribute,
|
|
169
|
+
}),
|
|
170
|
+
active: current.active,
|
|
171
|
+
exact: current.exact,
|
|
172
|
+
pending: !!app && linkPending(href, app.navigation.get(), app.baseURL),
|
|
173
|
+
external,
|
|
174
|
+
options: {
|
|
175
|
+
replace,
|
|
176
|
+
state: entryState,
|
|
177
|
+
mask,
|
|
178
|
+
unmaskOnReload,
|
|
179
|
+
viewTransition,
|
|
180
|
+
scroll: scroll === false ? false : undefined,
|
|
181
|
+
},
|
|
182
|
+
router: app?.router ?? null,
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
const plain = $derived(children as Snippet | undefined)
|
|
186
|
+
const marked = $derived({
|
|
187
|
+
...attrs,
|
|
188
|
+
...extras,
|
|
189
|
+
...(current.active ? { class: [rest.class, classes.active, current.exact && classes.exact] } : {}),
|
|
190
|
+
...(current.exact && attrs["aria-current"] === undefined ? { "aria-current": ariaCurrentValue } : {}),
|
|
191
|
+
})
|
|
192
|
+
</script>
|
|
193
|
+
|
|
194
|
+
{#if custom}
|
|
195
|
+
{@render children?.(slot)}
|
|
196
|
+
{:else}
|
|
197
|
+
<a
|
|
198
|
+
bind:this={anchor}
|
|
199
|
+
{...marked}
|
|
200
|
+
{href}
|
|
201
|
+
data-hamur-prefetch={mode}
|
|
202
|
+
data-hamur-replace={replace ? "" : undefined}
|
|
203
|
+
data-hamur-view-transition={viewTransitionAttribute(viewTransition)}
|
|
204
|
+
data-hamur-pending-class={classes.pending === LINK_DEFAULTS.pendingClass ? undefined : classes.pending}
|
|
205
|
+
data-hamur-trim={query ? "" : undefined}
|
|
206
|
+
data-hamur-mask={mask === undefined ? undefined : read ? read().href(mask) : detachedHref(mask)}
|
|
207
|
+
data-hamur-unmask={mask !== undefined && unmaskOnReload === false ? "false" : undefined}>{@render plain?.()}</a
|
|
208
|
+
>
|
|
209
|
+
{/if}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { loadingIndicatorOptions, paintLoadingIndicator, type LoadingIndicatorProps } from "hamur/adapter"
|
|
3
|
+
import { useLoadingIndicator } from "./index.ts"
|
|
4
|
+
|
|
5
|
+
let props: LoadingIndicatorProps = $props()
|
|
6
|
+
const indicator = useLoadingIndicator(loadingIndicatorOptions(props))
|
|
7
|
+
let element: HTMLElement | undefined = $state()
|
|
8
|
+
$effect(() => {
|
|
9
|
+
if (element)
|
|
10
|
+
paintLoadingIndicator(
|
|
11
|
+
element,
|
|
12
|
+
{ progress: indicator.progress, isLoading: indicator.isLoading, error: indicator.error },
|
|
13
|
+
{ color: props.color, errorColor: props.errorColor, height: props.height },
|
|
14
|
+
)
|
|
15
|
+
})
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<div bind:this={element} data-hamur-loading-indicator aria-hidden="true"></div>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component } from "svelte"
|
|
3
|
+
import { markdownLink, type MdView, type MdViewComponent } from "hamur/adapter"
|
|
4
|
+
import Link from "./Link.svelte"
|
|
5
|
+
import MarkdownNodes from "./MarkdownNodes.svelte"
|
|
6
|
+
import MarkdownSlots from "./MarkdownSlots.svelte"
|
|
7
|
+
|
|
8
|
+
let { views, components }: { views: MdView[]; components: Record<string, Component<any>> } = $props()
|
|
9
|
+
const VOID = new Set(["hr", "br", "img", "input"])
|
|
10
|
+
const resolve = (view: MdViewComponent) =>
|
|
11
|
+
Object.hasOwn(components, view.component) ? components[view.component] : undefined
|
|
12
|
+
const slotsOf = (view: MdViewComponent): Array<[string, MdView[]]> => [
|
|
13
|
+
...(view.children.length ? [["children", view.children] as [string, MdView[]]] : []),
|
|
14
|
+
...Object.entries(view.slots),
|
|
15
|
+
]
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
{#each views as view}{@const link =
|
|
19
|
+
typeof view === "object" && "tag" in view ? markdownLink(view) : undefined}{@const target =
|
|
20
|
+
typeof view === "object" && "component" in view
|
|
21
|
+
? resolve(view)
|
|
22
|
+
: undefined}{#if typeof view === "string"}{view}{:else if "html" in view}<hamur-html style="display:contents"
|
|
23
|
+
>{@html view.html}</hamur-html
|
|
24
|
+
>{:else if "component" in view}{#if target}<MarkdownSlots
|
|
25
|
+
component={target}
|
|
26
|
+
{view}
|
|
27
|
+
{components}
|
|
28
|
+
slots={slotsOf(view)}
|
|
29
|
+
/>{:else if view.attrs}<svelte:element this={view.component} {...view.attrs}
|
|
30
|
+
><MarkdownNodes views={view.children} {components} /></svelte:element
|
|
31
|
+
>{:else}<MarkdownNodes views={view.children} {components} />{/if}{:else if link}<Link {...link.attrs} to={link.to}
|
|
32
|
+
><MarkdownNodes views={view.children} {components} /></Link
|
|
33
|
+
>{:else if VOID.has(view.tag)}<svelte:element this={view.tag} {...view.attrs} />{:else}<svelte:element
|
|
34
|
+
this={view.tag}
|
|
35
|
+
{...view.attrs}><MarkdownNodes views={view.children} {components} /></svelte:element
|
|
36
|
+
>{/if}{/each}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component } from "svelte"
|
|
3
|
+
import { markdownProse, markdownView } from "hamur/adapter"
|
|
4
|
+
import type { MarkdownDocument, MdNode } from "hamur/markdown"
|
|
5
|
+
import MarkdownNodes from "./MarkdownNodes.svelte"
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
value,
|
|
9
|
+
components = {},
|
|
10
|
+
raw,
|
|
11
|
+
}: {
|
|
12
|
+
value: MdNode | MdNode[] | MarkdownDocument
|
|
13
|
+
components?: Record<string, Component<any>>
|
|
14
|
+
raw?: (html: string) => string
|
|
15
|
+
} = $props()
|
|
16
|
+
const prose = $derived(markdownProse(value, components))
|
|
17
|
+
const views = $derived(markdownView(prose.value, { html: raw !== undefined, components: prose.components }))
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<MarkdownNodes {views} components={prose.components} />
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component, Snippet } from "svelte"
|
|
3
|
+
import type { MdView, MdViewComponent } from "hamur/adapter"
|
|
4
|
+
import MarkdownNodes from "./MarkdownNodes.svelte"
|
|
5
|
+
import MarkdownSlots from "./MarkdownSlots.svelte"
|
|
6
|
+
|
|
7
|
+
// One level per slot: a snippet takes no arguments when the component renders it, so each level closes over its own slot.
|
|
8
|
+
let {
|
|
9
|
+
component: Target,
|
|
10
|
+
view,
|
|
11
|
+
components,
|
|
12
|
+
slots,
|
|
13
|
+
index = 0,
|
|
14
|
+
snippets = {},
|
|
15
|
+
}: {
|
|
16
|
+
component: Component<any>
|
|
17
|
+
view: MdViewComponent
|
|
18
|
+
components: Record<string, Component<any>>
|
|
19
|
+
slots: Array<[string, MdView[]]>
|
|
20
|
+
index?: number
|
|
21
|
+
snippets?: Record<string, Snippet>
|
|
22
|
+
} = $props()
|
|
23
|
+
const entry = $derived(slots[index])
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
{#snippet slot()}<MarkdownNodes views={entry![1]} {components} />{/snippet}{#if entry}<MarkdownSlots
|
|
27
|
+
component={Target}
|
|
28
|
+
{view}
|
|
29
|
+
{components}
|
|
30
|
+
{slots}
|
|
31
|
+
index={index + 1}
|
|
32
|
+
snippets={{ ...snippets, [entry[0]]: slot }}
|
|
33
|
+
/>{:else}<Target {...view.props} {...snippets} />{/if}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<script lang="ts" generics="T extends string = string">
|
|
2
|
+
import { onMount, untrack } from "svelte"
|
|
3
|
+
import { navigateTo, redirect, type RouteLocation } from "hamur/app"
|
|
4
|
+
import type { CheckedTarget } from "hamur/adapter"
|
|
5
|
+
|
|
6
|
+
let { to, replace }: { to: CheckedTarget<T, false> | RouteLocation; replace?: boolean } = $props()
|
|
7
|
+
|
|
8
|
+
if (typeof document === "undefined") throw redirect<string>(untrack(() => to) as string | RouteLocation)
|
|
9
|
+
onMount(() => void navigateTo<string>(to as string | RouteLocation, { replace }))
|
|
10
|
+
</script>
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { setContext } from "svelte"
|
|
3
|
+
import type { Component as SvelteComponent } from "svelte"
|
|
4
|
+
import type { KeptUnit, RenderTree } from "hamur/adapter"
|
|
5
|
+
import { LEVEL_KEY, keepAliveBase, keptOutlet } from "./context.ts"
|
|
6
|
+
import type { RouteValue } from "hamur/adapter"
|
|
7
|
+
import Nest from "./Nest.svelte"
|
|
8
|
+
import Caught from "./Caught.svelte"
|
|
9
|
+
import Thrown from "./Thrown.svelte"
|
|
10
|
+
|
|
11
|
+
let {
|
|
12
|
+
components,
|
|
13
|
+
index = 0,
|
|
14
|
+
route,
|
|
15
|
+
top,
|
|
16
|
+
bare = false,
|
|
17
|
+
}: {
|
|
18
|
+
components: RenderTree["components"]
|
|
19
|
+
index?: number
|
|
20
|
+
route?: RouteValue
|
|
21
|
+
top?: { kept?: KeptUnit[] }
|
|
22
|
+
bare?: boolean
|
|
23
|
+
} = $props()
|
|
24
|
+
const entry = $derived(components[index])
|
|
25
|
+
const Component = $derived(
|
|
26
|
+
((import.meta.hot
|
|
27
|
+
? (globalThis as Record<symbol, ((c: unknown) => unknown) | undefined>)[Symbol.for("hamur.svelte.hot")]?.(
|
|
28
|
+
entry?.component,
|
|
29
|
+
)
|
|
30
|
+
: undefined) ?? entry?.component) as SvelteComponent<any>,
|
|
31
|
+
)
|
|
32
|
+
const thrown = $derived(entry?.caught !== undefined)
|
|
33
|
+
const last = $derived(index >= components.length - 1)
|
|
34
|
+
const kept = $derived(bare ? undefined : (index === 0 ? top : components[index - 1])?.kept)
|
|
35
|
+
const Kept = $derived(kept ? keptOutlet() : undefined)
|
|
36
|
+
setContext(LEVEL_KEY, {
|
|
37
|
+
get id() {
|
|
38
|
+
return entry?.id
|
|
39
|
+
},
|
|
40
|
+
get components() {
|
|
41
|
+
return components
|
|
42
|
+
},
|
|
43
|
+
get index() {
|
|
44
|
+
return index
|
|
45
|
+
},
|
|
46
|
+
get route() {
|
|
47
|
+
return route
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
{#snippet level()}
|
|
53
|
+
<Component {...entry?.props}>
|
|
54
|
+
{#if !last}
|
|
55
|
+
<Nest {components} {route} index={index + 1} />
|
|
56
|
+
{/if}
|
|
57
|
+
</Component>
|
|
58
|
+
{/snippet}
|
|
59
|
+
|
|
60
|
+
{#snippet body(entry: RenderTree["components"][number])}
|
|
61
|
+
{#if entry.boundary || thrown}
|
|
62
|
+
<svelte:boundary>
|
|
63
|
+
{#if thrown}
|
|
64
|
+
<Thrown error={entry.props?.error as never} />
|
|
65
|
+
{:else}
|
|
66
|
+
{@render level()}
|
|
67
|
+
{/if}
|
|
68
|
+
{#snippet failed(error, reset)}
|
|
69
|
+
<Caught {entry} {error} {reset} {thrown} />
|
|
70
|
+
{/snippet}
|
|
71
|
+
</svelte:boundary>
|
|
72
|
+
{:else}
|
|
73
|
+
{@render level()}
|
|
74
|
+
{/if}
|
|
75
|
+
{/snippet}
|
|
76
|
+
|
|
77
|
+
{#if kept && Kept}
|
|
78
|
+
<Kept {kept} {entry} {components} {index} {route} {Nest} base={keepAliveBase} />
|
|
79
|
+
{:else if entry}
|
|
80
|
+
{#key entry.key}
|
|
81
|
+
{@render body(entry)}
|
|
82
|
+
{/key}
|
|
83
|
+
{/if}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, setContext } from "svelte"
|
|
3
|
+
import { noteOutlet } from "hamur/adapter"
|
|
4
|
+
import { LEVEL_KEY, OUTLET_VALUE_KEY, interceptedRoute, useAppState } from "./context.ts"
|
|
5
|
+
import type { Level } from "./context.ts"
|
|
6
|
+
import Nest from "./Nest.svelte"
|
|
7
|
+
|
|
8
|
+
let { context, name }: { context?: unknown; name?: string } = $props()
|
|
9
|
+
const level = getContext<Level | undefined>(LEVEL_KEY)
|
|
10
|
+
if (!level) throw new Error("Outlet must be rendered inside a Hamur app")
|
|
11
|
+
const above = getContext<{ readonly value: unknown } | undefined>(OUTLET_VALUE_KEY)
|
|
12
|
+
setContext(OUTLET_VALUE_KEY, {
|
|
13
|
+
get value() {
|
|
14
|
+
return context !== undefined ? context : above?.value
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
const slot = $derived(name === undefined ? undefined : level.components[level.index]?.outlets?.[name])
|
|
18
|
+
const read = useAppState("Outlet")
|
|
19
|
+
const route = $derived(slot?.route ? interceptedRoute(read(), slot) : undefined)
|
|
20
|
+
const isDev = (import.meta as { dev?: boolean }).dev === true
|
|
21
|
+
if (isDev && name === undefined) noteOutlet(level.id)
|
|
22
|
+
if (isDev && name !== undefined && !slot) console.warn(`Outlet name "${name}" has no slot in ${level.id}`)
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
{#if name !== undefined}
|
|
26
|
+
{#if slot?.components.length}
|
|
27
|
+
<Nest components={slot.components} {route} index={0} />
|
|
28
|
+
{/if}
|
|
29
|
+
{:else if level.index < level.components.length - 1}
|
|
30
|
+
<Nest components={level.components} index={level.index + 1} />
|
|
31
|
+
{/if}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { setContext } from "svelte"
|
|
3
|
+
import { reportClientError } from "hamur/adapter"
|
|
4
|
+
import { APP_KEY, IDS_KEY, rootIds } from "./context.ts"
|
|
5
|
+
import type { AppState } from "./context.ts"
|
|
6
|
+
import { track } from "./store.ts"
|
|
7
|
+
import type { Source } from "./store.ts"
|
|
8
|
+
import Nest from "./Nest.svelte"
|
|
9
|
+
|
|
10
|
+
let { source, idPrefix = "" }: { source: Source<AppState>; idPrefix?: string } = $props()
|
|
11
|
+
setContext(APP_KEY, source)
|
|
12
|
+
setContext(IDS_KEY, rootIds(idPrefix))
|
|
13
|
+
const read = track(source)
|
|
14
|
+
const components = $derived(read().components)
|
|
15
|
+
const tree = $derived(read().tree)
|
|
16
|
+
|
|
17
|
+
// No `failed` snippet: the server then renders no boundary and its errors still reach the error page.
|
|
18
|
+
function onerror(error: unknown, reset: () => void): void {
|
|
19
|
+
const before = source.get()
|
|
20
|
+
queueMicrotask(() => {
|
|
21
|
+
reportClientError(before.app, error, { render: true })
|
|
22
|
+
if (source.get() !== before) reset()
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<svelte:boundary {onerror}>
|
|
28
|
+
<Nest {components} top={tree} />
|
|
29
|
+
</svelte:boundary>
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import {
|
|
3
|
+
ROUTE_ANNOUNCER_ID,
|
|
4
|
+
announcerRole,
|
|
5
|
+
createRouteAnnouncer,
|
|
6
|
+
hideAnnouncer,
|
|
7
|
+
type RouteAnnouncerPoliteness,
|
|
8
|
+
} from "hamur/adapter"
|
|
9
|
+
import { onMount } from "svelte"
|
|
10
|
+
import { useApp, useRouteAnnouncer } from "./index.ts"
|
|
11
|
+
|
|
12
|
+
let {
|
|
13
|
+
politeness,
|
|
14
|
+
class: className,
|
|
15
|
+
style,
|
|
16
|
+
}: { politeness?: RouteAnnouncerPoliteness; class?: string; style?: string } = $props()
|
|
17
|
+
const store = createRouteAnnouncer(useApp())
|
|
18
|
+
const announcer = useRouteAnnouncer()
|
|
19
|
+
const key = {}
|
|
20
|
+
let tick = $state(0)
|
|
21
|
+
const owner = $derived.by(() => {
|
|
22
|
+
void tick
|
|
23
|
+
return store.claim(key, () => tick++)
|
|
24
|
+
})
|
|
25
|
+
let element: HTMLElement | undefined = $state()
|
|
26
|
+
const live = $derived(politeness ?? announcer.politeness)
|
|
27
|
+
onMount(() => () => store.release(key))
|
|
28
|
+
$effect(() => {
|
|
29
|
+
if (!owner) return
|
|
30
|
+
if (!className && !style && element) hideAnnouncer(element)
|
|
31
|
+
return store.mount()
|
|
32
|
+
})
|
|
33
|
+
</script>
|
|
34
|
+
|
|
35
|
+
{#if owner}
|
|
36
|
+
<div
|
|
37
|
+
bind:this={element}
|
|
38
|
+
id={ROUTE_ANNOUNCER_ID}
|
|
39
|
+
aria-live={live}
|
|
40
|
+
aria-atomic="true"
|
|
41
|
+
role={announcerRole(live)}
|
|
42
|
+
class={className}
|
|
43
|
+
{style}>{announcer.message}</div
|
|
44
|
+
>
|
|
45
|
+
{:else}
|
|
46
|
+
<div aria-live="off" aria-atomic="true"></div>
|
|
47
|
+
{/if}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { setContext } from "svelte"
|
|
3
|
+
import type { Snippet } from "svelte"
|
|
4
|
+
import type { ErrorPageProps } from "hamur/app"
|
|
5
|
+
import { ERROR_KEY } from "./context.ts"
|
|
6
|
+
|
|
7
|
+
let { error, children }: { error: ErrorPageProps; children?: Snippet } = $props()
|
|
8
|
+
setContext(ERROR_KEY, error)
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
{@render children?.()}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component } from "svelte"
|
|
3
|
+
import { serverComponentAttributes, serverComponentProps } from "hamur/adapter"
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
server,
|
|
7
|
+
children,
|
|
8
|
+
...rest
|
|
9
|
+
}: { server: { name: string; component: Component<Record<string, unknown>> }; children?: unknown } & Record<
|
|
10
|
+
string,
|
|
11
|
+
unknown
|
|
12
|
+
> = $props()
|
|
13
|
+
const uid = $props.id()
|
|
14
|
+
const name = $derived(server.name)
|
|
15
|
+
const Inner = $derived(server.component)
|
|
16
|
+
serverComponentProps(server.name, rest, children !== undefined && !server.name.startsWith("page:"))
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
<hamur-component {name} {uid} {...serverComponentAttributes(server.name)}><Inner {...rest} /></hamur-component>
|