|
1 | 1 | const fetch = require('node-fetch');
|
2 | 2 | const {createContentDigest} = require(`gatsby-core-utils`);
|
| 3 | +const { getGatsbyImageResolver } = require("gatsby-plugin-image/graphql-utils"); |
| 4 | +const { generateImageData, getLowResolutionImageURL } = require("gatsby-plugin-image"); |
3 | 5 | const {getContentTypes, getDeletedObjects, getContentObjects} = require('./src/data-loader');
|
4 | 6 | const {capitalize} = require('./src/utils')
|
5 | 7 |
|
@@ -209,6 +211,12 @@ exports.createResolvers = ({
|
209 | 211 | }
|
210 | 212 | }
|
211 | 213 | })
|
| 214 | + } else { |
| 215 | + createResolvers({ |
| 216 | + _media: { |
| 217 | + gatsbyImageData: getGatsbyImageResolver(resolveGatsbyImageData), |
| 218 | + }, |
| 219 | + }) |
212 | 220 | }
|
213 | 221 | }
|
214 | 222 |
|
@@ -245,130 +253,13 @@ const createTypeDefs = (contentTypesDefinitions, schema) => {
|
245 | 253 | typeDefs.push(schema.buildObjectType(additionalDef));
|
246 | 254 | }
|
247 | 255 | });
|
248 |
| - if (ctd.name === '_media' && !downloadMediaFileGlobal) { |
249 |
| - tmpDef.fields.fixed = { |
250 |
| - type: 'FlotiqImageFixed', |
251 |
| - args: { |
252 |
| - width: 'Int', |
253 |
| - height: 'Int' |
254 |
| - }, |
255 |
| - resolve(source, args) { |
256 |
| - let width = 0; |
257 |
| - let height = 0; |
258 |
| - if (args.width) { |
259 |
| - width = args.width; |
260 |
| - } |
261 |
| - if (args.height) { |
262 |
| - height = args.height |
263 |
| - } |
264 |
| - return { |
265 |
| - aspectRatio: (args.width && args.height) ? (args.width / args.height) : (source.width / source.height), |
266 |
| - height: args.height ? args.height : source.height, |
267 |
| - originalName: source.id + '.' + source.extension, |
268 |
| - src: apiUrl + source.url.replace('0x0', width + 'x' + height), |
269 |
| - srcSet: createSrcSetFixed(apiUrl, source, args), |
270 |
| - width: args.width ? args.width : source.width, |
271 |
| - } |
272 |
| - } |
273 |
| - }; |
274 |
| - tmpDef.fields.fluid = { |
275 |
| - type: 'FlotiqImageFluid', |
276 |
| - args: { |
277 |
| - maxWidth: 'Int', |
278 |
| - sizes: 'String' |
279 |
| - }, |
280 |
| - resolve(source, args) { |
281 |
| - return { |
282 |
| - aspectRatio: source.width / source.height, |
283 |
| - originalName: source.id + '.' + source.extension, |
284 |
| - src: apiUrl + (args.maxWidth ? source.url.replace('0x0', args.maxWidth + 'x0') : source.url), |
285 |
| - srcSet: createSrcSetFluid(apiUrl, source, args), |
286 |
| - sizes: args.sizes ? args.sizes : '(max-width: ' + (args.maxWidth ? args.maxWidth : source.width) + 'px) 100vw, ' + (args.maxWidth ? args.maxWidth : source.width) + 'px' |
287 |
| - } |
288 |
| - } |
289 |
| - }; |
290 |
| - } |
291 | 256 |
|
292 | 257 | tmpDef.fields.flotiqInternal = `FlotiqInternal!`;
|
293 | 258 | typeDefs.push(schema.buildObjectType(tmpDef));
|
294 | 259 | });
|
295 | 260 | typeDefinitionsDeferred.resolve(typeDefs);
|
296 | 261 | };
|
297 | 262 |
|
298 |
| -const createSrcSetFluid = (apiUrl, source, args) => { |
299 |
| - let array = []; |
300 |
| - if (!args.maxWidth) { |
301 |
| - if (source.width >= 200) { |
302 |
| - array.push(apiUrl + '/image/200x0/' + source.id + '.' + source.extension + ' 200w'); |
303 |
| - if (source.width >= 400) { |
304 |
| - array.push(apiUrl + '/image/400x0/' + source.id + '.' + source.extension + ' 400w'); |
305 |
| - if (source.width >= 800) { |
306 |
| - array.push(apiUrl + '/image/800x0/' + source.id + '.' + source.extension + ' 800w'); |
307 |
| - if (source.width >= 1200) { |
308 |
| - array.push(apiUrl + '/image/1200x0/' + source.id + '.' + source.extension + ' 1200w'); |
309 |
| - if (source.width >= 1600) { |
310 |
| - array.push(apiUrl + '/image/1600x0/' + source.id + '.' + source.extension + ' 1600w'); |
311 |
| - if (source.width >= 1920) { |
312 |
| - array.push(apiUrl + '/image/1920x0/' + source.id + '.' + source.extension + ' 1920w'); |
313 |
| - } |
314 |
| - } |
315 |
| - } |
316 |
| - } |
317 |
| - } |
318 |
| - } |
319 |
| - } else { |
320 |
| - if (args.maxWidth <= source.width) { |
321 |
| - let per25 = args.maxWidth / 4; |
322 |
| - let per50 = args.maxWidth / 2; |
323 |
| - let per150 = args.maxWidth * 1.5; |
324 |
| - let per200 = args.maxWidth * 2; |
325 |
| - array.push(apiUrl + '/image/' + Math.floor(per25) + 'x0/' + source.id + '.' + source.extension + ' ' + per25 + 'w'); |
326 |
| - array.push(apiUrl + '/image/' + Math.floor(per50) + 'x0/' + source.id + '.' + source.extension + ' ' + per50 + 'w'); |
327 |
| - array.push(apiUrl + '/image/' + args.maxWidth + 'x0/' + source.id + '.' + source.extension + ' ' + args.maxWidth + 'w'); |
328 |
| - if (per150 <= source.width) { |
329 |
| - array.push(apiUrl + '/image/' + Math.floor(per150) + 'x0/' + source.id + '.' + source.extension + ' ' + per150 + 'w'); |
330 |
| - if (per200 <= source.width) { |
331 |
| - array.push(apiUrl + '/image/' + Math.floor(per200) + 'x0/' + source.id + '.' + source.extension + ' ' + per200 + 'w'); |
332 |
| - } |
333 |
| - } |
334 |
| - } else { |
335 |
| - let per25 = args.maxWidth / 4; |
336 |
| - if (per25 < source.width) { |
337 |
| - array.push(apiUrl + '/image/' + Math.floor(per25) + 'x0/' + source.id + '.' + source.extension + ' ' + per25 + 'w'); |
338 |
| - let per50 = args.maxWidth / 2; |
339 |
| - if (per50 < source.width) { |
340 |
| - array.push(apiUrl + '/image/' + Math.floor(per50) + 'x0/' + source.id + '.' + source.extension + ' ' + per50 + 'w'); |
341 |
| - } |
342 |
| - } |
343 |
| - |
344 |
| - array.push(apiUrl + '/image/' + source.width + 'x0/' + source.id + '.' + source.extension + ' ' + source.width + 'w'); |
345 |
| - } |
346 |
| - } |
347 |
| - return array.join(',\n') |
348 |
| -} |
349 |
| - |
350 |
| -const createSrcSetFixed = (apiUrl, source, args) => { |
351 |
| - let width = 0; |
352 |
| - let height = 0; |
353 |
| - if (args.width) { |
354 |
| - width = args.width; |
355 |
| - } |
356 |
| - if (args.height) { |
357 |
| - height = args.height |
358 |
| - } |
359 |
| - let array = [ |
360 |
| - apiUrl + '/image/' + width + 'x' + height + '/' + source.id + '.' + source.extension + ' 1x' |
361 |
| - ]; |
362 |
| - if (width * 1.5 <= source.width && height * 1.5 <= source.height) { |
363 |
| - array.push(apiUrl + '/image/' + width * 1.5 + 'x' + height * 1.5 + '/' + source.id + '.' + source.extension + ' 1.5x'); |
364 |
| - if (width * 2 <= source.width && height * 2 <= source.height) { |
365 |
| - array.push(apiUrl + '/image/' + width * 2 + 'x' + height * 2 + '/' + source.id + '.' + source.extension + ' 2x'); |
366 |
| - } |
367 |
| - } |
368 |
| - |
369 |
| - return array.join(',\n') |
370 |
| -} |
371 |
| - |
372 | 263 |
|
373 | 264 | const getType = (propertyConfig, required, property, ctdName) => {
|
374 | 265 |
|
@@ -448,3 +339,33 @@ const getType = (propertyConfig, required, property, ctdName) => {
|
448 | 339 | return 'FlotiqBlock'
|
449 | 340 | }
|
450 | 341 | };
|
| 342 | + |
| 343 | +const generateImageSource = (baseURL, width = 0, height = 0, format, fit, options) => { |
| 344 | + const src = `https://api.flotiq.com/image/${width || options.width}x${height || options.height}/${baseURL}.${format}` |
| 345 | + return {src, width, height, format} |
| 346 | +} |
| 347 | + |
| 348 | + |
| 349 | +const resolveGatsbyImageData = async (image, options) => { |
| 350 | + const filename = image.id |
| 351 | + const sourceMetadata = { |
| 352 | + width: image.width, |
| 353 | + height: image.height, |
| 354 | + format: image.extension |
| 355 | + } |
| 356 | + const imageDataArgs = { |
| 357 | + ...options, |
| 358 | + pluginName: `gatsby-source-flotiq`, |
| 359 | + sourceMetadata, |
| 360 | + filename, |
| 361 | + placeholderURL: '', |
| 362 | + generateImageSource, |
| 363 | + formats: [image.extension], |
| 364 | + options: {...options, extension: image.extension}, |
| 365 | + } |
| 366 | + // if(options.placeholder === "blurred") { |
| 367 | + // const lowResImage = getLowResolutionImageURL(imageDataArgs) |
| 368 | + // imageDataArgs.placeholderURL = await getBase64Image(lowResImage) |
| 369 | + // } |
| 370 | + return generateImageData(imageDataArgs) |
| 371 | +} |
0 commit comments