react-native-nitro-image-pipeline 1.5.0 → 1.7.0
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/README.md +142 -18
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/HybridNitroImagePipeline.kt +72 -16
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/PipelineImageLoader.kt +47 -8
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/transform/FitGeometry.kt +104 -0
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/transform/PipelineRoundedCornersTransformation.kt +103 -0
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/transform/ResizeTransformation.kt +69 -16
- package/ios/FitResizeProcessor.swift +127 -0
- package/ios/HybridNitroImagePipeline.swift +88 -24
- package/ios/PipelineImageLoader.swift +108 -18
- package/lib/commonjs/NativePipelineImage.js +20 -4
- package/lib/commonjs/NativePipelineImage.js.map +1 -1
- package/lib/commonjs/PipelineImage.js +13 -1
- package/lib/commonjs/PipelineImage.js.map +1 -1
- package/lib/commonjs/index.js +13 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/resizeForStyle.js +3 -1
- package/lib/commonjs/resizeForStyle.js.map +1 -1
- package/lib/commonjs/resolveImageSource.js +89 -0
- package/lib/commonjs/resolveImageSource.js.map +1 -0
- package/lib/commonjs/useImage.js +14 -6
- package/lib/commonjs/useImage.js.map +1 -1
- package/lib/commonjs/usePipelineImageLoader.js +114 -11
- package/lib/commonjs/usePipelineImageLoader.js.map +1 -1
- package/lib/module/NativePipelineImage.js +20 -4
- package/lib/module/NativePipelineImage.js.map +1 -1
- package/lib/module/PipelineImage.js +13 -1
- package/lib/module/PipelineImage.js.map +1 -1
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/resizeForStyle.js +3 -1
- package/lib/module/resizeForStyle.js.map +1 -1
- package/lib/module/resolveImageSource.js +83 -0
- package/lib/module/resolveImageSource.js.map +1 -0
- package/lib/module/useImage.js +14 -6
- package/lib/module/useImage.js.map +1 -1
- package/lib/module/usePipelineImageLoader.js +115 -12
- package/lib/module/usePipelineImageLoader.js.map +1 -1
- package/lib/typescript/src/NativePipelineImage.d.ts +51 -5
- package/lib/typescript/src/NativePipelineImage.d.ts.map +1 -1
- package/lib/typescript/src/PipelineImage.d.ts +35 -3
- package/lib/typescript/src/PipelineImage.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/resizeForStyle.d.ts +3 -1
- package/lib/typescript/src/resizeForStyle.d.ts.map +1 -1
- package/lib/typescript/src/resolveImageSource.d.ts +56 -0
- package/lib/typescript/src/resolveImageSource.d.ts.map +1 -0
- package/lib/typescript/src/specs/nitro-image-toolkit.nitro.d.ts +95 -8
- package/lib/typescript/src/specs/nitro-image-toolkit.nitro.d.ts.map +1 -1
- package/lib/typescript/src/useImage.d.ts +13 -6
- package/lib/typescript/src/useImage.d.ts.map +1 -1
- package/lib/typescript/src/usePipelineImageLoader.d.ts +7 -3
- package/lib/typescript/src/usePipelineImageLoader.d.ts.map +1 -1
- package/nitrogen/generated/android/NitroImagePipelineOnLoad.cpp +4 -0
- package/nitrogen/generated/android/c++/JFunc_void_double_double.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string.hpp +76 -0
- package/nitrogen/generated/android/c++/JHybridNitroImagePipelineSpec.cpp +8 -0
- package/nitrogen/generated/android/c++/JOptions.hpp +2 -0
- package/nitrogen/generated/android/c++/JResizeFit.hpp +64 -0
- package/nitrogen/generated/android/c++/JResizeOptions.hpp +14 -4
- package/nitrogen/generated/android/c++/JViewOptions.hpp +42 -3
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/Func_void_double_double.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/Func_void_std__string.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/ResizeFit.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/ResizeOptions.kt +14 -4
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/ViewOptions.kt +29 -5
- package/nitrogen/generated/ios/NitroImagePipeline-Swift-Cxx-Bridge.cpp +16 -0
- package/nitrogen/generated/ios/NitroImagePipeline-Swift-Cxx-Bridge.hpp +107 -0
- package/nitrogen/generated/ios/NitroImagePipeline-Swift-Cxx-Umbrella.hpp +4 -0
- package/nitrogen/generated/ios/c++/HybridNitroImagePipelineSpecSwift.hpp +4 -0
- package/nitrogen/generated/ios/swift/Func_void_double_double.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/ResizeFit.swift +48 -0
- package/nitrogen/generated/ios/swift/ResizeOptions.swift +31 -2
- package/nitrogen/generated/ios/swift/ViewOptions.swift +82 -1
- package/nitrogen/generated/shared/c++/ResizeFit.hpp +84 -0
- package/nitrogen/generated/shared/c++/ResizeOptions.hpp +14 -4
- package/nitrogen/generated/shared/c++/ViewOptions.hpp +24 -3
- package/package.json +1 -1
- package/src/NativePipelineImage.tsx +75 -6
- package/src/PipelineImage.tsx +46 -3
- package/src/index.ts +6 -0
- package/src/resizeForStyle.ts +3 -1
- package/src/resolveImageSource.ts +83 -0
- package/src/specs/nitro-image-toolkit.nitro.ts +96 -8
- package/src/useImage.ts +38 -17
- package/src/usePipelineImageLoader.ts +126 -11
|
@@ -6,8 +6,35 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.usePipelineImageLoader = usePipelineImageLoader;
|
|
7
7
|
var _react = require("react");
|
|
8
8
|
var _NitroImagePipeline = require("./NitroImagePipeline");
|
|
9
|
+
var _resolveImageSource = require("./resolveImageSource");
|
|
10
|
+
// Identifies a loader in the callbacks below without them capturing it. A
|
|
11
|
+
// callback the native loader owns that captured the loader back would be a
|
|
12
|
+
// cycle across the language boundary — neither side collectable, and the JS
|
|
13
|
+
// runtime left to take it apart at teardown.
|
|
14
|
+
let nextLoaderToken = 0;
|
|
9
15
|
/**
|
|
10
|
-
*
|
|
16
|
+
* The callbacks handed to a native loader. Built out here, where the scope
|
|
17
|
+
* holds nothing but the token and the two refs, so the loader can't end up
|
|
18
|
+
* captured by a callback it owns.
|
|
19
|
+
*/
|
|
20
|
+
function makeReporters(token, activeToken, callbacks) {
|
|
21
|
+
return {
|
|
22
|
+
onLoad: (width, height) => {
|
|
23
|
+
if (activeToken.current === token) {
|
|
24
|
+
callbacks.current.onLoad?.(width, height);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
onError: message => {
|
|
28
|
+
if (activeToken.current === token) {
|
|
29
|
+
callbacks.current.onError?.(message);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates (and memoizes) an {@linkcode ImageLoader} for `source` — a URL
|
|
37
|
+
* string or a `require()`d asset, see {@linkcode ImageSource} — to pass to
|
|
11
38
|
* `<NativeNitroImage image={...} />`. The view drives it entirely natively:
|
|
12
39
|
* the request starts when the view attaches — at the view's laid-out size,
|
|
13
40
|
* with no JS round trips — and is cancelled when it detaches. See
|
|
@@ -15,9 +42,11 @@ var _NitroImagePipeline = require("./NitroImagePipeline");
|
|
|
15
42
|
*
|
|
16
43
|
* `blur`/`cornerRadius` are in **points** (unlike `useImage`, where they are
|
|
17
44
|
* bitmap pixels); the screen scale is applied natively. Inline object
|
|
18
|
-
* literals are fine — options are compared by value, not identity
|
|
45
|
+
* literals are fine — options are compared by value, not identity, and the
|
|
46
|
+
* `onLoad`/`onError` callbacks are read through a ref, so inline arrows don't
|
|
47
|
+
* recreate the loader either.
|
|
19
48
|
*/
|
|
20
|
-
function usePipelineImageLoader(
|
|
49
|
+
function usePipelineImageLoader(source, options) {
|
|
21
50
|
// Split the options into primitives (like useImage does) so an inline
|
|
22
51
|
// literal — a new identity every render — doesn't recreate the loader;
|
|
23
52
|
// recreating it would re-trigger the native load.
|
|
@@ -35,35 +64,109 @@ function usePipelineImageLoader(url, options) {
|
|
|
35
64
|
} = isUniformRadius || cornerRadius === undefined ? {} : cornerRadius;
|
|
36
65
|
const resizeWidth = options?.resize?.width;
|
|
37
66
|
const resizeHeight = options?.resize?.height;
|
|
38
|
-
|
|
67
|
+
const resizeFit = options?.resize?.fit;
|
|
68
|
+
const resizeAllowUpscale = options?.resize?.allowUpscale;
|
|
69
|
+
const fit = options?.fit;
|
|
70
|
+
const allowUpscale = options?.allowUpscale;
|
|
71
|
+
const onLoad = options?.onLoad;
|
|
72
|
+
const onError = options?.onError;
|
|
73
|
+
// Only *whether* a callback is set can change the loader: what the native
|
|
74
|
+
// side gets is a wrapper that reads the latest callbacks from a ref (like
|
|
75
|
+
// PipelineImage does). So an inline arrow — a new identity every render —
|
|
76
|
+
// doesn't recreate the loader and re-trigger the native load.
|
|
77
|
+
const hasOnLoad = onLoad !== undefined;
|
|
78
|
+
const hasOnError = onError !== undefined;
|
|
79
|
+
const callbacks = (0, _react.useRef)({
|
|
80
|
+
onLoad,
|
|
81
|
+
onError
|
|
82
|
+
});
|
|
83
|
+
// A layout effect, not a passive one: passive effects are flushed in a later
|
|
84
|
+
// task, so a load reported between the commit and that flush would read the
|
|
85
|
+
// previous render's callbacks — a callback added alongside a new loader
|
|
86
|
+
// could miss its only event. This runs in the same task as the commit, and
|
|
87
|
+
// the view can't reach the loader before the commit installs it, so the ref
|
|
88
|
+
// is always current by the time native calls back. (Assigning it during
|
|
89
|
+
// render instead would mutate on renders React throws away.)
|
|
90
|
+
(0, _react.useLayoutEffect)(() => {
|
|
91
|
+
callbacks.current = {
|
|
92
|
+
onLoad,
|
|
93
|
+
onError
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
// The token of the loader the view is currently on. Swapping loaders (a
|
|
97
|
+
// changed url or options) never drops the old one — the view just stops
|
|
98
|
+
// referring to it — so a load it already had in flight can still report.
|
|
99
|
+
// Without this check the wrappers, which read the latest callbacks, would
|
|
100
|
+
// hand the previous image's size to the current `onLoad`.
|
|
101
|
+
const activeToken = (0, _react.useRef)(-1);
|
|
102
|
+
const memoized = (0, _react.useMemo)(() => {
|
|
103
|
+
// Runs during render, so an unregistered `require()` id must not throw:
|
|
104
|
+
// it becomes a URL the native loader fails on at load time instead.
|
|
105
|
+
const url = (0, _resolveImageSource.resolveImageUrlOrFallback)(source);
|
|
39
106
|
const cornerRadiusOption = isUniformRadius ? uniformRadius : hasCornerObject ? {
|
|
40
107
|
topLeft,
|
|
41
108
|
topRight,
|
|
42
109
|
bottomLeft,
|
|
43
110
|
bottomRight
|
|
44
111
|
} : undefined;
|
|
112
|
+
// What is loaded — the callbacks are deliberately not part of it, so two
|
|
113
|
+
// loaders that differ only in them are the same image.
|
|
45
114
|
const stableOptions = {
|
|
46
115
|
blur,
|
|
47
116
|
cache,
|
|
48
117
|
cornerRadius: cornerRadiusOption,
|
|
49
118
|
resize: resizeWidth !== undefined && resizeHeight !== undefined ? {
|
|
50
119
|
width: resizeWidth,
|
|
51
|
-
height: resizeHeight
|
|
52
|
-
|
|
120
|
+
height: resizeHeight,
|
|
121
|
+
fit: resizeFit,
|
|
122
|
+
allowUpscale: resizeAllowUpscale
|
|
123
|
+
} : undefined,
|
|
124
|
+
fit,
|
|
125
|
+
allowUpscale
|
|
53
126
|
};
|
|
54
|
-
const
|
|
127
|
+
const token = nextLoaderToken++;
|
|
128
|
+
// The reporters read the refs, but only when the native side calls them —
|
|
129
|
+
// the rule can't see that they aren't called here.
|
|
130
|
+
// oxlint-disable-next-line react/refs
|
|
131
|
+
const reporters = makeReporters(token, activeToken, callbacks);
|
|
132
|
+
const loader = _NitroImagePipeline.NitroImagePipeline.createImageLoader(url, {
|
|
133
|
+
...stableOptions,
|
|
134
|
+
// Passed only when the caller wants them: without a callback nothing
|
|
135
|
+
// ever crosses into JS, which is the point of the native path.
|
|
136
|
+
onLoad: hasOnLoad ? reporters.onLoad : undefined,
|
|
137
|
+
onError: hasOnError ? reporters.onError : undefined
|
|
138
|
+
});
|
|
55
139
|
// `NativeNitroImage` needs a way to tell two loader instances apart when
|
|
56
140
|
// diffing its `image` prop; tag the loader with what it will load (the
|
|
57
|
-
// same convention react-native-nitro-image's own loaders use).
|
|
141
|
+
// same convention react-native-nitro-image's own loaders use). Toggling a
|
|
142
|
+
// callback on or off makes a different loader, so the tag has to say so —
|
|
143
|
+
// otherwise the view keeps the old one installed. Their identities stay
|
|
144
|
+
// out of it: those don't change what is loaded, or which loader this is.
|
|
58
145
|
Object.defineProperty(loader, '__source', {
|
|
59
146
|
enumerable: true,
|
|
60
147
|
configurable: true,
|
|
61
148
|
value: {
|
|
62
149
|
url,
|
|
63
|
-
options: stableOptions
|
|
150
|
+
options: stableOptions,
|
|
151
|
+
callbacks: {
|
|
152
|
+
onLoad: hasOnLoad,
|
|
153
|
+
onError: hasOnError
|
|
154
|
+
}
|
|
64
155
|
}
|
|
65
156
|
});
|
|
66
|
-
return
|
|
67
|
-
|
|
157
|
+
return {
|
|
158
|
+
loader,
|
|
159
|
+
token
|
|
160
|
+
};
|
|
161
|
+
}, [source, blur, cache, isUniformRadius, uniformRadius, hasCornerObject, topLeft, topRight, bottomLeft, bottomRight, resizeWidth, resizeHeight, resizeFit, resizeAllowUpscale, fit, allowUpscale, hasOnLoad, hasOnError,
|
|
162
|
+
// Stable ref objects; listed to satisfy the exhaustive-deps rules.
|
|
163
|
+
activeToken, callbacks]);
|
|
164
|
+
|
|
165
|
+
// Same reasoning as the callbacks ref: a layout effect is current before the
|
|
166
|
+
// view — which the commit has only just handed this loader — can report.
|
|
167
|
+
(0, _react.useLayoutEffect)(() => {
|
|
168
|
+
activeToken.current = memoized.token;
|
|
169
|
+
}, [memoized, activeToken]);
|
|
170
|
+
return memoized.loader;
|
|
68
171
|
}
|
|
69
172
|
//# sourceMappingURL=usePipelineImageLoader.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","_NitroImagePipeline","usePipelineImageLoader","
|
|
1
|
+
{"version":3,"names":["_react","require","_NitroImagePipeline","_resolveImageSource","nextLoaderToken","makeReporters","token","activeToken","callbacks","onLoad","width","height","current","onError","message","usePipelineImageLoader","source","options","blur","cache","cornerRadius","isUniformRadius","uniformRadius","hasCornerObject","undefined","topLeft","topRight","bottomLeft","bottomRight","resizeWidth","resize","resizeHeight","resizeFit","fit","resizeAllowUpscale","allowUpscale","hasOnLoad","hasOnError","useRef","useLayoutEffect","memoized","useMemo","url","resolveImageUrlOrFallback","cornerRadiusOption","stableOptions","reporters","loader","NitroImagePipeline","createImageLoader","Object","defineProperty","enumerable","configurable","value"],"sourceRoot":"../../src","sources":["usePipelineImageLoader.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAGA,IAAAC,mBAAA,GAAAD,OAAA;AACA,IAAAE,mBAAA,GAAAF,OAAA;AASA;AACA;AACA;AACA;AACA,IAAIG,eAAe,GAAG,CAAC;AAIvB;AACA;AACA;AACA;AACA;AACA,SAASC,aAAaA,CACpBC,KAAa,EACbC,WAA8B,EAC9BC,SAA+B,EACV;EACrB,OAAO;IACLC,MAAM,EAAEA,CAACC,KAAK,EAAEC,MAAM,KAAK;MACzB,IAAIJ,WAAW,CAACK,OAAO,KAAKN,KAAK,EAAE;QACjCE,SAAS,CAACI,OAAO,CAACH,MAAM,GAAGC,KAAK,EAAEC,MAAM,CAAC;MAC3C;IACF,CAAC;IACDE,OAAO,EAAGC,OAAO,IAAK;MACpB,IAAIP,WAAW,CAACK,OAAO,KAAKN,KAAK,EAAE;QACjCE,SAAS,CAACI,OAAO,CAACC,OAAO,GAAGC,OAAO,CAAC;MACtC;IACF;EACF,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,sBAAsBA,CACpCC,MAAmB,EACnBC,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,EAAE,IAAI;MAChBC,YAAY,EAAE,IAAI;MAClBC,KAAK,EAAE;QACLZ,GAAG;QACHzB,OAAO,EAAE4B,aAAa;QACtBrC,SAAS,EAAE;UAAEC,MAAM,EAAE2B,SAAS;UAAEvB,OAAO,EAAEwB;QAAW;MACtD;IACF,CAAC,CAAC;IACF,OAAO;MAAEU,MAAM;MAAEzC;IAAM,CAAC;EAC1B,CAAC,EAAE,CACDU,MAAM,EACNE,IAAI,EACJC,KAAK,EACLE,eAAe,EACfC,aAAa,EACbC,eAAe,EACfE,OAAO,EACPC,QAAQ,EACRC,UAAU,EACVC,WAAW,EACXC,WAAW,EACXE,YAAY,EACZC,SAAS,EACTE,kBAAkB,EAClBD,GAAG,EACHE,YAAY,EACZC,SAAS,EACTC,UAAU;EACV;EACA9B,WAAW,EACXC,SAAS,CACV,CAAC;;EAEF;EACA;EACA,IAAA+B,sBAAe,EAAC,MAAM;IACpBhC,WAAW,CAACK,OAAO,GAAG4B,QAAQ,CAAClC,KAAK;EACtC,CAAC,EAAE,CAACkC,QAAQ,EAAEjC,WAAW,CAAC,CAAC;EAE3B,OAAOiC,QAAQ,CAACO,MAAM;AACxB","ignoreList":[]}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { forwardRef } from 'react';
|
|
4
4
|
import { NativeNitroImage } from 'react-native-nitro-image';
|
|
5
5
|
import { cornerRadiusForStyle } from './resizeForStyle';
|
|
6
|
+
import { recyclingKeyFor } from './resolveImageSource';
|
|
6
7
|
import { usePipelineImageLoader } from './usePipelineImageLoader';
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -20,8 +21,9 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
20
21
|
* the same pipeline and caches as `useImage`/`preLoadImage`.
|
|
21
22
|
*
|
|
22
23
|
* Compared to `PipelineImage`:
|
|
23
|
-
* -
|
|
24
|
-
* JS
|
|
24
|
+
* - `onLoad` reports the bitmap's pixel size rather than the `Image` itself,
|
|
25
|
+
* which never crosses into JS, and can fire more than once for a view. Use
|
|
26
|
+
* `PipelineImage` (or `useImage`) when you need the `Image`.
|
|
25
27
|
* - The bitmap is loaded once at the size the view first has; if the view is
|
|
26
28
|
* resized later, the bitmap scales with it instead of reloading.
|
|
27
29
|
* @example
|
|
@@ -39,24 +41,38 @@ export const NativePipelineImage = /*#__PURE__*/forwardRef(function NativePipeli
|
|
|
39
41
|
cornerRadius,
|
|
40
42
|
cache,
|
|
41
43
|
resize,
|
|
44
|
+
fit,
|
|
45
|
+
allowUpscale,
|
|
46
|
+
onLoad,
|
|
47
|
+
onError,
|
|
42
48
|
style,
|
|
49
|
+
resizeMode,
|
|
43
50
|
...viewProps
|
|
44
51
|
}, ref) {
|
|
45
52
|
// Same precedence as PipelineImage: an explicit prop wins over style.
|
|
46
53
|
const effectiveCornerRadius = cornerRadius ?? cornerRadiusForStyle(style);
|
|
54
|
+
// Inline arrows are fine here — the hook keeps the callbacks in a ref, so a
|
|
55
|
+
// new identity doesn't recreate the loader (which would reload the image).
|
|
47
56
|
const loader = usePipelineImageLoader(url, {
|
|
48
57
|
blur,
|
|
49
58
|
cornerRadius: effectiveCornerRadius,
|
|
50
59
|
cache,
|
|
51
|
-
resize
|
|
60
|
+
resize,
|
|
61
|
+
// Same four values as the view's resizeMode; an explicit `fit` decouples
|
|
62
|
+
// them.
|
|
63
|
+
fit: fit ?? resizeMode,
|
|
64
|
+
allowUpscale,
|
|
65
|
+
onLoad,
|
|
66
|
+
onError
|
|
52
67
|
});
|
|
53
68
|
return /*#__PURE__*/_jsx(NativeNitroImage
|
|
54
69
|
// Before the spread so a caller-provided recyclingKey wins.
|
|
55
70
|
, {
|
|
56
|
-
recyclingKey: url,
|
|
71
|
+
recyclingKey: recyclingKeyFor(url),
|
|
57
72
|
...viewProps,
|
|
58
73
|
ref: ref,
|
|
59
74
|
style: style,
|
|
75
|
+
resizeMode: resizeMode,
|
|
60
76
|
image: loader
|
|
61
77
|
});
|
|
62
78
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["forwardRef","NativeNitroImage","cornerRadiusForStyle","usePipelineImageLoader","jsx","_jsx","NativePipelineImage","url","blur","cornerRadius","cache","resize","style","viewProps","ref","effectiveCornerRadius","loader","recyclingKey","image","displayName"],"sourceRoot":"../../src","sources":["NativePipelineImage.tsx"],"mappings":";;AAAA,SAA4BA,UAAU,QAAQ,OAAO;AAErD,SAASC,gBAAgB,QAAQ,0BAA0B;AAE3D,SAASC,oBAAoB,QAAQ,kBAAkB;
|
|
1
|
+
{"version":3,"names":["forwardRef","NativeNitroImage","cornerRadiusForStyle","recyclingKeyFor","usePipelineImageLoader","jsx","_jsx","NativePipelineImage","url","blur","cornerRadius","cache","resize","fit","allowUpscale","onLoad","onError","style","resizeMode","viewProps","ref","effectiveCornerRadius","loader","recyclingKey","image","displayName"],"sourceRoot":"../../src","sources":["NativePipelineImage.tsx"],"mappings":";;AAAA,SAA4BA,UAAU,QAAQ,OAAO;AAErD,SAASC,gBAAgB,QAAQ,0BAA0B;AAE3D,SAASC,oBAAoB,QAAQ,kBAAkB;AACvD,SAA2BC,eAAe,QAAQ,sBAAsB;AAOxE,SAASC,sBAAsB,QAAQ,0BAA0B;;AAKjE;AACA;AACA;AACA;AACA;AAJA,SAAAC,GAAA,IAAAC,IAAA;AA+EA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,mBAAmB,gBAAGP,UAAU,CAG3C,SAASO,mBAAmBA,CAC5B;EACEC,GAAG;EACHC,IAAI;EACJC,YAAY;EACZC,KAAK;EACLC,MAAM;EACNC,GAAG;EACHC,YAAY;EACZC,MAAM;EACNC,OAAO;EACPC,KAAK;EACLC,UAAU;EACV,GAAGC;AACL,CAAC,EACDC,GAAG,EACH;EACA;EACA,MAAMC,qBAAqB,GAAGX,YAAY,IAAIR,oBAAoB,CAACe,KAAK,CAAC;EACzE;EACA;EACA,MAAMK,MAAM,GAAGlB,sBAAsB,CAACI,GAAG,EAAE;IACzCC,IAAI;IACJC,YAAY,EAAEW,qBAAqB;IACnCV,KAAK;IACLC,MAAM;IACN;IACA;IACAC,GAAG,EAAEA,GAAG,IAAIK,UAAU;IACtBJ,YAAY;IACZC,MAAM;IACNC;EACF,CAAC,CAAC;EAEF,oBACEV,IAAA,CAACL;EACC;EAAA;IACAsB,YAAY,EAAEpB,eAAe,CAACK,GAAG,CAAE;IAAA,GAC/BW,SAAS;IACbC,GAAG,EAAEA,GAAI;IACTH,KAAK,EAAEA,KAAM;IACbC,UAAU,EAAEA,UAAW;IACvBM,KAAK,EAAEF;EAAO,CACf,CAAC;AAEN,CAAC,CAAC;;AAEF;AACA;AACAf,mBAAmB,CAACkB,WAAW,GAAG,qBAAqB","ignoreList":[]}
|
|
@@ -57,10 +57,13 @@ export const PipelineImage = /*#__PURE__*/forwardRef(function PipelineImage({
|
|
|
57
57
|
blur = 0,
|
|
58
58
|
cornerRadius,
|
|
59
59
|
cache,
|
|
60
|
+
fit,
|
|
61
|
+
allowUpscale,
|
|
60
62
|
onLoad,
|
|
61
63
|
onError,
|
|
62
64
|
style,
|
|
63
65
|
onLayout,
|
|
66
|
+
resizeMode,
|
|
64
67
|
...viewProps
|
|
65
68
|
}, ref) {
|
|
66
69
|
const scale = PixelRatio.get();
|
|
@@ -68,7 +71,15 @@ export const PipelineImage = /*#__PURE__*/forwardRef(function PipelineImage({
|
|
|
68
71
|
const [layoutSize, setLayoutSize] = useState(undefined);
|
|
69
72
|
// A numeric style is what the caller declared, so it wins and starts the
|
|
70
73
|
// request a frame earlier; the measured layout is the fallback.
|
|
71
|
-
const
|
|
74
|
+
const size = styleSize ?? layoutSize;
|
|
75
|
+
// The view's resizeMode and the bitmap's fit are the same four values;
|
|
76
|
+
// an explicit `fit` decouples them.
|
|
77
|
+
const effectiveFit = fit ?? resizeMode;
|
|
78
|
+
const resize = size ? {
|
|
79
|
+
...size,
|
|
80
|
+
fit: effectiveFit,
|
|
81
|
+
allowUpscale
|
|
82
|
+
} : undefined;
|
|
72
83
|
// Same precedence: an explicit prop wins over what style implies.
|
|
73
84
|
const effectiveCornerRadius = cornerRadius ?? cornerRadiusForStyle(style) ?? 0;
|
|
74
85
|
const {
|
|
@@ -111,6 +122,7 @@ export const PipelineImage = /*#__PURE__*/forwardRef(function PipelineImage({
|
|
|
111
122
|
...viewProps,
|
|
112
123
|
ref: ref,
|
|
113
124
|
style: style,
|
|
125
|
+
resizeMode: resizeMode,
|
|
114
126
|
onLayout: handleLayout,
|
|
115
127
|
image: image
|
|
116
128
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["forwardRef","useEffect","useRef","useState","PixelRatio","NativeNitroImage","cornerRadiusForStyle","resizeForLayout","resizeForStyle","useImage","jsx","_jsx","sameSize","a","b","width","height","scaleRadius","cornerRadius","scale","topLeft","topRight","bottomLeft","bottomRight","PipelineImage","url","blur","cache","onLoad","onError","style","onLayout","viewProps","ref","get","styleSize","layoutSize","setLayoutSize","undefined","resize","effectiveCornerRadius","image","error","enabled","onLoadRef","onErrorRef","current","handleLayout","event","nativeEvent","layout","next","prev","displayName"],"sourceRoot":"../../src","sources":["PipelineImage.tsx"],"mappings":";;AAAA,SAEEA,UAAU,EACVC,SAAS,EACTC,MAAM,EACNC,QAAQ,QACH,OAAO;AACd,SAGEC,UAAU,QACL,cAAc;AACrB,SAAqBC,gBAAgB,QAAQ,0BAA0B;AAEvE,SACEC,oBAAoB,EACpBC,eAAe,EACfC,cAAc,QACT,kBAAkB;
|
|
1
|
+
{"version":3,"names":["forwardRef","useEffect","useRef","useState","PixelRatio","NativeNitroImage","cornerRadiusForStyle","resizeForLayout","resizeForStyle","useImage","jsx","_jsx","sameSize","a","b","width","height","scaleRadius","cornerRadius","scale","topLeft","topRight","bottomLeft","bottomRight","PipelineImage","url","blur","cache","fit","allowUpscale","onLoad","onError","style","onLayout","resizeMode","viewProps","ref","get","styleSize","layoutSize","setLayoutSize","undefined","size","effectiveFit","resize","effectiveCornerRadius","image","error","enabled","onLoadRef","onErrorRef","current","handleLayout","event","nativeEvent","layout","next","prev","displayName"],"sourceRoot":"../../src","sources":["PipelineImage.tsx"],"mappings":";;AAAA,SAEEA,UAAU,EACVC,SAAS,EACTC,MAAM,EACNC,QAAQ,QACH,OAAO;AACd,SAGEC,UAAU,QACL,cAAc;AACrB,SAAqBC,gBAAgB,QAAQ,0BAA0B;AAEvE,SACEC,oBAAoB,EACpBC,eAAe,EACfC,cAAc,QACT,kBAAkB;AAQzB,SAASC,QAAQ,QAAQ,YAAY;;AAKrC;AACA;AACA;AACA;AACA;AAJA,SAAAC,GAAA,IAAAC,IAAA;AAmEA,SAASC,QAAQA,CAACC,CAAiB,EAAEC,CAAiB,EAAW;EAC/D,OAAOD,CAAC,EAAEE,KAAK,KAAKD,CAAC,EAAEC,KAAK,IAAIF,CAAC,EAAEG,MAAM,KAAKF,CAAC,EAAEE,MAAM;AACzD;AAEA,SAASC,WAAWA,CAClBC,YAAkC,EAClCC,KAAa,EACS;EACtB,IAAI,OAAOD,YAAY,KAAK,QAAQ,EAAE;IACpC,OAAOA,YAAY,GAAGC,KAAK;EAC7B;EACA,OAAO;IACLC,OAAO,EAAE,CAACF,YAAY,CAACE,OAAO,IAAI,CAAC,IAAID,KAAK;IAC5CE,QAAQ,EAAE,CAACH,YAAY,CAACG,QAAQ,IAAI,CAAC,IAAIF,KAAK;IAC9CG,UAAU,EAAE,CAACJ,YAAY,CAACI,UAAU,IAAI,CAAC,IAAIH,KAAK;IAClDI,WAAW,EAAE,CAACL,YAAY,CAACK,WAAW,IAAI,CAAC,IAAIJ;EACjD,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMK,aAAa,gBAAGxB,UAAU,CACrC,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AS,CAAC,MAAM;IACd,IAAI6C,KAAK,EAAEG,SAAS,CAACE,OAAO,GAAGL,KAAK,CAAC;EACvC,CAAC,EAAE,CAACA,KAAK,CAAC,CAAC;EACX7C,SAAS,CAAC,MAAM;IACd,IAAI8C,KAAK,EAAEG,UAAU,CAACC,OAAO,GAAGJ,KAAK,CAAC;EACxC,CAAC,EAAE,CAACA,KAAK,CAAC,CAAC;EAEX,MAAMK,YAAY,GAAIC,KAAwB,IAAK;IACjDpB,QAAQ,GAAGoB,KAAK,CAAC;IACjB,MAAM;MAAEtC,KAAK;MAAEC;IAAO,CAAC,GAAGqC,KAAK,CAACC,WAAW,CAACC,MAAM;IAClD,MAAMC,IAAI,GAAGjD,eAAe,CAACQ,KAAK,EAAEC,MAAM,CAAC;IAC3C;IACA;IACAwB,aAAa,CAAEiB,IAAI,IAAM7C,QAAQ,CAAC6C,IAAI,EAAED,IAAI,CAAC,GAAGC,IAAI,GAAGD,IAAK,CAAC;EAC/D,CAAC;EAED,oBACE7C,IAAA,CAACN,gBAAgB;IAAA,GACX8B,SAAS;IACbC,GAAG,EAAEA,GAAI;IACTJ,KAAK,EAAEA,KAAM;IACbE,UAAU,EAAEA,UAAW;IACvBD,QAAQ,EAAEmB,YAAa;IACvBN,KAAK,EAAEA;EAAM,CACd,CAAC;AAEN,CACF,CAAC;;AAED;AACA;AACAtB,aAAa,CAACkC,WAAW,GAAG,eAAe","ignoreList":[]}
|
package/lib/module/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
export { NativePipelineImage } from './NativePipelineImage';
|
|
4
4
|
export { NitroImagePipeline } from './NitroImagePipeline';
|
|
5
5
|
export { PipelineImage } from './PipelineImage';
|
|
6
|
+
export { resolveImageUrl, UNREGISTERED_ASSET_URL } from './resolveImageSource';
|
|
6
7
|
export { cornerRadiusForStyle, resizeForLayout, resizeForStyle } from './resizeForStyle';
|
|
7
8
|
export { useImage } from './useImage';
|
|
8
9
|
export { usePipelineImageLoader } from './usePipelineImageLoader';
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NativePipelineImage","NitroImagePipeline","PipelineImage","cornerRadiusForStyle","resizeForLayout","resizeForStyle","useImage","usePipelineImageLoader"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA,SACEA,mBAAmB,QAGd,uBAAuB;AAC9B,SAASC,kBAAkB,QAAQ,sBAAsB;AACzD,SACEC,aAAa,QAGR,iBAAiB;AACxB,SACEC,oBAAoB,EACpBC,eAAe,EACfC,cAAc,QACT,kBAAkB;
|
|
1
|
+
{"version":3,"names":["NativePipelineImage","NitroImagePipeline","PipelineImage","resolveImageUrl","UNREGISTERED_ASSET_URL","cornerRadiusForStyle","resizeForLayout","resizeForStyle","useImage","usePipelineImageLoader"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA,SACEA,mBAAmB,QAGd,uBAAuB;AAC9B,SAASC,kBAAkB,QAAQ,sBAAsB;AACzD,SACEC,aAAa,QAGR,iBAAiB;AACxB,SAEEC,eAAe,EACfC,sBAAsB,QACjB,sBAAsB;AAC7B,SACEC,oBAAoB,EACpBC,eAAe,EACfC,cAAc,QACT,kBAAkB;AASzB,SAASC,QAAQ,QAAQ,YAAY;AACrC,SAASC,sBAAsB,QAAQ,0BAA0B","ignoreList":[]}
|
|
@@ -4,7 +4,9 @@ import { PixelRatio, StyleSheet } from 'react-native';
|
|
|
4
4
|
/**
|
|
5
5
|
* Converts a layout size in points (dp) to the pipeline's `resize` option in
|
|
6
6
|
* whole bitmap pixels using `PixelRatio.getPixelSizeForLayoutSize`. Returns
|
|
7
|
-
* `undefined` unless both values are positive numbers.
|
|
7
|
+
* `undefined` unless both values are positive numbers. Spread a `fit` into
|
|
8
|
+
* the result for the other resize modes — under `contain` and `center` the
|
|
9
|
+
* size is the box the bitmap fits in, not necessarily its final size.
|
|
8
10
|
*/
|
|
9
11
|
export function resizeForLayout(width, height) {
|
|
10
12
|
if (typeof width !== 'number' || typeof height !== 'number') {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["PixelRatio","StyleSheet","resizeForLayout","width","height","undefined","pixelWidth","getPixelSizeForLayoutSize","pixelHeight","resizeForStyle","style","flat","flatten","cornerRadiusForStyle","base","borderRadius","topLeft","borderTopLeftRadius","topRight","borderTopRightRadius","bottomLeft","borderBottomLeftRadius","bottomRight","borderBottomRightRadius"],"sourceRoot":"../../src","sources":["resizeForStyle.ts"],"mappings":";;AAAA,SACEA,UAAU,EAEVC,UAAU,QAEL,cAAc;AAOrB;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAC7BC,KAAc,EACdC,MAAe,EACY;EAC3B,IAAI,OAAOD,KAAK,KAAK,QAAQ,IAAI,OAAOC,MAAM,KAAK,QAAQ,EAAE;IAC3D,OAAOC,SAAS;EAClB;EACA,MAAMC,UAAU,GAAGN,UAAU,CAACO,yBAAyB,CAACJ,KAAK,CAAC;EAC9D,MAAMK,WAAW,GAAGR,UAAU,CAACO,yBAAyB,CAACH,MAAM,CAAC;EAChE,OAAOE,UAAU,GAAG,CAAC,IAAIE,WAAW,GAAG,CAAC,GACpC;IAAEL,KAAK,EAAEG,UAAU;IAAEF,MAAM,EAAEI;EAAY,CAAC,GAC1CH,SAAS;AACf;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASI,cAAcA,CAC5BC,KAA2B,EACA;EAC3B,MAAMC,IAAI,GAAGV,UAAU,CAACW,OAAO,CAACF,KAAK,CAAC;EACtC,OAAOR,eAAe,CAACS,IAAI,EAAER,KAAK,EAAEQ,IAAI,EAAEP,MAAM,CAAC;AACnD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASS,oBAAoBA,CAClCH,KAA2B,EACO;EAClC,MAAMC,IAAI,GAAGV,UAAU,CAACW,OAAO,CAACF,KAAK,CAAC;EACtC,MAAMI,IAAI,GACR,OAAOH,IAAI,EAAEI,YAAY,KAAK,QAAQ,GAAGJ,IAAI,CAACI,YAAY,GAAGV,SAAS;EACxE,MAAMW,OAAO,GACX,OAAOL,IAAI,EAAEM,mBAAmB,KAAK,QAAQ,GACzCN,IAAI,CAACM,mBAAmB,GACxBZ,SAAS;EACf,MAAMa,QAAQ,GACZ,OAAOP,IAAI,EAAEQ,oBAAoB,KAAK,QAAQ,GAC1CR,IAAI,CAACQ,oBAAoB,GACzBd,SAAS;EACf,MAAMe,UAAU,GACd,OAAOT,IAAI,EAAEU,sBAAsB,KAAK,QAAQ,GAC5CV,IAAI,CAACU,sBAAsB,GAC3BhB,SAAS;EACf,MAAMiB,WAAW,GACf,OAAOX,IAAI,EAAEY,uBAAuB,KAAK,QAAQ,GAC7CZ,IAAI,CAACY,uBAAuB,GAC5BlB,SAAS;EAEf,IACEW,OAAO,KAAKX,SAAS,IACrBa,QAAQ,KAAKb,SAAS,IACtBe,UAAU,KAAKf,SAAS,IACxBiB,WAAW,KAAKjB,SAAS,EACzB;IACA,OAAOS,IAAI;EACb;EAEA,OAAO;IACLE,OAAO,EAAEA,OAAO,IAAIF,IAAI;IACxBI,QAAQ,EAAEA,QAAQ,IAAIJ,IAAI;IAC1BM,UAAU,EAAEA,UAAU,IAAIN,IAAI;IAC9BQ,WAAW,EAAEA,WAAW,IAAIR;EAC9B,CAAC;AACH","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["PixelRatio","StyleSheet","resizeForLayout","width","height","undefined","pixelWidth","getPixelSizeForLayoutSize","pixelHeight","resizeForStyle","style","flat","flatten","cornerRadiusForStyle","base","borderRadius","topLeft","borderTopLeftRadius","topRight","borderTopRightRadius","bottomLeft","borderBottomLeftRadius","bottomRight","borderBottomRightRadius"],"sourceRoot":"../../src","sources":["resizeForStyle.ts"],"mappings":";;AAAA,SACEA,UAAU,EAEVC,UAAU,QAEL,cAAc;AAOrB;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,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AAS;EACf,MAAMiB,WAAW,GACf,OAAOX,IAAI,EAAEY,uBAAuB,KAAK,QAAQ,GAC7CZ,IAAI,CAACY,uBAAuB,GAC5BlB,SAAS;EAEf,IACEW,OAAO,KAAKX,SAAS,IACrBa,QAAQ,KAAKb,SAAS,IACtBe,UAAU,KAAKf,SAAS,IACxBiB,WAAW,KAAKjB,SAAS,EACzB;IACA,OAAOS,IAAI;EACb;EAEA,OAAO;IACLE,OAAO,EAAEA,OAAO,IAAIF,IAAI;IACxBI,QAAQ,EAAEA,QAAQ,IAAIJ,IAAI;IAC1BM,UAAU,EAAEA,UAAU,IAAIN,IAAI;IAC9BQ,WAAW,EAAEA,WAAW,IAAIR;EAC9B,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Image as RNImage } from 'react-native';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What the pipeline can load: a URL string, or the id a `require()`d image
|
|
7
|
+
* asset evaluates to.
|
|
8
|
+
*
|
|
9
|
+
* As a string, `url` is passed to the native loader as-is. Accepted forms:
|
|
10
|
+
*
|
|
11
|
+
* - `https://` / `http://` — fetched over the network and cached on disk.
|
|
12
|
+
* - `file://` URLs and plain absolute paths (`/var/…/photo.jpg`) — read from
|
|
13
|
+
* the file system. The result is cached in memory only; there is nothing
|
|
14
|
+
* to gain from copying a local file into the disk cache.
|
|
15
|
+
* - Android also accepts `content://` URIs and, from a release build's
|
|
16
|
+
* resources, a bare drawable name — which is what `require()` resolves to
|
|
17
|
+
* there. iOS resolves `require()` to a `file://` URL into the app bundle.
|
|
18
|
+
*
|
|
19
|
+
* A `require('./photo.png')` is resolved with `Image.resolveAssetSource`, so
|
|
20
|
+
* it works the same in debug (streamed from Metro) and release (bundled).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Turns an {@linkcode ImageSource} into the URL string the native pipeline
|
|
25
|
+
* loads: strings pass through unchanged, a `require()`d asset is resolved via
|
|
26
|
+
* `Image.resolveAssetSource` (the scale-matched variant, like `<Image>`).
|
|
27
|
+
*
|
|
28
|
+
* The components and hooks call this for you; use it when calling
|
|
29
|
+
* `NitroImagePipeline.loadImage`/`preLoadImage` directly with a `require()`.
|
|
30
|
+
* @throws If `source` is a number that is not a registered asset.
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const image = await NitroImagePipeline.loadImage(
|
|
34
|
+
* resolveImageUrl(require('./photo.png')),
|
|
35
|
+
* { blur: 4 },
|
|
36
|
+
* );
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function resolveImageUrl(source) {
|
|
40
|
+
if (typeof source === 'string') {
|
|
41
|
+
return source;
|
|
42
|
+
}
|
|
43
|
+
const resolved = RNImage.resolveAssetSource(source);
|
|
44
|
+
if (resolved == null) {
|
|
45
|
+
throw new Error(`Not a registered image asset: require() id ${source}`);
|
|
46
|
+
}
|
|
47
|
+
return resolved.uri;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A URL no loader can resolve, standing in for a `require()` id that is not
|
|
52
|
+
* a registered asset. Its scheme has no fetcher on either platform, so the
|
|
53
|
+
* request fails at load time — the same way a missing file does — instead of
|
|
54
|
+
* throwing from a component's render.
|
|
55
|
+
*/
|
|
56
|
+
export const UNREGISTERED_ASSET_URL = 'unregistered-asset://';
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* {@linkcode resolveImageUrl} for code that runs during render: an
|
|
60
|
+
* unregistered `require()` id yields {@linkcode UNREGISTERED_ASSET_URL} (and
|
|
61
|
+
* a warning in development) rather than throwing.
|
|
62
|
+
*/
|
|
63
|
+
export function resolveImageUrlOrFallback(source) {
|
|
64
|
+
try {
|
|
65
|
+
return resolveImageUrl(source);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (__DEV__) {
|
|
68
|
+
console.warn(`[react-native-nitro-image-pipeline] ${error.message}`);
|
|
69
|
+
}
|
|
70
|
+
return UNREGISTERED_ASSET_URL;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A stable, per-source `recyclingKey` for `<NativeNitroImage>`: the URL
|
|
76
|
+
* itself for strings, and a tag derived from the asset id for a `require()`
|
|
77
|
+
* (resolving it would cost an asset-registry lookup per render for nothing
|
|
78
|
+
* more than a distinct key).
|
|
79
|
+
*/
|
|
80
|
+
export function recyclingKeyFor(source) {
|
|
81
|
+
return typeof source === 'string' ? source : `asset:${source}`;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=resolveImageSource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Image","RNImage","resolveImageUrl","source","resolved","resolveAssetSource","Error","uri","UNREGISTERED_ASSET_URL","resolveImageUrlOrFallback","error","__DEV__","console","warn","message","recyclingKeyFor"],"sourceRoot":"../../src","sources":["resolveImageSource.ts"],"mappings":";;AAAA,SAASA,KAAK,IAAIC,OAAO,QAAQ,cAAc;;AAE/C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAACC,MAAmB,EAAU;EAC3D,IAAI,OAAOA,MAAM,KAAK,QAAQ,EAAE;IAC9B,OAAOA,MAAM;EACf;EACA,MAAMC,QAAQ,GAAGH,OAAO,CAACI,kBAAkB,CAACF,MAAM,CAAC;EACnD,IAAIC,QAAQ,IAAI,IAAI,EAAE;IACpB,MAAM,IAAIE,KAAK,CAAC,8CAA8CH,MAAM,EAAE,CAAC;EACzE;EACA,OAAOC,QAAQ,CAACG,GAAG;AACrB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,sBAAsB,GAAG,uBAAuB;;AAE7D;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,yBAAyBA,CAACN,MAAmB,EAAU;EACrE,IAAI;IACF,OAAOD,eAAe,CAACC,MAAM,CAAC;EAChC,CAAC,CAAC,OAAOO,KAAK,EAAE;IACd,IAAIC,OAAO,EAAE;MACXC,OAAO,CAACC,IAAI,CACV,uCAAwCH,KAAK,CAAWI,OAAO,EACjE,CAAC;IACH;IACA,OAAON,sBAAsB;EAC/B;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASO,eAAeA,CAACZ,MAAmB,EAAU;EAC3D,OAAO,OAAOA,MAAM,KAAK,QAAQ,GAAGA,MAAM,GAAG,SAASA,MAAM,EAAE;AAChE","ignoreList":[]}
|
package/lib/module/useImage.js
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
import { useEffect, useRef, useState } from 'react';
|
|
4
4
|
import { NitroImagePipeline } from './NitroImagePipeline';
|
|
5
|
+
import { resolveImageUrl } from './resolveImageSource';
|
|
5
6
|
/**
|
|
6
|
-
* A hook to asynchronously load an image from the
|
|
7
|
-
*
|
|
7
|
+
* A hook to asynchronously load an image from `url` through the pipeline
|
|
8
|
+
* into memory.
|
|
8
9
|
* @example
|
|
9
10
|
* ```ts
|
|
10
|
-
* const { image, error } = useImage({
|
|
11
|
+
* const { image, error } = useImage({ url: 'https://example.com/photo.jpg' });
|
|
12
|
+
* const { image: logo } = useImage({ url: require('./logo.png') });
|
|
11
13
|
* ```
|
|
12
14
|
*/
|
|
13
15
|
export function useImage({
|
|
@@ -36,6 +38,8 @@ export function useImage({
|
|
|
36
38
|
} = isUniformRadius ? {} : cornerRadius;
|
|
37
39
|
const resizeWidth = resize?.width ?? 0;
|
|
38
40
|
const resizeHeight = resize?.height ?? 0;
|
|
41
|
+
const resizeFit = resize?.fit;
|
|
42
|
+
const resizeAllowUpscale = resize?.allowUpscale;
|
|
39
43
|
useEffect(() => {
|
|
40
44
|
let cancelled = false;
|
|
41
45
|
// Only reset to the loading state when the URL changes; for same-URL
|
|
@@ -51,7 +55,9 @@ export function useImage({
|
|
|
51
55
|
if (enabled) {
|
|
52
56
|
(async () => {
|
|
53
57
|
try {
|
|
54
|
-
|
|
58
|
+
// Resolved inside the try so an unregistered `require()` id
|
|
59
|
+
// surfaces as the error state instead of throwing from the effect.
|
|
60
|
+
const result = await NitroImagePipeline.loadImage(resolveImageUrl(url), {
|
|
55
61
|
blur,
|
|
56
62
|
cornerRadius: isUniformRadius ? uniformRadius : {
|
|
57
63
|
topLeft,
|
|
@@ -61,7 +67,9 @@ export function useImage({
|
|
|
61
67
|
},
|
|
62
68
|
resize: resizeWidth > 0 && resizeHeight > 0 ? {
|
|
63
69
|
width: resizeWidth,
|
|
64
|
-
height: resizeHeight
|
|
70
|
+
height: resizeHeight,
|
|
71
|
+
fit: resizeFit,
|
|
72
|
+
allowUpscale: resizeAllowUpscale
|
|
65
73
|
} : undefined,
|
|
66
74
|
cache
|
|
67
75
|
});
|
|
@@ -85,7 +93,7 @@ export function useImage({
|
|
|
85
93
|
return () => {
|
|
86
94
|
cancelled = true;
|
|
87
95
|
};
|
|
88
|
-
}, [url, blur, isUniformRadius, uniformRadius, topLeft, topRight, bottomLeft, bottomRight, resizeWidth, resizeHeight, cache, enabled]);
|
|
96
|
+
}, [url, blur, isUniformRadius, uniformRadius, topLeft, topRight, bottomLeft, bottomRight, resizeWidth, resizeHeight, resizeFit, resizeAllowUpscale, cache, enabled]);
|
|
89
97
|
return image;
|
|
90
98
|
}
|
|
91
99
|
//# sourceMappingURL=useImage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useEffect","useRef","useState","NitroImagePipeline","useImage","url","blur","cornerRadius","resize","cache","enabled","image","setImage","undefined","error","loadedUrlRef","isUniformRadius","uniformRadius","topLeft","topRight","bottomLeft","bottomRight","resizeWidth","width","resizeHeight","height","cancelled","current","result","loadImage","e","Error"],"sourceRoot":"../../src","sources":["useImage.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAGnD,SAASC,kBAAkB,QAAQ,sBAAsB;
|
|
1
|
+
{"version":3,"names":["useEffect","useRef","useState","NitroImagePipeline","resolveImageUrl","useImage","url","blur","cornerRadius","resize","cache","enabled","image","setImage","undefined","error","loadedUrlRef","isUniformRadius","uniformRadius","topLeft","topRight","bottomLeft","bottomRight","resizeWidth","width","resizeHeight","height","resizeFit","fit","resizeAllowUpscale","allowUpscale","cancelled","current","result","loadImage","e","Error"],"sourceRoot":"../../src","sources":["useImage.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAGnD,SAASC,kBAAkB,QAAQ,sBAAsB;AACzD,SAA2BC,eAAe,QAAQ,sBAAsB;AAwBxE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,QAAQA,CAAC;EACvBC,GAAG;EACHC,IAAI,GAAG,CAAC;EACRC,YAAY,GAAG,CAAC;EAChBC,MAAM;EACNC,KAAK;EACLC,OAAO,GAAG;AAoCZ,CAAC,EAAU;EACT,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAGX,QAAQ,CAAS;IACzCU,KAAK,EAAEE,SAAS;IAChBC,KAAK,EAAED;EACT,CAAC,CAAC;EACF,MAAME,YAAY,GAAGf,MAAM,CAACK,GAAG,CAAC;;EAEhC;EACA;EACA,MAAMW,eAAe,GAAG,OAAOT,YAAY,KAAK,QAAQ;EACxD,MAAMU,aAAa,GAAGD,eAAe,GAAGT,YAAY,GAAG,CAAC;EACxD,MAAM;IACJW,OAAO,GAAG,CAAC;IACXC,QAAQ,GAAG,CAAC;IACZC,UAAU,GAAG,CAAC;IACdC,WAAW,GAAG;EAChB,CAAC,GAAGL,eAAe,GAAG,CAAC,CAAC,GAAGT,YAAY;EACvC,MAAMe,WAAW,GAAGd,MAAM,EAAEe,KAAK,IAAI,CAAC;EAC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