forked from nuxt/image
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
directus
provider (nuxt#787)
- Loading branch information
Showing
8 changed files
with
165 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Directus | ||
|
||
Nuxt Image with Directus integration | ||
|
||
Integration between [Directus](https://directus.io/) and the image module. | ||
|
||
To use this provider you just need to specify the base URL of your project. | ||
|
||
```ts [nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
image: { | ||
directus: { | ||
// This URL needs to include the final `assets/` directory | ||
baseURL: 'http://localhost:8055/assets/', | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
You can easily override default options: | ||
|
||
```ts [nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
image: { | ||
directus: { | ||
baseURL: 'http://mydirectus-domain.com/assets/', | ||
modifiers: { | ||
withoutEnlargement: 'true', | ||
transforms: [['blur', 4], ['negate']] | ||
} | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
## Modifiers | ||
All the default modifiers from [Directus documentation](https://docs.directus.io/reference/files.html#requesting-a-thumbnail) are available. | ||
|
||
```vue | ||
<nuxt-img | ||
provider="directus" | ||
src="ad514db1-eb90-4523-8183-46781437e7ee" | ||
height="512" | ||
fit="inside" | ||
quality="20" | ||
:modifiers="{ withoutEnlargement: 'true' }" | ||
/> | ||
``` | ||
|
||
Since Directus exposes [the full sharp API](https://sharp.pixelplumbing.com/api-operation) through the `transforms` parameter, we can use it inside our `modifiers` prop: | ||
|
||
```vue | ||
<nuxt-img | ||
provider="directus" | ||
src="ad514db1-eb90-4523-8183-46781437e7ee" | ||
:modifiers="{ height: '512', withoutEnlargement: 'true', transforms: [['blur', 4], ['negate']] }" | ||
/> | ||
``` | ||
|
||
::alert{type="info"} | ||
Note that the `transforms` parameter, as stated in the [Directus documentation](https://docs.directus.io/reference/files.html#advanced-transformations), is basically a list of lists. This is to facilitate the use of those sharp functions, like [`normalise`](https://sharp.pixelplumbing.com/api-operation#normalise), that would need multiple values in input: `transforms: [['normalise', 1, 99], ['blur', 4], ['negate']]`. | ||
:: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { joinURL } from 'ufo' | ||
import type { ProviderGetImage } from '../../types' | ||
import { createOperationsGenerator } from '#image' | ||
|
||
export const operationsGenerator = createOperationsGenerator({ | ||
joinWith: '&' | ||
}) | ||
|
||
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL } = {}) => { | ||
// Separating the transforms from the rest of the modifiers | ||
const transforms = modifiers.transforms | ||
if (transforms && transforms.length > 0) { | ||
// We stringify and encode in URL the list of lists, then apply it back to the modifiers | ||
modifiers.transforms = new URLSearchParams(JSON.stringify(transforms)).toString().replace(/=+$/, '') as unknown as (string | number)[][] | ||
} | ||
const operations = operationsGenerator(modifiers) | ||
return { | ||
url: joinURL(baseURL, src + (operations ? ('?' + operations) : '')) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters