@floegence/floe-webapp-core 0.101.19 → 0.101.20

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,15 @@
1
+ import { type ImageAnnotation, type ImageDimensions } from './imageAnnotation';
2
+ export type ImageAnnotationSurfaceProps = ImageDimensions & {
3
+ src: string;
4
+ label: string;
5
+ annotation?: ImageAnnotation;
6
+ onChange?: (annotation: ImageAnnotation) => void;
7
+ disabled?: boolean;
8
+ zoom?: number;
9
+ showAnnotation?: boolean;
10
+ class?: string;
11
+ };
12
+ /** A contained immutable image. Click selects a point; drag selects a rectangle.
13
+ * Scroll pans at host-controlled zoom. No iframe access or capture is performed.
14
+ */
15
+ export declare function ImageAnnotationSurface(props: ImageAnnotationSurfaceProps): import("solid-js").JSX.Element;
@@ -0,0 +1,177 @@
1
+ import { use as H, insert as q, createComponent as N, effect as K, setAttribute as c, setStyleProperty as S, memo as G, className as J, template as U, delegateEvents as Q } from "solid-js/web";
2
+ import { createSignal as O, createMemo as V, onMount as Z, onCleanup as _, Show as p, For as tt } from "solid-js";
3
+ import { validateImageAnnotation as T, imagePointFromClient as et, imageAnnotationMarkup as it } from "./imageAnnotation.js";
4
+ var nt = /* @__PURE__ */ U("<div data-floe-image-annotation style=width:100%;height:100%;overflow:auto;overscroll-behavior:contain><div style=display:grid;place-items:center><svg xmlns=http://www.w3.org/2000/svg role=img style=display:block;user-select:none;flex:none><image></image><g data-annotation-target>"), ot = /* @__PURE__ */ U("<svg><rect data-annotation-target fill=white stroke=#111827></svg>", !1, !0, !1);
5
+ function dt(e) {
6
+ let m, v;
7
+ const [z, W] = O({
8
+ width: 1,
9
+ height: 1
10
+ }), [D, f] = O();
11
+ let u;
12
+ const E = () => D() ?? e.annotation, l = V(() => Math.min(z().width / e.width, z().height / e.height) * Math.max(1, e.zoom ?? 1)), $ = () => e.showAnnotation !== !1 && E() && T(e, E()) ? it(e, E()) : "";
13
+ Z(() => {
14
+ const i = new ResizeObserver(() => W({
15
+ width: m.clientWidth,
16
+ height: m.clientHeight
17
+ }));
18
+ i.observe(m), _(() => i.disconnect());
19
+ });
20
+ const L = (i, r = !1) => et(e, v.getBoundingClientRect(), {
21
+ x: i.clientX,
22
+ y: i.clientY
23
+ }, r), g = () => !!e.onChange && !e.disabled, B = (i) => {
24
+ const r = u;
25
+ if (!r || r.id !== i.pointerId || r.src !== e.src || !g()) return;
26
+ const n = L(i, !0);
27
+ if (!n) return;
28
+ const a = r.original;
29
+ if (a && r.corner === void 0) {
30
+ const s = a.kind === "rectangle" ? a.width : 0, w = a.kind === "rectangle" ? a.height : 0;
31
+ f({
32
+ ...a,
33
+ x: Math.max(0, Math.min(e.width - s, a.x + n.x - r.start.x)),
34
+ y: Math.max(0, Math.min(e.height - w, a.y + n.y - r.start.y))
35
+ });
36
+ } else {
37
+ const s = a?.kind === "rectangle" && r.corner !== void 0 ? {
38
+ x: a.x + (r.corner % 2 ? 0 : a.width),
39
+ y: a.y + (r.corner < 2 ? a.height : 0)
40
+ } : r.start, w = Math.abs(n.x - s.x), y = Math.abs(n.y - s.y);
41
+ Math.hypot(i.clientX - r.client.x, i.clientY - r.client.y) < 5 && !a ? f({
42
+ kind: "point",
43
+ ...s
44
+ }) : w > 0 && y > 0 && f({
45
+ kind: "rectangle",
46
+ x: Math.min(s.x, n.x),
47
+ y: Math.min(s.y, n.y),
48
+ width: w,
49
+ height: y
50
+ });
51
+ }
52
+ }, P = (i, r = !1) => {
53
+ const n = u;
54
+ if (!n || n.id !== i.pointerId) return;
55
+ r || B(i);
56
+ const a = D();
57
+ u = void 0, f(void 0), v.hasPointerCapture(i.pointerId) && v.releasePointerCapture(i.pointerId), !r && n.src === e.src && g() && a && T(e, a) && e.onChange?.(a);
58
+ }, j = () => {
59
+ const i = E();
60
+ return i?.kind === "rectangle" ? [{
61
+ x: i.x,
62
+ y: i.y
63
+ }, {
64
+ x: i.x + i.width,
65
+ y: i.y
66
+ }, {
67
+ x: i.x,
68
+ y: i.y + i.height
69
+ }, {
70
+ x: i.x + i.width,
71
+ y: i.y + i.height
72
+ }] : [];
73
+ };
74
+ return (() => {
75
+ var i = nt(), r = i.firstChild, n = r.firstChild, a = n.firstChild, s = a.nextSibling, w = m;
76
+ typeof w == "function" ? H(w, i) : m = i, n.$$keydown = (t) => {
77
+ if (!g()) return;
78
+ if (t.key === "Escape") {
79
+ u = void 0, f(void 0);
80
+ return;
81
+ }
82
+ const d = {
83
+ ArrowLeft: [-1, 0],
84
+ ArrowRight: [1, 0],
85
+ ArrowUp: [0, -1],
86
+ ArrowDown: [0, 1]
87
+ }[t.key];
88
+ if (!d && t.key !== "Enter" && t.key !== " ") return;
89
+ t.preventDefault();
90
+ const h = e.annotation ?? {
91
+ kind: "point",
92
+ x: e.width / 2,
93
+ y: e.height / 2
94
+ }, o = t.shiftKey ? 10 : 1;
95
+ e.onChange?.({
96
+ ...h,
97
+ x: Math.max(0, Math.min(e.width - (h.kind === "rectangle" ? h.width : 0), h.x + (d?.[0] ?? 0) * o)),
98
+ y: Math.max(0, Math.min(e.height - (h.kind === "rectangle" ? h.height : 0), h.y + (d?.[1] ?? 0) * o))
99
+ });
100
+ }, n.addEventListener("lostpointercapture", (t) => P(t, !0)), n.addEventListener("pointercancel", (t) => P(t, !0)), n.$$pointerup = (t) => P(t), n.$$pointermove = B, n.$$pointerdown = (t) => {
101
+ if (!g() || t.button !== 0 || u) return;
102
+ const d = L(t);
103
+ if (!d) return;
104
+ const h = t.target, o = h.getAttribute("data-corner");
105
+ u = {
106
+ id: t.pointerId,
107
+ start: d,
108
+ client: {
109
+ x: t.clientX,
110
+ y: t.clientY
111
+ },
112
+ src: e.src,
113
+ ...h.closest("[data-annotation-target]") ? {
114
+ original: e.annotation
115
+ } : {},
116
+ ...o !== null ? {
117
+ corner: Number(o)
118
+ } : {}
119
+ }, f(u.original ?? {
120
+ kind: "point",
121
+ ...d
122
+ }), v.setPointerCapture(t.pointerId), v.focus({
123
+ preventScroll: !0
124
+ }), t.preventDefault();
125
+ };
126
+ var y = v;
127
+ return typeof y == "function" ? H(y, n) : v = n, q(n, N(p, {
128
+ get when() {
129
+ return G(() => !!g())() && e.showAnnotation !== !1;
130
+ },
131
+ get children() {
132
+ return N(tt, {
133
+ get each() {
134
+ return j();
135
+ },
136
+ children: (t, d) => (() => {
137
+ var h = ot();
138
+ return K((o) => {
139
+ var x = d(), k = t.x - 5 / l(), M = t.y - 5 / l(), b = 10 / l(), C = 10 / l(), A = 1 / l(), I = d() === 0 || d() === 3 ? "nwse-resize" : "nesw-resize";
140
+ return x !== o.e && c(h, "data-corner", o.e = x), k !== o.t && c(h, "x", o.t = k), M !== o.a && c(h, "y", o.a = M), b !== o.o && c(h, "width", o.o = b), C !== o.i && c(h, "height", o.i = C), A !== o.n && c(h, "stroke-width", o.n = A), I !== o.s && S(h, "cursor", o.s = I), o;
141
+ }, {
142
+ e: void 0,
143
+ t: void 0,
144
+ a: void 0,
145
+ o: void 0,
146
+ i: void 0,
147
+ n: void 0,
148
+ s: void 0
149
+ }), h;
150
+ })()
151
+ });
152
+ }
153
+ }), null), K((t) => {
154
+ var d = e.class, h = `${Math.max(z().width, e.width * l())}px`, o = `${Math.max(z().height, e.height * l())}px`, x = e.width * l(), k = e.height * l(), M = `0 0 ${e.width} ${e.height}`, b = e.label, C = g() ? 0 : void 0, A = g() ? "none" : "auto", I = g() ? "crosshair" : "default", R = e.src, X = e.width, Y = e.height, F = $();
155
+ return d !== t.e && J(i, t.e = d), h !== t.t && S(r, "width", t.t = h), o !== t.a && S(r, "height", t.a = o), x !== t.o && c(n, "width", t.o = x), k !== t.i && c(n, "height", t.i = k), M !== t.n && c(n, "viewBox", t.n = M), b !== t.s && c(n, "aria-label", t.s = b), C !== t.h && c(n, "tabindex", t.h = C), A !== t.r && S(n, "touch-action", t.r = A), I !== t.d && S(n, "cursor", t.d = I), R !== t.l && c(a, "href", t.l = R), X !== t.u && c(a, "width", t.u = X), Y !== t.c && c(a, "height", t.c = Y), F !== t.w && (s.innerHTML = t.w = F), t;
156
+ }, {
157
+ e: void 0,
158
+ t: void 0,
159
+ a: void 0,
160
+ o: void 0,
161
+ i: void 0,
162
+ n: void 0,
163
+ s: void 0,
164
+ h: void 0,
165
+ r: void 0,
166
+ d: void 0,
167
+ l: void 0,
168
+ u: void 0,
169
+ c: void 0,
170
+ w: void 0
171
+ }), i;
172
+ })();
173
+ }
174
+ Q(["pointerdown", "pointermove", "pointerup", "keydown"]);
175
+ export {
176
+ dt as ImageAnnotationSurface
177
+ };
@@ -0,0 +1,31 @@
1
+ export type ImageDimensions = Readonly<{
2
+ width: number;
3
+ height: number;
4
+ }>;
5
+ export type ImagePoint = Readonly<{
6
+ x: number;
7
+ y: number;
8
+ }>;
9
+ /** Continuous original-image pixels, independent of CSS pixels and device scale. */
10
+ export type ImageAnnotation = Readonly<{
11
+ kind: 'point';
12
+ x: number;
13
+ y: number;
14
+ } | {
15
+ kind: 'rectangle';
16
+ x: number;
17
+ y: number;
18
+ width: number;
19
+ height: number;
20
+ }>;
21
+ export declare function validateImageAnnotation(image: ImageDimensions, annotation: ImageAnnotation): boolean;
22
+ /** Pass the image's rendered rectangle, excluding contain letterboxing. */
23
+ export declare function imagePointFromClient(image: ImageDimensions, rect: Readonly<{
24
+ left: number;
25
+ top: number;
26
+ width: number;
27
+ height: number;
28
+ }>, client: ImagePoint, clamp?: boolean): ImagePoint | undefined;
29
+ /** One deterministic overlay is used by the editor and exported/model-visible PNG. */
30
+ export declare function imageAnnotationMarkup(image: ImageDimensions, annotation: ImageAnnotation): string;
31
+ export declare function annotateImage(source: Blob, annotation: ImageAnnotation): Promise<Blob>;
@@ -0,0 +1,37 @@
1
+ function f(i, e) {
2
+ return ![i.width, i.height].every((t) => Number.isSafeInteger(t) && t > 0 && t <= 16384) || !e || ![e.x, e.y].every(Number.isFinite) || e.x < 0 || e.y < 0 || e.x > i.width || e.y > i.height ? !1 : e.kind === "point" ? !0 : e.kind === "rectangle" && [e.width, e.height].every((t) => Number.isFinite(t) && t > 0) && e.x + e.width <= i.width && e.y + e.height <= i.height;
3
+ }
4
+ function $(i, e, t, s = !1) {
5
+ if (e.width <= 0 || e.height <= 0) return;
6
+ const h = (t.x - e.left) * i.width / e.width, r = (t.y - e.top) * i.height / e.height;
7
+ if (!(![h, r].every(Number.isFinite) || !s && (h < 0 || r < 0 || h > i.width || r > i.height)))
8
+ return { x: Math.max(0, Math.min(i.width, h)), y: Math.max(0, Math.min(i.height, r)) };
9
+ }
10
+ function g(i, e) {
11
+ if (!f(i, e)) throw new Error("Invalid image annotation");
12
+ const t = Math.max(1, Math.min(i.width, i.height) / 500), { x: s, y: h } = e, r = 9 * t, w = Math.max(r + t, Math.min(i.width - r - t, s + 17 * t)), n = Math.max(r + t, Math.min(i.height - r - t, h - 17 * t)), l = e.kind === "rectangle" ? `<rect x="${s}" y="${h}" width="${e.width}" height="${e.height}" fill="#f59e0b" fill-opacity=".08"/>` : `<path d="M${s - 6 * t},${h}h${12 * t}M${s},${h - 6 * t}v${12 * t}"/><circle cx="${s}" cy="${h}" r="${2 * t}" fill="#f59e0b"/>`;
13
+ return `<g fill="none" stroke-linejoin="round"><g stroke="#111827" stroke-width="${4 * t}">${l}</g><g stroke="#fbbf24" stroke-width="${2 * t}">${l}<path d="M${s},${h}L${w},${n}"/></g><circle cx="${w}" cy="${n}" r="${r}" fill="#fbbf24" stroke="#111827" stroke-width="${t}"/><text x="${w}" y="${n}" dy=".35em" text-anchor="middle" fill="#111827" stroke="none" font-family="sans-serif" font-size="${12 * t}" font-weight="700">1</text></g>`;
14
+ }
15
+ async function x(i, e) {
16
+ const t = await createImageBitmap(i);
17
+ let s;
18
+ try {
19
+ const { width: h, height: r } = t;
20
+ if (h * r > 32e6) throw new Error("Image exceeds annotation pixel limit");
21
+ const w = g({ width: h, height: r }, e), n = document.createElement("canvas");
22
+ n.width = h, n.height = r;
23
+ const l = n.getContext("2d");
24
+ if (!l) throw new Error("Image annotation is unavailable");
25
+ l.drawImage(t, 0, 0), s = URL.createObjectURL(new Blob([`<svg xmlns="http://www.w3.org/2000/svg" width="${h}" height="${r}" viewBox="0 0 ${h} ${r}">${w}</svg>`], { type: "image/svg+xml" }));
26
+ const c = new Image();
27
+ return c.src = s, await c.decode(), l.drawImage(c, 0, 0), await new Promise((o, a) => n.toBlob((d) => d ? o(d) : a(new Error("Image annotation is unavailable")), "image/png"));
28
+ } finally {
29
+ t.close(), s && URL.revokeObjectURL(s);
30
+ }
31
+ }
32
+ export {
33
+ x as annotateImage,
34
+ g as imageAnnotationMarkup,
35
+ $ as imagePointFromClient,
36
+ f as validateImageAnnotation
37
+ };
@@ -0,0 +1,4 @@
1
+ export { ImageAnnotationSurface } from './components/media/ImageAnnotationSurface';
2
+ export type { ImageAnnotationSurfaceProps } from './components/media/ImageAnnotationSurface';
3
+ export { imagePointFromClient, validateImageAnnotation, imageAnnotationMarkup, annotateImage } from './components/media/imageAnnotation';
4
+ export type { ImageAnnotation, ImageDimensions, ImagePoint } from './components/media/imageAnnotation';
@@ -0,0 +1,9 @@
1
+ import { ImageAnnotationSurface as o } from "./components/media/ImageAnnotationSurface.js";
2
+ import { annotateImage as e, imageAnnotationMarkup as i, imagePointFromClient as m, validateImageAnnotation as r } from "./components/media/imageAnnotation.js";
3
+ export {
4
+ o as ImageAnnotationSurface,
5
+ e as annotateImage,
6
+ i as imageAnnotationMarkup,
7
+ m as imagePointFromClient,
8
+ r as validateImageAnnotation
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@floegence/floe-webapp-core",
3
- "version": "0.101.19",
3
+ "version": "0.101.20",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -160,7 +160,11 @@
160
160
  "types": "./dist/diagrams.d.ts",
161
161
  "import": "./dist/diagrams.js"
162
162
  },
163
- "./diagrams.css": "./dist/diagrams.css"
163
+ "./diagrams.css": "./dist/diagrams.css",
164
+ "./image-annotation": {
165
+ "types": "./dist/image-annotation.d.ts",
166
+ "import": "./dist/image-annotation.js"
167
+ }
164
168
  },
165
169
  "files": [
166
170
  "dist",