Skip to content

Commit 2194be2

Browse files
committed
fix #23 responsive feature
1 parent d75613a commit 2194be2

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

src/components/CldImage/CldImage.vue

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<template>
2-
<div class="cld-image">
3-
<img v-bind="imageAttrs" :style="style"/>
2+
<div class="cld-image" :style="style">
3+
<img v-bind="imageAttrs" :style="style" />
44
<slot></slot>
55
</div>
66
</template>
77
<script>
88
import { Cloudinary, Transformation } from "cloudinary-core";
99
import { merge, range } from "../../utils";
10-
import { normalizeNonCloudinary, normalizeTransformation, normalizeConfiguration, getHTMLAttributes, hasZeroSizeTransformation } from "../../helpers/attributes";
10+
import {
11+
normalizeNonCloudinary,
12+
normalizeTransformation,
13+
normalizeConfiguration,
14+
getHTMLAttributes,
15+
hasZeroSizeTransformation
16+
} from "../../helpers/attributes";
1117
import { evalBreakpoints } from "../../helpers/evalBreakpoints";
1218
import {
1319
getResizeTransformation,
@@ -18,7 +24,7 @@ import { getPlaceholderURL } from "../../helpers/getPlaceholderURL";
1824
import { size } from "../../mixins/size";
1925
import { lazy } from "../../mixins/lazy";
2026
import { withOptions } from "../../mixins/withOptions";
21-
import { generateUrl } from '../../helpers/URLGenerator';
27+
import { generateUrl } from "../../helpers/URLGenerator";
2228
2329
/**
2430
* Deliver images and specify image transformations using the cld-image (CldImage) component,
@@ -37,7 +43,7 @@ export default {
3743
name: "CldImage",
3844
provide() {
3945
return {
40-
registerTransformation: this.registerTransformation,
46+
registerTransformation: this.registerTransformation
4147
};
4248
},
4349
inject: {
@@ -99,16 +105,19 @@ export default {
99105
},
100106
methods: {
101107
registerTransformation(transformation) {
102-
this.transformations = [...this.transformations, normalizeTransformation(transformation)];
108+
this.transformations = [
109+
...this.transformations,
110+
normalizeTransformation(transformation)
111+
];
103112
},
104113
computeLazyLoadSrc() {
105114
const src = getPlaceholderURL(
106-
this.placeholder,
107-
this.publicId,
108-
this.configuration,
109-
this.transformOptions
110-
);
111-
115+
this.placeholder,
116+
this.publicId,
117+
this.configuration,
118+
this.transformOptions
119+
);
120+
112121
return {
113122
...this.nonCldAttrs,
114123
src
@@ -117,15 +126,17 @@ export default {
117126
},
118127
computed: {
119128
isWithoutTransformation() {
120-
return !this.publicId ||
129+
return (
130+
!this.publicId ||
121131
hasZeroSizeTransformation(this.transformations) ||
122132
(this.responsive && !this.size)
133+
);
123134
},
124135
style() {
125-
return getResponsiveStyle(this.responsive)
136+
return getResponsiveStyle(this.responsive);
126137
},
127138
nonCldAttrs() {
128-
return normalizeNonCloudinary(this.$attrs)
139+
return normalizeNonCloudinary(this.$attrs);
129140
},
130141
transformOptions() {
131142
return {
@@ -134,10 +145,10 @@ export default {
134145
...(this.options.transformation || []),
135146
...this.transformations
136147
]
137-
}
148+
};
138149
},
139150
isLazyLoadInvisible() {
140-
return this.lazy && !this.visible
151+
return this.lazy && !this.visible;
141152
},
142153
imageAttrs() {
143154
if (this.isWithoutTransformation) {
@@ -168,14 +179,14 @@ export default {
168179
});
169180
170181
return {
171-
...this.nonCldAttrs,
172-
...htmlAttrs,
173-
src
182+
...this.nonCldAttrs,
183+
...htmlAttrs,
184+
src
174185
};
175186
},
176187
shouldMeasureSize() {
177-
return !this.responsive;
188+
return this.responsive !== false;
178189
}
179-
},
190+
}
180191
};
181192
</script>

src/helpers/responsiveness.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function getResizeTransformation(mode, size, breakpoints) {
5959
: {
6060
width: Math.floor(size.width),
6161
height: Math.floor(size.height)
62-
}
62+
};
6363
return normalizeTransformation({
6464
...getDPRAttr(),
6565
crop: "fill",
@@ -72,21 +72,18 @@ export function getResizeTransformation(mode, size, breakpoints) {
7272
...getDPRAttr(),
7373
crop: "scale",
7474
width: Math.floor(
75-
breakpoints
76-
? findBreakpoint(breakpoints, size.width)
77-
: size.width
75+
breakpoints ? findBreakpoint(breakpoints, size.width) : size.width
7876
)
7977
});
8078

8179
case "height":
82-
return normalizeTransformation({
83-
...getDPRAttr(),
84-
crop: "scale",
85-
height: Math.floor(breakpoints
86-
? findBreakpoint(breakpoints, size.height)
87-
: size.height
88-
)
89-
});
80+
return normalizeTransformation({
81+
...getDPRAttr(),
82+
crop: "scale",
83+
height: Math.floor(
84+
breakpoints ? findBreakpoint(breakpoints, size.height) : size.height
85+
)
86+
});
9087
default:
9188
return {};
9289
}

src/mixins/size.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { pick, debounce } from "../utils";
22

3-
43
/**
54
* If necessary posts root element
65
* size information
@@ -25,8 +24,12 @@ export const size = {
2524
this.cancelSizeListener = watchElementSize(this.$el, size => {
2625
if (!size) return;
2726

28-
if (!this.size || this.size.width !== size.width || this.size.height !== size.height) {
29-
this.size = { ...size };
27+
if (
28+
!this.size ||
29+
this.size.width !== size.width ||
30+
this.size.height !== size.height
31+
) {
32+
this.size = size;
3033
}
3134
});
3235
}
@@ -35,7 +38,7 @@ export const size = {
3538
this.cancelSizeListener();
3639
}
3740
}
38-
},
41+
}
3942
},
4043

4144
created() {
@@ -71,8 +74,10 @@ function watchElementSize(element, cb) {
7174
if ("ResizeObserver" in window) {
7275
const resizeObserver = new ResizeObserver(entries => {
7376
for (const entry of entries) {
74-
const size = pick(entry.contentRect, ["width", "height"]);
75-
delayedCallback(size);
77+
delayedCallback({
78+
width: entry.contentRect.width,
79+
height: entry.contentRect.height
80+
});
7681
}
7782
});
7883
resizeObserver.observe(element);
@@ -85,8 +90,8 @@ function watchElementSize(element, cb) {
8590
};
8691
} else {
8792
const handleWindowResize = () => {
88-
const size = pick(element.getBoundingClientRect(), ["width", "height"]);
89-
delayedCallback(size);
93+
const rect = element.getBoundingClientRect();
94+
delayedCallback({ width: rect.width, height: rect.height });
9095
};
9196
window.addEventListener("resize", handleWindowResize);
9297
handleWindowResize();

0 commit comments

Comments
 (0)