Skip to content

Commit b9f45ce

Browse files
Add image hash
1 parent 099359f commit b9f45ce

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

dotcom-rendering/src/components/GalleryCaption.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type Props = {
1414
format: ArticleFormat;
1515
pageId: string;
1616
webTitle: string;
17+
/** Position of the image in the gallery used to build share fragment */
18+
position?: number;
1719
};
1820

1921
const styles = css`
@@ -68,6 +70,7 @@ export const GalleryCaption = ({
6870
format,
6971
pageId,
7072
webTitle,
73+
position,
7174
}: Props) => {
7275
const emptyCaption = captionHtml === undefined || captionHtml.trim() === '';
7376
const hideCredit =
@@ -101,6 +104,11 @@ export const GalleryCaption = ({
101104
pageId={pageId}
102105
webTitle={webTitle}
103106
context="ImageCaption"
107+
hash={
108+
typeof position === 'number'
109+
? `img-${position}`
110+
: undefined
111+
}
104112
/>
105113
</Island>
106114
</div>

dotcom-rendering/src/components/GalleryImage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export const GalleryImage = ({
128128
format={format}
129129
pageId={pageId}
130130
webTitle={webTitle}
131+
position={image.position}
131132
/>
132133
</figure>
133134
);

dotcom-rendering/src/components/ShareButton.importable.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ type Props = {
1717
size?: Size;
1818
pageId: string;
1919
blockId?: string;
20+
/**
21+
* Optional hash fragment (without the leading '#') to append to the url when sharing.
22+
* If provided this takes precedence over `blockId`.
23+
*/
24+
hash?: string;
2025
webTitle: string;
2126
format: ArticleFormat;
2227
context: Context;
@@ -86,15 +91,23 @@ const liveBlogMobileMeta = (isCopied: boolean) => css`
8691
}
8792
`;
8893

89-
const getUrl = ({ pageId, blockId }: { pageId: string; blockId?: string }) => {
94+
const getUrl = ({
95+
pageId,
96+
blockId,
97+
hash,
98+
}: {
99+
pageId: string;
100+
blockId?: string;
101+
hash?: string;
102+
}) => {
90103
const searchParams = new URLSearchParams({});
91104
searchParams.append('CMP', 'share_btn_url');
92105
if (blockId) searchParams.append('page', `with:block-${blockId}`);
93106

94-
const blockHash = blockId ? `#block-${blockId}` : '';
107+
const fragment = hash ? `#${hash}` : blockId ? `#block-${blockId}` : '';
95108
const paramsString = searchParams.toString();
96109
return new URL(
97-
`${pageId}${paramsString ? '?' + paramsString : ''}${blockHash}`,
110+
`${pageId}${paramsString ? '?' + paramsString : ''}${fragment}`,
98111
'https://www.theguardian.com/',
99112
).href;
100113
};
@@ -181,6 +194,7 @@ export const ShareButton = ({
181194
size = 'small',
182195
pageId,
183196
blockId,
197+
hash,
184198
webTitle,
185199
format,
186200
context,
@@ -202,9 +216,10 @@ export const ShareButton = ({
202216
url: getUrl({
203217
pageId,
204218
blockId,
219+
hash,
205220
}),
206221
}),
207-
[webTitle, pageId, blockId],
222+
[webTitle, pageId, blockId, hash],
208223
);
209224

210225
useEffect(() => {
@@ -251,6 +266,7 @@ export const ShareButton = ({
251266
getUrl({
252267
pageId,
253268
blockId,
269+
hash,
254270
}),
255271
)
256272
.then(() => {
@@ -269,6 +285,7 @@ export const ShareButton = ({
269285
href={`mailto:?subject=${webTitle}&body=${getUrl({
270286
pageId,
271287
blockId,
288+
hash,
272289
})}`}
273290
size={size}
274291
isLiveBlogMeta={isLiveBlogMeta}

0 commit comments

Comments
 (0)