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
610c67d8
Commit
610c67d8
authored
Jun 30, 2015
by
Randall Leeds
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up the PDF cache usage
parent
a6d56437
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
27 deletions
+29
-27
pdf.coffee
h/static/scripts/annotator/anchoring/pdf.coffee
+26
-27
pdf.coffee
h/static/scripts/annotator/plugin/pdf.coffee
+3
-0
No files found.
h/static/scripts/annotator/anchoring/pdf.coffee
View file @
610c67d8
...
@@ -22,30 +22,33 @@ getPage = (pageIndex) ->
...
@@ -22,30 +22,33 @@ getPage = (pageIndex) ->
return
PDFViewerApplication
.
pdfViewer
.
pages
[
pageIndex
]
return
PDFViewerApplication
.
pdfViewer
.
pages
[
pageIndex
]
getPageTextContent
=
(
pageIndex
)
->
getPageTextContent
=
(
pageIndex
,
cache
)
->
return
PDFViewerApplication
.
pdfViewer
.
getPageTextContent
(
pageIndex
)
if
cache
[
pageIndex
]
?
.
then
((
textContent
)
->
(
item
.
str
for
item
in
textContent
.
items
).
join
(
''
))
return
Promise
.
resolve
(
cache
[
pageIndex
])
else
return
PDFViewerApplication
.
pdfViewer
.
getPageTextContent
(
pageIndex
)
.
then
((
textContent
)
->
(
item
.
str
for
item
in
textContent
.
items
).
join
(
''
))
.
then
((
textContent
)
->
cache
[
pageIndex
]
=
textContent
return
textContent
)
getPageOffset
=
(
pageIndex
,
cache
=
{})
->
getPageOffset
=
(
pageIndex
,
cache
=
{})
->
index
=
-
1
index
=
-
1
if
cache
[
pageIndex
]
?
return
Promise
.
resolve
(
cache
[
pageIndex
])
next
=
(
offset
)
->
next
=
(
offset
)
->
if
++
index
is
pageIndex
if
++
index
is
pageIndex
cache
[
pageIndex
]
=
offset
return
Promise
.
resolve
(
offset
)
return
Promise
.
resolve
(
offset
)
return
getPageTextContent
(
index
)
return
getPageTextContent
(
index
,
cache
)
.
then
((
textContent
)
->
next
(
offset
+
textContent
.
length
))
.
then
((
textContent
)
->
next
(
offset
+
textContent
.
length
))
return
next
(
0
)
return
next
(
0
)
findPage
=
(
offset
)
->
findPage
=
(
offset
,
cache
=
{}
)
->
index
=
0
index
=
0
total
=
0
total
=
0
...
@@ -56,9 +59,9 @@ findPage = (offset) ->
...
@@ -56,9 +59,9 @@ findPage = (offset) ->
else
else
index
++
index
++
total
+=
textContent
.
length
total
+=
textContent
.
length
return
getPageTextContent
(
index
).
then
(
count
)
return
getPageTextContent
(
index
,
cache
).
then
(
count
)
return
getPageTextContent
(
0
).
then
(
count
)
return
getPageTextContent
(
0
,
cache
).
then
(
count
)
###*
###*
...
@@ -73,10 +76,7 @@ findPage = (offset) ->
...
@@ -73,10 +76,7 @@ findPage = (offset) ->
# :rtype: Promise
# :rtype: Promise
####
####
exports
.
anchor
=
(
selectors
,
options
=
{})
->
exports
.
anchor
=
(
selectors
,
options
=
{})
->
# Cache of anchoring data
cache
=
options
.
cache
?
{}
cache
=
options
.
cache
?
{}
cache
.
pageOffset
?=
{}
cache
.
quotePosition
?=
{}
# Selectors
# Selectors
position
=
null
position
=
null
...
@@ -122,7 +122,7 @@ exports.anchor = (selectors, options = {}) ->
...
@@ -122,7 +122,7 @@ exports.anchor = (selectors, options = {}) ->
if
position
?
if
position
?
promise
=
promise
.
catch
->
promise
=
promise
.
catch
->
return
findPage
(
position
.
start
)
return
findPage
(
position
.
start
,
cache
.
pageText
)
.
then
({
index
,
offset
,
textContent
})
->
.
then
({
index
,
offset
,
textContent
})
->
page
=
getPage
(
index
)
page
=
getPage
(
index
)
start
=
position
.
start
-
offset
start
=
position
.
start
-
offset
...
@@ -136,18 +136,21 @@ exports.anchor = (selectors, options = {}) ->
...
@@ -136,18 +136,21 @@ exports.anchor = (selectors, options = {}) ->
promise
=
promise
.
catch
->
promise
=
promise
.
catch
->
{
pagesCount
}
=
PDFViewerApplication
.
pdfViewer
{
pagesCount
}
=
PDFViewerApplication
.
pdfViewer
if
cache
.
quotePosition
[
quote
.
exact
]
?
if
position
?
{
page
,
anchor
}
=
cache
.
quotePosition
[
quote
.
exact
]
if
cache
.
quotePosition
[
quote
.
exact
]
?
[
position
.
start
]
?
return
anchorByPosition
(
page
,
anchor
)
{
page
,
anchor
}
=
cache
.
quotePosition
[
quote
.
exact
][
position
.
start
]
return
anchorByPosition
(
page
,
anchor
)
storeAndAnchor
=
(
page
,
anchor
)
->
storeAndAnchor
=
(
page
,
anchor
)
->
cache
.
quotePosition
[
quote
.
exact
]
=
{
page
,
anchor
}
if
position
?
cache
.
quotePosition
[
quote
.
exact
]
?=
{}
cache
.
quotePosition
[
quote
.
exact
][
position
.
start
]
=
{
page
,
anchor
}
return
anchorByPosition
(
page
,
anchor
)
return
anchorByPosition
(
page
,
anchor
)
findInPages
=
([
pageIndex
,
rest
...])
->
findInPages
=
([
pageIndex
,
rest
...])
->
page
=
getPage
(
pageIndex
)
page
=
getPage
(
pageIndex
)
content
=
getPageTextContent
(
pageIndex
)
content
=
getPageTextContent
(
pageIndex
,
cache
.
pageText
)
offset
=
getPageOffset
(
pageIndex
,
cache
.
page
Offse
t
)
offset
=
getPageOffset
(
pageIndex
,
cache
.
page
Tex
t
)
Promise
.
all
([
content
,
offset
,
page
])
Promise
.
all
([
content
,
offset
,
page
])
.
then
(
results
)
->
.
then
(
results
)
->
[
content
,
offset
,
page
]
=
results
[
content
,
offset
,
page
]
=
results
...
@@ -170,7 +173,7 @@ exports.anchor = (selectors, options = {}) ->
...
@@ -170,7 +173,7 @@ exports.anchor = (selectors, options = {}) ->
pages
=
[
0
...
pagesCount
]
pages
=
[
0
...
pagesCount
]
if
position
?
if
position
?
return
findPage
(
position
.
start
)
return
findPage
(
position
.
start
,
cache
.
pageText
)
.
then
({
index
,
offset
,
textContent
})
->
.
then
({
index
,
offset
,
textContent
})
->
left
=
pages
.
slice
(
0
,
index
)
left
=
pages
.
slice
(
0
,
index
)
right
=
pages
.
slice
(
index
)
right
=
pages
.
slice
(
index
)
...
@@ -188,11 +191,7 @@ exports.anchor = (selectors, options = {}) ->
...
@@ -188,11 +191,7 @@ exports.anchor = (selectors, options = {}) ->
exports
.
describe
=
(
range
,
options
=
{})
->
exports
.
describe
=
(
range
,
options
=
{})
->
# Cache of anchoring data
cache
=
options
.
cache
?
{}
cache
=
options
.
cache
?
{}
cache
.
pageOffset
?=
{}
cache
.
quotePosition
?=
{}
range
=
new
xpathRange
.
BrowserRange
(
range
).
normalize
()
range
=
new
xpathRange
.
BrowserRange
(
range
).
normalize
()
startTextLayer
=
getNodeTextLayer
(
range
.
start
)
startTextLayer
=
getNodeTextLayer
(
range
.
start
)
...
@@ -213,7 +212,7 @@ exports.describe = (range, options = {}) ->
...
@@ -213,7 +212,7 @@ exports.describe = (range, options = {}) ->
start
=
seek
(
iter
,
range
.
start
)
start
=
seek
(
iter
,
range
.
start
)
end
=
seek
(
iter
,
range
.
end
)
+
start
+
range
.
end
.
textContent
.
length
end
=
seek
(
iter
,
range
.
end
)
+
start
+
range
.
end
.
textContent
.
length
return
getPageOffset
(
startPageIndex
,
cache
.
page
Offse
t
).
then
(
pageOffset
)
->
return
getPageOffset
(
startPageIndex
,
cache
.
page
Tex
t
).
then
(
pageOffset
)
->
# XXX: range covers only one page
# XXX: range covers only one page
start
+=
pageOffset
start
+=
pageOffset
end
+=
pageOffset
end
+=
pageOffset
...
...
h/static/scripts/annotator/plugin/pdf.coffee
View file @
610c67d8
...
@@ -8,6 +8,9 @@ class PDF extends Annotator.Plugin
...
@@ -8,6 +8,9 @@ class PDF extends Annotator.Plugin
pdfViewer
:
null
pdfViewer
:
null
pluginInit
:
->
pluginInit
:
->
@
annotator
.
anchoringCache
.
pageText
=
{}
@
annotator
.
anchoringCache
.
quotePosition
=
{}
@
pdfViewer
=
PDFViewerApplication
.
pdfViewer
@
pdfViewer
=
PDFViewerApplication
.
pdfViewer
@
pdfViewer
.
viewer
.
classList
.
add
(
'has-transparent-text-layer'
)
@
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