Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
23a681a9
Commit
23a681a9
authored
Jul 15, 2015
by
Randall Leeds
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move PDF cache into pdf anchoring module
parent
4d0019c9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
36 deletions
+30
-36
pdf.coffee
h/static/scripts/annotator/anchoring/pdf.coffee
+29
-21
pdf.coffee
h/static/scripts/annotator/plugin/pdf.coffee
+1
-15
No files found.
h/static/scripts/annotator/anchoring/pdf.coffee
View file @
23a681a9
...
...
@@ -7,6 +7,11 @@ html = require('./html')
{
TextPositionAnchor
,
TextQuoteAnchor
}
=
require
(
'./types'
)
# Caches for performance
pageTextCache
=
{}
quotePositionCache
=
{}
getSiblingIndex
=
(
node
)
->
siblings
=
Array
.
prototype
.
slice
.
call
(
node
.
parentNode
.
childNodes
)
return
siblings
.
indexOf
(
node
)
...
...
@@ -22,34 +27,35 @@ getPage = (pageIndex) ->
return
PDFViewerApplication
.
pdfViewer
.
pages
[
pageIndex
]
getPageTextContent
=
(
pageIndex
,
cache
)
->
if
c
ache
[
pageIndex
]
?
return
Promise
.
resolve
(
c
ache
[
pageIndex
])
getPageTextContent
=
(
pageIndex
)
->
if
pageTextC
ache
[
pageIndex
]
?
return
Promise
.
resolve
(
pageTextC
ache
[
pageIndex
])
else
joinItems
=
({
items
})
->
nonEmpty
=
(
item
.
str
for
item
in
items
when
/\S/
.
test
(
item
.
str
))
textContent
=
nonEmpty
.
join
(
''
)
c
ache
[
pageIndex
]
=
textContent
pageTextC
ache
[
pageIndex
]
=
textContent
return
textContent
return
PDFViewerApplication
.
pdfViewer
.
getPageTextContent
(
pageIndex
)
.
then
(
joinItems
)
getPageOffset
=
(
pageIndex
,
cache
=
{})
->
getPageOffset
=
(
pageIndex
)
->
index
=
-
1
next
=
(
offset
)
->
if
++
index
is
pageIndex
return
Promise
.
resolve
(
offset
)
return
getPageTextContent
(
index
,
cache
)
return
getPageTextContent
(
index
)
.
then
((
textContent
)
->
next
(
offset
+
textContent
.
length
))
return
next
(
0
)
findPage
=
(
offset
,
cache
=
{}
)
->
findPage
=
(
offset
)
->
index
=
0
total
=
0
...
...
@@ -60,9 +66,9 @@ findPage = (offset, cache = {}) ->
else
index
++
total
+=
textContent
.
length
return
getPageTextContent
(
index
,
cache
).
then
(
count
)
return
getPageTextContent
(
index
).
then
(
count
)
return
getPageTextContent
(
0
,
cache
).
then
(
count
)
return
getPageTextContent
(
0
).
then
(
count
)
###*
...
...
@@ -79,8 +85,6 @@ findPage = (offset, cache = {}) ->
# :rtype: Promise
####
exports
.
anchor
=
(
root
,
selectors
,
options
=
{})
->
cache
=
options
.
cache
?
{}
# Selectors
position
=
null
quote
=
null
...
...
@@ -125,7 +129,7 @@ exports.anchor = (root, selectors, options = {}) ->
if
position
?
promise
=
promise
.
catch
->
return
findPage
(
position
.
start
,
cache
.
pageText
)
return
findPage
(
position
.
start
)
.
then
({
index
,
offset
,
textContent
})
->
page
=
getPage
(
index
)
start
=
position
.
start
-
offset
...
...
@@ -140,14 +144,14 @@ exports.anchor = (root, selectors, options = {}) ->
{
pagesCount
}
=
PDFViewerApplication
.
pdfViewer
if
position
?
if
cache
.
quotePosition
[
quote
.
exact
]
?
[
position
.
start
]
?
{
page
,
anchor
}
=
cache
.
quotePosition
[
quote
.
exact
][
position
.
start
]
if
quotePositionCache
[
quote
.
exact
]
?
[
position
.
start
]
?
{
page
,
anchor
}
=
quotePositionCache
[
quote
.
exact
][
position
.
start
]
return
anchorByPosition
(
page
,
anchor
)
findInPages
=
([
pageIndex
,
rest
...])
->
page
=
getPage
(
pageIndex
)
content
=
getPageTextContent
(
pageIndex
,
cache
.
pageText
)
offset
=
getPageOffset
(
pageIndex
,
cache
.
pageText
)
content
=
getPageTextContent
(
pageIndex
)
offset
=
getPageOffset
(
pageIndex
)
Promise
.
all
([
content
,
offset
,
page
])
.
then
(
results
)
->
[
content
,
offset
,
page
]
=
results
...
...
@@ -157,8 +161,8 @@ exports.anchor = (root, selectors, options = {}) ->
pageOptions
.
hint
=
position
.
start
-
offset
anchor
=
new
TextQuoteAnchor
.
fromSelector
(
root
,
quote
)
anchor
=
anchor
.
toPositionAnchor
(
pageOptions
)
cache
.
quotePosition
[
quote
.
exact
]
?=
{}
cache
.
quotePosition
[
quote
.
exact
][
position
.
start
]
=
{
page
,
anchor
}
quotePositionCache
[
quote
.
exact
]
?=
{}
quotePositionCache
[
quote
.
exact
][
position
.
start
]
=
{
page
,
anchor
}
return
anchorByPosition
(
page
,
anchor
)
.
catch
->
if
rest
.
length
...
...
@@ -169,7 +173,7 @@ exports.anchor = (root, selectors, options = {}) ->
pages
=
[
0
...
pagesCount
]
if
position
?
return
findPage
(
position
.
start
,
cache
.
pageText
)
return
findPage
(
position
.
start
)
.
then
({
index
,
offset
,
textContent
})
->
left
=
pages
.
slice
(
0
,
index
)
right
=
pages
.
slice
(
index
)
...
...
@@ -187,7 +191,6 @@ exports.anchor = (root, selectors, options = {}) ->
exports
.
describe
=
(
root
,
range
,
options
=
{})
->
cache
=
options
.
cache
?
{}
range
=
new
xpathRange
.
BrowserRange
(
range
).
normalize
()
startTextLayer
=
getNodeTextLayer
(
range
.
start
)
...
...
@@ -208,7 +211,7 @@ exports.describe = (root, range, options = {}) ->
start
=
seek
(
iter
,
range
.
start
)
end
=
seek
(
iter
,
range
.
end
)
+
start
+
range
.
end
.
textContent
.
length
return
getPageOffset
(
startPageIndex
,
cache
.
pageText
).
then
(
pageOffset
)
->
return
getPageOffset
(
startPageIndex
).
then
(
pageOffset
)
->
# XXX: range covers only one page
start
+=
pageOffset
end
+=
pageOffset
...
...
@@ -222,3 +225,8 @@ exports.describe = (root, range, options = {}) ->
quote
=
TextQuoteAnchor
.
fromRange
(
root
,
r
,
options
).
toSelector
(
options
)
return
Promise
.
all
([
position
,
quote
])
exports
.
purgeCache
=
->
pageTextCache
=
{}
quotePositionCache
=
{}
h/static/scripts/annotator/plugin/pdf.coffee
View file @
23a681a9
...
...
@@ -9,21 +9,7 @@ class PDF extends Annotator.Plugin
pdfViewer
:
null
pluginInit
:
->
cache
=
{
pageText
:
{}
quotePosition
:
{}
}
anchoring
=
require
(
'../anchoring/pdf'
)
@
annotator
.
anchoring
=
{
anchor
:
(
root
,
selectors
,
options
=
{})
->
options
=
extend
({},
options
,
{
cache
})
return
anchoring
.
anchor
(
root
,
selectors
,
options
)
describe
:
(
root
,
range
,
options
=
{})
->
options
=
extend
({},
options
,
{
cache
})
return
anchoring
.
describe
(
root
,
range
,
options
)
}
@
annotator
.
anchoring
=
require
(
'../anchoring/pdf'
)
@
pdfViewer
=
PDFViewerApplication
.
pdfViewer
@
pdfViewer
.
viewer
.
classList
.
add
(
'has-transparent-text-layer'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment