jazz-tools 0.18.2 → 0.18.4
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/.turbo/turbo-build.log +43 -41
- package/CHANGELOG.md +20 -0
- package/dist/{chunk-IERUTUXB.js → chunk-LHQQZH7I.js} +121 -36
- package/dist/chunk-LHQQZH7I.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/media/{chunk-KR2V6X2N.js → chunk-W3S526L3.js} +96 -93
- package/dist/media/chunk-W3S526L3.js.map +1 -0
- package/dist/media/create-image/browser.d.ts +43 -0
- package/dist/media/create-image/browser.d.ts.map +1 -0
- package/dist/media/create-image/react-native.d.ts +37 -0
- package/dist/media/create-image/react-native.d.ts.map +1 -0
- package/dist/media/create-image/server.d.ts +34 -0
- package/dist/media/create-image/server.d.ts.map +1 -0
- package/dist/media/create-image/server.test.d.ts +2 -0
- package/dist/media/create-image/server.test.d.ts.map +1 -0
- package/dist/media/{create-image.d.ts → create-image-factory.d.ts} +8 -7
- package/dist/media/create-image-factory.d.ts.map +1 -0
- package/dist/media/create-image-factory.test.d.ts +2 -0
- package/dist/media/create-image-factory.test.d.ts.map +1 -0
- package/dist/media/exports.d.ts +3 -0
- package/dist/media/exports.d.ts.map +1 -0
- package/dist/media/index.browser.d.ts +2 -14
- package/dist/media/index.browser.d.ts.map +1 -1
- package/dist/media/index.browser.js +11 -20
- package/dist/media/index.browser.js.map +1 -1
- package/dist/media/index.d.ts +12 -4
- package/dist/media/index.d.ts.map +1 -1
- package/dist/media/index.js +1 -1
- package/dist/media/index.native.d.ts +2 -16
- package/dist/media/index.native.d.ts.map +1 -1
- package/dist/media/index.native.js +23 -42
- package/dist/media/index.native.js.map +1 -1
- package/dist/media/index.server.d.ts +3 -0
- package/dist/media/index.server.d.ts.map +1 -0
- package/dist/media/index.server.js +103 -0
- package/dist/media/index.server.js.map +1 -0
- package/dist/react/index.js +7 -7
- package/dist/react/index.js.map +1 -1
- package/dist/react-core/index.js +120 -35
- package/dist/react-core/index.js.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/tools/coValues/account.d.ts.map +1 -1
- package/dist/tools/coValues/coFeed.d.ts +12 -0
- package/dist/tools/coValues/coFeed.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +19 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/subscribe/CoValueCoreSubscription.d.ts +61 -11
- package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
- package/dist/tools/subscribe/CoValueCoreSubscription.test.d.ts +2 -0
- package/dist/tools/subscribe/CoValueCoreSubscription.test.d.ts.map +1 -0
- package/dist/tools/testing.d.ts.map +1 -1
- package/package.json +27 -11
- package/src/media/create-image/browser.ts +161 -0
- package/src/media/create-image/react-native.ts +158 -0
- package/src/media/create-image/server.test.ts +74 -0
- package/src/media/create-image/server.ts +181 -0
- package/src/media/{create-image.test.ts → create-image-factory.test.ts} +1 -1
- package/src/media/{create-image.ts → create-image-factory.ts} +22 -12
- package/src/media/exports.ts +2 -0
- package/src/media/index.browser.ts +2 -150
- package/src/media/index.native.ts +2 -166
- package/src/media/index.server.ts +2 -0
- package/src/media/index.ts +16 -8
- package/src/tools/coValues/account.ts +3 -1
- package/src/tools/coValues/coFeed.ts +5 -0
- package/src/tools/coValues/coMap.ts +3 -1
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +19 -0
- package/src/tools/subscribe/CoValueCoreSubscription.test.ts +1000 -0
- package/src/tools/subscribe/CoValueCoreSubscription.ts +179 -43
- package/src/tools/tests/account.test.ts +12 -0
- package/src/tools/tests/coFeed.test.ts +25 -0
- package/src/tools/tests/coList.test.ts +20 -0
- package/src/tools/tests/coMap.record.test.ts +1 -0
- package/src/tools/tests/coMap.test.ts +12 -2
- package/tsup.config.ts +1 -0
- package/dist/chunk-IERUTUXB.js.map +0 -1
- package/dist/media/chunk-KR2V6X2N.js.map +0 -1
- package/dist/media/create-image.d.ts.map +0 -1
- package/dist/media/create-image.test.d.ts +0 -2
- package/dist/media/create-image.test.d.ts.map +0 -1
package/dist/index.js
CHANGED
@@ -1,89 +1,5 @@
|
|
1
|
-
// src/media/create-image.ts
|
2
|
-
import {
|
3
|
-
ImageDefinition
|
4
|
-
} from "jazz-tools";
|
5
|
-
function createImageFactory(impl) {
|
6
|
-
return (source, options) => createImage(source, options, impl);
|
7
|
-
}
|
8
|
-
async function createImage(imageBlobOrFile, options, impl) {
|
9
|
-
const { width: originalWidth, height: originalHeight } = await impl.getImageSize(imageBlobOrFile);
|
10
|
-
const def = {
|
11
|
-
originalSize: [originalWidth, originalHeight],
|
12
|
-
progressive: false,
|
13
|
-
placeholderDataURL: void 0,
|
14
|
-
files: {}
|
15
|
-
};
|
16
|
-
if (options?.placeholder === "blur") {
|
17
|
-
def.placeholderDataURL = await impl.getPlaceholderBase64(imageBlobOrFile);
|
18
|
-
}
|
19
|
-
if (options?.maxSize === void 0) {
|
20
|
-
def.original = await impl.createFileStreamFromSource(
|
21
|
-
imageBlobOrFile,
|
22
|
-
options?.owner
|
23
|
-
);
|
24
|
-
def.files[`${originalWidth}x${originalHeight}`] = def.original;
|
25
|
-
} else if (options?.maxSize >= originalWidth && options?.maxSize >= originalHeight) {
|
26
|
-
def.original = await impl.createFileStreamFromSource(
|
27
|
-
imageBlobOrFile,
|
28
|
-
options?.owner
|
29
|
-
);
|
30
|
-
def.files[`${originalWidth}x${originalHeight}`] = def.original;
|
31
|
-
} else {
|
32
|
-
const { width, height } = getNewDimensions(
|
33
|
-
originalWidth,
|
34
|
-
originalHeight,
|
35
|
-
options.maxSize
|
36
|
-
);
|
37
|
-
const blob = await impl.resize(imageBlobOrFile, width, height);
|
38
|
-
def.originalSize = [width, height];
|
39
|
-
def.original = await impl.createFileStreamFromSource(blob, options?.owner);
|
40
|
-
def.files[`${width}x${height}`] = def.original;
|
41
|
-
}
|
42
|
-
const imageCoValue = ImageDefinition.create(
|
43
|
-
{
|
44
|
-
originalSize: def.originalSize,
|
45
|
-
progressive: def.progressive,
|
46
|
-
placeholderDataURL: def.placeholderDataURL,
|
47
|
-
original: def.original,
|
48
|
-
...def.files
|
49
|
-
},
|
50
|
-
options?.owner
|
51
|
-
);
|
52
|
-
if (options?.progressive) {
|
53
|
-
imageCoValue.$jazz.set("progressive", true);
|
54
|
-
const resizes = [256, 1024, 2048].filter(
|
55
|
-
(s) => s < Math.max(imageCoValue.originalSize[0], imageCoValue.originalSize[1])
|
56
|
-
);
|
57
|
-
for (const size of resizes) {
|
58
|
-
const { width, height } = getNewDimensions(
|
59
|
-
originalWidth,
|
60
|
-
originalHeight,
|
61
|
-
size
|
62
|
-
);
|
63
|
-
const blob = await impl.resize(imageBlobOrFile, width, height);
|
64
|
-
imageCoValue.$jazz.set(
|
65
|
-
`${width}x${height}`,
|
66
|
-
await impl.createFileStreamFromSource(blob, options?.owner)
|
67
|
-
);
|
68
|
-
}
|
69
|
-
}
|
70
|
-
return imageCoValue;
|
71
|
-
}
|
72
|
-
var getNewDimensions = (originalWidth, originalHeight, maxSize) => {
|
73
|
-
if (originalWidth > originalHeight) {
|
74
|
-
return {
|
75
|
-
width: maxSize,
|
76
|
-
height: Math.round(maxSize * (originalHeight / originalWidth))
|
77
|
-
};
|
78
|
-
}
|
79
|
-
return {
|
80
|
-
width: Math.round(maxSize * (originalWidth / originalHeight)),
|
81
|
-
height: maxSize
|
82
|
-
};
|
83
|
-
};
|
84
|
-
|
85
1
|
// src/media/utils.ts
|
86
|
-
import { Account
|
2
|
+
import { Account, FileStream, ImageDefinition } from "jazz-tools";
|
87
3
|
function highestResAvailable(image, wantedWidth, wantedHeight) {
|
88
4
|
const availableSizes = image.$jazz.raw.keys().filter((key) => /^\d+x\d+$/.test(key)).map((key) => {
|
89
5
|
const [w, h] = key.split("x").map(Number);
|
@@ -139,11 +55,11 @@ function isLoaded(id) {
|
|
139
55
|
if (!id) {
|
140
56
|
return false;
|
141
57
|
}
|
142
|
-
return !!
|
58
|
+
return !!Account.getMe().$jazz.localNode.getLoaded(id);
|
143
59
|
}
|
144
60
|
async function loadImage(imageOrId) {
|
145
61
|
if (typeof imageOrId === "string") {
|
146
|
-
const image = await
|
62
|
+
const image = await ImageDefinition.load(imageOrId, {
|
147
63
|
resolve: {
|
148
64
|
original: true
|
149
65
|
}
|
@@ -161,7 +77,7 @@ async function loadImage(imageOrId) {
|
|
161
77
|
console.warn("Unable to find the original image");
|
162
78
|
return null;
|
163
79
|
}
|
164
|
-
const loadedOriginal = await
|
80
|
+
const loadedOriginal = await FileStream.load(imageOrId.original.$jazz.id);
|
165
81
|
if (!loadedOriginal) {
|
166
82
|
console.warn("Unable to find the original image");
|
167
83
|
return null;
|
@@ -173,7 +89,7 @@ async function loadImage(imageOrId) {
|
|
173
89
|
};
|
174
90
|
}
|
175
91
|
async function loadImageBySize(imageOrId, wantedWidth, wantedHeight) {
|
176
|
-
const image = typeof imageOrId === "string" ? await
|
92
|
+
const image = typeof imageOrId === "string" ? await ImageDefinition.load(imageOrId) : imageOrId;
|
177
93
|
if (image === null) {
|
178
94
|
return null;
|
179
95
|
}
|
@@ -196,7 +112,7 @@ async function loadImageBySize(imageOrId, wantedWidth, wantedHeight) {
|
|
196
112
|
if (!file) {
|
197
113
|
return null;
|
198
114
|
}
|
199
|
-
const loadedFile = await
|
115
|
+
const loadedFile = await FileStream.load(file.id);
|
200
116
|
if (!loadedFile) {
|
201
117
|
return null;
|
202
118
|
}
|
@@ -207,10 +123,97 @@ async function loadImageBySize(imageOrId, wantedWidth, wantedHeight) {
|
|
207
123
|
};
|
208
124
|
}
|
209
125
|
|
126
|
+
// src/media/create-image-factory.ts
|
127
|
+
import {
|
128
|
+
ImageDefinition as ImageDefinition2
|
129
|
+
} from "jazz-tools";
|
130
|
+
function createImageFactory(impl, imageTypeGuard) {
|
131
|
+
return (source, options) => {
|
132
|
+
imageTypeGuard?.(source);
|
133
|
+
return createImage(source, options ?? {}, impl);
|
134
|
+
};
|
135
|
+
}
|
136
|
+
async function createImage(imageBlobOrFile, options, impl) {
|
137
|
+
const { width: originalWidth, height: originalHeight } = await impl.getImageSize(imageBlobOrFile);
|
138
|
+
const def = {
|
139
|
+
originalSize: [originalWidth, originalHeight],
|
140
|
+
progressive: false,
|
141
|
+
placeholderDataURL: void 0,
|
142
|
+
files: {}
|
143
|
+
};
|
144
|
+
if (options?.placeholder === "blur") {
|
145
|
+
def.placeholderDataURL = await impl.getPlaceholderBase64(imageBlobOrFile);
|
146
|
+
}
|
147
|
+
if (options?.maxSize === void 0) {
|
148
|
+
def.original = await impl.createFileStreamFromSource(
|
149
|
+
imageBlobOrFile,
|
150
|
+
options?.owner
|
151
|
+
);
|
152
|
+
def.files[`${originalWidth}x${originalHeight}`] = def.original;
|
153
|
+
} else if (options?.maxSize >= originalWidth && options?.maxSize >= originalHeight) {
|
154
|
+
def.original = await impl.createFileStreamFromSource(
|
155
|
+
imageBlobOrFile,
|
156
|
+
options?.owner
|
157
|
+
);
|
158
|
+
def.files[`${originalWidth}x${originalHeight}`] = def.original;
|
159
|
+
} else {
|
160
|
+
const { width, height } = getNewDimensions(
|
161
|
+
originalWidth,
|
162
|
+
originalHeight,
|
163
|
+
options.maxSize
|
164
|
+
);
|
165
|
+
const blob = await impl.resize(imageBlobOrFile, width, height);
|
166
|
+
def.originalSize = [width, height];
|
167
|
+
def.original = await impl.createFileStreamFromSource(blob, options?.owner);
|
168
|
+
def.files[`${width}x${height}`] = def.original;
|
169
|
+
}
|
170
|
+
const imageCoValue = ImageDefinition2.create(
|
171
|
+
{
|
172
|
+
originalSize: def.originalSize,
|
173
|
+
progressive: def.progressive,
|
174
|
+
placeholderDataURL: def.placeholderDataURL,
|
175
|
+
original: def.original,
|
176
|
+
...def.files
|
177
|
+
},
|
178
|
+
options?.owner
|
179
|
+
);
|
180
|
+
if (options?.progressive) {
|
181
|
+
imageCoValue.$jazz.set("progressive", true);
|
182
|
+
const resizes = [256, 1024, 2048].filter(
|
183
|
+
(s) => s < Math.max(imageCoValue.originalSize[0], imageCoValue.originalSize[1])
|
184
|
+
);
|
185
|
+
for (const size of resizes) {
|
186
|
+
const { width, height } = getNewDimensions(
|
187
|
+
originalWidth,
|
188
|
+
originalHeight,
|
189
|
+
size
|
190
|
+
);
|
191
|
+
const blob = await impl.resize(imageBlobOrFile, width, height);
|
192
|
+
imageCoValue.$jazz.set(
|
193
|
+
`${width}x${height}`,
|
194
|
+
await impl.createFileStreamFromSource(blob, options?.owner)
|
195
|
+
);
|
196
|
+
}
|
197
|
+
}
|
198
|
+
return imageCoValue;
|
199
|
+
}
|
200
|
+
var getNewDimensions = (originalWidth, originalHeight, maxSize) => {
|
201
|
+
if (originalWidth > originalHeight) {
|
202
|
+
return {
|
203
|
+
width: maxSize,
|
204
|
+
height: Math.round(maxSize * (originalHeight / originalWidth))
|
205
|
+
};
|
206
|
+
}
|
207
|
+
return {
|
208
|
+
width: Math.round(maxSize * (originalWidth / originalHeight)),
|
209
|
+
height: maxSize
|
210
|
+
};
|
211
|
+
};
|
212
|
+
|
210
213
|
export {
|
211
|
-
createImageFactory,
|
212
214
|
highestResAvailable,
|
213
215
|
loadImage,
|
214
|
-
loadImageBySize
|
216
|
+
loadImageBySize,
|
217
|
+
createImageFactory
|
215
218
|
};
|
216
|
-
//# sourceMappingURL=chunk-
|
219
|
+
//# sourceMappingURL=chunk-W3S526L3.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/media/utils.ts","../../src/media/create-image-factory.ts"],"sourcesContent":["import type { CoID } from \"cojson\";\nimport { Account, FileStream, ImageDefinition } from \"jazz-tools\";\n\nexport function highestResAvailable(\n image: ImageDefinition,\n wantedWidth: number,\n wantedHeight: number,\n): { width: number; height: number; image: FileStream } | null {\n const availableSizes: [number, number, string][] = image.$jazz.raw\n .keys()\n .filter((key) => /^\\d+x\\d+$/.test(key))\n .map((key) => {\n const [w, h] = key.split(\"x\").map(Number) as [number, number];\n return [w, h, key];\n });\n\n if (availableSizes.length === 0) {\n return image.original\n ? {\n width: image.originalSize[0],\n height: image.originalSize[1],\n image: image.original,\n }\n : null;\n }\n\n const sortedSizes = availableSizes\n .map((size) => {\n return {\n size,\n match: sizesMatchWanted(size[0], size[1], wantedWidth, wantedHeight),\n isLoaded: isLoaded(\n image.$jazz.raw.get(size[2]) as CoID<any> | undefined,\n ),\n };\n })\n .sort((a, b) => a.match - b.match);\n\n // We try to find the better already loaded image\n // note: `toReversed` is not available in react-native.\n const bestLoaded = [...sortedSizes]\n .reverse()\n .find((el) => el.isLoaded && image[el.size[2]]?.getChunks());\n\n // if I can't find a good match, let's use the highest resolution\n const bestTarget =\n sortedSizes.find((el) => el.match > 0.95) || sortedSizes.at(-1);\n\n // if the best target is already loaded, we are done\n if (image[bestTarget!.size[2]]?.getChunks()) {\n return image[bestTarget!.size[2]]\n ? {\n width: bestTarget!.size[0],\n height: bestTarget!.size[1],\n image: image[bestTarget!.size[2]]!,\n }\n : null;\n }\n\n // if the best already loaded is not the best target\n // let's trigger the load of the best target\n if (bestLoaded) {\n image[bestTarget!.size[2]]?.getChunks();\n return image[bestLoaded.size[2]]\n ? {\n width: bestLoaded.size[0],\n height: bestLoaded.size[1],\n image: image[bestLoaded.size[2]]!,\n }\n : null;\n }\n\n // if nothing is loaded, then start fetching all the images till the best\n for (let size of sortedSizes) {\n if (size.match <= bestTarget!.match) {\n image[size.size[2]]?.getChunks();\n }\n }\n\n return null;\n}\n\nfunction sizesMatchWanted(\n w: number,\n h: number,\n wantedW: number,\n wantedH: number,\n): number {\n const area1 = w * h;\n const area2 = wantedW * wantedH;\n\n const areaRatio = area1 / area2;\n\n // // Below 0.95 means the image is too small, we don't want to upscale it\n // if (areaRatio < 0.95) {\n // return 9999;\n // }\n\n return areaRatio;\n}\n\nfunction isLoaded(id: CoID<any> | null | undefined): boolean {\n if (!id) {\n return false;\n }\n\n return !!Account.getMe().$jazz.localNode.getLoaded(id);\n}\n\nexport async function loadImage(\n imageOrId: ImageDefinition | string,\n): Promise<{ width: number; height: number; image: FileStream } | null> {\n if (typeof imageOrId === \"string\") {\n const image = await ImageDefinition.load(imageOrId, {\n resolve: {\n original: true,\n },\n });\n\n if (image === null || image.original === null) {\n return null;\n }\n\n return {\n width: image.originalSize[0],\n height: image.originalSize[1],\n image: image.original,\n };\n }\n\n if (!imageOrId.original) {\n console.warn(\"Unable to find the original image\");\n return null;\n }\n\n const loadedOriginal = await FileStream.load(imageOrId.original.$jazz.id);\n\n if (!loadedOriginal) {\n console.warn(\"Unable to find the original image\");\n return null;\n }\n\n return {\n width: imageOrId.originalSize[0],\n height: imageOrId.originalSize[1],\n image: loadedOriginal,\n };\n}\n\nexport async function loadImageBySize(\n imageOrId: ImageDefinition | string,\n wantedWidth: number,\n wantedHeight: number,\n): Promise<{ width: number; height: number; image: FileStream } | null> {\n // @ts-expect-error The resolved type for CoMap does not include catchall properties\n const image: ImageDefinition | null =\n typeof imageOrId === \"string\"\n ? await ImageDefinition.load(imageOrId)\n : imageOrId;\n\n if (image === null) {\n return null;\n }\n\n if (image.progressive === false) {\n return loadImage(imageOrId);\n }\n\n const availableSizes: [number, number, string][] = image.$jazz.raw\n .keys()\n .filter((key) => /^\\d+x\\d+$/.test(key))\n .map((key) => {\n const [w, h] = key.split(\"x\").map(Number) as [number, number];\n return [w, h, key];\n });\n\n if (availableSizes.length === 0) {\n return null;\n }\n\n const sortedSizes = availableSizes\n .map((size) => ({\n size,\n match: sizesMatchWanted(size[0], size[1], wantedWidth, wantedHeight),\n }))\n .sort((a, b) => a.match - b.match);\n\n const bestTarget =\n sortedSizes.find((el) => el.match > 0.95) || sortedSizes.at(-1)!;\n\n // The image's `wxh` keys reference FileStream.\n // image[bestTarget.size[2]] returns undefined if FileStream hasn't loaded yet.\n // Since we only need the file's ID to fetch it later, we check the raw _refs\n // which contain only the linked covalue's ID.\n const file = image.$jazz.refs[bestTarget.size[2]];\n\n if (!file) {\n return null;\n }\n\n const loadedFile = await FileStream.load(file.id);\n\n if (!loadedFile) {\n return null;\n }\n\n return {\n width: bestTarget.size[0],\n height: bestTarget.size[1],\n image: loadedFile,\n };\n}\n","import {\n Account,\n FileStream,\n Group,\n ImageDefinition,\n type Loaded,\n} from \"jazz-tools\";\n\nexport type SourceType = Blob | File | string;\n\nexport type ResizeOutput = Blob | string;\n\nexport type CreateImageOptions = {\n /** The owner of the image. Can be either a Group or Account. If not specified, the current user will be the owner. */\n owner?: Group | Account;\n /**\n * Controls placeholder generation for the image.\n * - `\"blur\"`: Generates a blurred placeholder image (default)\n * - `false`: No placeholder is generated\n * @default \"blur\"\n */\n placeholder?: \"blur\" | false;\n /**\n * Maximum size constraint for the image. The image will be resized to fit within this size while maintaining aspect ratio.\n * If the image is smaller than maxSize in both dimensions, no resizing occurs.\n * @example 1024 // Resizes image to fit within 1024px in the largest dimension\n */\n maxSize?: number; // | [number, number];\n /**\n * The progressive loading pattern is a technique that allows images to load incrementally, starting with a small version and gradually replacing it with a larger version as it becomes available.\n * This is useful for improving the user experience by showing a placeholder while the image is loading.\n *\n * Passing progressive: true to createImage() will create internal smaller versions of the image for future uses.\n *\n * @default false\n */\n progressive?: boolean;\n};\n\nexport type CreateImageImpl<\n TSourceType = SourceType,\n TResizeOutput = ResizeOutput,\n> = {\n createFileStreamFromSource: (\n imageBlobOrFile: TSourceType | TResizeOutput,\n owner?: Group | Account,\n ) => Promise<FileStream>;\n getImageSize: (\n imageBlobOrFile: TSourceType,\n ) => Promise<{ width: number; height: number }>;\n getPlaceholderBase64: (imageBlobOrFile: TSourceType) => Promise<string>;\n resize: (\n imageBlobOrFile: TSourceType,\n width: number,\n height: number,\n ) => Promise<TResizeOutput>;\n};\n\nexport function createImageFactory<TSourceType, TResizeOutput>(\n impl: CreateImageImpl<TSourceType, TResizeOutput>,\n imageTypeGuard?: (imageBlobOrFile: TSourceType) => void,\n) {\n return (source: TSourceType, options?: CreateImageOptions) => {\n imageTypeGuard?.(source);\n return createImage(source, options ?? {}, impl);\n };\n}\n\nasync function createImage<TSourceType, TResizeOutput>(\n imageBlobOrFile: TSourceType,\n options: CreateImageOptions,\n impl: CreateImageImpl<TSourceType, TResizeOutput>,\n): Promise<Loaded<typeof ImageDefinition, { $each: true }>> {\n // Get the original size of the image\n const { width: originalWidth, height: originalHeight } =\n await impl.getImageSize(imageBlobOrFile);\n\n const def: {\n originalSize: [number, number];\n progressive: boolean;\n placeholderDataURL: string | undefined;\n original?: FileStream;\n files: Record<string, FileStream>;\n } = {\n originalSize: [originalWidth, originalHeight],\n progressive: false,\n placeholderDataURL: undefined,\n files: {},\n };\n\n // Placeholder\n if (options?.placeholder === \"blur\") {\n def.placeholderDataURL = await impl.getPlaceholderBase64(imageBlobOrFile);\n }\n\n /**\n * Original\n *\n * Save the original image.\n * If the maxSize is set, resize the image to the maxSize if needed\n */\n if (options?.maxSize === undefined) {\n def.original = await impl.createFileStreamFromSource(\n imageBlobOrFile,\n options?.owner,\n );\n def.files[`${originalWidth}x${originalHeight}`] = def.original;\n } else if (\n options?.maxSize >= originalWidth &&\n options?.maxSize >= originalHeight\n ) {\n // no resizes required, just return the original image\n def.original = await impl.createFileStreamFromSource(\n imageBlobOrFile,\n options?.owner,\n );\n def.files[`${originalWidth}x${originalHeight}`] = def.original;\n } else {\n const { width, height } = getNewDimensions(\n originalWidth,\n originalHeight,\n options.maxSize,\n );\n\n const blob = await impl.resize(imageBlobOrFile, width, height);\n def.originalSize = [width, height];\n def.original = await impl.createFileStreamFromSource(blob, options?.owner);\n def.files[`${width}x${height}`] = def.original;\n }\n\n const imageCoValue = ImageDefinition.create(\n {\n originalSize: def.originalSize,\n progressive: def.progressive,\n placeholderDataURL: def.placeholderDataURL,\n original: def.original,\n ...def.files,\n },\n options?.owner,\n );\n\n /**\n * Progressive loading\n *\n * Save a set of resized images using three sizes: 256, 1024, 2048\n *\n * On the client side, the image will be loaded progressively, starting from the smallest size and increasing the size until the original size is reached.\n */\n if (options?.progressive) {\n imageCoValue.$jazz.set(\"progressive\", true);\n\n const resizes = ([256, 1024, 2048] as const).filter(\n (s) =>\n s <\n Math.max(imageCoValue.originalSize[0], imageCoValue.originalSize[1]),\n );\n\n for (const size of resizes) {\n const { width, height } = getNewDimensions(\n originalWidth,\n originalHeight,\n size,\n );\n\n const blob = await impl.resize(imageBlobOrFile, width, height);\n imageCoValue.$jazz.set(\n `${width}x${height}`,\n await impl.createFileStreamFromSource(blob, options?.owner),\n );\n }\n }\n\n return imageCoValue;\n}\n\nconst getNewDimensions = (\n originalWidth: number,\n originalHeight: number,\n maxSize: number,\n) => {\n if (originalWidth > originalHeight) {\n return {\n width: maxSize,\n height: Math.round(maxSize * (originalHeight / originalWidth)),\n };\n }\n\n return {\n width: Math.round(maxSize * (originalWidth / originalHeight)),\n height: maxSize,\n };\n};\n"],"mappings":";AACA,SAAS,SAAS,YAAY,uBAAuB;AAE9C,SAAS,oBACd,OACA,aACA,cAC6D;AAC7D,QAAM,iBAA6C,MAAM,MAAM,IAC5D,KAAK,EACL,OAAO,CAAC,QAAQ,YAAY,KAAK,GAAG,CAAC,EACrC,IAAI,CAAC,QAAQ;AACZ,UAAM,CAAC,GAAG,CAAC,IAAI,IAAI,MAAM,GAAG,EAAE,IAAI,MAAM;AACxC,WAAO,CAAC,GAAG,GAAG,GAAG;AAAA,EACnB,CAAC;AAEH,MAAI,eAAe,WAAW,GAAG;AAC/B,WAAO,MAAM,WACT;AAAA,MACE,OAAO,MAAM,aAAa,CAAC;AAAA,MAC3B,QAAQ,MAAM,aAAa,CAAC;AAAA,MAC5B,OAAO,MAAM;AAAA,IACf,IACA;AAAA,EACN;AAEA,QAAM,cAAc,eACjB,IAAI,CAAC,SAAS;AACb,WAAO;AAAA,MACL;AAAA,MACA,OAAO,iBAAiB,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,aAAa,YAAY;AAAA,MACnE,UAAU;AAAA,QACR,MAAM,MAAM,IAAI,IAAI,KAAK,CAAC,CAAC;AAA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MAAM,KAAK,2BAA2B,MAAM,SAAS,KAAK;AAAA,MAC5D;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEA,IAAM,mBAAmB,CACvB,eACA,gBACA,YACG;AACH,MAAI,gBAAgB,gBAAgB;AAClC,WAAO;AAAA,MACL,OAAO;AAAA,MACP,QAAQ,KAAK,MAAM,WAAW,iBAAiB,cAAc;AAAA,IAC/D;AAAA,EACF;AAEA,SAAO;AAAA,IACL,OAAO,KAAK,MAAM,WAAW,gBAAgB,eAAe;AAAA,IAC5D,QAAQ;AAAA,EACV;AACF;","names":["ImageDefinition"]}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import { FileStream } from "jazz-tools";
|
2
|
+
/**
|
3
|
+
* Creates an ImageDefinition from an image File or Blob with built-in UX features.
|
4
|
+
*
|
5
|
+
* This function creates a specialized CoValue for managing images in Jazz applications.
|
6
|
+
* It supports blurry placeholders, built-in resizing, and progressive loading patterns.
|
7
|
+
*
|
8
|
+
* @returns Promise that resolves to an ImageDefinition
|
9
|
+
*
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* import { createImage } from "jazz-tools/media";
|
13
|
+
*
|
14
|
+
* // Create an image from a file input
|
15
|
+
* async function handleFileUpload(event: React.ChangeEvent<HTMLInputElement>) {
|
16
|
+
* const file = event.target.files?.[0];
|
17
|
+
* if (file) {
|
18
|
+
* // Creates ImageDefinition with a blurry placeholder, limited to 1024px
|
19
|
+
* // on the longest side, and multiple resolutions automatically
|
20
|
+
* const image = await createImage(file, {
|
21
|
+
* owner: me._owner,
|
22
|
+
* maxSize: 1024,
|
23
|
+
* placeholder: "blur",
|
24
|
+
* progressive: true,
|
25
|
+
* });
|
26
|
+
*
|
27
|
+
* // Store the image in your application data
|
28
|
+
* me.profile.image = image;
|
29
|
+
* }
|
30
|
+
* }
|
31
|
+
* ```
|
32
|
+
*/
|
33
|
+
export declare const createImage: (source: Blob | File, options?: import("../create-image-factory").CreateImageOptions) => Promise<{
|
34
|
+
readonly [key: string]: FileStream;
|
35
|
+
} & {
|
36
|
+
readonly original: FileStream | null;
|
37
|
+
readonly originalSize: [number, number];
|
38
|
+
readonly placeholderDataURL: string | undefined;
|
39
|
+
readonly progressive: boolean;
|
40
|
+
} & {
|
41
|
+
readonly [key: string]: FileStream | null;
|
42
|
+
} & import("jazz-tools").CoMap>;
|
43
|
+
//# sourceMappingURL=browser.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../../src/media/create-image/browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,UAAU,EAAS,MAAM,YAAY,CAAC;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,WAAW;;;;;;;;;+BAYvB,CAAC"}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import type { Account, Group } from "jazz-tools";
|
2
|
+
import { FileStream } from "jazz-tools";
|
3
|
+
/**
|
4
|
+
* Creates an ImageDefinition from an image file path with built-in UX features.
|
5
|
+
*
|
6
|
+
* This function creates a specialized CoValue for managing images in Jazz applications.
|
7
|
+
* It supports blurry placeholders, built-in resizing, and progressive loading patterns.
|
8
|
+
*
|
9
|
+
* @returns Promise that resolves to an ImageDefinition
|
10
|
+
*
|
11
|
+
* @example
|
12
|
+
* ```ts
|
13
|
+
* import { createImage } from "jazz-tools/media";
|
14
|
+
*
|
15
|
+
* async function uploadImageFromCamera(imagePath: string) {
|
16
|
+
* const image = await createImage(imagePath, {
|
17
|
+
* maxSize: 800,
|
18
|
+
* placeholder: "blur",
|
19
|
+
* progressive: false,
|
20
|
+
* });
|
21
|
+
*
|
22
|
+
* return image;
|
23
|
+
* }
|
24
|
+
* ```
|
25
|
+
*/
|
26
|
+
export declare const createImage: (source: string, options?: import("../create-image-factory").CreateImageOptions) => Promise<{
|
27
|
+
readonly [key: string]: FileStream;
|
28
|
+
} & {
|
29
|
+
readonly original: FileStream | null;
|
30
|
+
readonly originalSize: [number, number];
|
31
|
+
readonly placeholderDataURL: string | undefined;
|
32
|
+
readonly progressive: boolean;
|
33
|
+
} & {
|
34
|
+
readonly [key: string]: FileStream | null;
|
35
|
+
} & import("jazz-tools").CoMap>;
|
36
|
+
export declare function createFileStreamFromSource(filePath: string, owner?: Account | Group): Promise<FileStream>;
|
37
|
+
//# sourceMappingURL=react-native.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"react-native.d.ts","sourceRoot":"","sources":["../../../src/media/create-image/react-native.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAMxC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,WAAW;;;;;;;;;+BAcvB,CAAC;AAwEF,wBAAsB,0BAA0B,CAC9C,QAAQ,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,OAAO,GAAG,KAAK,GACtB,OAAO,CAAC,UAAU,CAAC,CAOrB"}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { FileStream } from "jazz-tools";
|
2
|
+
export type SharpImageType = File | Blob | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | Float32Array | Float64Array;
|
3
|
+
/**
|
4
|
+
* Creates an ImageDefinition from an image File, Blob or Buffer with built-in UX features.
|
5
|
+
*
|
6
|
+
* This function creates a specialized CoValue for managing images in Jazz applications.
|
7
|
+
* It supports blurry placeholders, built-in resizing, and progressive loading patterns.
|
8
|
+
*
|
9
|
+
* @returns Promise that resolves to an ImageDefinition
|
10
|
+
*
|
11
|
+
* @example
|
12
|
+
* ```ts
|
13
|
+
* import fs from "node:fs";
|
14
|
+
* import { createImage } from "jazz-tools/media";
|
15
|
+
*
|
16
|
+
* const imageBuffer = fs.readFileSync("path/to/image.jpg");
|
17
|
+
* const image = await createImage(imageBuffer, {
|
18
|
+
* maxSize: 800,
|
19
|
+
* placeholder: "blur",
|
20
|
+
* progressive: false,
|
21
|
+
* });
|
22
|
+
* ```
|
23
|
+
*/
|
24
|
+
export declare const createImage: (source: SharpImageType, options?: import("../create-image-factory").CreateImageOptions) => Promise<{
|
25
|
+
readonly [key: string]: FileStream;
|
26
|
+
} & {
|
27
|
+
readonly original: FileStream | null;
|
28
|
+
readonly originalSize: [number, number];
|
29
|
+
readonly placeholderDataURL: string | undefined;
|
30
|
+
readonly progressive: boolean;
|
31
|
+
} & {
|
32
|
+
readonly [key: string]: FileStream | null;
|
33
|
+
} & import("jazz-tools").CoMap>;
|
34
|
+
//# sourceMappingURL=server.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../src/media/create-image/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,UAAU,EAAS,MAAM,YAAY,CAAC;AAIxD,MAAM,MAAM,cAAc,GACtB,IAAI,GACJ,IAAI,GACJ,MAAM,GACN,WAAW,GACX,UAAU,GACV,iBAAiB,GACjB,SAAS,GACT,WAAW,GACX,UAAU,GACV,WAAW,GACX,UAAU,GACV,YAAY,GACZ,YAAY,CAAC;AAYjB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,WAAW;;;;;;;;;+BAYvB,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"server.test.d.ts","sourceRoot":"","sources":["../../../src/media/create-image/server.test.ts"],"names":[],"mappings":""}
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { Account, FileStream, Group } from "jazz-tools";
|
2
2
|
export type SourceType = Blob | File | string;
|
3
|
+
export type ResizeOutput = Blob | string;
|
3
4
|
export type CreateImageOptions = {
|
4
5
|
/** The owner of the image. Can be either a Group or Account. If not specified, the current user will be the owner. */
|
5
6
|
owner?: Group | Account;
|
@@ -26,16 +27,16 @@ export type CreateImageOptions = {
|
|
26
27
|
*/
|
27
28
|
progressive?: boolean;
|
28
29
|
};
|
29
|
-
export type CreateImageImpl = {
|
30
|
-
createFileStreamFromSource: (imageBlobOrFile:
|
31
|
-
getImageSize: (imageBlobOrFile:
|
30
|
+
export type CreateImageImpl<TSourceType = SourceType, TResizeOutput = ResizeOutput> = {
|
31
|
+
createFileStreamFromSource: (imageBlobOrFile: TSourceType | TResizeOutput, owner?: Group | Account) => Promise<FileStream>;
|
32
|
+
getImageSize: (imageBlobOrFile: TSourceType) => Promise<{
|
32
33
|
width: number;
|
33
34
|
height: number;
|
34
35
|
}>;
|
35
|
-
getPlaceholderBase64: (imageBlobOrFile:
|
36
|
-
resize: (imageBlobOrFile:
|
36
|
+
getPlaceholderBase64: (imageBlobOrFile: TSourceType) => Promise<string>;
|
37
|
+
resize: (imageBlobOrFile: TSourceType, width: number, height: number) => Promise<TResizeOutput>;
|
37
38
|
};
|
38
|
-
export declare function createImageFactory(impl: CreateImageImpl): (source:
|
39
|
+
export declare function createImageFactory<TSourceType, TResizeOutput>(impl: CreateImageImpl<TSourceType, TResizeOutput>, imageTypeGuard?: (imageBlobOrFile: TSourceType) => void): (source: TSourceType, options?: CreateImageOptions) => Promise<{
|
39
40
|
readonly [key: string]: FileStream;
|
40
41
|
} & {
|
41
42
|
readonly original: FileStream | null;
|
@@ -45,4 +46,4 @@ export declare function createImageFactory(impl: CreateImageImpl): (source: Sour
|
|
45
46
|
} & {
|
46
47
|
readonly [key: string]: FileStream | null;
|
47
48
|
} & import("jazz-tools").CoMap>;
|
48
|
-
//# sourceMappingURL=create-image.d.ts.map
|
49
|
+
//# sourceMappingURL=create-image-factory.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"create-image-factory.d.ts","sourceRoot":"","sources":["../../src/media/create-image-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,UAAU,EACV,KAAK,EAGN,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,UAAU,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":["../../src/media/create-image-factory.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"exports.d.ts","sourceRoot":"","sources":["../../src/media/exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC"}
|
@@ -1,15 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
export { highestResAvailable, loadImage, loadImageBySize } from "./utils.js";
|
4
|
-
export { createImageFactory };
|
5
|
-
export declare function createImage(imageBlobOrFile: Blob | File | string, options?: CreateImageOptions): Promise<{
|
6
|
-
readonly [key: string]: FileStream;
|
7
|
-
} & {
|
8
|
-
readonly original: FileStream | null;
|
9
|
-
readonly originalSize: [number, number];
|
10
|
-
readonly placeholderDataURL: string | undefined;
|
11
|
-
readonly progressive: boolean;
|
12
|
-
} & {
|
13
|
-
readonly [key: string]: FileStream | null;
|
14
|
-
} & import("jazz-tools").CoMap>;
|
1
|
+
export * from "./exports";
|
2
|
+
export { createImage } from "./create-image/browser";
|
15
3
|
//# sourceMappingURL=index.browser.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../../src/media/index.browser.ts"],"names":[],"mappings":"AAAA,
|
1
|
+
{"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../../src/media/index.browser.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC"}
|
@@ -3,24 +3,24 @@ import {
|
|
3
3
|
highestResAvailable,
|
4
4
|
loadImage,
|
5
5
|
loadImageBySize
|
6
|
-
} from "./chunk-
|
6
|
+
} from "./chunk-W3S526L3.js";
|
7
7
|
|
8
|
-
// src/media/
|
8
|
+
// src/media/create-image/browser.ts
|
9
9
|
import { FileStream } from "jazz-tools";
|
10
|
-
|
11
|
-
|
10
|
+
var createImage = createImageFactory(
|
11
|
+
{
|
12
12
|
createFileStreamFromSource,
|
13
13
|
getImageSize,
|
14
14
|
getPlaceholderBase64,
|
15
15
|
resize
|
16
|
-
}
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
"createFileStreamFromSource(string) is not supported on this platform"
|
22
|
-
);
|
16
|
+
},
|
17
|
+
(imageBlobOrFile) => {
|
18
|
+
if (typeof imageBlobOrFile === "string") {
|
19
|
+
throw new Error("createImage(string) is not supported on this platform");
|
20
|
+
}
|
23
21
|
}
|
22
|
+
);
|
23
|
+
async function createFileStreamFromSource(imageBlobOrFile, owner) {
|
24
24
|
return FileStream.createFromBlob(imageBlobOrFile, owner);
|
25
25
|
}
|
26
26
|
function getImageFromBlob(blob) {
|
@@ -38,16 +38,10 @@ function getImageFromBlob(blob) {
|
|
38
38
|
});
|
39
39
|
}
|
40
40
|
async function getImageSize(imageBlobOrFile) {
|
41
|
-
if (typeof imageBlobOrFile === "string") {
|
42
|
-
throw new Error("getImageSize(string) is not supported on browser");
|
43
|
-
}
|
44
41
|
const image = await getImageFromBlob(imageBlobOrFile);
|
45
42
|
return { width: image.width, height: image.height };
|
46
43
|
}
|
47
44
|
async function getPlaceholderBase64(imageBlobOrFile) {
|
48
|
-
if (typeof imageBlobOrFile === "string") {
|
49
|
-
throw new Error("getPlaceholderBase64(string) is not supported on browser");
|
50
|
-
}
|
51
45
|
const image = await getImageFromBlob(imageBlobOrFile);
|
52
46
|
const { width, height } = resizeDimensionsKeepingAspectRatio(
|
53
47
|
image.width,
|
@@ -76,9 +70,6 @@ var resizeDimensionsKeepingAspectRatio = (width, height, maxSize) => {
|
|
76
70
|
}
|
77
71
|
};
|
78
72
|
async function resize(imageBlobOrFile, width, height) {
|
79
|
-
if (typeof imageBlobOrFile === "string") {
|
80
|
-
throw new Error("resize(string) is not supported on browser");
|
81
|
-
}
|
82
73
|
const mimeType = imageBlobOrFile.type;
|
83
74
|
const image = await getImageFromBlob(imageBlobOrFile);
|
84
75
|
const canvas = document.createElement("canvas");
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/media/
|
1
|
+
{"version":3,"sources":["../../src/media/create-image/browser.ts"],"sourcesContent":["import { Account, FileStream, Group } from \"jazz-tools\";\nimport { createImageFactory } from \"../create-image-factory\";\n\n/**\n * Creates an ImageDefinition from an image File or Blob with built-in UX features.\n *\n * This function creates a specialized CoValue for managing images in Jazz applications.\n * It supports blurry placeholders, built-in resizing, and progressive loading patterns.\n *\n * @returns Promise that resolves to an ImageDefinition\n *\n * @example\n * ```ts\n * import { createImage } from \"jazz-tools/media\";\n *\n * // Create an image from a file input\n * async function handleFileUpload(event: React.ChangeEvent<HTMLInputElement>) {\n * const file = event.target.files?.[0];\n * if (file) {\n * // Creates ImageDefinition with a blurry placeholder, limited to 1024px\n * // on the longest side, and multiple resolutions automatically\n * const image = await createImage(file, {\n * owner: me._owner,\n * maxSize: 1024,\n * placeholder: \"blur\",\n * progressive: true,\n * });\n *\n * // Store the image in your application data\n * me.profile.image = image;\n * }\n * }\n * ```\n */\nexport const createImage = createImageFactory(\n {\n createFileStreamFromSource,\n getImageSize,\n getPlaceholderBase64,\n resize,\n },\n (imageBlobOrFile) => {\n if (typeof imageBlobOrFile === \"string\") {\n throw new Error(\"createImage(string) is not supported on this platform\");\n }\n },\n);\n\n// Image Manipulations\nasync function createFileStreamFromSource(\n imageBlobOrFile: Blob | File,\n owner?: Account | Group,\n): Promise<FileStream> {\n return FileStream.createFromBlob(imageBlobOrFile, owner);\n}\n\n// using createImageBitmap is ~10x slower than Image object\n// Image object: 640 milliseconds\n// createImageBitmap: 8128 milliseconds\nfunction getImageFromBlob(blob: Blob): Promise<HTMLImageElement> {\n return new Promise((resolve, reject) => {\n const img = new Image();\n img.onload = () => {\n resolve(img);\n URL.revokeObjectURL(img.src);\n };\n img.onerror = () => {\n reject(new Error(\"Failed to load image\"));\n URL.revokeObjectURL(img.src);\n };\n img.src = URL.createObjectURL(blob);\n });\n}\n\nasync function getImageSize(\n imageBlobOrFile: Blob | File,\n): Promise<{ width: number; height: number }> {\n const image = await getImageFromBlob(imageBlobOrFile);\n\n return { width: image.width, height: image.height };\n}\n\nasync function getPlaceholderBase64(\n imageBlobOrFile: Blob | File,\n): Promise<string> {\n const image = await getImageFromBlob(imageBlobOrFile);\n\n const { width, height } = resizeDimensionsKeepingAspectRatio(\n image.width,\n image.height,\n 8,\n );\n\n const canvas = document.createElement(\"canvas\");\n canvas.width = width;\n canvas.height = height;\n\n const ctx = canvas.getContext(\"2d\");\n\n if (!ctx) {\n throw new Error(\"Failed to get context\");\n }\n\n ctx.drawImage(image, 0, 0, width, height);\n\n return canvas.toDataURL(\"image/png\");\n}\n\nconst resizeDimensionsKeepingAspectRatio = (\n width: number,\n height: number,\n maxSize: number,\n): { width: number; height: number } => {\n if (width <= maxSize && height <= maxSize) {\n return { width, height };\n }\n\n const aspectRatio = width / height;\n\n if (width >= height) {\n return { width: maxSize, height: Math.round(maxSize / aspectRatio) };\n } else {\n return { width: Math.round(maxSize * aspectRatio), height: maxSize };\n }\n};\n\nasync function resize(\n imageBlobOrFile: Blob | File,\n width: number,\n height: number,\n): Promise<Blob> {\n const mimeType = imageBlobOrFile.type;\n\n const image = await getImageFromBlob(imageBlobOrFile);\n\n const canvas = document.createElement(\"canvas\");\n canvas.width = width;\n canvas.height = height;\n\n const ctx = canvas.getContext(\"2d\");\n\n if (!ctx) {\n throw new Error(\"Failed to get context\");\n }\n\n ctx.drawImage(image, 0, 0, width, height);\n\n return new Promise<Blob>((resolve, reject) => {\n canvas.toBlob(\n (blob) => {\n if (!blob) {\n reject(new Error(\"Failed to convert canvas to blob\"));\n return;\n }\n resolve(blob);\n },\n mimeType,\n 0.8,\n );\n });\n}\n"],"mappings":";;;;;;;;AAAA,SAAkB,kBAAyB;AAkCpC,IAAM,cAAc;AAAA,EACzB;AAAA,IACE;AAAA,IACA;AAAA,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AO;AAAA,EACzB;AAEA,QAAM,cAAc,QAAQ;AAE5B,MAAI,SAAS,QAAQ;AACnB,WAAO,EAAE,OAAO,SAAS,QAAQ,KAAK,MAAM,UAAU,WAAW,EAAE;AAAA,EACrE,OAAO;AACL,WAAO,EAAE,OAAO,KAAK,MAAM,UAAU,WAAW,GAAG,QAAQ,QAAQ;AAAA,EACrE;AACF;AAEA,eAAe,OACb,iBACA,OACA,QACe;AACf,QAAM,WAAW,gBAAgB;AAEjC,QAAM,QAAQ,MAAM,iBAAiB,eAAe;AAEpD,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,QAAQ;AACf,SAAO,SAAS;AAEhB,QAAM,MAAM,OAAO,WAAW,IAAI;AAElC,MAAI,CAAC,KAAK;AACR,UAAM,IAAI,MAAM,uBAAuB;AAAA,EACzC;AAEA,MAAI,UAAU,OAAO,GAAG,GAAG,OAAO,MAAM;AAExC,SAAO,IAAI,QAAc,CAAC,SAAS,WAAW;AAC5C,WAAO;AAAA,MACL,CAAC,SAAS;AACR,YAAI,CAAC,MAAM;AACT,iBAAO,IAAI,MAAM,kCAAkC,CAAC;AACpD;AAAA,QACF;AACA,gBAAQ,IAAI;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/dist/media/index.d.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import type { ImageDefinition } from "jazz-tools";
|
2
|
-
import { CreateImageOptions
|
3
|
-
export
|
4
|
-
export { createImageFactory };
|
2
|
+
import { CreateImageOptions } from "./create-image-factory";
|
3
|
+
export * from "./exports";
|
5
4
|
/**
|
6
5
|
* Creates an ImageDefinition from an image file or blob with built-in UX features.
|
7
6
|
*
|
@@ -32,7 +31,16 @@ export { createImageFactory };
|
|
32
31
|
* }
|
33
32
|
* }
|
34
33
|
* ```
|
34
|
+
* ```
|
35
|
+
*/
|
36
|
+
export declare function createImage(imageBlobOrFile: Blob | File, options?: CreateImageOptions): Promise<ImageDefinition>;
|
37
|
+
/**
|
38
|
+
* Creates an ImageDefinition from an image file path with built-in UX features.
|
35
39
|
*
|
40
|
+
* This function creates a specialized CoValue for managing images in Jazz applications.
|
41
|
+
* It supports blurry placeholders, built-in resizing, and progressive loading patterns.
|
42
|
+
*
|
43
|
+
* @returns Promise that resolves to an ImageDefinition
|
36
44
|
* @example
|
37
45
|
* ```ts
|
38
46
|
* // React Native example
|
@@ -49,5 +57,5 @@ export { createImageFactory };
|
|
49
57
|
* }
|
50
58
|
* ```
|
51
59
|
*/
|
52
|
-
export declare function createImage(
|
60
|
+
export declare function createImage(filePath: string, options?: CreateImageOptions): Promise<ImageDefinition>;
|
53
61
|
//# sourceMappingURL=index.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/media/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/media/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAE5D,cAAc,WAAW,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CACjC,eAAe,EAAE,IAAI,GAAG,IAAI,EAC5B,OAAO,CAAC,EAAE,kBAAkB,GAC3B,OAAO,CAAC,eAAe,CAAC,CAAC;AAE5B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CACjC,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,kBAAkB,GAC3B,OAAO,CAAC,eAAe,CAAC,CAAC"}
|