Skip to content

Commit c4eaedf

Browse files
committed
Display linked chapters and sections in preview link tooltip
The tooltip for links to entries that are not published yet or password protected names the linked chapter or section. Its preview link points to the chapter or section in the draft.
1 parent 3717fd0 commit c4eaedf

5 files changed

Lines changed: 63 additions & 13 deletions

File tree

‎entry_types/scrolled/config/locales/de.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,3 +2190,4 @@ de:
21902190
open_draft_preview: Vorschau des Entwurfs öffnen
21912191
open_preview: Vorschau öffnen
21922192
password_protected: Passwortgeschützt
2193+
section_in: Abschnitt in

‎entry_types/scrolled/config/locales/en.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,3 +2016,4 @@ en:
20162016
open_draft_preview: Open draft preview
20172017
open_preview: Open preview
20182018
password_protected: Password protected
2019+
section_in: Section in

‎entry_types/scrolled/package/spec/frontend/commenting/features/linkTooltip-spec.js‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ describe('link tooltip in commenting mode', () => {
1414
'pageflow_scrolled.review.link_tooltip.password_protected': 'Password protected',
1515
'pageflow_scrolled.review.link_tooltip.open_preview': 'Open preview',
1616
'pageflow_scrolled.review.link_tooltip.open_draft_preview': 'Open draft preview',
17+
'pageflow_scrolled.review.link_tooltip.section_in': 'Section in',
1718
'pageflow_scrolled.inline_editing.link_tooltip.opens_in_same_tab': 'Opens in same tab'
1819
});
1920

20-
function renderLink(href, {publicationState = 'not_published'} = {}) {
21+
function renderLink(href, {publicationState = 'not_published', chapters} = {}) {
2122
return renderEntry({
2223
seed: {
2324
linkedEntries: [{
2425
id: 7,
2526
title: 'Other story',
2627
url: 'https://example.com/other',
2728
previewUrl: '/admin/entries/other/preview',
28-
publicationState
29+
publicationState,
30+
chapters
2931
}]
3032
},
3133
contentElement: {ui: <Link href={href}>A link</Link>}
@@ -92,4 +94,43 @@ describe('link tooltip in commenting mode', () => {
9294

9395
expect(entry.getAllByRole('link')).toHaveLength(1);
9496
});
97+
98+
describe('with chapter or section', () => {
99+
it('offers preview of chapter', async () => {
100+
const user = userEvent.setup();
101+
const entry = renderLink({entry: 7, chapter: 20, signature: 's'}, {
102+
chapters: [{permaId: 20, title: 'New chapter', slug: 'new-chapter',
103+
previewSlug: 'new-chapter'}]
104+
});
105+
106+
await user.hover(entry.getByText('A link'));
107+
108+
expect(entry.getByText('Other story › New chapter')).toBeInTheDocument();
109+
expect(entry.getByRole('link', {name: 'Open preview'}))
110+
.toHaveAttribute('href', '/admin/entries/other/preview#new-chapter');
111+
});
112+
113+
it('offers preview of section', async () => {
114+
const user = userEvent.setup();
115+
const entry = renderLink({entry: 7, section: 30, signature: 's'});
116+
117+
await user.hover(entry.getByText('A link'));
118+
119+
expect(entry.getByText('Section in Other story')).toBeInTheDocument();
120+
expect(entry.getByRole('link', {name: 'Open preview'}))
121+
.toHaveAttribute('href', '/admin/entries/other/preview#section-30');
122+
});
123+
124+
it('does not display tooltip for chapters of published entries', async () => {
125+
const user = userEvent.setup();
126+
const entry = renderLink({entry: 7, chapter: 20, signature: 's'}, {
127+
publicationState: 'published_without_password_protection',
128+
chapters: [{permaId: 20, title: 'Intro', slug: 'intro', previewSlug: 'intro'}]
129+
});
130+
131+
await user.hover(entry.getByText('A link'));
132+
133+
expect(entry.getAllByRole('link')).toHaveLength(1);
134+
});
135+
});
95136
});

‎entry_types/scrolled/package/src/frontend/commenting/Link.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import React from 'react';
22

33
import {PlainLink} from '../Link';
44
import {useLinkTooltip} from '../../shared/linkTooltip';
5-
import {usePreviewableLinkedEntry} from './LinkTooltip';
5+
import {usePreviewableLinkDestination} from './LinkTooltip';
66

77
export function Link(props) {
88
const linkTooltip = useLinkTooltip();
9-
const linkedEntry = usePreviewableLinkedEntry(props.href);
9+
const destination = usePreviewableLinkDestination(props.href);
1010

11-
if (!linkedEntry || !linkTooltip) {
11+
if (!destination || !linkTooltip) {
1212
return <PlainLink {...props} />;
1313
}
1414

‎entry_types/scrolled/package/src/frontend/commenting/LinkTooltip.js‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import {FloatingArrow} from '@floating-ui/react';
33

44
import {useI18n} from '../i18n';
5-
import {useLinkedEntry} from 'pageflow-scrolled/entryState';
5+
import {useLinkedEntryDestination} from 'pageflow-scrolled/entryState';
66
import {
77
LinkTooltipProvider as SharedLinkTooltipProvider,
88
useLinkTooltip
@@ -20,21 +20,24 @@ export function LinkTooltipProvider({children}) {
2020
);
2121
}
2222

23-
export function usePreviewableLinkedEntry(href) {
24-
const linkedEntry = useLinkedEntry(href?.entry);
23+
export function usePreviewableLinkDestination(href) {
24+
const destination = useLinkedEntryDestination(href?.entry ? href : {});
2525

26-
return linkedEntry && previewLabels[linkedEntry.publicationState] ? linkedEntry : null;
26+
return destination && previewLabels[destination.linkedEntry.publicationState] ?
27+
destination :
28+
null;
2729
}
2830

2931
function LinkTooltip({setFloating, floatingStyles, floatingContext, arrowRef, state}) {
3032
const {keep, deactivate} = useLinkTooltip();
31-
const linkedEntry = usePreviewableLinkedEntry(state?.href);
33+
const destination = usePreviewableLinkDestination(state?.href);
3234
const {t} = useI18n({locale: 'ui'});
3335

34-
if (!linkedEntry) {
36+
if (!destination) {
3537
return null;
3638
}
3739

40+
const {linkedEntry} = destination;
3841
const labels = previewLabels[linkedEntry.publicationState];
3942

4043
return (
@@ -47,13 +50,17 @@ function LinkTooltip({setFloating, floatingStyles, floatingContext, arrowRef, st
4750
<FloatingArrow ref={arrowRef}
4851
context={floatingContext} />
4952
<div className={styles.entry}>
50-
<span>{linkedEntry.title}</span>
53+
<span>
54+
{destination.section ?
55+
`${t('pageflow_scrolled.review.link_tooltip.section_in')} ${linkedEntry.title}` :
56+
[linkedEntry.title, destination.chapter?.title].filter(Boolean).join(' › ')}
57+
</span>
5158
<span className={styles.pill}>
5259
{t(`pageflow_scrolled.review.link_tooltip.${labels.state}`)}
5360
</span>
5461
</div>
5562
<div className={styles.previewLink}>
56-
<a href={linkedEntry.previewUrl}
63+
<a href={destination.previewUrl}
5764
target="_blank"
5865
rel="noopener noreferrer">
5966
{t(`pageflow_scrolled.review.link_tooltip.${labels.link}`)}

0 commit comments

Comments
 (0)