Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Combine VImageCellSquare with VImageCell (#814)
Olga Bulat83232542fc44
diff --git a/frontend/src/components/VAllResultsGrid/VImageCellSquare.vue b/frontend/src/components/VAllResultsGrid/VImageCellSquare.vuedeleted file mode 100644index 10c3ebbdd..000000000--- a/frontend/src/components/VAllResultsGrid/VImageCellSquare.vue+++ /dev/null@@ -1,89 +0,0 @@-<template>- <VLink- itemprop="contentUrl"- :title="image.title"- :href="`/image/${image.id}?q=${searchTerm}`"- class="group block rounded-sm focus-bold-filled"- >- <figure- itemprop="image"- itemscope- itemtype="https://schema.org/ImageObject"- class="relative aspect-square rounded-sm"- >- <img- ref="img"- class="h-full w-full rounded-sm bg-dark-charcoal-10 object-cover text-dark-charcoal-10"- loading="lazy"- :alt="image.title"- :src="getImageUrl()"- :width="250"- :height="250"- itemprop="thumbnailUrl"- @error="onImageLoadError($event)"- />- <figcaption- class="invisible absolute left-0 bottom-0 bg-white p-1 text-dark-charcoal group-hover:visible group-focus:visible"- >- <span class="sr-only">{{ image.title }}</span>- <VLicense :license="image.license" :hide-name="true" />- </figcaption>- </figure>- </VLink>-</template>--<script lang="ts">-import { defineComponent, PropType } from "vue"--import type { ImageDetail } from "~/types/media"--import VLink from "~/components/VLink.vue"-import VLicense from "~/components/VLicense/VLicense.vue"--import errorImage from "~/assets/image_not_available_placeholder.png"--const toAbsolutePath = (url: string, prefix = "https://") => {- if (url.indexOf("http://") >= 0 || url.indexOf("https://") >= 0) {- return url- }- return `${prefix}${url}`-}--export default defineComponent({- name: "VImageCell",- components: { VLink, VLicense },- props: {- image: {- type: Object as PropType<ImageDetail>,- required: true,- },- searchTerm: {- type: String,- required: true,- },- },- setup(props) {- const getImageUrl = () => {- if (!props.image) return ""- const url = props.image.thumbnail || props.image.url- return toAbsolutePath(url)- }- const getImageForeignUrl = () =>- toAbsolutePath(props.image.foreign_landing_url)-- const onImageLoadError = (event: Event) => {- const element = event.target as HTMLImageElement- if (element.src !== props.image.url) {- element.src = props.image.url- } else {- element.src = errorImage- }- }- return {- getImageUrl,- getImageForeignUrl,- onImageLoadError,- }- },-})-</script>diff --git a/frontend/src/components/VImageDetails/VRelatedImages.vue b/frontend/src/components/VImageDetails/VRelatedImages.vueindex 6b02898b4..2e80c6f46 100644--- a/frontend/src/components/VImageDetails/VRelatedImages.vue+++ b/frontend/src/components/VImageDetails/VRelatedImages.vue@@ -18,7 +18,7 @@ import { defineComponent, PropType } from "vue"import type { ImageDetail } from "~/types/media"import type { FetchState } from "~/types/fetch-state"-import VImageGrid from "~/components/VImageGrid/VImageGrid.vue"+import VImageGrid from "~/components/VSearchResultsGrid/VImageGrid.vue"import VLoadingIcon from "~/components/LoadingIcon.vue"export default defineComponent({diff --git a/frontend/src/components/VAllResultsGrid/VAllResultsGrid.vue b/frontend/src/components/VSearchResultsGrid/VAllResultsGrid.vuesimilarity index 94%rename from frontend/src/components/VAllResultsGrid/VAllResultsGrid.vuerename to frontend/src/components/VSearchResultsGrid/VAllResultsGrid.vueindex c58703f9d..471f3447a 100644--- a/frontend/src/components/VAllResultsGrid/VAllResultsGrid.vue+++ b/frontend/src/components/VSearchResultsGrid/VAllResultsGrid.vue@@ -29,11 +29,12 @@class="results-grid grid grid-cols-2 gap-4 lg:grid-cols-5 2xl:grid-cols-6"><div v-for="item in allMedia" :key="item.id">- <VImageCellSquare+ <VImageCellv-if="item.frontendMediaType === 'image'":key="item.id":image="item":search-term="searchTerm"+ aspect-ratio="square"/><VAudioCellv-if="item.frontendMediaType === 'audio'"@@ -60,8 +61,8 @@ import { useUiStore } from "~/stores/ui"import { useI18n } from "~/composables/use-i18n"import VSnackbar from "~/components/VSnackbar.vue"-import VImageCellSquare from "~/components/VAllResultsGrid/VImageCellSquare.vue"-import VAudioCell from "~/components/VAllResultsGrid/VAudioCell.vue"+import VImageCell from "~/components/VSearchResultsGrid/VImageCell.vue"+import VAudioCell from "~/components/VSearchResultsGrid/VAudioCell.vue"import VLoadMore from "~/components/VLoadMore.vue"import VContentLink from "~/components/VContentLink/VContentLink.vue"import VGridSkeleton from "~/components/VSkeleton/VGridSkeleton.vue"@@ -70,7 +71,7 @@ export default defineComponent({name: "VAllResultsGrid",components: {VSnackbar,- VImageCellSquare,+ VImageCell,VAudioCell,VLoadMore,VGridSkeleton,diff --git a/frontend/src/components/VAllResultsGrid/VAudioCell.vue b/frontend/src/components/VSearchResultsGrid/VAudioCell.vuesimilarity index 100%rename from frontend/src/components/VAllResultsGrid/VAudioCell.vuerename to frontend/src/components/VSearchResultsGrid/VAudioCell.vuediff --git a/frontend/src/components/VImageGrid/VImageCell.vue b/frontend/src/components/VSearchResultsGrid/VImageCell.vuesimilarity index 52%rename from frontend/src/components/VImageGrid/VImageCell.vuerename to frontend/src/components/VSearchResultsGrid/VImageCell.vueindex 77577d4f0..a0ed13aed 100644--- a/frontend/src/components/VImageGrid/VImageCell.vue+++ b/frontend/src/components/VSearchResultsGrid/VImageCell.vue@@ -1,20 +1,30 @@<template><VLink+ itemprop="contentUrl"+ :title="image.title":href="`/image/${image.id}?q=${searchTerm}`"- class="group relative block w-full overflow-hidden rounded-sm bg-dark-charcoal-10 text-dark-charcoal-10 focus:outline-none focus:ring-[3px] focus:ring-pink focus:ring-offset-[3px]"+ class="group relative block w-full overflow-hidden rounded-sm bg-dark-charcoal-10 text-dark-charcoal-10 focus-bold-filled":aria-label="image.title"- :style="containerStyle"- @keydown.native.shift.tab.exact="$emit('shift-tab', $event)"+ :style="styles.container">- <figure class="absolute w-full" :style="figureStyle">+ <figure+ itemprop="image"+ itemscope+ itemtype="https://schema.org/ImageObject"+ class="absolute w-full rounded-sm"+ :class="{ 'relative aspect-square': isSquare }"+ :style="styles.figure"+ ><imgref="img"loading="lazy"- class="margin-auto block w-full"+ class="block w-full rounded-sm object-cover"+ :class="isSquare ? 'h-full' : 'margin-auto'":alt="image.title":src="imageUrl":width="imgWidth":height="imgHeight"+ itemprop="thumbnailUrl"@load="getImgDimension"@error="onImageLoadError($event)"/>@@ -25,26 +35,21 @@<VLicense :license="image.license" :hide-name="true" /></figcaption></figure>- <i :style="`padding-bottom:${iPadding}%`" class="block" aria-hidden />+ <i v-if="!isSquare" :style="styles.iPadding" class="block" aria-hidden /></VLink></template><script lang="ts">-import { computed, defineComponent, PropType, ref } from "vue"+import { computed, defineComponent, PropType } from "vue"-import type { ImageDetail } from "~/types/media"+import type { AspectRatio, ImageDetail } from "~/types/media"+import { useImageCellSize } from "~/composables/use-image-cell-size"import VLicense from "~/components/VLicense/VLicense.vue"import VLink from "~/components/VLink.vue"import errorImage from "~/assets/image_not_available_placeholder.png"-const minAspect = 3 / 4-const maxAspect = 16 / 9-const panoramaAspect = 21 / 9-const minRowWidth = 450-const widthBasis = minRowWidth / maxAspect-const toAbsolutePath = (url: string, prefix = "https://") => {if (url.indexOf("http://") >= 0 || url.indexOf("https://") >= 0) {return url@@ -64,46 +69,29 @@ export default defineComponent({type: String,required: true,},+ /**+ * All content view uses the square image cells, Image view+ * uses the image's intrinsic size.+ */+ aspectRatio: {+ type: String as PropType<AspectRatio>,+ default: "square",+ },},setup(props) {- const imgHeight = ref(props.image.height || 100)- const imgWidth = ref(props.image.width || 100)-- const imageAspect = computed(() => imgWidth.value / imgHeight.value)-- const containerAspect = computed(() => {- if (imageAspect.value > maxAspect) return maxAspect- if (imageAspect.value < minAspect) return minAspect- return imageAspect.value- })- const iPadding = computed(() => {- if (imageAspect.value < minAspect) return (1 / minAspect) * 100- if (imageAspect.value > maxAspect) return (1 / maxAspect) * 100- return (1 / imageAspect.value) * 100- })- const imageWidth = computed(() => {- if (imageAspect.value < maxAspect) return 100- return (imageAspect.value / maxAspect) * 100- })- const imageTop = computed(() => {- if (imageAspect.value > minAspect) return 0- return (- ((minAspect - imageAspect.value) /- (imageAspect.value * minAspect * minAspect)) *- -50- )- })- const imageLeft = computed(() => {- if (imageAspect.value < maxAspect) return 0- return ((imageAspect.value - maxAspect) / maxAspect) * -50+ const isSquare = computed(() => props.aspectRatio === "square")+ const { imgHeight, imgWidth, isPanorama, styles } = useImageCellSize({+ imageSize: { width: props.image.width, height: props.image.height },+ isSquare,})const imageUrl = computed(() => {// TODO: check if we have blurry panorama thumbnails// fix for blurry panorama thumbnails, introduced in// https://github.com/cc-archive/cccatalog-frontend/commit/4c9bdac5- if (imageAspect.value > panoramaAspect)+ if (isPanorama.value) {return toAbsolutePath(props.image.url)+ }const url = props.image.thumbnail || props.image.urlreturn toAbsolutePath(url)})@@ -118,39 +106,32 @@ export default defineComponent({*/const onImageLoadError = (event: Event) => {const element = event.target as HTMLImageElement- if (element.src !== props.image.url) {- element.src = props.image.url- } else {- element.src = errorImage- }+ element.src =+ element.src === props.image.url ? errorImage : props.image.url}+ /**+ * If the image is not square, on the image load event, update+ * the img's height and width with image natural dimensions.+ * @param event - the load event.+ */const getImgDimension = (event: Event) => {+ if (props.aspectRatio === "square") returnconst element = event.target as HTMLImageElementimgHeight.value = element.naturalHeightimgWidth.value = element.naturalWidth}- const containerStyle = computed(() => {- const containerWidth = containerAspect.value * widthBasis- return `width: ${containerWidth}px;flex-grow: ${containerWidth}`- })-- const figureStyle = computed(- () =>- `width: ${imageWidth.value}%; top: ${imageTop.value}%; left:${imageLeft.value}%;`- )-return {- imgHeight,+ styles,imgWidth,- containerStyle,- figureStyle,- iPadding,+ imgHeight,imageUrl,getImageForeignUrl,onImageLoadError,getImgDimension,++ isSquare,}},})diff --git a/frontend/src/components/VImageGrid/VImageGrid.vue b/frontend/src/components/VSearchResultsGrid/VImageGrid.vuesimilarity index 95%rename from frontend/src/components/VImageGrid/VImageGrid.vuerename to frontend/src/components/VSearchResultsGrid/VImageGrid.vueindex 030f99090..cdd189d3f 100644--- a/frontend/src/components/VImageGrid/VImageGrid.vue+++ b/frontend/src/components/VSearchResultsGrid/VImageGrid.vue@@ -6,6 +6,7 @@:key="image.id":image="image":search-term="searchTerm"+ aspect-ratio="intrinsic"/></div><h5 v-if="isError && !fetchState.isFinished" class="py-4">@@ -33,7 +34,7 @@ import type { FetchState } from "~/types/fetch-state"import type { ImageDetail } from "~/types/media"import VLoadMore from "~/components/VLoadMore.vue"-import VImageCell from "~/components/VImageGrid/VImageCell.vue"+import VImageCell from "~/components/VSearchResultsGrid/VImageCell.vue"export default defineComponent({name: "ImageGrid",diff --git a/frontend/src/composables/use-image-cell-size.ts b/frontend/src/composables/use-image-cell-size.tsnew file mode 100644index 000000000..d5ead64f6--- /dev/null+++ b/frontend/src/composables/use-image-cell-size.ts@@ -0,0 +1,82 @@+import { computed, Ref, ref } from "@nuxtjs/composition-api"++import type { ImageDimensions } from "~/types/media"++const minAspect = 3 / 4+const maxAspect = 16 / 9+const panoramaAspect = 21 / 9+const minRowWidth = 450+const widthBasis = minRowWidth / maxAspect+const squareSize = 250+const defaultImgSize = 100++const getContainerAspect = (aspect: number) => {+ if (aspect > maxAspect) return maxAspect+ if (aspect < minAspect) return minAspect+ return aspect+}++export const useImageCellSize = ({+ imageSize,+ isSquare,+}: {+ imageSize: ImageDimensions+ isSquare: Ref<boolean>+}) => {+ const imgHeight = ref(+ isSquare.value ? squareSize : imageSize.height || defaultImgSize+ )+ const imgWidth = ref(+ isSquare.value ? squareSize : imageSize.width || defaultImgSize+ )++ const imageAspect = computed(() => imgWidth.value / imgHeight.value)+ const isPanorama = computed(() => imageAspect.value > panoramaAspect)++ /**+ * Returns the style declarations for container, figure and i padding.+ * For the square cell, the styles are empty.+ */+ const styles = computed(() => {+ const aspect = imageAspect.value++ if (isSquare.value)+ return {+ container: "",+ figure: "",+ iPadding: "",+ }++ const containerAspect = getContainerAspect(aspect)+ const containerWidth = containerAspect * widthBasis++ let imageWidth, imageLeft, imageTop++ if (aspect < maxAspect) {+ imageWidth = 100+ imageLeft = 0+ } else {+ imageWidth = (aspect / maxAspect) * 100+ imageLeft = ((aspect - maxAspect) / maxAspect) * -50+ }++ if (aspect > minAspect) {+ imageTop = 0+ } else {+ imageTop = ((minAspect - aspect) / (aspect * minAspect * minAspect)) * -50+ }++ return {+ container: `width: ${containerWidth}px;flex-grow: ${containerWidth}`,+ figure: `width: ${imageWidth}%; top: ${imageTop}%; left:${imageLeft}%;`,+ iPadding: `padding-bottom:${(1 / containerAspect) * 100}%`,+ }+ })++ return {+ imgHeight,+ imgWidth,+ isPanorama,+ styles,+ }+}diff --git a/frontend/src/pages/search/image.vue b/frontend/src/pages/search/image.vueindex ce969f559..6210ba643 100644--- a/frontend/src/pages/search/image.vue+++ b/frontend/src/pages/search/image.vue@@ -11,7 +11,7 @@ import { computed, defineComponent } from "vue"import { propTypes } from "~/pages/search/search-page.types"-import VImageGrid from "~/components/VImageGrid/VImageGrid.vue"+import VImageGrid from "~/components/VSearchResultsGrid/VImageGrid.vue"export default defineComponent({name: "ImageSearch",diff --git a/frontend/src/pages/search/index.vue b/frontend/src/pages/search/index.vueindex 04732e7fd..9e7f609e2 100644--- a/frontend/src/pages/search/index.vue+++ b/frontend/src/pages/search/index.vue@@ -7,7 +7,7 @@ import { defineComponent } from "vue"import { propTypes } from "~/pages/search/search-page.types"-import VAllResultsGrid from "~/components/VAllResultsGrid/VAllResultsGrid.vue"+import VAllResultsGrid from "~/components/VSearchResultsGrid/VAllResultsGrid.vue"export default defineComponent({name: "SearchIndex",diff --git a/frontend/src/types/media.ts b/frontend/src/types/media.tsindex 84dc139d0..f69554173 100644--- a/frontend/src/types/media.ts+++ b/frontend/src/types/media.ts@@ -92,3 +92,10 @@ export interface ApiMediatitle?: stringoriginalTitle?: string}++export interface ImageDimensions {+ width?: number+ height?: number+}++export type AspectRatio = "square" | "intrinsic"diff --git a/frontend/test/unit/specs/components/image-grid.spec.js b/frontend/test/unit/specs/components/image-grid.spec.jsindex fd4237df6..77f66ecad 100644--- a/frontend/test/unit/specs/components/image-grid.spec.js+++ b/frontend/test/unit/specs/components/image-grid.spec.js@@ -7,7 +7,7 @@ import { PiniaVuePlugin, createPinia } from "~~/test/unit/test-utils/pinia"import messages from "~/locales/en.json"-import VImageGrid from "~/components/VImageGrid/VImageGrid.vue"+import VImageGrid from "~/components/VSearchResultsGrid/VImageGrid.vue"const i18n = new VueI18n({locale: "en",diff --git a/frontend/test/unit/specs/composables/use-image-cell-size.spec.js b/frontend/test/unit/specs/composables/use-image-cell-size.spec.jsnew file mode 100644index 000000000..636bcd60f--- /dev/null+++ b/frontend/test/unit/specs/composables/use-image-cell-size.spec.js@@ -0,0 +1,71 @@+import { ref } from "@nuxtjs/composition-api"++import { useImageCellSize } from "~/composables/use-image-cell-size"++describe("useImageCellSize", () => {+ it("Should return correct values for square image", () => {+ const { imgHeight, imgWidth, isPanorama, styles } = useImageCellSize({+ imageSize: {},+ isSquare: ref(true),+ })++ expect(imgHeight.value).toEqual(250)+ expect(imgWidth.value).toEqual(250)+ expect(isPanorama.value).toEqual(false)+ expect(styles.value).toEqual({ container: "", figure: "", iPadding: "" })+ })++ it("Should return correct values for intrinsic panorama image", () => {+ const HEIGHT = 25+ const WIDTH = 300+ const { imgHeight, imgWidth, isPanorama, styles } = useImageCellSize({+ imageSize: { width: WIDTH, height: HEIGHT },+ isSquare: ref(false),+ })++ expect(imgHeight.value).toEqual(HEIGHT)+ expect(imgWidth.value).toEqual(WIDTH)+ expect(isPanorama.value).toEqual(true)+ expect(styles.value).toEqual({+ container: "width: 450px;flex-grow: 450",+ figure: "width: 675%; top: 0%; left:-287.5%;",+ iPadding: "padding-bottom:56.25%",+ })+ })++ it("Should return correct values for intrinsic tall image", () => {+ const HEIGHT = 300+ const WIDTH = 25+ const { imgHeight, imgWidth, isPanorama, styles } = useImageCellSize({+ imageSize: { width: WIDTH, height: HEIGHT },+ isSquare: ref(false),+ })++ expect(imgHeight.value).toEqual(HEIGHT)+ expect(imgWidth.value).toEqual(WIDTH)+ expect(isPanorama.value).toEqual(false)+ expect(styles.value).toEqual({+ container: "width: 189.84375px;flex-grow: 189.84375",+ figure: "width: 100%; top: -711.1111111111111%; left:0%;",+ iPadding: "padding-bottom:133.33333333333331%",+ })+ })++ it("Should return correct values for intrinsic square image", () => {+ const HEIGHT = 300+ const WIDTH = 300+ const { imgHeight, imgWidth, isPanorama, styles } = useImageCellSize({+ imageSize: { width: WIDTH, height: HEIGHT },+ isSquare: ref(false),+ })++ expect(imgHeight.value).toEqual(HEIGHT)+ expect(imgWidth.value).toEqual(WIDTH)+ expect(isPanorama.value).toEqual(false)+ expect(styles.value).toEqual({+ container: "width: 253.125px;flex-grow: 253.125",+ figure: "width: 100%; top: 0%; left:0%;",+ iPadding: "padding-bottom:100%",+ })+ })+})diff --git a/frontend/tsconfig.json b/frontend/tsconfig.jsonindex 207ae9fa7..6e2104ebd 100644--- a/frontend/tsconfig.json+++ b/frontend/tsconfig.json@@ -59,7 +59,6 @@"src/components/VHomeGallery/**.vue","src/components/VIcon/**.vue","src/components/VIconButton/**.vue",- "src/components/VImageGrid/**.vue","src/components/VInputField/**.vue","src/components/VItemGroup/**.vue","src/components/VLicense/**.vue",@@ -71,19 +70,19 @@"src/components/VRecentSearches/**.vue","src/components/VSkeleton/**.vue","src/components/VTabs/**.vue",- "src/components/VAllResultsGrid/VAudioCell.vue",- "src/components/VAllResultsGrid/VImageCellSquare.vue","src/components/VContentReport/VReportDescForm.vue","src/components/VContentSwitcher/VSearchTypeButton.vue","src/components/VContentSwitcher/VSearchTypeItem.vue","src/components/VContentSwitcher/VSearchTypeRadio.vue",- "src/components/VContentSwitcherOld/VSearchTypeButton.vue","src/components/VErrorSection/VErrorImage.vue","src/components/VFilters/VLicenseExplanation.vue","src/components/VHeader/VFilterButton.vue","src/components/VHeader/VPageLinks.vue","src/components/VHeader/VSearchBar/**.vue","src/components/VImageDetails/VRelatedImages.vue",+ "src/components/VSearchResultsGrid/VAudioCell.vue",+ "src/components/VSearchResultsGrid/VImageCell.vue",+ "src/components/VSearchResultsGrid/VImageGrid.vue","src/pages/about.vue","src/pages/feedback.vue",