@vibes.diy/base 14.3.368 → 14.3.370

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.
@@ -14,6 +14,8 @@ export interface ImgGenProps {
14
14
  alt?: string;
15
15
  style?: React.CSSProperties;
16
16
  showControls?: boolean;
17
+ shareToStory?: boolean;
18
+ storyLink?: string;
17
19
  model?: string;
18
20
  imgGen?: (prompt: string, opts?: ImgGenRequestOptions) => Promise<Result<ImgGenResult>>;
19
21
  useFireproof?: UseFireproofHook;
@@ -22,4 +24,4 @@ export interface ImgGenProps {
22
24
  localFileUrl?: LocalFileUrl | null;
23
25
  captureRender?: boolean;
24
26
  }
25
- export declare function ImgGen({ prompt, _id: propId, images, database, className, alt, style, showControls, model, imgGen, useFireproof, confirmedMediaProbe, pictures: picturesProp, localFileUrl, captureRender: captureRenderProp, }: ImgGenProps): React.JSX.Element;
27
+ export declare function ImgGen({ prompt, _id: propId, images, database, className, alt, style, showControls, shareToStory, storyLink, model, imgGen, useFireproof, confirmedMediaProbe, pictures: picturesProp, localFileUrl, captureRender: captureRenderProp, }: ImgGenProps): React.JSX.Element;
@@ -1,9 +1,10 @@
1
- import React, { useState, useCallback, useMemo } from "react";
1
+ import React, { useState, useCallback, useMemo, useEffect, useRef } from "react";
2
2
  import { getRegisteredVibeApi } from "@vibes.diy/vibe-runtime";
3
3
  import { IMG_GEN_PAUSED_MESSAGE, isCaptureRefused } from "@vibes.diy/vibe-types";
4
4
  import { useImgGen } from "../hooks/img-gen/use-img-gen.js";
5
5
  import { usePictureSrc } from "../hooks/img-gen/use-picture-src.js";
6
6
  import { registeredPictureStore } from "../hooks/img-gen/local-pictures.js";
7
+ import { buildStoryFile, shareStory, storyLinkFor, storyShareSupported } from "./story-share.js";
7
8
  const GENERATED_IMAGE_NAME = "Generated image";
8
9
  function promptToId(prompt) {
9
10
  let hash = 0x811c9dc5;
@@ -13,7 +14,7 @@ function promptToId(prompt) {
13
14
  }
14
15
  return `img-${(hash >>> 0).toString(36)}`;
15
16
  }
16
- export function ImgGen({ prompt, _id: propId, images, database, className, alt, style, showControls = true, model, imgGen, useFireproof, confirmedMediaProbe, pictures: picturesProp, localFileUrl, captureRender: captureRenderProp, }) {
17
+ export function ImgGen({ prompt, _id: propId, images, database, className, alt, style, showControls = true, shareToStory = false, storyLink, model, imgGen, useFireproof, confirmedMediaProbe, pictures: picturesProp, localFileUrl, captureRender: captureRenderProp, }) {
17
18
  const pictures = picturesProp === undefined ? registeredPictureStore() : (picturesProp ?? undefined);
18
19
  const inputImage = images?.[0];
19
20
  const imageKey = inputImage
@@ -159,6 +160,7 @@ export function ImgGen({ prompt, _id: propId, images, database, className, alt,
159
160
  "Error: ",
160
161
  error.message)),
161
162
  needsTap === true && React.createElement("div", { style: { position: "absolute", top: 8, left: 8, zIndex: 11 } }, playgroundButton),
163
+ shareToStory && inCapture === false && previewUrl === undefined && picture.src !== undefined && (React.createElement(StoryButton, { src: picture.src, link: storyLink })),
162
164
  showControls && (React.createElement("div", { style: {
163
165
  position: "absolute",
164
166
  bottom: 8,
@@ -176,6 +178,76 @@ export function ImgGen({ prompt, _id: propId, images, database, className, alt,
176
178
  React.createElement("button", { onClick: handleNext, disabled: currentVersion >= versions.length - 1, style: btnStyle, title: "Next version" }, "\u203A"))),
177
179
  inCapture === false && (React.createElement("button", { onClick: handleTapGeneration, disabled: loading, style: btnStyle, title: "Regenerate" }, "\u21BB"))))));
178
180
  }
181
+ function StoryButton({ src, link }) {
182
+ const [supported] = useState(() => storyShareSupported());
183
+ const [file, setFile] = useState(undefined);
184
+ const [note, setNote] = useState(undefined);
185
+ const ref = useRef(null);
186
+ const href = storyLinkFor(link, typeof location === "undefined" ? undefined : location);
187
+ useEffect(() => {
188
+ setFile(undefined);
189
+ const el = ref.current;
190
+ if (!supported || el === null)
191
+ return;
192
+ let live = true;
193
+ let started = false;
194
+ const build = () => {
195
+ if (started)
196
+ return;
197
+ started = true;
198
+ buildStoryFile(src, href)
199
+ .then((f) => {
200
+ if (live && f !== undefined && navigator.canShare({ files: [f] }))
201
+ setFile(f);
202
+ })
203
+ .catch(() => undefined);
204
+ };
205
+ if (typeof IntersectionObserver === "undefined") {
206
+ build();
207
+ return () => {
208
+ live = false;
209
+ };
210
+ }
211
+ const io = new IntersectionObserver((entries) => {
212
+ if (entries.some((e) => e.isIntersecting)) {
213
+ io.disconnect();
214
+ build();
215
+ }
216
+ });
217
+ io.observe(el);
218
+ return () => {
219
+ live = false;
220
+ io.disconnect();
221
+ };
222
+ }, [supported, src, href]);
223
+ const onTap = useCallback(() => {
224
+ if (file === undefined)
225
+ return;
226
+ if (href !== "")
227
+ setNote("Link copied. Paste it in a link sticker.");
228
+ void shareStory(file, href).then((r) => {
229
+ if (r === "failed")
230
+ setNote("The share sheet didn't open here.");
231
+ setTimeout(() => setNote(undefined), 4000);
232
+ });
233
+ }, [file, href]);
234
+ if (!supported)
235
+ return null;
236
+ return (React.createElement("div", { style: {
237
+ position: "absolute",
238
+ bottom: 8,
239
+ left: 8,
240
+ display: "flex",
241
+ flexDirection: "column",
242
+ alignItems: "flex-start",
243
+ gap: 4,
244
+ zIndex: 11,
245
+ } },
246
+ note !== undefined && (React.createElement("div", { role: "status", style: { ...btnStyle, cursor: "default", fontSize: 12, padding: "4px 8px" } }, note)),
247
+ React.createElement("button", { ref: ref, type: "button", "data-imggen-story": true, onClick: onTap, disabled: file === undefined, "aria-busy": file === undefined, style: { ...storyBtnStyle, opacity: file === undefined ? 0.6 : 1 }, title: "Add to your story" },
248
+ React.createElement("span", { "aria-hidden": "true", style: { fontSize: 16, lineHeight: 1 } }, "\u2295"),
249
+ "Add to story")));
250
+ }
179
251
  const btnStyle = {
180
252
  background: "rgba(0,0,0,0.6)",
181
253
  color: "#fff",
@@ -202,3 +274,15 @@ const playgroundBtnStyle = {
202
274
  fontSize: 15,
203
275
  lineHeight: 1.2,
204
276
  };
277
+ const storyBtnStyle = {
278
+ ...btnStyle,
279
+ display: "inline-flex",
280
+ alignItems: "center",
281
+ gap: 6,
282
+ padding: "8px 12px",
283
+ borderRadius: 999,
284
+ fontSize: 14,
285
+ fontWeight: 600,
286
+ background: "linear-gradient(45deg, #f09433 0%, #dc2743 50%, #bc1888 100%)",
287
+ boxShadow: "0 1px 4px rgba(0,0,0,0.3)",
288
+ };
@@ -0,0 +1,16 @@
1
+ export declare const STORY_WIDTH = 1080;
2
+ export declare const STORY_HEIGHT = 1920;
3
+ export declare function storyLinkFor(link: string | undefined, loc: {
4
+ host: string;
5
+ origin: string;
6
+ } | undefined): string;
7
+ export declare function printedLink(link: string): string;
8
+ export declare function fitPicture(w: number, h: number): {
9
+ x: number;
10
+ y: number;
11
+ w: number;
12
+ h: number;
13
+ };
14
+ export declare function storyShareSupported(nav?: Navigator | undefined): boolean;
15
+ export declare function buildStoryFile(src: string, link: string): Promise<File | undefined>;
16
+ export declare function shareStory(file: File, link: string, nav?: Navigator): Promise<"shared" | "cancelled" | "failed">;
@@ -0,0 +1,115 @@
1
+ import { publicVibeBase } from "@vibes.diy/vibe-runtime";
2
+ export const STORY_WIDTH = 1080;
3
+ export const STORY_HEIGHT = 1920;
4
+ export function storyLinkFor(link, loc) {
5
+ if (link !== undefined && link !== "")
6
+ return link;
7
+ if (loc === undefined)
8
+ return "";
9
+ return publicVibeBase(loc.host) ?? loc.origin;
10
+ }
11
+ export function printedLink(link) {
12
+ return link.replace(/^https?:\/\//, "").replace(/\/$/, "");
13
+ }
14
+ export function fitPicture(w, h) {
15
+ const maxW = STORY_WIDTH - 120;
16
+ const maxH = STORY_HEIGHT - 520;
17
+ const scale = Math.min(maxW / w, maxH / h);
18
+ const dw = Math.round(w * scale);
19
+ const dh = Math.round(h * scale);
20
+ return { x: Math.round((STORY_WIDTH - dw) / 2), y: Math.round(200 + (maxH - dh) / 2), w: dw, h: dh };
21
+ }
22
+ export function storyShareSupported(nav = typeof navigator === "undefined" ? undefined : navigator) {
23
+ if (nav === undefined || typeof nav.canShare !== "function" || typeof nav.share !== "function")
24
+ return false;
25
+ if (typeof matchMedia === "function" && matchMedia("(pointer: coarse)").matches === false)
26
+ return false;
27
+ try {
28
+ return nav.canShare({ files: [new File([new Uint8Array(1)], "story.jpg", { type: "image/jpeg" })] });
29
+ }
30
+ catch {
31
+ return false;
32
+ }
33
+ }
34
+ function roundedRect(ctx, x, y, w, h, r) {
35
+ ctx.beginPath();
36
+ ctx.moveTo(x + r, y);
37
+ ctx.arcTo(x + w, y, x + w, y + h, r);
38
+ ctx.arcTo(x + w, y + h, x, y + h, r);
39
+ ctx.arcTo(x, y + h, x, y, r);
40
+ ctx.arcTo(x, y, x + w, y, r);
41
+ ctx.closePath();
42
+ }
43
+ export async function buildStoryFile(src, link) {
44
+ const res = await fetch(src, { credentials: "include" });
45
+ if (!res.ok)
46
+ return undefined;
47
+ const bmp = await createImageBitmap(await res.blob());
48
+ const canvas = document.createElement("canvas");
49
+ canvas.width = STORY_WIDTH;
50
+ canvas.height = STORY_HEIGHT;
51
+ const ctx = canvas.getContext("2d");
52
+ if (ctx === null)
53
+ return undefined;
54
+ const cover = Math.max(STORY_WIDTH / bmp.width, STORY_HEIGHT / bmp.height);
55
+ const cw = bmp.width * cover;
56
+ const ch = bmp.height * cover;
57
+ ctx.fillStyle = "#111";
58
+ ctx.fillRect(0, 0, STORY_WIDTH, STORY_HEIGHT);
59
+ ctx.filter = "blur(48px) brightness(0.55)";
60
+ ctx.drawImage(bmp, (STORY_WIDTH - cw) / 2 - 60, (STORY_HEIGHT - ch) / 2 - 60, cw + 120, ch + 120);
61
+ ctx.filter = "none";
62
+ ctx.fillStyle = "rgba(0,0,0,0.25)";
63
+ ctx.fillRect(0, 0, STORY_WIDTH, STORY_HEIGHT);
64
+ const box = fitPicture(bmp.width, bmp.height);
65
+ ctx.save();
66
+ ctx.shadowColor = "rgba(0,0,0,0.45)";
67
+ ctx.shadowBlur = 48;
68
+ ctx.shadowOffsetY = 16;
69
+ roundedRect(ctx, box.x, box.y, box.w, box.h, 36);
70
+ ctx.fillStyle = "#000";
71
+ ctx.fill();
72
+ ctx.restore();
73
+ ctx.save();
74
+ roundedRect(ctx, box.x, box.y, box.w, box.h, 36);
75
+ ctx.clip();
76
+ ctx.drawImage(bmp, box.x, box.y, box.w, box.h);
77
+ ctx.restore();
78
+ bmp.close();
79
+ const text = printedLink(link);
80
+ if (text !== "") {
81
+ let size = 44;
82
+ const font = (px) => `600 ${px}px -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif`;
83
+ ctx.font = font(size);
84
+ while (ctx.measureText(text).width > STORY_WIDTH - 200 && size > 22) {
85
+ size -= 2;
86
+ ctx.font = font(size);
87
+ }
88
+ const tw = ctx.measureText(text).width;
89
+ const ph = size + 44;
90
+ const pw = tw + 80;
91
+ const py = Math.min(STORY_HEIGHT - 220, box.y + box.h + 70);
92
+ roundedRect(ctx, (STORY_WIDTH - pw) / 2, py, pw, ph, ph / 2);
93
+ ctx.fillStyle = "rgba(255,255,255,0.92)";
94
+ ctx.fill();
95
+ ctx.fillStyle = "#111";
96
+ ctx.textAlign = "center";
97
+ ctx.textBaseline = "middle";
98
+ ctx.fillText(text, STORY_WIDTH / 2, py + ph / 2 + 1);
99
+ }
100
+ const blob = await new Promise((resolve) => canvas.toBlob(resolve, "image/jpeg", 0.9));
101
+ if (blob === null)
102
+ return undefined;
103
+ return new File([blob], "story.jpg", { type: "image/jpeg" });
104
+ }
105
+ export async function shareStory(file, link, nav = navigator) {
106
+ if (link !== "")
107
+ nav.clipboard?.writeText(link).catch(() => undefined);
108
+ try {
109
+ await nav.share({ files: [file] });
110
+ return "shared";
111
+ }
112
+ catch (err) {
113
+ return err?.name === "AbortError" ? "cancelled" : "failed";
114
+ }
115
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vibes.diy/base",
3
- "version": "14.3.368",
3
+ "version": "14.3.370",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -10,10 +10,10 @@
10
10
  "dependencies": {
11
11
  "@adviser/cement": "~0.5.37",
12
12
  "@emotion/css": "~11.13.5",
13
- "@vibes.diy/api-types": "14.3.368",
14
- "@vibes.diy/use-vibes-types": "14.3.368",
15
- "@vibes.diy/vibe-runtime": "14.3.368",
16
- "@vibes.diy/vibe-types": "14.3.368",
13
+ "@vibes.diy/api-types": "14.3.370",
14
+ "@vibes.diy/use-vibes-types": "14.3.370",
15
+ "@vibes.diy/vibe-runtime": "14.3.370",
16
+ "@vibes.diy/vibe-types": "14.3.370",
17
17
  "arktype": "~2.2.9",
18
18
  "officeparser": "~8.7.1",
19
19
  "qrcode-generator": "~2.0.4",