@hamur/adapter-angular 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/adapter.js +88 -37
- package/dist/types/src/app.d.ts +3 -0
- package/dist/types/src/client-parts.d.ts +3 -0
- package/dist/types/src/compile.d.ts +13 -3
- package/dist/types/src/data.d.ts +3 -3
- package/dist/types/src/error-page.d.ts +5 -2
- package/dist/types/src/form.d.ts +16 -13
- package/dist/types/src/hmr.d.ts +1 -1
- package/dist/types/src/indicators.d.ts +15 -8
- package/dist/types/src/island.d.ts +9 -6
- package/dist/types/src/link.d.ts +25 -22
- package/dist/types/src/markdown.d.ts +6 -3
- package/dist/types/src/navigate.d.ts +6 -3
- package/dist/types/src/outlet.d.ts +10 -5
- package/dist/types/src/server-island.d.ts +6 -3
- package/dist/types/src/time.d.ts +28 -25
- package/package.json +30 -9
- package/src/app.ts +11 -5
- package/src/client-parts.ts +83 -103
- package/src/compile.ts +123 -60
- package/src/data.ts +3 -3
- package/src/error-page.ts +23 -10
- package/src/form.ts +166 -237
- package/src/hmr.ts +3 -2
- package/src/indicators.ts +198 -229
- package/src/island.ts +78 -84
- package/src/link.ts +204 -241
- package/src/markdown.ts +129 -159
- package/src/navigate.ts +20 -17
- package/src/outlet.ts +297 -325
- package/src/server-island.ts +62 -49
- package/src/time.ts +108 -101
package/src/markdown.ts
CHANGED
|
@@ -1,170 +1,140 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Renderer2,
|
|
8
|
-
createComponent,
|
|
9
|
-
effect,
|
|
10
|
-
inject,
|
|
11
|
-
input,
|
|
12
|
-
reflectComponentType,
|
|
13
|
-
untracked,
|
|
14
|
-
type ComponentRef,
|
|
15
|
-
type Type,
|
|
16
|
-
} from "@angular/core"
|
|
17
|
-
import {
|
|
18
|
-
linkAttributes,
|
|
19
|
-
linkDefaultsOf,
|
|
20
|
-
linkPrefetchProps,
|
|
21
|
-
markdownLink,
|
|
22
|
-
markdownProse,
|
|
23
|
-
markdownView,
|
|
24
|
-
} from "hamur/adapter"
|
|
25
|
-
import type { MdView, MdViewComponent } from "hamur/adapter"
|
|
26
|
-
import type { MarkdownDocument, MdNode, MdProps } from "hamur/markdown"
|
|
27
|
-
import { HAMUR_APP, HAMUR_HREF, detachedHref } from "./context.ts"
|
|
28
|
-
|
|
29
|
-
export interface MarkdownRendererProps {
|
|
30
|
-
/** A Markdown AST node, a list of nodes, or a document, which renders its `ast`. */
|
|
31
|
-
value: MdNode | MdNode[] | MarkdownDocument
|
|
32
|
-
/** Components by Markdown component name (kebab case); each gets the node's props, its default slot and a slot per named slot. */
|
|
33
|
-
components?: Record<string, Type<unknown>>
|
|
34
|
-
/** Renders `html` nodes, writing what it returns as their inner HTML; without it they are left out. A Markdown page under config `markdown.html` passes the core's Trusted Types writer. */
|
|
35
|
-
raw?: (html: string) => string | { readonly __brand: "TrustedHTML" }
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type Components = Record<string, Type<unknown>>
|
|
39
|
-
|
|
40
|
-
const camelize = (name: string) => name.replace(/-(\w)/g, (_, char: string) => char.toUpperCase())
|
|
41
|
-
const SLOT = /^\[(?:slot=)?["']?([\w-]+)["']?\]$|^([\w-]+)$/
|
|
42
|
-
|
|
1
|
+
import { ApplicationRef, Component, ElementRef, EnvironmentInjector, Injector, Renderer2, createComponent, effect, inject, input, reflectComponentType, untracked, } from "@angular/core";
|
|
2
|
+
import { linkAttributes, linkDefaultsOf, linkPrefetchProps, markdownLink, markdownProse, markdownView, } from "hamur/adapter";
|
|
3
|
+
import { HAMUR_APP, HAMUR_HREF, detachedHref } from "./context.ts";
|
|
4
|
+
import * as i0 from "@angular/core";
|
|
5
|
+
const camelize = (name) => name.replace(/-(\w)/g, (_, char) => char.toUpperCase());
|
|
6
|
+
const SLOT = /^\[(?:slot=)?["']?([\w-]+)["']?\]$|^([\w-]+)$/;
|
|
43
7
|
/**
|
|
44
8
|
* Renders the Markdown AST as elements: an internal link as the anchor `Link` writes, a component node as its component
|
|
45
9
|
* in `components` (its props as inputs, the others as attributes; its default slot and each named slot projected into the `ng-content`
|
|
46
10
|
* whose selector is the slot's name, as `[name]` or `[slot=name]`). The elements are made with the renderer, so Angular skips
|
|
47
11
|
* the host in hydration and the browser builds them again from the same AST.
|
|
48
12
|
*/
|
|
49
|
-
@Component({ selector: "hamur-markdown", template: "", host: { ngSkipHydration: "true" } })
|
|
50
13
|
export class MarkdownRenderer {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
raw: MarkdownRendererProps["raw"],
|
|
82
|
-
components: Components,
|
|
83
|
-
refs: Array<ComponentRef<unknown>>,
|
|
84
|
-
): void {
|
|
85
|
-
const renderer = this.renderer
|
|
86
|
-
if (typeof view === "string") return renderer.appendChild(parent, renderer.createText(view))
|
|
87
|
-
if ("html" in view) {
|
|
88
|
-
const element = renderer.createElement("hamur-html")
|
|
89
|
-
renderer.setStyle(element, "display", "contents")
|
|
90
|
-
renderer.setProperty(element, "innerHTML", raw!(view.html))
|
|
91
|
-
return renderer.appendChild(parent, element)
|
|
14
|
+
value = input.required(/* @ts-ignore */
|
|
15
|
+
...(ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []));
|
|
16
|
+
components = input(/* @ts-ignore */
|
|
17
|
+
...(ngDevMode ? [undefined, { debugName: "components" }] : /* istanbul ignore next */ []));
|
|
18
|
+
raw = input(/* @ts-ignore */
|
|
19
|
+
...(ngDevMode ? [undefined, { debugName: "raw" }] : /* istanbul ignore next */ []));
|
|
20
|
+
host = inject(ElementRef).nativeElement;
|
|
21
|
+
renderer = inject(Renderer2);
|
|
22
|
+
app = inject(HAMUR_APP);
|
|
23
|
+
resolve = inject(HAMUR_HREF, { optional: true }) ?? detachedHref;
|
|
24
|
+
injector = inject(Injector);
|
|
25
|
+
environment = inject(EnvironmentInjector);
|
|
26
|
+
appRef = inject(ApplicationRef);
|
|
27
|
+
constructor() {
|
|
28
|
+
effect((onCleanup) => {
|
|
29
|
+
const raw = this.raw();
|
|
30
|
+
const prose = markdownProse(this.value(), this.components() ?? {});
|
|
31
|
+
const views = markdownView(prose.value, { html: raw !== undefined, components: prose.components });
|
|
32
|
+
const refs = [];
|
|
33
|
+
untracked(() => {
|
|
34
|
+
for (const view of views)
|
|
35
|
+
this.append(this.host, view, raw, prose.components, refs);
|
|
36
|
+
});
|
|
37
|
+
onCleanup(() => {
|
|
38
|
+
for (const ref of refs)
|
|
39
|
+
ref.destroy();
|
|
40
|
+
while (this.host.firstChild)
|
|
41
|
+
this.renderer.removeChild(this.host, this.host.firstChild);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
92
44
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
for (const child of views) this.append(holder, child, raw, components, refs)
|
|
130
|
-
return Array.from(holder.childNodes)
|
|
45
|
+
append(parent, view, raw, components, refs) {
|
|
46
|
+
const renderer = this.renderer;
|
|
47
|
+
if (typeof view === "string")
|
|
48
|
+
return renderer.appendChild(parent, renderer.createText(view));
|
|
49
|
+
if ("html" in view) {
|
|
50
|
+
const element = renderer.createElement("hamur-html");
|
|
51
|
+
renderer.setStyle(element, "display", "contents");
|
|
52
|
+
renderer.setProperty(element, "innerHTML", raw(view.html));
|
|
53
|
+
return renderer.appendChild(parent, element);
|
|
54
|
+
}
|
|
55
|
+
if ("component" in view)
|
|
56
|
+
return this.appendComponent(parent, view, raw, components, refs);
|
|
57
|
+
const link = markdownLink(view);
|
|
58
|
+
const element = renderer.createElement(view.tag);
|
|
59
|
+
if (link) {
|
|
60
|
+
const defaults = linkDefaultsOf(this.app);
|
|
61
|
+
const attributes = linkAttributes({
|
|
62
|
+
href: this.resolve(link.to),
|
|
63
|
+
external: false,
|
|
64
|
+
...linkPrefetchProps({}, defaults),
|
|
65
|
+
replace: false,
|
|
66
|
+
pendingClass: defaults.pendingClass,
|
|
67
|
+
trim: false,
|
|
68
|
+
prefetchedClass: defaults.prefetchedClass,
|
|
69
|
+
prefetchDelay: defaults.prefetchDelay,
|
|
70
|
+
externalRelAttribute: defaults.externalRelAttribute,
|
|
71
|
+
});
|
|
72
|
+
for (const [name, value] of Object.entries({ ...link.attrs, ...attributes }))
|
|
73
|
+
renderer.setAttribute(element, name, value === true ? "" : value);
|
|
74
|
+
}
|
|
75
|
+
else
|
|
76
|
+
for (const [name, value] of Object.entries(view.attrs))
|
|
77
|
+
renderer.setAttribute(element, name, value === true ? "" : value);
|
|
78
|
+
for (const child of view.children)
|
|
79
|
+
this.append(element, child, raw, components, refs);
|
|
80
|
+
renderer.appendChild(parent, element);
|
|
131
81
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
82
|
+
appendComponent(parent, view, raw, components, refs) {
|
|
83
|
+
const renderer = this.renderer;
|
|
84
|
+
const component = Object.hasOwn(components, view.component) ? components[view.component] : undefined;
|
|
85
|
+
const nodes = (views) => {
|
|
86
|
+
const holder = renderer.createElement("div");
|
|
87
|
+
for (const child of views)
|
|
88
|
+
this.append(holder, child, raw, components, refs);
|
|
89
|
+
return Array.from(holder.childNodes);
|
|
90
|
+
};
|
|
91
|
+
if (!component) {
|
|
92
|
+
if (!view.attrs) {
|
|
93
|
+
for (const child of view.children)
|
|
94
|
+
this.append(parent, child, raw, components, refs);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const element = renderer.createElement(view.component);
|
|
98
|
+
for (const [name, value] of Object.entries(view.attrs))
|
|
99
|
+
renderer.setAttribute(element, name, value === true ? "" : value);
|
|
100
|
+
for (const child of view.children)
|
|
101
|
+
this.append(element, child, raw, components, refs);
|
|
102
|
+
return renderer.appendChild(parent, element);
|
|
103
|
+
}
|
|
104
|
+
const mirror = reflectComponentType(component);
|
|
105
|
+
const declared = new Set(mirror?.inputs.map((input) => input.templateName));
|
|
106
|
+
const entries = Object.entries(view.props);
|
|
107
|
+
const [element] = markdownView([
|
|
108
|
+
"span",
|
|
109
|
+
Object.fromEntries(entries.filter(([name]) => !declared.has(camelize(name)))),
|
|
110
|
+
]);
|
|
111
|
+
const slots = new Map();
|
|
112
|
+
for (const [name, views] of Object.entries(view.slots))
|
|
113
|
+
slots.set(name, nodes(views));
|
|
114
|
+
const content = nodes(view.children);
|
|
115
|
+
const ref = createComponent(component, {
|
|
116
|
+
environmentInjector: this.environment,
|
|
117
|
+
elementInjector: this.injector,
|
|
118
|
+
projectableNodes: (mirror?.ngContentSelectors ?? []).map((selector) => {
|
|
119
|
+
const named = SLOT.exec(selector);
|
|
120
|
+
const name = named?.[1] ?? named?.[2];
|
|
121
|
+
return selector === "*" ? content : name !== undefined ? (slots.get(name) ?? []) : [];
|
|
122
|
+
}),
|
|
123
|
+
});
|
|
124
|
+
for (const [name, value] of entries)
|
|
125
|
+
if (declared.has(camelize(name)))
|
|
126
|
+
ref.setInput(camelize(name), value);
|
|
127
|
+
const host = ref.location.nativeElement;
|
|
128
|
+
for (const [name, value] of Object.entries(element?.attrs ?? {}))
|
|
129
|
+
renderer.setAttribute(host, name, value === true ? "" : value);
|
|
130
|
+
this.appRef.attachView(ref.hostView);
|
|
131
|
+
refs.push(ref);
|
|
132
|
+
renderer.appendChild(parent, host);
|
|
142
133
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const entries = Object.entries(view.props as MdProps)
|
|
146
|
-
const [element] = markdownView([
|
|
147
|
-
"span",
|
|
148
|
-
Object.fromEntries(entries.filter(([name]) => !declared.has(camelize(name)))),
|
|
149
|
-
]) as Array<{ attrs: Record<string, string | true> }>
|
|
150
|
-
const slots = new Map<string, Node[]>()
|
|
151
|
-
for (const [name, views] of Object.entries(view.slots)) slots.set(name, nodes(views))
|
|
152
|
-
const content = nodes(view.children)
|
|
153
|
-
const ref = createComponent(component, {
|
|
154
|
-
environmentInjector: this.environment,
|
|
155
|
-
elementInjector: this.injector,
|
|
156
|
-
projectableNodes: (mirror?.ngContentSelectors ?? []).map((selector) => {
|
|
157
|
-
const named = SLOT.exec(selector)
|
|
158
|
-
const name = named?.[1] ?? named?.[2]
|
|
159
|
-
return selector === "*" ? content : name !== undefined ? (slots.get(name) ?? []) : []
|
|
160
|
-
}),
|
|
161
|
-
})
|
|
162
|
-
for (const [name, value] of entries) if (declared.has(camelize(name))) ref.setInput(camelize(name), value)
|
|
163
|
-
const host = ref.location.nativeElement as HTMLElement
|
|
164
|
-
for (const [name, value] of Object.entries(element?.attrs ?? {}))
|
|
165
|
-
renderer.setAttribute(host, name, value === true ? "" : value)
|
|
166
|
-
this.appRef.attachView(ref.hostView)
|
|
167
|
-
refs.push(ref)
|
|
168
|
-
renderer.appendChild(parent, host)
|
|
169
|
-
}
|
|
134
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: MarkdownRenderer, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
135
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: MarkdownRenderer, isStandalone: true, selector: "hamur-markdown", inputs: { value: { classPropertyName: "value", publicName: "value", isSignal: true, isRequired: true, transformFunction: null }, components: { classPropertyName: "components", publicName: "components", isSignal: true, isRequired: false, transformFunction: null }, raw: { classPropertyName: "raw", publicName: "raw", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "ngSkipHydration": "true" } }, ngImport: i0, template: "", isInline: true });
|
|
170
136
|
}
|
|
137
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: MarkdownRenderer, decorators: [{
|
|
138
|
+
type: Component,
|
|
139
|
+
args: [{ selector: "hamur-markdown", template: "", host: { ngSkipHydration: "true" } }]
|
|
140
|
+
}], ctorParameters: () => [], propDecorators: { value: [{ type: i0.Input, args: [{ isSignal: true, alias: "value", required: true }] }], components: [{ type: i0.Input, args: [{ isSignal: true, alias: "components", required: false }] }], raw: [{ type: i0.Input, args: [{ isSignal: true, alias: "raw", required: false }] }] } });
|
package/src/navigate.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import { Component, afterNextRender, booleanAttribute, input
|
|
2
|
-
import { navigateTo, redirect } from "hamur/app"
|
|
3
|
-
import
|
|
4
|
-
import type { CheckedTarget } from "hamur/adapter"
|
|
5
|
-
|
|
1
|
+
import { Component, afterNextRender, booleanAttribute, input } from "@angular/core";
|
|
2
|
+
import { navigateTo, redirect } from "hamur/app";
|
|
3
|
+
import * as i0 from "@angular/core";
|
|
6
4
|
/** Navigates to `to` once after mount; during SSR it throws the redirect `navigateTo` throws there. */
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
export class Navigate {
|
|
6
|
+
to = input.required(/* @ts-ignore */
|
|
7
|
+
...(ngDevMode ? [{ debugName: "to" }] : /* istanbul ignore next */ []));
|
|
8
|
+
replace = input(false, { ...(ngDevMode ? { debugName: "replace" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
9
|
+
constructor() {
|
|
10
|
+
afterNextRender(() => void navigateTo(this.to(), { replace: this.replace() }));
|
|
11
|
+
}
|
|
12
|
+
ngOnInit() {
|
|
13
|
+
if (typeof window === "undefined")
|
|
14
|
+
throw redirect(this.to());
|
|
15
|
+
}
|
|
16
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Navigate, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
17
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: Navigate, isStandalone: true, selector: "hamur-navigate", inputs: { to: { classPropertyName: "to", publicName: "to", isSignal: true, isRequired: true, transformFunction: null }, replace: { classPropertyName: "replace", publicName: "replace", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0, template: "", isInline: true });
|
|
19
18
|
}
|
|
19
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Navigate, decorators: [{
|
|
20
|
+
type: Component,
|
|
21
|
+
args: [{ selector: "hamur-navigate", template: "" }]
|
|
22
|
+
}], ctorParameters: () => [], propDecorators: { to: [{ type: i0.Input, args: [{ isSignal: true, alias: "to", required: true }] }], replace: [{ type: i0.Input, args: [{ isSignal: true, alias: "replace", required: false }] }] } });
|