Skip to content

Commit 4b6aaef

Browse files
Fix PDF behaviour
- Open PDFs from search injection - Make open flow of missing pdfs easier to understand
1 parent f1d3a96 commit 4b6aaef

File tree

11 files changed

+178
-84
lines changed

11 files changed

+178
-84
lines changed

src/content-scripts/content_script/global.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ export async function main(
259259
RemoteContentSharingByTabsInterface<'caller'>
260260
>()
261261
const searchBG = runInBackground<RemoteSearchInterface>()
262+
const pdfViewerBG = runInBackground<PDFRemoteInterface>()
262263
const contentScriptsBG = runInBackground<
263264
ContentScriptsInterface<'caller'>
264265
>()
@@ -1081,6 +1082,16 @@ export async function main(
10811082
openPDFinViewer: async (originalPageURL) => {
10821083
let urlToOpen = originalPageURL
10831084

1085+
if (
1086+
urlToOpen.includes('memex.cloud') &&
1087+
urlToOpen.includes('upload_id')
1088+
) {
1089+
const url = new URL(urlToOpen)
1090+
const uploadId = url.searchParams.get('upload_id')
1091+
urlToOpen = await pdfViewerBG.getTempPdfAccessUrl(
1092+
uploadId,
1093+
)
1094+
}
10841095
if (
10851096
urlToOpen.includes('https://arxiv.org/pdf/') &&
10861097
!urlToOpen.includes('.pdf')
@@ -1091,7 +1102,9 @@ export async function main(
10911102
await contentScriptsBG.openPdfInViewer({
10921103
fullPageUrl: urlToOpen,
10931104
})
1105+
return true
10941106
},
1107+
10951108
events: sidebarEvents,
10961109
browserAPIs: browser,
10971110
})
@@ -1221,6 +1234,32 @@ export async function main(
12211234
services: createUIServices(),
12221235
renderUpdateNotifBanner: () => null,
12231236
bgScriptBG,
1237+
openPDFinViewer: async (originalPageURL) => {
1238+
let urlToOpen = originalPageURL
1239+
1240+
if (
1241+
urlToOpen.includes('memex.cloud') &&
1242+
urlToOpen.includes('upload_id')
1243+
) {
1244+
const url = new URL(urlToOpen)
1245+
const uploadId = url.searchParams.get('upload_id')
1246+
console.log('uploadId', uploadId)
1247+
urlToOpen = await pdfViewerBG.getTempPdfAccessUrl(
1248+
uploadId,
1249+
)
1250+
console.log('urlToOpen', urlToOpen)
1251+
}
1252+
if (
1253+
urlToOpen.includes('https://arxiv.org/pdf/') &&
1254+
!urlToOpen.includes('.pdf')
1255+
) {
1256+
urlToOpen = urlToOpen.concat('.pdf')
1257+
}
1258+
1259+
await contentScriptsBG.openPdfInViewer({
1260+
fullPageUrl: urlToOpen,
1261+
})
1262+
},
12241263
},
12251264
upgradeModalProps: {
12261265
createCheckOutLink: bgScriptBG.createCheckoutLink,

src/content-scripts/content_script/in-page-ui-injections.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export const main: InPageUIInjectionsMain = async ({
8282
query: 'settings',
8383
},
8484
),
85+
searchDisplayProps.searchBG,
86+
searchDisplayProps.openPDFinViewer,
8587
)
8688
} catch (err) {
8789
console.error(err)

src/dashboard-refactor/index.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,14 @@ export class DashboardContainer extends StatefulUIElement<
867867
heightAndWidth={'40px'}
868868
hoverOff
869869
/>
870-
<DropZoneTitle>Drop PDF here to open it</DropZoneTitle>
870+
<DropZoneTitle>
871+
Download, then drop the PDF here
872+
</DropZoneTitle>
873+
<DropZoneSubTitle>
874+
In rare cases the PDF file is not reachable
875+
directly. In this case, you can drag and drop the
876+
PDF file here.
877+
</DropZoneSubTitle>
871878
</DropZoneContent>
872879
</DropZoneFrame>
873880
</DropZoneBackground>
@@ -2375,6 +2382,12 @@ const DropZoneTitle = styled.div`
23752382
text-align: center;
23762383
`
23772384

2385+
const DropZoneSubTitle = styled.div`
2386+
color: ${(props) => props.theme.colors.greyScale6};
2387+
font-size: 18px;
2388+
text-align: center;
2389+
`
2390+
23782391
const MainContent = styled.div<{
23792392
inPageMode: boolean
23802393
}>`

src/dashboard-refactor/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export type DashboardDependencies = {
133133
openSettings?: () => void
134134
bgScriptBG?: RemoteBGScriptInterface<'caller'>
135135
getPortalElement?: () => HTMLElement
136+
openPDFinViewer?: (url: string) => Promise<void>
136137
} & (
137138
| {
138139
inPageMode: true

src/in-page-ui/ribbon/react/containers/ribbon/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface RibbonContainerDependencies {
4545
>
4646
currentUser?: UserReference
4747
getRootElement: () => HTMLElement
48-
openPDFinViewer: (url: string) => Promise<void>
48+
openPDFinViewer: (url: string) => Promise<boolean>
4949
events: AnnotationsSidebarInPageEventEmitter
5050
browserAPIs: Browser
5151
}

src/search-injection/components/ResultItem.js

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,70 @@ import React from 'react'
22
import PropTypes from 'prop-types'
33
import niceTime from 'src/util/nice-time'
44
import styled from 'styled-components'
5-
6-
const showTags = (tags) => {
7-
return tags.map((tag, i) => <TagItem>{tag}</TagItem>)
8-
}
9-
10-
const ResultItem = (props) => (
11-
<RootContainer>
12-
<Root href={props.url} target="_blank" onClick={props.onLinkClick}>
5+
import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize'
6+
import LoadingIndicator from '@worldbrain/memex-common/lib/common-ui/components/loading-indicator'
7+
8+
const ResultItem = (props) => {
9+
return (
10+
<RootContainer>
11+
{props.isLoadingPDFReader && (
12+
<LoadingBlocker>
13+
<LoadingIndicator size={20} />
14+
Opening PDF
15+
</LoadingBlocker>
16+
)}
1317
<InfoContainer>
1418
<DetailsContainer>
15-
<Url>
16-
{
17-
props.url
18-
.split('://')[1]
19-
.replace('www.', '')
20-
.split('/')[0]
21-
}
22-
</Url>
19+
<Url>{normalizeUrl(props.url).split('/')[0]}</Url>
2320
<DisplayTime> {niceTime(props.displayTime)} </DisplayTime>
2421
</DetailsContainer>
2522
<Title>{props.title}</Title>
26-
{props.tags.length > 0 && (
27-
<TagBox>{showTags(props.tags)}</TagBox>
28-
)}
2923
</InfoContainer>
30-
</Root>
31-
</RootContainer>
32-
)
24+
</RootContainer>
25+
)
26+
}
3327

34-
const Root = styled.a`
28+
const RootContainer = styled.div`
29+
height: fit-content;
30+
width: fill-available;
31+
border-bottom: 1px solid ${(props) => props.theme.colors.greyScale3};
32+
background: ${(props) => props.theme.colors.black};
3533
padding: 20px 20px;
3634
text-decoration: none !important;
3735
display: flex;
36+
position: relative;
3837
border-bottom: 1px solid ${(props) => props.theme.colors.greyScale3};
3938
4039
&:last-child {
4140
border-bottom: none;
4241
}
43-
`
44-
45-
const RootContainer = styled.div`
46-
height: fit-content;
47-
width: fill-available;
48-
border-bottom: 1px solid ${(props) => props.theme.colors.greyScale3};
49-
background: ${(props) => props.theme.colors.black};
5042
5143
&:last-child {
5244
border-bottom: none;
5345
}
5446
55-
&:hover ${Root} {
47+
&:hover {
5648
background: ${(props) => props.theme.colors.greyScale2};
49+
cursor: pointer;
5750
}
5851
`
5952

53+
const LoadingBlocker = styled.div`
54+
position: absolute;
55+
top: 0;
56+
left: 0;
57+
width: 100%;
58+
height: 100%;
59+
background: ${(props) => props.theme.colors.black}98;
60+
backdrop-filter: blur(5px);
61+
display: flex;
62+
justify-content: center;
63+
align-items: center;
64+
color: white;
65+
font-size: 14px;
66+
grid-gap: 10px;
67+
`
68+
6069
const InfoContainer = styled.div`
6170
display: flex;
6271
justify-content: center;
@@ -65,26 +74,6 @@ const InfoContainer = styled.div`
6574
width: fill-available;
6675
grid-gap: 8px;
6776
`
68-
69-
const TagItem = styled.div`
70-
padding: 2px 8px;
71-
border-radius: 3px;
72-
background-color: ${(props) => props.theme.colors.greyScale3};
73-
color: white;
74-
display: flex;
75-
align-items: center;
76-
justify-content: center;
77-
height: 20px;
78-
font-size: 12px;
79-
font-weight: 400;
80-
`
81-
82-
const TagBox = styled.div`
83-
margin-top: 5px;
84-
display: flex;
85-
grid-gap: 5px;
86-
`
87-
8877
const Title = styled.div`
8978
font-size: 15px;
9079
color: ${(props) => props.theme.colors.white};
@@ -115,8 +104,7 @@ ResultItem.propTypes = {
115104
displayTime: PropTypes.number.isRequired,
116105
url: PropTypes.string.isRequired,
117106
title: PropTypes.string.isRequired,
118-
tags: PropTypes.array.isRequired,
119-
onLinkClick: PropTypes.func.isRequired,
107+
isLoadingPDFReader: PropTypes.bool,
120108
}
121109

122110
export default ResultItem

src/search-injection/components/Results.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface ResultsProps {
2929
updateQuery: (query: string) => Promise<void>
3030
query: string
3131
openSettings: () => void
32+
openPDFinViewer: (url: string) => Promise<void>
3233
}
3334

3435
interface ResultsState {

src/search-injection/components/container.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import LoadingIndicator from '@worldbrain/memex-common/lib/common-ui/components/
2424
import Icon from '@worldbrain/memex-common/lib/common-ui/components/icon'
2525
import IconBox from '@worldbrain/memex-common/lib/common-ui/components/icon-box'
2626
import { UITaskState } from '@worldbrain/memex-common/lib/main-ui/types'
27+
import SearchBackground from 'src/search/background'
2728

2829
const search = browser.runtime.getURL('/img/search.svg')
2930

@@ -38,6 +39,8 @@ export interface Props {
3839
updateQuery: (query: string) => Promise<void>
3940
query: string
4041
openSettings: () => void
42+
searchBG: SearchBackground
43+
openPDFinViewer: (url: string) => Promise<void>
4144
}
4245

4346
interface State {
@@ -49,6 +52,7 @@ interface State {
4952
isNotif: boolean
5053
position: null | 'side' | 'above'
5154
notification: any
55+
isLoadingPDFReader?: string
5256
isStickyContainer: boolean
5357
}
5458

@@ -85,6 +89,7 @@ class Container extends React.Component<Props, State> {
8589
isNotif: true,
8690
notification: {},
8791
isStickyContainer: true,
92+
isLoadingPDFReader: null,
8893
}
8994

9095
async componentDidMount() {
@@ -171,7 +176,16 @@ class Container extends React.Component<Props, State> {
171176
}
172177
}
173178

174-
handleResultLinkClick = () => {}
179+
handleResultLinkClick = async (url: string) => {
180+
if (url.includes('memex.cloud') || url.includes('pdf')) {
181+
this.setState({ isLoadingPDFReader: url })
182+
183+
await this.props.openPDFinViewer(url)
184+
this.setState({ isLoadingPDFReader: null })
185+
} else {
186+
this.handleClickOpenNewTabButton(url)
187+
}
188+
}
175189

176190
renderResultItems() {
177191
if (this.props.searchResDocs == null) {
@@ -182,18 +196,21 @@ class Container extends React.Component<Props, State> {
182196
)
183197
} else if (this.props.searchResDocs.length > 0) {
184198
const resultItems = this.props.searchResDocs.map((result, i) => (
185-
<>
199+
<ClickItem
200+
onClick={() => this.handleResultLinkClick(result.url)}
201+
>
186202
<ResultItem
187203
key={i}
188-
onLinkClick={this.handleResultLinkClick}
189204
searchEngine={this.props.searchEngine}
190205
{...result}
191206
displayTime={result.displayTime}
192207
url={result.url}
193208
title={result.title}
194-
tags={result.tags}
209+
isLoadingPDFReader={
210+
this.state.isLoadingPDFReader === result.url
211+
}
195212
/>
196-
</>
213+
</ClickItem>
197214
))
198215

199216
return resultItems
@@ -400,7 +417,7 @@ class Container extends React.Component<Props, State> {
400417
<Results
401418
position={this.state.position}
402419
searchEngine={this.props.searchEngine}
403-
totalCount={this.props.searchResDocs.length}
420+
totalCount={this.props.searchResDocs?.length}
404421
seeMoreResults={this.seeMoreResults}
405422
toggleHideResults={this.toggleHideResults}
406423
hideResults={this.state.hideResults}
@@ -417,12 +434,15 @@ class Container extends React.Component<Props, State> {
417434
updateQuery={this.props.updateQuery}
418435
query={this.props.query}
419436
openSettings={this.props.openSettings}
437+
openPDFinViewer={this.props.openPDFinViewer}
420438
/>
421439
</>
422440
)
423441
}
424442
}
425443

444+
const ClickItem = styled.div``
445+
426446
const SearchLink = styled.span`
427447
padding-left: 2px;
428448
cursor: pointer;

0 commit comments

Comments
 (0)