@@ -6,7 +6,7 @@ import {isNum} from '../utils/isNum.js';
66import { isBrowser } from "../utils/isBrowser.js" ;
77import { Action } from "@cloudinary/url-gen/internal/Action" ;
88import { isImage } from "../utils/isImage.js" ;
9- import { getAnalyticsOptions } from " ../utils/analytics" ;
9+ import { getAnalyticsOptions } from ' ../utils/analytics'
1010
1111/**
1212 * @namespace
@@ -18,7 +18,7 @@ import {getAnalyticsOptions} from "../utils/analytics";
1818 * <AdvancedImage cldImg={img} plugins={[responsive({steps: [800, 1000, 1400]})]} />
1919 */
2020export function responsive ( { steps} :{ steps ?: number | number [ ] } = { } ) : Plugin {
21- return responsivePlugin . bind ( null , steps ) ;
21+ return responsivePlugin . bind ( null , steps ) ;
2222}
2323
2424/**
@@ -31,29 +31,29 @@ export function responsive({steps}:{steps?: number | number[]}={}): Plugin{
3131 */
3232function responsivePlugin ( steps ?: number | number [ ] , element ?:HTMLImageElement , responsiveImage ?: CloudinaryImage , htmlPluginState ?: HtmlPluginState , analyticsOptions ?: AnalyticsOptions ) : Promise < void | string > | boolean {
3333
34- if ( ! isBrowser ( ) ) return true ;
34+ if ( ! isBrowser ( ) ) return true ;
3535
36- if ( ! isImage ( element ) ) return ;
36+ if ( ! isImage ( element ) ) return ;
3737
38- return new Promise ( ( resolve ) => {
39- htmlPluginState . cleanupCallbacks . push ( ( ) => {
40- window . removeEventListener ( "resize" , resizeRef ) ;
41- resolve ( 'canceled' ) ;
42- } ) ;
38+ return new Promise ( ( resolve ) => {
39+ htmlPluginState . cleanupCallbacks . push ( ( ) => {
40+ window . removeEventListener ( "resize" , resizeRef ) ;
41+ resolve ( 'canceled' ) ;
42+ } ) ;
4343
44- // Use a tagged generic action that can be later searched and replaced.
45- responsiveImage . addAction ( new Action ( ) . setActionTag ( 'responsive' ) ) ;
46- // Immediately run the resize plugin, ensuring that first render gets a responsive image.
47- onResize ( steps , element , responsiveImage , analyticsOptions ) ;
44+ // Use a tagged generic action that can be later searched and replaced.
45+ responsiveImage . addAction ( new Action ( ) . setActionTag ( 'responsive' ) ) ;
46+ // Immediately run the resize plugin, ensuring that first render gets a responsive image.
47+ onResize ( steps , element , responsiveImage , analyticsOptions ) ;
4848
49- let resizeRef : any ;
50- htmlPluginState . pluginEventSubscription . push ( ( ) => {
51- window . addEventListener ( 'resize' , resizeRef = debounce ( ( ) => {
52- onResize ( steps , element , responsiveImage , analyticsOptions ) ;
53- } , 100 ) ) ;
54- } ) ;
55- resolve ( ) ;
49+ let resizeRef : any ;
50+ htmlPluginState . pluginEventSubscription . push ( ( ) => {
51+ window . addEventListener ( 'resize' , resizeRef = debounce ( ( ) => {
52+ onResize ( steps , element , responsiveImage , analyticsOptions ) ;
53+ } , 100 ) ) ;
5654 } ) ;
55+ resolve ( ) ;
56+ } ) ;
5757}
5858
5959/**
@@ -65,8 +65,8 @@ function responsivePlugin(steps?: number | number[], element?:HTMLImageElement,
6565 * @param analyticsOptions {AnalyticsOptions} analytics options for the url to be created
6666 */
6767function onResize ( steps ?: number | number [ ] , element ?:HTMLImageElement , responsiveImage ?: CloudinaryImage , analyticsOptions ?: AnalyticsOptions ) {
68- updateByContainerWidth ( steps , element , responsiveImage ) ;
69- element . src = responsiveImage . toURL ( getAnalyticsOptions ( analyticsOptions ) ) ;
68+ updateByContainerWidth ( steps , element , responsiveImage ) ;
69+ element . src = responsiveImage . toURL ( getAnalyticsOptions ( analyticsOptions ) ) ;
7070}
7171
7272/**
@@ -77,24 +77,24 @@ function onResize(steps?: number | number[], element?:HTMLImageElement, responsi
7777 * @param responsiveImage {CloudinaryImage}
7878 */
7979function updateByContainerWidth ( steps ?: number | number [ ] , element ?:HTMLImageElement , responsiveImage ?: CloudinaryImage ) {
80- // Default value for responsiveImgWidth, used when no steps are passed.
81- let responsiveImgWidth = element . parentElement . clientWidth ;
80+ // Default value for responsiveImgWidth, used when no steps are passed.
81+ let responsiveImgWidth = element . parentElement . clientWidth ;
8282
83- if ( isNum ( steps ) ) {
84- const WIDTH_INTERVALS = steps as number ;
85- // We need to force the container width to be intervals of max width.
86- responsiveImgWidth = Math . ceil ( responsiveImgWidth / WIDTH_INTERVALS ) * WIDTH_INTERVALS ;
83+ if ( isNum ( steps ) ) {
84+ const WIDTH_INTERVALS = steps as number ;
85+ // We need to force the container width to be intervals of max width.
86+ responsiveImgWidth = Math . ceil ( responsiveImgWidth / WIDTH_INTERVALS ) * WIDTH_INTERVALS ;
8787
88- } else if ( Array . isArray ( steps ) ) {
89- responsiveImgWidth = steps . reduce ( ( prev , curr ) => {
90- return ( Math . abs ( curr - responsiveImgWidth ) < Math . abs ( prev - responsiveImgWidth ) ? curr : prev ) ;
91- } ) ;
92- }
93-
94- responsiveImage . transformation . actions . forEach ( ( action , index ) => {
95- if ( action instanceof Action && action . getActionTag ( ) === 'responsive' ) {
96- responsiveImage . transformation . actions [ index ] = scale ( responsiveImgWidth ) . setActionTag ( 'responsive' ) ;
97- }
88+ } else if ( Array . isArray ( steps ) ) {
89+ responsiveImgWidth = steps . reduce ( ( prev , curr ) => {
90+ return ( Math . abs ( curr - responsiveImgWidth ) < Math . abs ( prev - responsiveImgWidth ) ? curr : prev ) ;
9891 } ) ;
92+ }
93+
94+ responsiveImage . transformation . actions . forEach ( ( action , index ) => {
95+ if ( action instanceof Action && action . getActionTag ( ) === 'responsive' ) {
96+ responsiveImage . transformation . actions [ index ] = scale ( responsiveImgWidth ) . setActionTag ( 'responsive' ) ;
97+ }
98+ } ) ;
9999}
100100
0 commit comments