Skip to content

Commit

Permalink
feat-#197: useCldVideoUrl composable
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Mar 24, 2024
1 parent 95add99 commit 8ad2df6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 23 deletions.
35 changes: 35 additions & 0 deletions docs/content/1.getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ Configure Nuxt Cloudinary easily with the `cloudinary` property.
export default {
cloudinary: {
cloudName: 'fesfese4324',
uploadPreset?: 'my-custom-preset',
apiKey?: '12345',
analytics?: true,
cloud?: {},
url?: {},
}
}
```
Expand All @@ -72,6 +77,36 @@ export default {

Your unique Cloudinary Cloud Name. You can find it in the Cloudinary dashboard.

### `uploadPreset`

- Default: `-`

For example: `my-upload-preset`. Used with `CldUploadWidget` and `CldUploadButton` components

### `apiKey`

- Default: `-`

For example: `12345`. Used with `CldMediaLibrary` component.

### `analytics`

- Default: `true`

Enabling Cloudinary analytics.

### `cloud`

- Default: `-`

Top level configuration used for all composables and components. Check out all available options [here](https://cloudinary.com/documentation/cloudinary_sdks#configuration_parameters)

### `url`

- Default: `-`

Top level configuration used for all composables and components. Check out all available options [here](https://cloudinary.com/documentation/cloudinary_sdks#configuration_parameters)

## NuxtCloudinary vs Nuxt Image

You may be wondering what is the reason to have both Nuxt Cloudinary and Nuxt Image modules if they both have integration with Cloudinary and they tackle the aspect of optimized images.
Expand Down
28 changes: 28 additions & 0 deletions docs/content/3.composables/2.useCldVideoUrl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
description:
---

This composable is using [@cloudinary-util/url-loader](https://github.com/colbyfayock/cloudinary-util/tree/main/packages/url-loader) under the hood to generate the Cloudinary URL with certain optimizations.

## Usage

Configuration for `useCldVideoUrl` is the same as [useCldImageUrl](usecldimageurl).

```vue
<script lang="ts" setup>
const { url } = useCldVideoUrl({
options: {
src: "/videos/mountain-stars",
},
});
</script>
```

The only difference is getCldVideoUrl provides the following default settings:

| Option Name | Type | Default |
| ----------- | ------ | --------- |
| assetType | string | `"video"` |

Additionally, the transformations do not have access to image-specific transformations.
Find more configuration settings over at [useCldImageUrl](usecldimageurl#configuration) configuration.
26 changes: 9 additions & 17 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
const { url } = useCldImageUrl({ options: { src: "/cld-sample-5.jpg" } });
console.log(url);
const { url: videoUrl } = useCldVideoUrl({
options: { src: "videos/mountain-stars" },
});
console.log(videoUrl);
const mediaAssets = [
{ tag: "electric_car_product_gallery_demo" }, // by default mediaType: "image"
{ tag: "electric_car_product_gallery_demo", mediaType: "video" },
Expand All @@ -15,9 +20,7 @@ const cldVideoRef = ref();
</script>

<template>
<button :id="buttonId">
Select Image or Video
</button>
<button :id="buttonId">Select Image or Video</button>

Check warning on line 23 in playground/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 16)

Expected 1 line break after opening tag (`<button>`), but no line breaks found

Check warning on line 23 in playground/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 16)

Expected 1 line break before closing tag (`</button>`), but no line breaks found
<CldMediaLibrary
api-key="12345"
:button-id="buttonId"
Expand All @@ -29,10 +32,7 @@ const cldVideoRef = ref();
:button-id="buttonId"
/>
<!-- Usage of `CldOgImage.vue` component -->
<CldOgImage
src="cld-sample-2"
twitter-title="test"
/>
<CldOgImage src="cld-sample-2" twitter-title="test" />

Check warning on line 35 in playground/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 16)

'twitter-title' should be on a new line
<!-- Usage of `CldVideoPlayer.vue` component -->
<CldVideoPlayer
ref="cldVideoRef"
Expand All @@ -41,16 +41,8 @@ const cldVideoRef = ref();
src="videos/mountain-stars"
/>
<!-- Usage of `CldUploadWidget.vue` component -->
<CldUploadWidget
v-slot="{ open }"
upload-preset="nuxt-cloudinary-unsigned"
>
<button
type="button"
@click="open"
>
Upload an Image
</button>
<CldUploadWidget v-slot="{ open }" upload-preset="nuxt-cloudinary-unsigned">

Check warning on line 44 in playground/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 16)

'upload-preset' should be on a new line
<button type="button" @click="open">Upload an Image</button>

Check warning on line 45 in playground/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 16)

'@click' should be on a new line

Check warning on line 45 in playground/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 16)

Expected 1 line break after opening tag (`<button>`), but no line breaks found

Check warning on line 45 in playground/app.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 16)

Expected 1 line break before closing tag (`</button>`), but no line breaks found
</CldUploadWidget>
<!-- Usage of `CldUploadButton.vue` component -->
<CldUploadButton upload-preset="nuxt-cloudinary-unsigned">
Expand Down
8 changes: 2 additions & 6 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ export default defineNuxtConfig({
modules: ['../src/module'],
cloudinary: {
cloudName: 'nuxt-cloudinary',
url: {
cname: 'spacejelly.dev',
secureDistribution: 'spacejelly.dev',
secure: true,
privateCdn: true
}
url: {},
cloud: {}
}
})
47 changes: 47 additions & 0 deletions src/runtime/composables/useCldVideoUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useRuntimeConfig } from '#imports'
import { constructCloudinaryUrl, ConstructUrlProps, ConfigOptions, AnalyticsOptions, VideoOptions } from '@cloudinary-util/url-loader'
import nuxtPkg from 'nuxt/package.json';
import pkg from '../../../package.json'

export const useCldVideoUrl = (props: { options: VideoOptions, config?: ConfigOptions, analytics?: AnalyticsOptions }) => {
if (!props.options.src) console.error("`[@nuxtjs/cloudinary]`: Property `src` is missing")

const moduleConfig = useRuntimeConfig().public.cloudinary;

const cldCloudName = props.config?.cloud?.cloudName || moduleConfig.cloud?.cloudName || moduleConfig?.cloudName;

if (!cldCloudName) console.warn('`[@nuxtjs/cloudinary]` Environment variable `CLOUDINARY_CLOUD_NAME` or property `cloudinary.cloudName` missing')

let cldOptions: ConstructUrlProps = {
options: {
assetType: 'video',
// Have to spread the options because otherwise getting the error about props being updated while they are readonly.
...props.options
},
config: {
url: moduleConfig.url,
cloud: {
cloudName: cldCloudName,
...moduleConfig.cloud
},
...props.config,
},
analytics: false
}

if (moduleConfig.analytics) {
cldOptions = {
...cldOptions,
analytics: Object.assign({
sdkCode: 'D',
sdkSemver: `${pkg.version.split('.')[0]}.0.0`,
techVersion: `${nuxtPkg.version.split('.')[0]}.0.0`,
product: 'B'
}, props.analytics)
}
}

return {
url: constructCloudinaryUrl(cldOptions)
}
}

0 comments on commit 8ad2df6

Please sign in to comment.