Skip to content

Commit

Permalink
Fix external links in outline view and allow toggling by clicking text
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtcode committed Oct 31, 2023
1 parent 3b96e7d commit 4b079bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pdfjs/pdf.js
1 change: 1 addition & 0 deletions src/common/components/reader-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const ReaderUI = React.forwardRef((props, ref) => {
<OutlineView
outline={state.outline}
onNavigate={props.onNavigate}
onOpenLink={props.onOpenLink}
onUpdate={props.onUpdateOutline}
/>
}
Expand Down
46 changes: 33 additions & 13 deletions src/common/components/sidebar/outline-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,47 @@ import { IconTreeItemCollapsed, IconTreeItemExpanded } from '../common/icons';



function Item({ item, children, onNavigate, onUpdate }) {
function Item({ item, children, onNavigate, onOpenLink, onUpdate }) {
function handleExpandToggleClick(event) {
item.expanded = !item.expanded;
onUpdate();
}

function handleKeyDown(event) {
event.preventDefault();
if (event.key === 'Enter') {
handleExpandToggleClick();
}
else if (!window.rtl && event.key === 'ArrowLeft' || window.rtl && event.key === 'ArrowRight') {
item.expanded = false;
onUpdate();
if (event.key === 'Enter' && item.url) {
onOpenLink(item.url);
}
else if (!window.rtl && event.key === 'ArrowRight' || window.rtl && event.key === 'ArrowLeft') {
item.expanded = true;
onUpdate();
if (item?.items.length) {
if (event.key === 'Enter') {
handleExpandToggleClick();
}
else if (!window.rtl && event.key === 'ArrowLeft' || window.rtl && event.key === 'ArrowRight') {
item.expanded = false;
onUpdate();
}
else if (!window.rtl && event.key === 'ArrowRight' || window.rtl && event.key === 'ArrowLeft') {
item.expanded = true;
onUpdate();
}
}
}

function handleFocus(event) {
onNavigate(item.location);
if (item.location) {
onNavigate(item.location);
}
}

function handleClick() {
if (item.items?.length) {
handleExpandToggleClick();
}
}

function handleURLClick(event) {
event.preventDefault();
onOpenLink(item.url);
}

let toggle;
Expand All @@ -49,14 +67,15 @@ function Item({ item, children, onNavigate, onUpdate }) {
tabIndex={-1}
onFocus={handleFocus}
onKeyDown={handleKeyDown}
>{item.title}</div>
onClick={handleClick}
>{item.title}{item.url && (<> [<a href={item.url} onClick={handleURLClick}>URL</a>]</>)}</div>
</div>
{children && <div className="children">{children}</div>}
</li>
);
}

function OutlineView({ outline, onNavigate, onUpdate}) {
function OutlineView({ outline, onNavigate, onOpenLink, onUpdate}) {
const intl = useIntl();

function handleUpdate() {
Expand All @@ -71,6 +90,7 @@ function OutlineView({ outline, onNavigate, onUpdate}) {
key={index}
item={item}
onNavigate={onNavigate}
onOpenLink={onOpenLink}
onUpdate={handleUpdate}
>
{item.expanded && item.items && renderItems(item.items)}
Expand Down

0 comments on commit 4b079bf

Please sign in to comment.