-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable automatic URL linking #19110
Open
ryzokuken
wants to merge
11
commits into
mozilla:master
Choose a base branch
from
ryzokuken:autolink-demo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−5
Open
Enable automatic URL linking #19110
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
99d90b0
Enable automatic URL linking
ryzokuken e543238
[SQUASHME] Don't inject link annotations from textLayer if annotation…
ryzokuken 0a3d6f9
[SQUASHME] Fix generated link annotation data to match defaults
ryzokuken afe1c4e
Add viewer option to enable autolinking
ryzokuken 40d16f8
[SQUASHME] Remove redundant annotation options
ryzokuken f2a988f
only create valid urls, fix lint errors and render multiline annotations
ryzokuken f44093f
move linking logic to 'static' class
ryzokuken ebeee90
make processLinks sync by using textContent from textLayer
ryzokuken a20d5d9
make processLinks switch to imperative style to be cleaner and consis…
ryzokuken 50ad6b1
simplify fetching textContents
ryzokuken 7849bc0
fix bug with textContents and check links for duplicates
ryzokuken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { createValidAbsoluteUrl, Util } from "../src/shared/util.js"; | ||
import { getOriginalIndex, normalize } from "./pdf_find_controller.js"; | ||
|
||
class Autolinker { | ||
static #urlRegex = | ||
/\b(?:https?:\/\/|mailto:|www.)(?:[[\S--\[]--\p{P}]|\/|[\p{P}--\[]+[[\S--\[]--\p{P}])+/gmv; | ||
|
||
|
||
static #addLinkAnnotations(url, index, length, pdfPageView) { | ||
// TODO refactor out the logic for a single match from this function | ||
const convertedMatch = pdfPageView._textHighlighter._convertMatches( | ||
[index], | ||
[length] | ||
)[0]; | ||
|
||
const range = new Range(); | ||
range.setStart( | ||
pdfPageView._textHighlighter.textDivs[convertedMatch.begin.divIdx] | ||
.firstChild, | ||
convertedMatch.begin.offset | ||
); | ||
range.setEnd( | ||
pdfPageView._textHighlighter.textDivs[convertedMatch.end.divIdx] | ||
.firstChild, | ||
convertedMatch.end.offset | ||
); | ||
|
||
const pageBox = pdfPageView.textLayer.div.getBoundingClientRect(); | ||
const linkAnnotations = []; | ||
for (const linkBox of range.getClientRects()) { | ||
if (linkBox.width === 0 || linkBox.height === 0) { | ||
continue; | ||
} | ||
|
||
const bottomLeft = pdfPageView.getPagePoint( | ||
linkBox.left - pageBox.left, | ||
linkBox.top - pageBox.top | ||
); | ||
const topRight = pdfPageView.getPagePoint( | ||
linkBox.left - pageBox.left + linkBox.width, | ||
linkBox.top - pageBox.top + linkBox.height | ||
); | ||
|
||
const rect = Util.normalizeRect([ | ||
bottomLeft[0], | ||
bottomLeft[1], | ||
topRight[0], | ||
topRight[1], | ||
]); | ||
|
||
linkAnnotations.push({ | ||
unsafeUrl: url, | ||
url, | ||
rect, | ||
annotationType: 2, | ||
rotation: 0, | ||
// This is just the default for AnnotationBorderStyle. At some point we | ||
// should switch to something better like `new LinkAnnotation` here. | ||
borderStyle: { | ||
width: 1, | ||
rawWidth: 1, | ||
style: 1, // SOLID | ||
dashArray: [3], | ||
horizontalCornerRadius: 0, | ||
verticalCornerRadius: 0, | ||
}, | ||
}); | ||
} | ||
return linkAnnotations; | ||
} | ||
|
||
static processLinks(pdfPageView) { | ||
const [text, diffs] = normalize( | ||
pdfPageView._textHighlighter.textContentItemsStr.join("\n") | ||
); | ||
const matches = text.matchAll(Autolinker.#urlRegex); | ||
const links = []; | ||
for (const match of matches) { | ||
const url = createValidAbsoluteUrl(match[0]); | ||
if (url) { | ||
const [index, length] = getOriginalIndex( | ||
diffs, | ||
match.index, | ||
match[0].length | ||
); | ||
links.push( | ||
...this.#addLinkAnnotations(url.href, index, length, pdfPageView) | ||
); | ||
} | ||
} | ||
return links; | ||
} | ||
} | ||
|
||
export { Autolinker }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to avoid some corner case bug, maybe a better way to do would be to compute the ratio area(intersection) / area(annotation.rect) and if it's greater than a threshold then consider that links are very likely the same.