Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #23 responsive feature #49

Merged
merged 1 commit into from Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 33 additions & 22 deletions src/components/CldImage/CldImage.vue
@@ -1,13 +1,19 @@
<template>
<div class="cld-image">
<img v-bind="imageAttrs" :style="style"/>
<div class="cld-image" :style="style">
<img v-bind="imageAttrs" :style="style" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "this"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and in any case I've added that cause resizing was built around having a single node - just img. Now both have to adapt to parent node size, so both have the size adjusting styles.

<slot></slot>
</div>
</template>
<script>
import { Cloudinary, Transformation } from "cloudinary-core";
import { merge, range } from "../../utils";
import { normalizeNonCloudinary, normalizeTransformation, normalizeConfiguration, getHTMLAttributes, hasZeroSizeTransformation } from "../../helpers/attributes";
import {
normalizeNonCloudinary,
normalizeTransformation,
normalizeConfiguration,
getHTMLAttributes,
hasZeroSizeTransformation
} from "../../helpers/attributes";
import { evalBreakpoints } from "../../helpers/evalBreakpoints";
import {
getResizeTransformation,
Expand All @@ -18,7 +24,7 @@ import { getPlaceholderURL } from "../../helpers/getPlaceholderURL";
import { size } from "../../mixins/size";
import { lazy } from "../../mixins/lazy";
import { withOptions } from "../../mixins/withOptions";
import { generateUrl } from '../../helpers/URLGenerator';
import { generateUrl } from "../../helpers/URLGenerator";

/**
* Deliver images and specify image transformations using the cld-image (CldImage) component,
Expand All @@ -37,7 +43,7 @@ export default {
name: "CldImage",
provide() {
return {
registerTransformation: this.registerTransformation,
registerTransformation: this.registerTransformation
};
},
inject: {
Expand Down Expand Up @@ -99,16 +105,19 @@ export default {
},
methods: {
registerTransformation(transformation) {
this.transformations = [...this.transformations, normalizeTransformation(transformation)];
this.transformations = [
...this.transformations,
normalizeTransformation(transformation)
];
},
computeLazyLoadSrc() {
const src = getPlaceholderURL(
this.placeholder,
this.publicId,
this.configuration,
this.transformOptions
);
this.placeholder,
this.publicId,
this.configuration,
this.transformOptions
);

return {
...this.nonCldAttrs,
src
Expand All @@ -117,15 +126,17 @@ export default {
},
computed: {
isWithoutTransformation() {
return !this.publicId ||
return (
!this.publicId ||
hasZeroSizeTransformation(this.transformations) ||
(this.responsive && !this.size)
);
},
style() {
return getResponsiveStyle(this.responsive)
return getResponsiveStyle(this.responsive);
},
nonCldAttrs() {
return normalizeNonCloudinary(this.$attrs)
return normalizeNonCloudinary(this.$attrs);
},
transformOptions() {
return {
Expand All @@ -134,10 +145,10 @@ export default {
...(this.options.transformation || []),
...this.transformations
]
}
};
},
isLazyLoadInvisible() {
return this.lazy && !this.visible
return this.lazy && !this.visible;
},
imageAttrs() {
if (this.isWithoutTransformation) {
Expand Down Expand Up @@ -168,14 +179,14 @@ export default {
});

return {
...this.nonCldAttrs,
...htmlAttrs,
src
...this.nonCldAttrs,
...htmlAttrs,
src
};
},
shouldMeasureSize() {
return !this.responsive;
return this.responsive !== false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any difference or advantage in writing like this instead of !this.responsive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

produces empty string and it should mean 'default value', yet here was interpreted as no value

}
},
}
};
</script>
21 changes: 9 additions & 12 deletions src/helpers/responsiveness.js
Expand Up @@ -59,7 +59,7 @@ export function getResizeTransformation(mode, size, breakpoints) {
: {
width: Math.floor(size.width),
height: Math.floor(size.height)
}
};
return normalizeTransformation({
...getDPRAttr(),
crop: "fill",
Expand All @@ -72,21 +72,18 @@ export function getResizeTransformation(mode, size, breakpoints) {
...getDPRAttr(),
crop: "scale",
width: Math.floor(
breakpoints
? findBreakpoint(breakpoints, size.width)
: size.width
breakpoints ? findBreakpoint(breakpoints, size.width) : size.width
)
});

case "height":
return normalizeTransformation({
...getDPRAttr(),
crop: "scale",
height: Math.floor(breakpoints
? findBreakpoint(breakpoints, size.height)
: size.height
)
});
return normalizeTransformation({
...getDPRAttr(),
crop: "scale",
height: Math.floor(
breakpoints ? findBreakpoint(breakpoints, size.height) : size.height
)
});
default:
return {};
}
Expand Down
21 changes: 13 additions & 8 deletions src/mixins/size.js
@@ -1,6 +1,5 @@
import { pick, debounce } from "../utils";


/**
* If necessary posts root element
* size information
Expand All @@ -25,8 +24,12 @@ export const size = {
this.cancelSizeListener = watchElementSize(this.$el, size => {
if (!size) return;

if (!this.size || this.size.width !== size.width || this.size.height !== size.height) {
this.size = { ...size };
if (
!this.size ||
this.size.width !== size.width ||
this.size.height !== size.height
) {
this.size = size;
}
});
}
Expand All @@ -35,7 +38,7 @@ export const size = {
this.cancelSizeListener();
}
}
},
}
},

created() {
Expand Down Expand Up @@ -71,8 +74,10 @@ function watchElementSize(element, cb) {
if ("ResizeObserver" in window) {
const resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
const size = pick(entry.contentRect, ["width", "height"]);
delayedCallback(size);
delayedCallback({
width: entry.contentRect.width,
height: entry.contentRect.height
});
}
});
resizeObserver.observe(element);
Expand All @@ -85,8 +90,8 @@ function watchElementSize(element, cb) {
};
} else {
const handleWindowResize = () => {
const size = pick(element.getBoundingClientRect(), ["width", "height"]);
delayedCallback(size);
const rect = element.getBoundingClientRect();
delayedCallback({ width: rect.width, height: rect.height });
};
window.addEventListener("resize", handleWindowResize);
handleWindowResize();
Expand Down