Skip to content

Commit fd88b27

Browse files
committed
2 parents 67707c3 + 1c2a59d commit fd88b27

File tree

6 files changed

+70
-43
lines changed

6 files changed

+70
-43
lines changed

test/UI/event.spec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('UI::event', function () {
1414
beforeEach(function () {
1515
svg = mockSVGContainer();
1616
text = mockTextAnnotation();
17-
17+
1818
document.body.appendChild(svg);
1919
svg.appendChild(text);
2020
svg.style.width = '100px';
@@ -45,22 +45,24 @@ describe('UI::event', function () {
4545
});
4646

4747
it('should emit an event when an annotation is clicked', function (done) {
48+
4849
simulant.fire(svg, 'click', {
49-
clientX: rect.left + 15,
50-
clientY: rect.top + 15
50+
clientX: text.getBoundingClientRect().left + 1,
51+
clientY: text.getBoundingClientRect().top + 1
5152
});
5253

5354
setTimeout(function () {
5455
equal(annotationClickSpy.calledOnce, true);
56+
console.log(annotationClickSpy.getCall(0).args[0], text)
5557
equal(annotationClickSpy.getCall(0).args[0], text);
5658
done();
5759
}, 0);
5860
});
5961

6062
it('should emit an event when an annotation is blurred', function (done) {
6163
simulant.fire(svg, 'click', {
62-
clientX: rect.left + 15,
63-
clientY: rect.top + 15
64+
clientX: text.getBoundingClientRect().left + 1,
65+
clientY: text.getBoundingClientRect().top + 1
6466
});
6567

6668
setTimeout(function () {

test/UI/utils.spec.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
findSVGAtPoint,
1212
findAnnotationAtPoint,
1313
pointIntersectsRect,
14-
getAnnotationRect,
14+
getOffsetAnnotationRect,
1515
scaleUp,
1616
scaleDown,
1717
getScroll,
@@ -41,12 +41,14 @@ function createPath() {
4141
let div;
4242
let svg;
4343
let text;
44+
let textSvgGroup;
4445

4546
describe('UI::utils', function () {
4647
beforeEach(function () {
4748
div = document.createElement('div');
4849
svg = mockSVGContainer();
49-
text = mockTextAnnotation();
50+
textSvgGroup = mockTextAnnotation();
51+
text = textSvgGroup.firstChild;
5052
});
5153

5254
afterEach(function () {
@@ -66,17 +68,16 @@ describe('UI::utils', function () {
6668
});
6769

6870
it('should find svg container', function () {
69-
svg.appendChild(text);
71+
svg.appendChild(textSvgGroup);
7072

71-
equal(findSVGContainer(text), svg);
73+
equal(findSVGContainer(textSvgGroup), svg);
7274
});
7375

7476
it('should find svg at point', function () {
7577
svg.style.width = '10px';
7678
svg.style.height = '10px';
7779
document.body.appendChild(svg);
7880
let rect = svg.getBoundingClientRect();
79-
8081
equal(findSVGAtPoint(rect.left, rect.top), svg);
8182
equal(findSVGAtPoint(rect.left + rect.width, rect.top + rect.height), svg);
8283
equal(findSVGAtPoint(rect.left - 1, rect.top - 1), null);
@@ -85,20 +86,18 @@ describe('UI::utils', function () {
8586

8687
it('should find annotation at point', function () {
8788
text.setAttribute('data-pdf-annotate-type', 'text');
88-
svg.appendChild(text);
89+
svg.appendChild(textSvgGroup);
8990
document.body.appendChild(svg);
9091

9192
let rect = svg.getBoundingClientRect();
9293
let textRect = text.getBoundingClientRect();
9394
let textW = textRect.width;
9495
let textH = textRect.height;
9596
let textX = parseInt(text.getAttribute('x'), 10);
96-
let textY = parseInt(text.getAttribute('y'), 10) - textH; // NOTE this needs to be done to account for how text is rendered
97+
let textY = parseInt(text.getAttribute('y'), 10);
9798

98-
equal(findAnnotationAtPoint(rect.left + textX, rect.top + textY), text);
99-
equal(findAnnotationAtPoint(rect.left + textX + textW, rect.top + textY + textH), text);
100-
equal(findAnnotationAtPoint(rect.left + textX - 1, rect.top + textY - 1), null);
101-
equal(findAnnotationAtPoint(rect.left + textX + textW + 1, rect.top + textY + textH + 1), null);
99+
equal(findAnnotationAtPoint(textRect.left + 1, textRect.top + 1), text);
100+
equal(findAnnotationAtPoint(textRect.right + 1, textRect.bottom + 1), null);
102101
});
103102

104103
it('should detect if a rect collides with points', function () {
@@ -163,18 +162,19 @@ describe('UI::utils', function () {
163162
});
164163

165164
it('should get the size of text', function () {
166-
svg.appendChild(text);
165+
svg.appendChild(textSvgGroup);
167166
document.body.appendChild(svg);
168167

169-
let rect = text.getBoundingClientRect();
168+
let rect = textSvgGroup.getBoundingClientRect();
169+
let svgRect = svg.getBoundingClientRect()
170170

171-
deepEqual(getAnnotationRect(text), {
171+
deepEqual(getOffsetAnnotationRect(text), {
172172
width: rect.width,
173173
height: rect.height,
174-
left: parseInt(text.getAttribute('x'), 10),
175-
top: parseInt(text.getAttribute('y'), 10) - rect.height,
176-
right: parseInt(text.getAttribute('x'), 10) + rect.width,
177-
bottom: parseInt(text.getAttribute('y'), 10)
174+
left: rect.left - svgRect.left,
175+
top: rect.top - svgRect.top,
176+
right: rect.right - svgRect.left,
177+
bottom: rect.bottom - svgRect.top
178178
});
179179
});
180180

@@ -195,7 +195,7 @@ describe('UI::utils', function () {
195195

196196
svg.appendChild(rect);
197197

198-
deepEqual(getAnnotationRect(rect.children[0]), {
198+
deepEqual(getOffsetAnnotationRect(rect.children[0]), {
199199
width: parseInt(rect.children[0].getAttribute('width'), 10),
200200
height: parseInt(rect.children[0].getAttribute('height'), 10),
201201
left: parseInt(rect.children[0].getAttribute('x'), 10),
@@ -205,7 +205,7 @@ describe('UI::utils', function () {
205205
});
206206
});
207207
});
208-
208+
209209
it('should get the size of a rectangle', function () {
210210
document.body.appendChild(svg);
211211
let rect = renderRect({
@@ -236,7 +236,7 @@ describe('UI::utils', function () {
236236
rect.setAttribute('data-pdf-annotate-id', 'ann-foo');
237237
svg.appendChild(rect);
238238

239-
let size = getAnnotationRect(rect);
239+
let size = getOffsetAnnotationRect(rect);
240240

241241
equal(size.left, 53);
242242
equal(size.top, 103);
@@ -270,7 +270,7 @@ describe('UI::utils', function () {
270270
equal(rect.width, 300);
271271
equal(rect.height, 300);
272272
});
273-
273+
274274
it('should scale down', function () {
275275
svg.setAttribute('data-pdf-annotate-viewport', JSON.stringify(mockViewport(undefined, undefined, 1.5)));
276276
let rect = scaleDown(svg, {top: 150, left: 150, width: 300, height: 300});
@@ -309,7 +309,7 @@ describe('UI::utils', function () {
309309

310310
equal(document.head.querySelector('style[data-pdf-annotate-user-select]'), null);
311311
});
312-
312+
313313
it('should get metadata', function () {
314314
let {
315315
documentId,

test/render/appendChild.spec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ function testScale(scale = 0.5, passViewportArg = true) {
1313
x: 200,
1414
y: 100
1515
};
16-
16+
1717
let nested = appendChild(svg, annotation, passViewportArg ? viewport : undefined);
18-
18+
1919
if (isFirefox) {
2020
equal(nested.getAttribute('x'), annotation.x);
2121
equal(nested.getAttribute('y'), annotation.y);
@@ -90,13 +90,13 @@ describe('render::appendChild', function () {
9090
width: 25,
9191
height: 25
9292
}, viewport);
93-
93+
9494
equal(point.getAttribute('data-pdf-annotate-id'), '1234');
9595
equal(point.getAttribute('data-pdf-annotate-type'), 'point');
9696
equal(area.getAttribute('data-pdf-annotate-id'), '5678');
9797
equal(area.getAttribute('data-pdf-annotate-type'), 'area');
9898
});
99-
99+
100100
it('should render area', function () {
101101
let area = appendChild(svg, {
102102
type: 'area',
@@ -146,7 +146,8 @@ describe('render::appendChild', function () {
146146
});
147147

148148
it('should render textbox', function () {
149-
let textbox = appendChild(svg, {
149+
150+
let textboxGroup = appendChild(svg, {
150151
type: 'textbox',
151152
x: 125,
152153
y: 400,
@@ -156,6 +157,7 @@ describe('render::appendChild', function () {
156157
color: '000000',
157158
content: 'Lorem Ipsum'
158159
}, viewport);
160+
let textbox = textboxGroup.firstChild;
159161

160162
equal(textbox.nodeName.toLowerCase(), 'text');
161163
});

test/render/index.spec.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import render from '../../src/render';
22
import mockViewport from '../mockViewport';
33
import { equal } from 'assert';
4+
import uuid from "../../src/utils/uuid"
45

56
function _render(annotations) {
67
let data = Array.isArray(annotations) ? { annotations } : annotations;
@@ -16,29 +17,40 @@ describe('render::index', function () {
1617
viewport = mockViewport();
1718
});
1819

19-
it('should reset SVG on each render', function () {
20+
it('should not render the same annotation multiple times', function () {
2021
let viewport = mockViewport(undefined, undefined, .5);
22+
let id1 = uuid();
23+
let id2 = uuid();
2124

2225
_render([
2326
{
24-
type: 'point',
27+
type: 'area',
2528
x: 0,
26-
y: 0
29+
y: 0,
30+
width: 10,
31+
height: 10,
32+
uuid: id1
2733
}
2834
]);
2935

3036
equal(svg.children.length, 1);
3137

3238
_render([
3339
{
34-
type: 'point',
40+
type: 'area',
3541
x: 0,
36-
y: 0
42+
y: 0,
43+
width: 10,
44+
height: 10,
45+
uuid: id1
3746
},
3847
{
39-
type: 'point',
48+
type: 'area',
4049
x: 25,
41-
y: 25
50+
y: 25,
51+
width: 10,
52+
height: 10,
53+
uuid: id2
4254
}
4355
]);
4456

test/render/renderText.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ describe('render::renderText', function () {
77
const y = 100;
88
const size = 20;
99
const color = '000';
10-
let text = renderText({
10+
let gNode = renderText({
1111
x,
1212
y,
1313
size,
1414
color
1515
});
16+
let text = gNode.firstChild
1617

1718
equal(text.nodeName, 'text');
1819
equal(text.getAttribute('x'), x);
19-
equal(text.getAttribute('y'), y + size);
20+
equal(text.getAttribute('y'), y );
2021
equal(text.getAttribute('fill'), `#${color}`);
2122
equal(text.getAttribute('font-size'), size);
2223
});

web/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ PDFJS.workerSrc = './shared/pdf.worker.js';
1717

1818
// Render stuff
1919
let NUM_PAGES = 0;
20+
let renderedPages = [];
21+
let okToRender = false;
2022
document.getElementById('content-wrapper').addEventListener('scroll', function (e) {
2123
let visiblePageNum = Math.round(e.target.scrollTop / PAGE_HEIGHT) + 1;
2224
let visiblePage = document.querySelector(`.page[data-page-number="${visiblePageNum}"][data-loaded="false"]`);
23-
if (visiblePage) {
25+
26+
if (renderedPages.indexOf(visiblePageNum) == -1){
27+
okToRender = true;
28+
renderedPages.push(visiblePageNum);
29+
} else {
30+
okToRender = false;
31+
}
32+
33+
if (visiblePage && okToRender) {
2434
setTimeout(function () {
2535
UI.renderPage(visiblePageNum, RENDER_OPTIONS);
2636
});

0 commit comments

Comments
 (0)