Skip to content

Commit

Permalink
Use default navigation for links in Assigned, and open them in new ta…
Browse files Browse the repository at this point in the history
…bs (#1919)

* Use default navigation for links in Assigned, and open them in new tabs

* Code review changes

* Move function to contentDOMTransformations

Revert test data and snapshot updates

* Fix code coverage

* Update routes.spec.tsx.snap

* use basic dynos on heroku

---------

Co-authored-by: tom <[email protected]>
Co-authored-by: staxly[bot] <35789409+staxly[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 21, 2023
1 parent 2f93b13 commit 9040a4c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"formation": {
"web": {
"size": "hobby"
"size": "basic"
}
},
"addons": [
Expand Down
2 changes: 1 addition & 1 deletion src/app/content/__snapshots__/routes.spec.tsx.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/app/content/components/Assigned.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default () => {
<ErrorModal />
<ErrorBoundary>
<AssignedTopBar section={section} />
<Page>
<Page lockNavigation={true} >
{prevNext
? <PrevNextBar
book={book}
Expand Down
6 changes: 5 additions & 1 deletion src/app/content/components/Page/PageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { preloadedPageIdIs } from '../../utils';
import getCleanContent from '../../utils/getCleanContent';
import PageToasts from '../Page/PageToasts';
import { PagePropTypes } from './connector';
import { transformContent } from './contentDOMTransformations';
import { transformContent, linksToOtherPagesOpenInNewTab } from './contentDOMTransformations';
import * as contentLinks from './contentLinkHandler';
import highlightManager, { stubHighlightManager, UpdateOptions as HighlightUpdateOptions } from './highlightManager';
import * as lazyResources from './lazyResourceManager';
Expand Down Expand Up @@ -47,6 +47,10 @@ export default class PageComponent extends Component<PagePropTypes> {

transformContent(parsedContent, parsedContent.body, this.props.intl);

if (this.props.lockNavigation) {
linksToOtherPagesOpenInNewTab(parsedContent.body, this.props.currentPath);
}

/* this will be removed when all the books are in good order */
/* istanbul ignore else */
if (APP_ENV !== 'production') {
Expand Down
1 change: 1 addition & 0 deletions src/app/content/components/Page/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface PagePropTypes {
addToast: typeof addToast;
systemQueryParams: SystemQueryParams;
textSize: TextResizerValue;
lockNavigation: boolean;
}

export default connect(
Expand Down
13 changes: 13 additions & 0 deletions src/app/content/components/Page/contentDOMTransformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import { Document, HTMLElement } from '@openstax/types/lib.dom';
import { IntlShape } from 'react-intl';
import { assertNotNull } from '../../../utils';
import { expandClosestSolution } from '../../utils/domUtils';
import { fromRelativeUrl } from '../../utils/urlUtils';

export function linksToOtherPagesOpenInNewTab(rootEl: HTMLElement, currentPath: string) {
rootEl.querySelectorAll('a[href]').forEach(
(link) => {
const pathname = fromRelativeUrl(currentPath, link.getAttribute('href') as string);

if (pathname !== currentPath) {
link.setAttribute('target', '_blank');
}
}
);
}

// from https://github.com/openstax/webview/blob/f95b1d0696a70f0b61d83a85c173102e248354cd
// .../src/scripts/modules/media/body/body.coffee#L123
Expand Down
3 changes: 2 additions & 1 deletion src/app/content/components/Page/contentLinkHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ export const contentLinkHandler = (anchor: HTMLAnchorElement, getProps: () => Co
hasUnsavedHighlight,
} = getProps();
const href = anchor.getAttribute('href');
const target = anchor.getAttribute('target');

if (!href || !book || !page || isClickWithModifierKeys(e)) {
if (!href || !book || !page || target === '_blank' || isClickWithModifierKeys(e)) {
return;
}

Expand Down

Large diffs are not rendered by default.

0 comments on commit 9040a4c

Please sign in to comment.