-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c21e6a4
commit ca896fb
Showing
6 changed files
with
321 additions
and
12 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
6 changes: 3 additions & 3 deletions
6
src/hooks/sourceNodes/createTypes/fieldTypes/DatoCmsFileField.js
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
83 changes: 83 additions & 0 deletions
83
src/hooks/sourceNodes/createTypes/utils/buildGatsbyImageDataField.js
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,83 @@ | ||
const createUrl = require('./createUrl'); | ||
const { getGatsbyImageResolver } = require('gatsby-plugin-image/graphql-utils'); | ||
const { generateImageData } = require('gatsby-plugin-image'); | ||
const getBase64 = require('./getBase64'); | ||
const getTracedSVG = require('./getTracedSVG'); | ||
|
||
const blurHashCache = new Map(); | ||
|
||
const generateImageSource = ( | ||
baseURL, | ||
width, | ||
height, | ||
format, | ||
fit, | ||
{ focalPoint, ...options }, | ||
) => { | ||
const src = createUrl( | ||
baseURL, | ||
{ ...options, w: width, h: height }, | ||
{ autoFormat: true, focalPoint }, | ||
); | ||
|
||
return { src, width, height, format }; | ||
}; | ||
|
||
module.exports = ({ cacheDir }) => { | ||
async function resolve( | ||
node, | ||
{ imgixParams = {}, placeholder = 'DOMINANT_COLOR', ...props }, | ||
) { | ||
const image = node?.entityPayload?.attributes; | ||
|
||
if (!image.is_image || image.format === 'svg') { | ||
return null; | ||
} | ||
|
||
const sourceMetadata = { | ||
width: image.width, | ||
height: image.height, | ||
format: image.format, | ||
}; | ||
|
||
const otherProps = {}; | ||
if (placeholder === 'DOMINANT_COLOR') { | ||
otherProps.backgroundColor = image.colors[0].hex; | ||
} else if (placeholder === 'BLURRED') { | ||
otherProps.placeholderURL = await getBase64( | ||
{ ...sourceMetadata, src: image.url }, | ||
cacheDir, | ||
); | ||
} else if (placeholder === 'TRACED_SVG') { | ||
otherProps.placeholderURL = await getTracedSVG( | ||
{ ...sourceMetadata, src: image.url }, | ||
cacheDir, | ||
); | ||
} | ||
|
||
imgixParams.focalPoint = node.focalPoint; | ||
|
||
return generateImageData({ | ||
filename: image.url, | ||
pluginName: 'gatsby-source-datocms', | ||
generateImageSource, | ||
sourceMetadata, | ||
formats: ['auto'], | ||
options: imgixParams, | ||
...otherProps, | ||
...props, | ||
}); | ||
} | ||
return getGatsbyImageResolver(resolve, { | ||
imgixParams: 'DatoCmsImgixParams', | ||
placeholder: { | ||
type: | ||
'enum DatoImagePlaceholder { NONE, DOMINANT_COLOR, TRACED_SVG, BLURRED }', | ||
description: `Format of generated placeholder, displayed while the main image loads. | ||
DOMINANT_COLOR: a solid color, calculated from the dominant color of the image (default). | ||
BLURRED: a blurred, low resolution image, encoded as a base64 data URI | ||
TRACED_SVG: a low-resolution traced SVG of the image. Note that this will download the image at build time for processing. | ||
NONE: no placeholder. Set "backgroundColor" to use a fixed background color.`, | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.