@hamur/image 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 +24 -2
- package/dist/chunks/bun-index-n7xnn0a2.js +502 -0
- package/dist/src/adapters/angular.js +275 -0
- package/dist/src/adapters/html.js +70 -0
- package/dist/src/adapters/preact.js +82 -0
- package/dist/src/adapters/react.js +84 -0
- package/dist/src/adapters/solid.js +93 -0
- package/dist/src/adapters/svelte.js +15 -0
- package/dist/src/adapters/vue.js +147 -0
- package/dist/src/index.js +1442 -0
- package/dist/src/server/index.js +9 -0
- package/dist/types/src/adapters/angular-img.d.ts +40 -0
- package/dist/types/src/adapters/angular-picture.d.ts +36 -0
- package/dist/types/src/adapters/angular.d.ts +3 -0
- package/dist/types/src/adapters/html-img.d.ts +10 -0
- package/dist/types/src/adapters/html-picture.d.ts +6 -0
- package/dist/types/src/adapters/html.d.ts +6 -0
- package/dist/types/src/adapters/preact-img.d.ts +8 -0
- package/dist/types/src/adapters/preact-picture.d.ts +6 -0
- package/dist/types/src/adapters/preact.d.ts +6 -0
- package/dist/types/src/adapters/react-img.d.ts +8 -0
- package/dist/types/src/adapters/react-picture.d.ts +6 -0
- package/dist/types/src/adapters/react.d.ts +6 -0
- package/dist/types/src/adapters/solid-img.d.ts +8 -0
- package/dist/types/src/adapters/solid-picture.d.ts +6 -0
- package/dist/types/src/adapters/solid.d.ts +6 -0
- package/dist/types/src/adapters/svelte.d.ts +3 -0
- package/dist/types/src/adapters/vue-img.d.ts +119 -0
- package/dist/types/src/adapters/vue-picture.d.ts +121 -0
- package/dist/types/src/adapters/vue.d.ts +234 -0
- package/dist/types/src/build/endpoint.d.ts +12 -0
- package/dist/types/src/build/platform.d.ts +24 -0
- package/dist/types/src/build/runtime.d.ts +9 -0
- package/dist/types/src/build/types.d.ts +4 -0
- package/dist/types/src/build/variants.d.ts +8 -0
- package/dist/types/src/domains.d.ts +27 -0
- package/dist/types/src/index.d.ts +9 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/options.d.ts +15 -0
- package/dist/types/src/providers/bunny.d.ts +9 -0
- package/dist/types/src/providers/cloudflare.d.ts +9 -0
- package/dist/types/src/providers/cloudflareImages.d.ts +9 -0
- package/dist/types/src/providers/cloudinary.d.ts +9 -0
- package/dist/types/src/providers/contentful.d.ts +9 -0
- package/dist/types/src/providers/fastly.d.ts +9 -0
- package/dist/types/src/providers/hamur-url.d.ts +3 -0
- package/dist/types/src/providers/hamur.d.ts +3 -0
- package/dist/types/src/providers/imagekit.d.ts +9 -0
- package/dist/types/src/providers/imgix.d.ts +9 -0
- package/dist/types/src/providers/imgproxy.d.ts +9 -0
- package/dist/types/src/providers/ipx.d.ts +9 -0
- package/dist/types/src/providers/netlify.d.ts +9 -0
- package/dist/types/src/providers/none.d.ts +8 -0
- package/dist/types/src/providers/sanity.d.ts +9 -0
- package/dist/types/src/providers/shared.d.ts +7 -0
- package/dist/types/src/providers/storyblok.d.ts +9 -0
- package/dist/types/src/providers/strapi.d.ts +9 -0
- package/dist/types/src/providers/supabase.d.ts +9 -0
- package/dist/types/src/providers/twicpics.d.ts +9 -0
- package/dist/types/src/providers/uploadcare.d.ts +9 -0
- package/dist/types/src/providers/vercel-url.d.ts +3 -0
- package/dist/types/src/providers/vercel.d.ts +3 -0
- package/dist/types/src/providers/weserv.d.ts +9 -0
- package/dist/types/src/runtime/image.d.ts +28 -0
- package/dist/types/src/runtime/img.d.ts +71 -0
- package/dist/types/src/runtime/index.d.ts +9 -0
- package/dist/types/src/runtime/picture.d.ts +24 -0
- package/dist/types/src/runtime/sizes.d.ts +8 -0
- package/dist/types/src/runtime/source.d.ts +3 -0
- package/dist/types/src/server/door.d.ts +1 -0
- package/dist/types/src/server/endpoint.d.ts +46 -0
- package/dist/types/src/server/handler.d.ts +1 -0
- package/dist/types/src/server/index.d.ts +1 -0
- package/dist/types/src/server/miss-handler.d.ts +1 -0
- package/dist/types/src/server/miss.d.ts +1 -0
- package/dist/types/src/server/parse.d.ts +30 -0
- package/dist/types/src/server/remote.d.ts +20 -0
- package/dist/types/src/types.d.ts +178 -0
- package/package.json +114 -4
- package/src/adapters/Img.svelte +33 -0
- package/src/adapters/Picture.svelte +39 -0
- package/src/adapters/angular-img.ts +144 -0
- package/src/adapters/angular-picture.ts +146 -0
- package/src/adapters/angular.ts +3 -0
- package/src/adapters/html-img.ts +42 -0
- package/src/adapters/html-picture.ts +30 -0
- package/src/adapters/html.ts +9 -0
- package/src/adapters/preact-img.ts +51 -0
- package/src/adapters/preact-picture.ts +40 -0
- package/src/adapters/preact.ts +9 -0
- package/src/adapters/react-img.ts +54 -0
- package/src/adapters/react-picture.ts +49 -0
- package/src/adapters/react.ts +9 -0
- package/src/adapters/solid-img.ts +47 -0
- package/src/adapters/solid-picture.ts +43 -0
- package/src/adapters/solid.ts +9 -0
- package/src/adapters/svelte.ts +3 -0
- package/src/adapters/test/ImgCustomPage.svelte +14 -0
- package/src/adapters/test/ImgPage.svelte +5 -0
- package/src/adapters/test/ImgPreloadPage.svelte +6 -0
- package/src/adapters/test/PicturePage.svelte +6 -0
- package/src/adapters/test/PicturePreloadPage.svelte +5 -0
- package/src/adapters/test/support.ts +116 -0
- package/src/adapters/vue-img.ts +72 -0
- package/src/adapters/vue-picture.ts +64 -0
- package/src/adapters/vue.ts +7 -0
- package/src/build/endpoint.ts +78 -0
- package/src/build/platform.ts +99 -0
- package/src/build/runtime.ts +118 -0
- package/src/build/types.ts +23 -0
- package/src/build/variants.ts +174 -0
- package/src/domains.ts +84 -0
- package/src/env.d.ts +24 -0
- package/src/index.ts +23 -0
- package/src/module.ts +29 -0
- package/src/options.ts +329 -0
- package/src/providers/bunny.ts +21 -0
- package/src/providers/cloudflare.ts +22 -0
- package/src/providers/cloudflareImages.ts +25 -0
- package/src/providers/cloudinary.ts +31 -0
- package/src/providers/contentful.ts +26 -0
- package/src/providers/fastly.ts +29 -0
- package/src/providers/hamur-url.ts +81 -0
- package/src/providers/hamur.ts +8 -0
- package/src/providers/imagekit.ts +30 -0
- package/src/providers/imgix.ts +26 -0
- package/src/providers/imgproxy.ts +24 -0
- package/src/providers/ipx.ts +26 -0
- package/src/providers/netlify.ts +23 -0
- package/src/providers/none.ts +9 -0
- package/src/providers/sanity.ts +29 -0
- package/src/providers/shared.ts +17 -0
- package/src/providers/storyblok.ts +24 -0
- package/src/providers/strapi.ts +24 -0
- package/src/providers/supabase.ts +26 -0
- package/src/providers/twicpics.ts +26 -0
- package/src/providers/uploadcare.ts +32 -0
- package/src/providers/vercel-url.ts +18 -0
- package/src/providers/vercel.ts +8 -0
- package/src/providers/weserv.ts +25 -0
- package/src/runtime/image.ts +266 -0
- package/src/runtime/img.ts +241 -0
- package/src/runtime/index.ts +18 -0
- package/src/runtime/picture.ts +62 -0
- package/src/runtime/sizes.ts +52 -0
- package/src/runtime/source.ts +8 -0
- package/src/server/door.ts +1 -0
- package/src/server/endpoint.ts +428 -0
- package/src/server/handler.ts +33 -0
- package/src/server/index.ts +1 -0
- package/src/server/miss-handler.ts +20 -0
- package/src/server/miss.ts +1 -0
- package/src/server/parse.ts +152 -0
- package/src/server/remote.ts +166 -0
- package/src/types.ts +177 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Directive,
|
|
3
|
+
ElementRef,
|
|
4
|
+
Injector,
|
|
5
|
+
Renderer2,
|
|
6
|
+
DestroyRef,
|
|
7
|
+
afterNextRender,
|
|
8
|
+
booleanAttribute,
|
|
9
|
+
computed,
|
|
10
|
+
effect,
|
|
11
|
+
inject,
|
|
12
|
+
input,
|
|
13
|
+
numberAttribute,
|
|
14
|
+
runInInjectionContext,
|
|
15
|
+
signal,
|
|
16
|
+
type OnInit,
|
|
17
|
+
} from "@angular/core"
|
|
18
|
+
import type { ImageMeta } from "hamur"
|
|
19
|
+
import { injectHead } from "@hamur/adapter-angular"
|
|
20
|
+
import type { ImageFit, ImageFormat, ImageModifiers, HamurImagePreset, HamurImageProvider } from "../types.ts"
|
|
21
|
+
import { imgAttributes } from "../runtime/index.ts"
|
|
22
|
+
import { createImgEvents, imagePreloadLink, placeholderStyle } from "../runtime/img.ts"
|
|
23
|
+
|
|
24
|
+
const optionalNumber = (value: unknown): number | undefined =>
|
|
25
|
+
value === undefined ? undefined : numberAttribute(value)
|
|
26
|
+
|
|
27
|
+
/** An attribute binding takes a string, and `null` removes the attribute. */
|
|
28
|
+
export const strings = (attributes: Record<string, unknown>): Record<string, string | null> =>
|
|
29
|
+
Object.fromEntries(Object.entries(attributes).map(([name, value]) => [name, value == null ? null : String(value)]))
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `<img hamurImg src="/hero.jpg" width="1200" alt="">`. The host bindings write what the other adapters' `Img`
|
|
33
|
+
* writes on its `<img>`; every other attribute and listener stays the element's own. The props that are not attributes are
|
|
34
|
+
* taken off the element. `src`, `srcset` and `sizes` are written only when they differ: the browser loads the image again on
|
|
35
|
+
* every write, so a binding that rewrites the server's equal value during hydration fires `load` again.
|
|
36
|
+
*/
|
|
37
|
+
@Directive({
|
|
38
|
+
selector: "img[hamurImg]",
|
|
39
|
+
host: {
|
|
40
|
+
"[attr.hamurImg]": "null",
|
|
41
|
+
"[attr.densities]": "null",
|
|
42
|
+
"[attr.format]": "null",
|
|
43
|
+
"[attr.quality]": "null",
|
|
44
|
+
"[attr.fit]": "null",
|
|
45
|
+
"[attr.modifiers]": "null",
|
|
46
|
+
"[attr.provider]": "null",
|
|
47
|
+
"[attr.preset]": "null",
|
|
48
|
+
"[attr.preload]": "null",
|
|
49
|
+
"[attr.placeholder]": "null",
|
|
50
|
+
"[attr.placeholderClass]": "null",
|
|
51
|
+
"[attr.width]": "attrs()['width']",
|
|
52
|
+
"[attr.height]": "attrs()['height']",
|
|
53
|
+
"[attr.alt]": "attrs()['alt']",
|
|
54
|
+
"[attr.loading]": "attrs()['loading']",
|
|
55
|
+
"[attr.decoding]": "attrs()['decoding']",
|
|
56
|
+
"[attr.fetchpriority]": "attrs()['fetchpriority']",
|
|
57
|
+
"[style]": "painted()",
|
|
58
|
+
"[class]": "paintedClass()",
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
export class Img implements OnInit {
|
|
62
|
+
readonly src = input.required<string | ImageMeta>()
|
|
63
|
+
readonly alt = input<string>()
|
|
64
|
+
readonly width = input<number | string>()
|
|
65
|
+
readonly height = input<number | string>()
|
|
66
|
+
readonly sizes = input<string>()
|
|
67
|
+
readonly densities = input<number[]>()
|
|
68
|
+
readonly format = input<ImageFormat>()
|
|
69
|
+
readonly quality = input<number | undefined, unknown>(undefined, { transform: optionalNumber })
|
|
70
|
+
readonly fit = input<ImageFit>()
|
|
71
|
+
readonly modifiers = input<ImageModifiers>()
|
|
72
|
+
readonly provider = input<HamurImageProvider>()
|
|
73
|
+
readonly preset = input<HamurImagePreset>()
|
|
74
|
+
readonly loading = input<"lazy" | "eager">()
|
|
75
|
+
readonly decoding = input<string>()
|
|
76
|
+
readonly fetchpriority = input<"high" | "low" | "auto">()
|
|
77
|
+
readonly preload = input(false, { transform: booleanAttribute })
|
|
78
|
+
readonly placeholder = input<string>()
|
|
79
|
+
readonly placeholderClass = input<string>()
|
|
80
|
+
|
|
81
|
+
private readonly injector = inject(Injector)
|
|
82
|
+
private readonly loaded = signal(false)
|
|
83
|
+
private readonly events = createImgEvents(
|
|
84
|
+
() => ({}),
|
|
85
|
+
(value) => this.loaded.set(value),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
private readonly resolved = computed(() =>
|
|
89
|
+
imgAttributes({
|
|
90
|
+
src: this.src(),
|
|
91
|
+
alt: this.alt(),
|
|
92
|
+
width: this.width(),
|
|
93
|
+
height: this.height(),
|
|
94
|
+
sizes: this.sizes(),
|
|
95
|
+
densities: this.densities(),
|
|
96
|
+
format: this.format(),
|
|
97
|
+
quality: this.quality(),
|
|
98
|
+
fit: this.fit(),
|
|
99
|
+
modifiers: this.modifiers(),
|
|
100
|
+
provider: this.provider(),
|
|
101
|
+
preset: this.preset(),
|
|
102
|
+
loading: this.loading(),
|
|
103
|
+
decoding: this.decoding(),
|
|
104
|
+
fetchpriority: this.fetchpriority(),
|
|
105
|
+
preload: this.preload(),
|
|
106
|
+
}),
|
|
107
|
+
)
|
|
108
|
+
protected readonly attrs = computed(() => strings(this.resolved()))
|
|
109
|
+
protected readonly painted = computed(() => {
|
|
110
|
+
const url = this.placeholder()
|
|
111
|
+
return this.loaded() || !url ? {} : placeholderStyle(url)
|
|
112
|
+
})
|
|
113
|
+
protected readonly paintedClass = computed(() => (this.loaded() ? "" : (this.placeholderClass() ?? "")))
|
|
114
|
+
|
|
115
|
+
constructor() {
|
|
116
|
+
const element = inject<ElementRef<HTMLImageElement>>(ElementRef).nativeElement
|
|
117
|
+
const renderer = inject(Renderer2)
|
|
118
|
+
effect(() => {
|
|
119
|
+
const attrs = this.attrs()
|
|
120
|
+
for (const name of ["src", "srcset", "sizes"]) {
|
|
121
|
+
const value = attrs[name] ?? null
|
|
122
|
+
if (element.getAttribute(name) === value) continue
|
|
123
|
+
if (value === null) renderer.removeAttribute(element, name)
|
|
124
|
+
else renderer.setAttribute(element, name, value)
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
// Read at hydration: a server image the browser already finished fired its load before any listener existed.
|
|
128
|
+
const finished = element.isConnected && element.complete && element.getAttribute("src") !== null
|
|
129
|
+
const offLoad = renderer.listen(element, "load", (event: Event) => this.events.load(event))
|
|
130
|
+
const offError = renderer.listen(element, "error", (event: Event) => this.events.error(event))
|
|
131
|
+
inject(DestroyRef).onDestroy(() => {
|
|
132
|
+
offLoad()
|
|
133
|
+
offError()
|
|
134
|
+
})
|
|
135
|
+
// Angular's event replay captures a load that fired before hydration and replays it to `(load)` bindings itself;
|
|
136
|
+
// only the directive's own state is caught up here, so no second load is dispatched.
|
|
137
|
+
if (finished) afterNextRender(() => this.loaded.set(element.naturalWidth > 0))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
ngOnInit(): void {
|
|
141
|
+
if (this.preload())
|
|
142
|
+
runInInjectionContext(this.injector, () => injectHead(() => ({ link: [imagePreloadLink(this.resolved())] })))
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Component,
|
|
3
|
+
ElementRef,
|
|
4
|
+
Injector,
|
|
5
|
+
Renderer2,
|
|
6
|
+
afterRenderEffect,
|
|
7
|
+
booleanAttribute,
|
|
8
|
+
computed,
|
|
9
|
+
inject,
|
|
10
|
+
input,
|
|
11
|
+
numberAttribute,
|
|
12
|
+
runInInjectionContext,
|
|
13
|
+
signal,
|
|
14
|
+
viewChild,
|
|
15
|
+
type OnInit,
|
|
16
|
+
} from "@angular/core"
|
|
17
|
+
import type { ImageMeta } from "hamur"
|
|
18
|
+
import { injectHead } from "@hamur/adapter-angular"
|
|
19
|
+
import type { ImageFit, ImageFormat, ImageModifiers, HamurImagePreset, HamurImageProvider } from "../types.ts"
|
|
20
|
+
import { pictureAttributes } from "../runtime/index.ts"
|
|
21
|
+
import { applyPlaceholder, createImgEvents } from "../runtime/img.ts"
|
|
22
|
+
import { picturePreloadLink } from "../runtime/picture.ts"
|
|
23
|
+
import { strings } from "./angular-img.ts"
|
|
24
|
+
|
|
25
|
+
const optionalNumber = (value: unknown): number | undefined =>
|
|
26
|
+
value === undefined ? undefined : numberAttribute(value)
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* `<hamur-picture src="/hero.jpg" format="webp" width="1200" alt="" />`, one `<source>` per format and the
|
|
30
|
+
* `<img>` in the source's own format, or the `<img>` alone when the provider makes no other format.
|
|
31
|
+
*/
|
|
32
|
+
@Component({
|
|
33
|
+
selector: "hamur-picture",
|
|
34
|
+
template: `@if (parts().sources.length > 0) {
|
|
35
|
+
<picture>
|
|
36
|
+
@for (source of parts().sources; track source.type) {
|
|
37
|
+
<source [attr.type]="source.type" [attr.srcset]="source.srcset" [attr.sizes]="source.sizes" />
|
|
38
|
+
}
|
|
39
|
+
<img
|
|
40
|
+
#img
|
|
41
|
+
[attr.src]="shown()['src']"
|
|
42
|
+
[attr.srcset]="shown()['srcset']"
|
|
43
|
+
[attr.sizes]="shown()['sizes']"
|
|
44
|
+
[attr.width]="shown()['width']"
|
|
45
|
+
[attr.height]="shown()['height']"
|
|
46
|
+
[attr.alt]="shown()['alt']"
|
|
47
|
+
[attr.loading]="shown()['loading']"
|
|
48
|
+
[attr.decoding]="shown()['decoding']"
|
|
49
|
+
[attr.fetchpriority]="shown()['fetchpriority']"
|
|
50
|
+
[attr.style]="shown()['style']"
|
|
51
|
+
[attr.class]="shown()['class']"
|
|
52
|
+
/>
|
|
53
|
+
</picture>
|
|
54
|
+
} @else {
|
|
55
|
+
<img
|
|
56
|
+
#img
|
|
57
|
+
[attr.src]="shown()['src']"
|
|
58
|
+
[attr.srcset]="shown()['srcset']"
|
|
59
|
+
[attr.sizes]="shown()['sizes']"
|
|
60
|
+
[attr.width]="shown()['width']"
|
|
61
|
+
[attr.height]="shown()['height']"
|
|
62
|
+
[attr.alt]="shown()['alt']"
|
|
63
|
+
[attr.loading]="shown()['loading']"
|
|
64
|
+
[attr.decoding]="shown()['decoding']"
|
|
65
|
+
[attr.fetchpriority]="shown()['fetchpriority']"
|
|
66
|
+
[attr.style]="shown()['style']"
|
|
67
|
+
[attr.class]="shown()['class']"
|
|
68
|
+
/>
|
|
69
|
+
}`,
|
|
70
|
+
})
|
|
71
|
+
export class Picture implements OnInit {
|
|
72
|
+
readonly src = input.required<string | ImageMeta>()
|
|
73
|
+
readonly alt = input<string>()
|
|
74
|
+
readonly width = input<number | string>()
|
|
75
|
+
readonly height = input<number | string>()
|
|
76
|
+
readonly sizes = input<string>()
|
|
77
|
+
readonly densities = input<number[]>()
|
|
78
|
+
readonly format = input<ImageFormat | ImageFormat[]>()
|
|
79
|
+
readonly legacyFormat = input<ImageFormat>()
|
|
80
|
+
readonly quality = input<number | undefined, unknown>(undefined, { transform: optionalNumber })
|
|
81
|
+
readonly fit = input<ImageFit>()
|
|
82
|
+
readonly modifiers = input<ImageModifiers>()
|
|
83
|
+
readonly provider = input<HamurImageProvider>()
|
|
84
|
+
readonly preset = input<HamurImagePreset>()
|
|
85
|
+
readonly loading = input<"lazy" | "eager">()
|
|
86
|
+
readonly decoding = input<string>()
|
|
87
|
+
readonly fetchpriority = input<"high" | "low" | "auto">()
|
|
88
|
+
readonly preload = input(false, { transform: booleanAttribute })
|
|
89
|
+
readonly placeholder = input<string>()
|
|
90
|
+
readonly placeholderClass = input<string>()
|
|
91
|
+
|
|
92
|
+
private readonly injector = inject(Injector)
|
|
93
|
+
private readonly loaded = signal(false)
|
|
94
|
+
private readonly element = viewChild<ElementRef<HTMLImageElement>>("img")
|
|
95
|
+
private readonly events = createImgEvents(
|
|
96
|
+
() => ({}),
|
|
97
|
+
(value) => this.loaded.set(value),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
protected readonly parts = computed(() =>
|
|
101
|
+
pictureAttributes({
|
|
102
|
+
src: this.src(),
|
|
103
|
+
alt: this.alt(),
|
|
104
|
+
width: this.width(),
|
|
105
|
+
height: this.height(),
|
|
106
|
+
sizes: this.sizes(),
|
|
107
|
+
densities: this.densities(),
|
|
108
|
+
format: this.format(),
|
|
109
|
+
legacyFormat: this.legacyFormat(),
|
|
110
|
+
quality: this.quality(),
|
|
111
|
+
fit: this.fit(),
|
|
112
|
+
modifiers: this.modifiers(),
|
|
113
|
+
provider: this.provider(),
|
|
114
|
+
preset: this.preset(),
|
|
115
|
+
loading: this.loading(),
|
|
116
|
+
decoding: this.decoding(),
|
|
117
|
+
fetchpriority: this.fetchpriority(),
|
|
118
|
+
preload: this.preload(),
|
|
119
|
+
}),
|
|
120
|
+
)
|
|
121
|
+
protected readonly shown = computed(() =>
|
|
122
|
+
strings(
|
|
123
|
+
applyPlaceholder(
|
|
124
|
+
this.parts().img,
|
|
125
|
+
{ placeholder: this.placeholder(), placeholderClass: this.placeholderClass() },
|
|
126
|
+
this.loaded(),
|
|
127
|
+
),
|
|
128
|
+
),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
constructor() {
|
|
132
|
+
const renderer = inject(Renderer2)
|
|
133
|
+
afterRenderEffect((onCleanup) => {
|
|
134
|
+
const image = this.element()?.nativeElement
|
|
135
|
+
if (!image) return
|
|
136
|
+
onCleanup(renderer.listen(image, "load", (event: Event) => this.events.load(event)))
|
|
137
|
+
onCleanup(renderer.listen(image, "error", (event: Event) => this.events.error(event)))
|
|
138
|
+
this.events.replay(image)
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
ngOnInit(): void {
|
|
143
|
+
if (this.preload())
|
|
144
|
+
runInInjectionContext(this.injector, () => injectHead(() => ({ link: [picturePreloadLink(this.parts())] })))
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useHead } from "@hamur/adapter-html"
|
|
2
|
+
import {
|
|
3
|
+
applyPlaceholder,
|
|
4
|
+
imagePreloadLink,
|
|
5
|
+
type ImgAttributes,
|
|
6
|
+
type ImgOptions,
|
|
7
|
+
type ImgState as State,
|
|
8
|
+
} from "../runtime/img.ts"
|
|
9
|
+
|
|
10
|
+
/** html has no component events: `isLoaded` is `false` and `imgAttrs` holds strings. */
|
|
11
|
+
export type ImgState = State<Record<string, string>>
|
|
12
|
+
|
|
13
|
+
export type ImgProps = ImgOptions & {
|
|
14
|
+
fetchpriority?: "high" | "low" | "auto"
|
|
15
|
+
custom?: (state: ImgState) => string
|
|
16
|
+
[attribute: string]: unknown
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const ESCAPES: Record<string, string> = { "&": "&", "<": "<", ">": ">", '"': """ }
|
|
20
|
+
|
|
21
|
+
export const escape = (value: unknown): string => String(value).replace(/[&<>"]/g, (char) => ESCAPES[char]!)
|
|
22
|
+
|
|
23
|
+
export function createImg(attributes: ImgAttributes): (props: ImgProps) => string {
|
|
24
|
+
return function Img({ custom, ...props }) {
|
|
25
|
+
const attrs = applyPlaceholder(attributes(props), props, false)
|
|
26
|
+
if (props.preload) useHead({ link: [imagePreloadLink(attrs)] })
|
|
27
|
+
if (custom) {
|
|
28
|
+
const imgAttrs: Record<string, string> = {}
|
|
29
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
30
|
+
if (value === undefined || value === null || value === false) continue
|
|
31
|
+
imgAttrs[name] = value === true ? "" : String(value)
|
|
32
|
+
}
|
|
33
|
+
return custom({ imgAttrs, src: imgAttrs.src ?? "", isLoaded: false })
|
|
34
|
+
}
|
|
35
|
+
let out = "<img"
|
|
36
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
37
|
+
if (value === undefined || value === null || value === false) continue
|
|
38
|
+
out += value === true ? ` ${name}` : ` ${name}="${escape(value)}"`
|
|
39
|
+
}
|
|
40
|
+
return `${out}>`
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useHead } from "@hamur/adapter-html"
|
|
2
|
+
import { applyPlaceholder } from "../runtime/img.ts"
|
|
3
|
+
import { picturePreloadLink, type PictureAttributes, type PictureOptions } from "../runtime/picture.ts"
|
|
4
|
+
import { escape } from "./html-img.ts"
|
|
5
|
+
|
|
6
|
+
export type PictureProps = PictureOptions & {
|
|
7
|
+
fetchpriority?: "high" | "low" | "auto"
|
|
8
|
+
[attribute: string]: unknown
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const tag = (name: string, attributes: object): string => {
|
|
12
|
+
let out = `<${name}`
|
|
13
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
14
|
+
if (value === undefined || value === null || value === false) continue
|
|
15
|
+
out += value === true ? ` ${key}` : ` ${key}="${escape(value)}"`
|
|
16
|
+
}
|
|
17
|
+
return `${out}>`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createPicture(attributes: PictureAttributes): (props: PictureProps) => string {
|
|
21
|
+
return function Picture(props) {
|
|
22
|
+
const parts = attributes(props)
|
|
23
|
+
const { sources, img } = parts
|
|
24
|
+
if (props.preload) useHead({ link: [picturePreloadLink(parts)] })
|
|
25
|
+
const image = tag("img", applyPlaceholder(img, props, false))
|
|
26
|
+
return sources.length === 0
|
|
27
|
+
? image
|
|
28
|
+
: `<picture>${sources.map((source) => tag("source", source)).join("")}${image}</picture>`
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getImage, getSizes, imgAttributes, pictureAttributes } from "../runtime/index.ts"
|
|
2
|
+
import { createImg } from "./html-img.ts"
|
|
3
|
+
import { createPicture } from "./html-picture.ts"
|
|
4
|
+
|
|
5
|
+
export type { ImgProps, ImgState } from "./html-img.ts"
|
|
6
|
+
export type { PictureProps } from "./html-picture.ts"
|
|
7
|
+
export { getImage, getSizes }
|
|
8
|
+
export const Img = createImg(imgAttributes)
|
|
9
|
+
export const Picture = createPicture(pictureAttributes)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { h, type ComponentChildren, type JSX } from "preact"
|
|
2
|
+
import { useHead } from "@hamur/adapter-preact"
|
|
3
|
+
import { useEffect, useMemo, useRef, useState } from "preact/hooks"
|
|
4
|
+
import {
|
|
5
|
+
applyPlaceholder,
|
|
6
|
+
createImgEvents,
|
|
7
|
+
imagePreloadLink,
|
|
8
|
+
type ImgAttributes,
|
|
9
|
+
type ImgOptions,
|
|
10
|
+
type ImgState as State,
|
|
11
|
+
} from "../runtime/img.ts"
|
|
12
|
+
|
|
13
|
+
export type ImgState = State
|
|
14
|
+
|
|
15
|
+
export type ImgProps = ImgOptions & {
|
|
16
|
+
fetchpriority?: "high" | "low" | "auto"
|
|
17
|
+
children?: ComponentChildren | ((state: ImgState) => ComponentChildren)
|
|
18
|
+
} & Omit<
|
|
19
|
+
JSX.IntrinsicElements["img"],
|
|
20
|
+
keyof ImgOptions | "srcset" | "srcSet" | "fetchpriority" | "fetchPriority" | "children"
|
|
21
|
+
>
|
|
22
|
+
|
|
23
|
+
export function createImg(attributes: ImgAttributes): (props: ImgProps) => ComponentChildren {
|
|
24
|
+
return function Img({ children, onLoad, onError, ...props }) {
|
|
25
|
+
const [isLoaded, setLoaded] = useState(false)
|
|
26
|
+
const handlers = useRef({ onLoad, onError })
|
|
27
|
+
handlers.current = { onLoad, onError }
|
|
28
|
+
const events = useMemo(
|
|
29
|
+
() =>
|
|
30
|
+
createImgEvents(
|
|
31
|
+
() => ({ load: handlers.current.onLoad as never, error: handlers.current.onError as never }),
|
|
32
|
+
setLoaded,
|
|
33
|
+
),
|
|
34
|
+
[],
|
|
35
|
+
)
|
|
36
|
+
const image = useRef<HTMLImageElement | null>(null)
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (image.current) events.replay(image.current)
|
|
39
|
+
}, [events])
|
|
40
|
+
const attrs = attributes(props)
|
|
41
|
+
if (props.preload) useHead({ link: [imagePreloadLink(attrs)] })
|
|
42
|
+
const imgAttrs = {
|
|
43
|
+
...applyPlaceholder(attrs, props, isLoaded),
|
|
44
|
+
onLoad: events.load,
|
|
45
|
+
onError: events.error,
|
|
46
|
+
ref: image,
|
|
47
|
+
}
|
|
48
|
+
if (typeof children === "function") return children({ imgAttrs, src: String(attrs.src), isLoaded })
|
|
49
|
+
return h("img", imgAttrs)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { h, type JSX, type VNode } from "preact"
|
|
2
|
+
import { useHead } from "@hamur/adapter-preact"
|
|
3
|
+
import { useEffect, useMemo, useRef, useState } from "preact/hooks"
|
|
4
|
+
import { applyPlaceholder, createImgEvents } from "../runtime/img.ts"
|
|
5
|
+
import { picturePreloadLink, type PictureAttributes, type PictureOptions } from "../runtime/picture.ts"
|
|
6
|
+
|
|
7
|
+
export type PictureProps = PictureOptions & {
|
|
8
|
+
fetchpriority?: "high" | "low" | "auto"
|
|
9
|
+
} & Omit<JSX.IntrinsicElements["img"], keyof PictureOptions | "srcset" | "srcSet" | "fetchpriority" | "fetchPriority">
|
|
10
|
+
|
|
11
|
+
export function createPicture(attributes: PictureAttributes): (props: PictureProps) => VNode {
|
|
12
|
+
return function Picture({ onLoad, onError, ...props }) {
|
|
13
|
+
const [isLoaded, setLoaded] = useState(false)
|
|
14
|
+
const handlers = useRef({ onLoad, onError })
|
|
15
|
+
handlers.current = { onLoad, onError }
|
|
16
|
+
const events = useMemo(
|
|
17
|
+
() =>
|
|
18
|
+
createImgEvents(
|
|
19
|
+
() => ({ load: handlers.current.onLoad as never, error: handlers.current.onError as never }),
|
|
20
|
+
setLoaded,
|
|
21
|
+
),
|
|
22
|
+
[],
|
|
23
|
+
)
|
|
24
|
+
const node = useRef<HTMLImageElement | null>(null)
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (node.current) events.replay(node.current)
|
|
27
|
+
}, [events])
|
|
28
|
+
const parts = attributes(props)
|
|
29
|
+
const { sources, img } = parts
|
|
30
|
+
if (props.preload) useHead({ link: [picturePreloadLink(parts)] })
|
|
31
|
+
const image = h("img", {
|
|
32
|
+
...applyPlaceholder(img, props, isLoaded),
|
|
33
|
+
onLoad: events.load,
|
|
34
|
+
onError: events.error,
|
|
35
|
+
ref: node,
|
|
36
|
+
}) as VNode
|
|
37
|
+
if (sources.length === 0) return image
|
|
38
|
+
return h("picture", null, ...sources.map((source) => h("source", { key: source.type, ...source })), image)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getImage, getSizes, imgAttributes, pictureAttributes } from "../runtime/index.ts"
|
|
2
|
+
import { createImg } from "./preact-img.ts"
|
|
3
|
+
import { createPicture } from "./preact-picture.ts"
|
|
4
|
+
|
|
5
|
+
export type { ImgProps, ImgState } from "./preact-img.ts"
|
|
6
|
+
export type { PictureProps } from "./preact-picture.ts"
|
|
7
|
+
export { getImage, getSizes }
|
|
8
|
+
export const Img = createImg(imgAttributes)
|
|
9
|
+
export const Picture = createPicture(pictureAttributes)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createElement, useEffect, useMemo, useRef, useState, type ImgHTMLAttributes, type ReactNode } from "react"
|
|
2
|
+
import { useHead } from "@hamur/adapter-react"
|
|
3
|
+
import {
|
|
4
|
+
applyPlaceholder,
|
|
5
|
+
createImgEvents,
|
|
6
|
+
imagePreloadLink,
|
|
7
|
+
type ImgAttributes,
|
|
8
|
+
type ImgOptions,
|
|
9
|
+
type ImgState as State,
|
|
10
|
+
} from "../runtime/img.ts"
|
|
11
|
+
|
|
12
|
+
export type ImgState = State
|
|
13
|
+
|
|
14
|
+
export type ImgProps = ImgOptions & {
|
|
15
|
+
fetchPriority?: "high" | "low" | "auto"
|
|
16
|
+
children?: (state: ImgState) => ReactNode
|
|
17
|
+
} & Omit<ImgHTMLAttributes<HTMLImageElement>, keyof ImgOptions | "srcSet" | "fetchPriority" | "children">
|
|
18
|
+
|
|
19
|
+
const NAMES: Record<string, string> = { srcset: "srcSet", fetchpriority: "fetchPriority" }
|
|
20
|
+
|
|
21
|
+
export function createImg(attributes: ImgAttributes): (props: ImgProps) => ReactNode {
|
|
22
|
+
return function Img({ fetchPriority, children, onLoad, onError, ...props }) {
|
|
23
|
+
const [isLoaded, setLoaded] = useState(false)
|
|
24
|
+
const handlers = useRef({ onLoad, onError })
|
|
25
|
+
handlers.current = { onLoad, onError }
|
|
26
|
+
const events = useMemo(
|
|
27
|
+
() =>
|
|
28
|
+
createImgEvents(
|
|
29
|
+
() => ({ load: handlers.current.onLoad as never, error: handlers.current.onError as never }),
|
|
30
|
+
setLoaded,
|
|
31
|
+
),
|
|
32
|
+
[],
|
|
33
|
+
)
|
|
34
|
+
const image = useRef<HTMLImageElement | null>(null)
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (image.current) events.replay(image.current)
|
|
37
|
+
}, [events])
|
|
38
|
+
const attrs = attributes({ ...props, fetchpriority: fetchPriority })
|
|
39
|
+
if (props.preload) useHead({ link: [imagePreloadLink(attrs)] })
|
|
40
|
+
const imgAttrs = applyPlaceholder(
|
|
41
|
+
Object.fromEntries(Object.entries(attrs).map(([name, value]) => [NAMES[name] ?? name, value])),
|
|
42
|
+
props,
|
|
43
|
+
isLoaded,
|
|
44
|
+
{ react: true },
|
|
45
|
+
)
|
|
46
|
+
imgAttrs.onLoad = events.load
|
|
47
|
+
imgAttrs.onError = events.error
|
|
48
|
+
imgAttrs.ref = image
|
|
49
|
+
// react-dom writes its own preload link for an eager <img> with a string src; a String object makes it skip, so the head manager holds the only one
|
|
50
|
+
if (props.preload) imgAttrs.src = new String(imgAttrs.src)
|
|
51
|
+
if (children) return children({ imgAttrs, src: String(attrs.src), isLoaded })
|
|
52
|
+
return createElement("img", imgAttrs as ImgHTMLAttributes<HTMLImageElement>)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createElement, useEffect, useMemo, useRef, useState, type ImgHTMLAttributes, type ReactElement } from "react"
|
|
2
|
+
import { useHead } from "@hamur/adapter-react"
|
|
3
|
+
import { applyPlaceholder, createImgEvents } from "../runtime/img.ts"
|
|
4
|
+
import { picturePreloadLink, type PictureAttributes, type PictureOptions } from "../runtime/picture.ts"
|
|
5
|
+
|
|
6
|
+
export type PictureProps = PictureOptions & {
|
|
7
|
+
fetchPriority?: "high" | "low" | "auto"
|
|
8
|
+
} & Omit<ImgHTMLAttributes<HTMLImageElement>, keyof PictureOptions | "srcSet" | "fetchPriority">
|
|
9
|
+
|
|
10
|
+
const NAMES: Record<string, string> = { srcset: "srcSet", fetchpriority: "fetchPriority" }
|
|
11
|
+
|
|
12
|
+
const named = (attributes: object): Record<string, unknown> =>
|
|
13
|
+
Object.fromEntries(Object.entries(attributes).map(([name, value]) => [NAMES[name] ?? name, value]))
|
|
14
|
+
|
|
15
|
+
export function createPicture(attributes: PictureAttributes): (props: PictureProps) => ReactElement {
|
|
16
|
+
return function Picture({ fetchPriority, onLoad, onError, ...props }) {
|
|
17
|
+
const [isLoaded, setLoaded] = useState(false)
|
|
18
|
+
const handlers = useRef({ onLoad, onError })
|
|
19
|
+
handlers.current = { onLoad, onError }
|
|
20
|
+
const events = useMemo(
|
|
21
|
+
() =>
|
|
22
|
+
createImgEvents(
|
|
23
|
+
() => ({ load: handlers.current.onLoad as never, error: handlers.current.onError as never }),
|
|
24
|
+
setLoaded,
|
|
25
|
+
),
|
|
26
|
+
[],
|
|
27
|
+
)
|
|
28
|
+
const node = useRef<HTMLImageElement | null>(null)
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (node.current) events.replay(node.current)
|
|
31
|
+
}, [events])
|
|
32
|
+
const parts = attributes({ ...props, fetchpriority: fetchPriority })
|
|
33
|
+
const { sources, img } = parts
|
|
34
|
+
if (props.preload) useHead({ link: [picturePreloadLink(parts)] })
|
|
35
|
+
const imgAttrs = applyPlaceholder(named(img), props, isLoaded, { react: true })
|
|
36
|
+
imgAttrs.onLoad = events.load
|
|
37
|
+
imgAttrs.onError = events.error
|
|
38
|
+
imgAttrs.ref = node
|
|
39
|
+
if (props.preload) imgAttrs.src = new String(img.src)
|
|
40
|
+
const image = createElement("img", imgAttrs as ImgHTMLAttributes<HTMLImageElement>)
|
|
41
|
+
if (sources.length === 0) return image
|
|
42
|
+
return createElement(
|
|
43
|
+
"picture",
|
|
44
|
+
null,
|
|
45
|
+
...sources.map((source) => createElement("source", { key: source.type, ...named(source) })),
|
|
46
|
+
image,
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getImage, getSizes, imgAttributes, pictureAttributes } from "../runtime/index.ts"
|
|
2
|
+
import { createImg } from "./react-img.ts"
|
|
3
|
+
import { createPicture } from "./react-picture.ts"
|
|
4
|
+
|
|
5
|
+
export type { ImgProps, ImgState } from "./react-img.ts"
|
|
6
|
+
export type { PictureProps } from "./react-picture.ts"
|
|
7
|
+
export { getImage, getSizes }
|
|
8
|
+
export const Img = createImg(imgAttributes)
|
|
9
|
+
export const Picture = createPicture(pictureAttributes)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createComponent, createSignal, omit, onSettled, type Component } from "solid-js"
|
|
2
|
+
import { dynamic, type JSX } from "@solidjs/web"
|
|
3
|
+
import { useHead } from "@hamur/adapter-solid"
|
|
4
|
+
import {
|
|
5
|
+
applyPlaceholder,
|
|
6
|
+
createImgEvents,
|
|
7
|
+
imagePreloadLink,
|
|
8
|
+
type ImgAttributes,
|
|
9
|
+
type ImgOptions,
|
|
10
|
+
type ImgState as State,
|
|
11
|
+
} from "../runtime/img.ts"
|
|
12
|
+
|
|
13
|
+
export type ImgState = State
|
|
14
|
+
|
|
15
|
+
export type ImgProps = ImgOptions & {
|
|
16
|
+
fetchpriority?: "high" | "low" | "auto"
|
|
17
|
+
children?: ((state: ImgState) => JSX.Element) | JSX.Element
|
|
18
|
+
} & Omit<JSX.ImgHTMLAttributes<HTMLImageElement>, keyof ImgOptions | "srcset" | "fetchpriority" | "children">
|
|
19
|
+
|
|
20
|
+
const Image = dynamic(() => "img", { static: true }) as Component<Record<string, unknown>>
|
|
21
|
+
|
|
22
|
+
export function createImg(attributes: ImgAttributes): (props: ImgProps) => JSX.Element {
|
|
23
|
+
return function Img(props) {
|
|
24
|
+
const own = props
|
|
25
|
+
const rest = omit(props, "children", "onLoad", "onError")
|
|
26
|
+
const [isLoaded, setLoaded] = createSignal(false)
|
|
27
|
+
const events = createImgEvents(() => ({ load: own.onLoad as never, error: own.onError as never }), setLoaded)
|
|
28
|
+
let image: HTMLImageElement | undefined
|
|
29
|
+
onSettled(() => {
|
|
30
|
+
if (image) events.replay(image)
|
|
31
|
+
})
|
|
32
|
+
if (props.preload) useHead(() => ({ link: [imagePreloadLink(attributes(rest))] }))
|
|
33
|
+
return (() => {
|
|
34
|
+
const attrs = attributes(rest)
|
|
35
|
+
const imgAttrs = {
|
|
36
|
+
onLoad: events.load,
|
|
37
|
+
onError: events.error,
|
|
38
|
+
ref: (node: HTMLImageElement) => (image = node),
|
|
39
|
+
...applyPlaceholder(attrs, rest, isLoaded()),
|
|
40
|
+
}
|
|
41
|
+
const child = own.children
|
|
42
|
+
if (typeof child === "function")
|
|
43
|
+
return (child as (state: ImgState) => JSX.Element)({ imgAttrs, src: String(attrs.src), isLoaded: isLoaded() })
|
|
44
|
+
return createComponent(Image, imgAttrs)
|
|
45
|
+
}) as unknown as JSX.Element
|
|
46
|
+
}
|
|
47
|
+
}
|