-
Notifications
You must be signed in to change notification settings - Fork 24
/
author.js
175 lines (157 loc) · 5.2 KB
/
author.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React from "react"
import { graphql } from "gatsby"
import { stringify } from "qs"
import { ImageElement, getGatsbyImageData } from "@kontent-ai/gatsby-components"
import { ImageUrlTransformationBuilder } from "@kontent-ai/delivery-sdk"
import { Layout } from "../components/layout"
const Author = ({ data }) => {
if (!data.author.elements.avatar_image.value.length) {
return <pre>Author avatar note set.</pre>
}
const showcaseWidth = 250
const avatar = data.author.elements.avatar_image.value[0]
const rectSelection = [
160,
70,
0.7,
0.9, // https://docs.kontent.ai/reference/image-transformation#a-source-rectangle-region
]
const imageQuery = {
rect: rectSelection.join(","),
w: 230, // https://docs.kontent.ai/reference/image-transformation#a-image-width
}
// https://github.com/kontent-ai/delivery-sdk-js#image-transformation
const deliverySDKTransformedUrl = new ImageUrlTransformationBuilder(avatar.url)
.withRectangleCrop(...rectSelection)
.withWidth(imageQuery.w)
const imageData = {
image: avatar,
width: 800,
height: 200,
backgroundColor: "#bbbbbb"
}
const imageGatsbyData = getGatsbyImageData(imageData)
return (
<Layout>
<header>
<h1>{data.author.elements.name.value}</h1>
</header>
<section>
<ImageElement
alt={avatar.description}
{...imageData}
/>
<p>
This uses the <a href="https://github.com/kontent-ai/gatsby-packages/tree/master/packages/gatsby-components">ImageElement</a> component to display a
responsive image.
</p>
<div style={{ "max-width": "800px", "overflow": "auto" }}>
<p>
There is also exported function called getGatsbyImageData to obtain data used for GatsbyImage specifically. The following example shows the data for the image above.
</p>
<pre>{`const imageData = getGatsbyImageData({ image: avatar, width: 800, height: 200, backgroundColor:"#bbbbbb"})`}
</pre>
<pre>
{JSON.stringify(imageGatsbyData, undefined, 2).replace(/\\n/g, "\n")}
</pre>
</div>
</section>
<article
style={{ display: "flex", flexWrap: "wrap", alignItems: "flex-end" }}
>
<section style={{ padding: "1em", maxWidth: showcaseWidth }}>
<img
src={avatar.url}
alt={avatar.description || "Author avatar image"}
width="200px"
/>
<p>
This is the basic loading of the asset in full size and setting its
width right in <tt>img</tt> tag (don't do that).
</p>
</section>
<section style={{ padding: "1em", maxWidth: showcaseWidth }}>
<img
src={`${avatar.url}?${stringify(imageQuery, { encode: false })}`}
alt={avatar.description || "Author avatar image"}
/>
<p>
Showcase using{" "}
<a href="https://docs.kontent.ai/reference/image-transformation">
Kontent.ai Image transformation API
</a>{" "}
and <a href="https://www.npmjs.com/package/qs">qs npm package.</a>{" "}
for query string construction.
</p>
</section>
</article>
<article
style={{ display: "flex", flexWrap: "wrap", alignItems: "flex-end" }}
>
<section style={{ padding: "1em", maxWidth: showcaseWidth }}>
<img
src={deliverySDKTransformedUrl.getUrl()}
alt={avatar.description || "Author avatar image"}
/>
<p>
Showcase using{" "}
<a href="https://docs.kontent.ai/reference/image-transformation">
Kontent.ai Image transformation API
</a>{" "}
and{" "}
<a href="https://www.npmjs.com/package/@kontent-ai/delivery-sdk">
Delivery SDK
</a>{" "}
for query string construction.
</p>
</section>
{avatar.renditions &&
<section style={{ padding: "1em", maxWidth: showcaseWidth }}>
<img
src={avatar.url + '?' + avatar.renditions.default.query}
alt={avatar.description || "Author avatar image"}
width={avatar.renditions.default.width}
/>
<p>
Showcase using{" "}
<a href="https://docs.kontent.ai/reference/image-transformation">
Kontent.ai Image transformation API
</a>{" "}
and{" "}
<a href="https://kontent.ai/learn/tutorials/develop-apps/get-content/customized-images/">
Asset Renditions
</a>{" "}
for query string construction.
</p>
</section>
}
</article>
</Layout>
)
}
export const query = graphql`
{
author: kontentItemAuthor {
elements {
name {
value
}
avatar_image {
value {
url
width
height
description
type
renditions {
default {
query
}
}
}
}
}
}
}
`
export default Author