@haklex/rich-renderer-image 0.0.81 → 0.0.83

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.
@@ -0,0 +1,130 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import "react-photo-view/dist/react-photo-view.css";
3
+ import { vars } from "@haklex/rich-editor";
4
+ import { decodeThumbHash } from "@haklex/rich-editor/renderers";
5
+ import { useState, useMemo } from "react";
6
+ import { PhotoProvider, PhotoView } from "react-photo-view";
7
+ var semanticClassNames = { root: "rr-image-root", frame: "rr-image-frame", frameLoading: "rr-image-loading", frameLoaded: "rr-image-loaded", frameError: "rr-image-error", image: "rr-image-img", loader: "rr-image-loader", errorBadge: "rr-image-error", caption: "rr-image-caption", editTrigger: "rr-image-edit-trigger", editPlaceholder: "rr-image-edit-placeholder", editToolbar: "rr-image-edit-toolbar", editToolbarVisible: "rr-image-edit-toolbar-visible", editToolbarButton: "rr-image-edit-toolbar-button", editToolbarButtonDanger: "rr-image-edit-toolbar-danger", editField: "rr-image-edit-field", editFieldIcon: "rr-image-edit-field-icon", editInput: "rr-image-edit-input", replaceUploadArea: "rr-image-replace-upload-area", replacePreview: "rr-image-replace-preview", panelHint: "rr-image-panel-hint" };
8
+ var root = "_1n94osf0";
9
+ var image = "_1n94osf2";
10
+ var imageState = { loading: "_1n94osf3", loaded: "_1n94osf4", error: "_1n94osf5" };
11
+ var imageVisible = "_1n94osf6";
12
+ var frame = "_1n94osf7";
13
+ var frameEditMode = "_1n94osf8";
14
+ var loader = "_1n94osfa";
15
+ var errorBadge = "_1n94osfb";
16
+ var caption = "_1n94osfc";
17
+ var editTrigger = "_1n94osfk";
18
+ var editPlaceholder = "_1n94osfl";
19
+ var editToolbar = "_1n94osfm";
20
+ var editToolbarVisible = "_1n94osfn";
21
+ var editToolbarButton = "_1n94osfo";
22
+ var editToolbarButtonDanger = "_1n94osfp";
23
+ var editPanel = "_1n94osfq";
24
+ var editField = "_1n94osfr";
25
+ var editFieldIcon = "_1n94osfs";
26
+ var editInput = "_1n94osft";
27
+ var replaceUploadArea = "_1n94osfu";
28
+ var replacePreview = "_1n94osfv";
29
+ var panelHint = "_1n94osfw";
30
+ const frameStateSemanticClass = {
31
+ loading: semanticClassNames.frameLoading,
32
+ loaded: semanticClassNames.frameLoaded,
33
+ error: semanticClassNames.frameError
34
+ };
35
+ function getCaptionText(altText, caption2) {
36
+ if (caption2) return caption2;
37
+ if (!altText) return void 0;
38
+ if (altText.startsWith("!") || altText.startsWith("¡")) {
39
+ return altText.slice(1);
40
+ }
41
+ return void 0;
42
+ }
43
+ const ImageRenderer = ({
44
+ src,
45
+ altText,
46
+ width,
47
+ height,
48
+ caption: caption$1,
49
+ thumbhash,
50
+ accent
51
+ }) => {
52
+ const [state, setState] = useState("loading");
53
+ const captionText = useMemo(() => getCaptionText(altText, caption$1), [altText, caption$1]);
54
+ const placeholderUrl = useMemo(
55
+ () => thumbhash ? decodeThumbHash(thumbhash) : void 0,
56
+ [thumbhash]
57
+ );
58
+ if (!src) return null;
59
+ const frameStyle = {
60
+ backgroundColor: state !== "loaded" && !placeholderUrl ? accent || vars.color.bgTertiary : "transparent",
61
+ backgroundImage: placeholderUrl && state !== "loaded" ? `url(${placeholderUrl})` : void 0,
62
+ backgroundSize: "cover",
63
+ width: width ? Math.min(width, 1200) : void 0,
64
+ maxWidth: "100%",
65
+ ...width && height ? { aspectRatio: `${width} / ${height}` } : {}
66
+ };
67
+ return /* @__PURE__ */ jsxs("figure", { className: `${root} ${semanticClassNames.root}`, children: [
68
+ /* @__PURE__ */ jsx(PhotoProvider, { photoClosable: true, children: /* @__PURE__ */ jsx(PhotoView, { src, children: /* @__PURE__ */ jsxs(
69
+ "div",
70
+ {
71
+ "aria-label": `Open image: ${altText || "image"}`,
72
+ className: `${frame} ${semanticClassNames.frame} ${imageState[state]} ${frameStateSemanticClass[state]}`.trim(),
73
+ role: "button",
74
+ style: frameStyle,
75
+ tabIndex: 0,
76
+ onKeyDown: (e) => {
77
+ if (e.key !== "Enter" && e.key !== " ") return;
78
+ e.preventDefault();
79
+ e.currentTarget.click();
80
+ },
81
+ children: [
82
+ /* @__PURE__ */ jsx(
83
+ "img",
84
+ {
85
+ alt: altText,
86
+ className: `${image} ${state === "loaded" ? imageVisible : ""} ${semanticClassNames.image}`,
87
+ "data-state": state,
88
+ height,
89
+ loading: "lazy",
90
+ src,
91
+ style: width && height ? { height: "100%", objectFit: "cover" } : void 0,
92
+ width,
93
+ onError: () => setState("error"),
94
+ onLoad: () => setState("loaded")
95
+ }
96
+ ),
97
+ state === "loading" && /* @__PURE__ */ jsx("span", { className: `${loader} ${semanticClassNames.loader}` }),
98
+ state === "error" && /* @__PURE__ */ jsx("span", { className: `${errorBadge} ${semanticClassNames.errorBadge}`, children: "Image failed to load" })
99
+ ]
100
+ }
101
+ ) }) }),
102
+ captionText && /* @__PURE__ */ jsx("figcaption", { className: `${caption} ${semanticClassNames.caption}`, children: captionText })
103
+ ] });
104
+ };
105
+ export {
106
+ ImageRenderer as I,
107
+ editFieldIcon as a,
108
+ editInput as b,
109
+ replacePreview as c,
110
+ editToolbar as d,
111
+ editField as e,
112
+ editToolbarVisible as f,
113
+ editToolbarButton as g,
114
+ editPanel as h,
115
+ editToolbarButtonDanger as i,
116
+ editPlaceholder as j,
117
+ editTrigger as k,
118
+ root as l,
119
+ image as m,
120
+ imageVisible as n,
121
+ loader as o,
122
+ panelHint as p,
123
+ errorBadge as q,
124
+ replaceUploadArea as r,
125
+ semanticClassNames as s,
126
+ frame as t,
127
+ frameEditMode as u,
128
+ imageState as v,
129
+ caption as w
130
+ };