Skip to content

Commit

Permalink
switched the entire library so that we need to specify userId
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Nazzaro committed Jul 29, 2018
1 parent 5b58611 commit 8a47af9
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/PDFJSAnnotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default {
* @param {String} pageNumber The page number
* @return {Promise}
*/
getAnnotations(documentId, pageNumber) {
getAnnotations(documentId, userId, pageNumber) {
return this.getStoreAdapter().getAnnotations(...arguments);
},

Expand Down
4 changes: 2 additions & 2 deletions src/UI/arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ function handleDocumentMousedown(e) {
}

let svg = findSVGContainer(target);
let { documentId } = getMetadata(svg);
let { documentId, userId } = getMetadata(svg);
let annotationId = target.getAttribute('data-pdf-annotate-id');

let event = e;
PDFJSAnnotate.getStoreAdapter().getAnnotation(documentId, annotationId).then((annotation) => {
PDFJSAnnotate.getStoreAdapter().getAnnotation(documentId, userId, annotationId).then((annotation) => {
if (annotation) {
path = null;
lines = [];
Expand Down
4 changes: 2 additions & 2 deletions src/UI/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ function saveCircle(svg, type, pt, radius, color) {
r: radius
};

let { documentId, pageNumber } = getMetadata(svg);
let { documentId, userId, pageNumber } = getMetadata(svg);

// Add the annotation
PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, pageNumber, annotation)
PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, userId, pageNumber, annotation)
.then((annotation) => {
appendChild(svg, annotation);
});
Expand Down
4 changes: 2 additions & 2 deletions src/UI/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ function handleDocumentMouseup(e) {
let target = document.querySelectorAll(`[data-pdf-annotate-id="${annotationId}"]`);
let type = target[0].getAttribute('data-pdf-annotate-type');
let svg = overlay.parentNode.querySelector(config.annotationSvgQuery());
let { documentId } = getMetadata(svg);
let { documentId, userId } = getMetadata(svg);

overlay.querySelector('a').style.display = '';

PDFJSAnnotate.getStoreAdapter().getAnnotation(documentId, annotationId).then((annotation) => {
PDFJSAnnotate.getStoreAdapter().getAnnotation(documentId, userId, annotationId).then((annotation) => {
let attribX = 'x';
let attribY = 'y';
if (['circle', 'fillcircle', 'emptycircle'].indexOf(type) > -1) {
Expand Down
2 changes: 1 addition & 1 deletion src/UI/eraser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function handleDocumentMouseMove(e){
let target = findAnnotationAtPoint(e.clientX, e.clientY);
if(target){
console.log(target);
let annotationId = target.getAttribute('data-pdf-annotate-id');
// let annotationId = target.getAttribute('data-pdf-annotate-id');
// let nodes = document.querySelectorAll(`[data-pdf-annotate-id="${annotationId}"]`);
// let svg = overlay.parentNode.querySelector(config.annotationSvgQuery());
// let { documentId } = getMetadata(svg);
Expand Down
3 changes: 2 additions & 1 deletion src/UI/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function createPage(pageNumber) {
export function renderPage(pageNumber, renderOptions) {
let {
documentId,
userId,
pdfDocument,
scale,
rotate
Expand All @@ -56,7 +57,7 @@ export function renderPage(pageNumber, renderOptions) {
// Load the page and annotations
return Promise.all([
pdfDocument.getPage(pageNumber),
PDFJSAnnotate.getAnnotations(documentId, pageNumber)
PDFJSAnnotate.getAnnotations(documentId, userId, pageNumber)
]).then(([pdfPage, annotations]) => {
let page = document.getElementById(`pageContainer${pageNumber}`);
let svg = page.querySelector(config.annotationClassQuery());
Expand Down
5 changes: 2 additions & 3 deletions src/UI/pen.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ function saveToStorage(x, y){
_candraw = false;
let svg;
if (lines.length > 1 && (svg = findSVGAtPoint(x, y))) {
let { documentId, pageNumber } = getMetadata(svg);

PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, pageNumber, {
let { documentId, userId, pageNumber } = getMetadata(svg);
PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, userId, pageNumber, {
type: 'drawing',
width: _penSize,
color: _penColor,
Expand Down
4 changes: 2 additions & 2 deletions src/UI/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function savePoint() {
}

let rect = svg.getBoundingClientRect();
let { documentId, pageNumber } = getMetadata(svg);
let { documentId, userId, pageNumber } = getMetadata(svg);
let annotation = Object.assign({
type: 'point'
}, scaleDown(svg, {
Expand All @@ -79,7 +79,7 @@ function savePoint() {
})
);

PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, pageNumber, annotation)
PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, userId, pageNumber, annotation)
.then((annotation) => {
PDFJSAnnotate.getStoreAdapter().addComment(
documentId,
Expand Down
4 changes: 2 additions & 2 deletions src/UI/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ function saveRect(type, rects, color) {
annotation.height = rect.height;
}

let { documentId, pageNumber } = getMetadata(svg);
let { documentId, userId, pageNumber } = getMetadata(svg);

// Add the annotation
PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, pageNumber, annotation)
PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, userId, pageNumber, annotation)
.then((annotation) => {
appendChild(svg, annotation);
});
Expand Down
4 changes: 2 additions & 2 deletions src/UI/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function saveText() {
return;
}
let height = _textSize;
let { documentId, pageNumber, viewport } = getMetadata(svg);
let { documentId, userId, pageNumber, viewport } = getMetadata(svg);
let scale = 1 / viewport.scale;
let rect = svg.getBoundingClientRect();
let pt = convertToSvgPoint([
Expand All @@ -87,7 +87,7 @@ function saveText() {
rotation: -viewport.rotation
}

PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, pageNumber, annotation)
PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, userId, pageNumber, annotation)
.then((annotation) => {
appendChild(svg, annotation);
});
Expand Down
1 change: 1 addition & 0 deletions src/UI/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export function enableUserSelect() {
export function getMetadata(svg) {
return {
documentId: svg.getAttribute('data-pdf-annotate-document'),
userId: svg.getAttribute('data-pdf-annotate-user'),
pageNumber: parseInt(svg.getAttribute('data-pdf-annotate-page'), 10),
viewport: JSON.parse(svg.getAttribute('data-pdf-annotate-viewport'))
};
Expand Down
13 changes: 7 additions & 6 deletions src/a11y/initEventHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import PDFJSAnnotate from '../PDFJSAnnotate';
* Initialize the event handlers for keeping screen reader hints synced with data
*/
export default function initEventHandlers() {
addEventListener('annotation:add', (documentId, pageNumber, annotation) => {
reorderAnnotationsByType(documentId, pageNumber, annotation.type);
addEventListener('annotation:add', (documentId, userId, pageNumber, annotation) => {
reorderAnnotationsByType(documentId, userId, pageNumber, annotation.type);
});
addEventListener('annotation:edit', (documentId, annotationId, annotation) => {
reorderAnnotationsByType(documentId, annotation.page, annotation.type);
addEventListener('annotation:edit', (documentId, userId, annotationId, annotation) => {
reorderAnnotationsByType(documentId, userId, annotation.page, annotation.type);
});
addEventListener('annotation:delete', removeAnnotation);
addEventListener('comment:add', insertComment);
Expand All @@ -24,11 +24,12 @@ export default function initEventHandlers() {
* Reorder the annotation numbers by annotation type
*
* @param {String} documentId The ID of the document
* @param {String} userId The ID of the user
* @param {Number} pageNumber The page number of the annotations
* @param {Strig} type The annotation type
*/
function reorderAnnotationsByType(documentId, pageNumber, type) {
PDFJSAnnotate.getStoreAdapter().getAnnotations(documentId, pageNumber)
function reorderAnnotationsByType(documentId, userId, pageNumber, type) {
PDFJSAnnotate.getStoreAdapter().getAnnotations(documentId, userId, pageNumber)
.then((annotations) => {
return annotations.annotations.filter((a) => {
return a.type === type;
Expand Down
63 changes: 32 additions & 31 deletions src/adapter/LocalStoreAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,71 @@ import StoreAdapter from './StoreAdapter';
export default class LocalStoreAdapter extends StoreAdapter {
constructor() {
super({
getAnnotations(documentId, pageNumber) {
getAnnotations(documentId, userId, pageNumber) {
return new Promise((resolve, reject) => {
let annotations = getAnnotations(documentId).filter((i) => {
let annotations = getAnnotations(documentId, userId).filter((i) => {
return i.page === pageNumber && i.class === 'Annotation';
});

resolve({
documentId,
userId,
pageNumber,
annotations
});
});
},

getAnnotation(documentId, annotationId) {
return Promise.resolve(getAnnotations(documentId)[findAnnotation(documentId, annotationId)]);
getAnnotation(documentId, userId, annotationId) {
return Promise.resolve(getAnnotations(documentId, userId)[findAnnotation(documentId, userId, annotationId)]);
},

addAnnotation(documentId, pageNumber, annotation) {
addAnnotation(documentId, userId, pageNumber, annotation) {
return new Promise((resolve, reject) => {
annotation.class = 'Annotation';
annotation.uuid = uuid();
annotation.page = pageNumber;

let annotations = getAnnotations(documentId);
let annotations = getAnnotations(documentId, userId);
annotations.push(annotation);
updateAnnotations(documentId, annotations);
updateAnnotations(documentId, userId, annotations);

resolve(annotation);
});
},

editAnnotation(documentId, annotationId, annotation) {
editAnnotation(documentId, userId, annotationId, annotation) {
return new Promise((resolve, reject) => {
let annotations = getAnnotations(documentId);
annotations[findAnnotation(documentId, annotationId)] = annotation;
updateAnnotations(documentId, annotations);
let annotations = getAnnotations(documentId, userId);
annotations[findAnnotation(documentId, userId, annotationId)] = annotation;
updateAnnotations(documentId, userId, annotations);

resolve(annotation);
});
},

deleteAnnotation(documentId, annotationId) {
deleteAnnotation(documentId, userId, annotationId) {
return new Promise((resolve, reject) => {
let index = findAnnotation(documentId, annotationId);
let index = findAnnotation(documentId, userId, annotationId);
if (index > -1) {
let annotations = getAnnotations(documentId);
let annotations = getAnnotations(documentId, userId);
annotations.splice(index, 1);
updateAnnotations(documentId, annotations);
updateAnnotations(documentId, userId, annotations);
}

resolve(true);
});
},

getComments(documentId, annotationId) {
getComments(documentId, userId, annotationId) {
return new Promise((resolve, reject) => {
resolve(getAnnotations(documentId).filter((i) => {
resolve(getAnnotations(documentId, userId).filter((i) => {
return i.class === 'Comment' && i.annotation === annotationId;
}));
});
},

addComment(documentId, annotationId, content) {
addComment(documentId, userId, annotationId, content) {
return new Promise((resolve, reject) => {
let comment = {
class: 'Comment',
Expand All @@ -78,19 +79,19 @@ export default class LocalStoreAdapter extends StoreAdapter {
content: content
};

let annotations = getAnnotations(documentId);
let annotations = getAnnotations(documentId, userId);
annotations.push(comment);
updateAnnotations(documentId, annotations);
updateAnnotations(documentId, userId, annotations);

resolve(comment);
});
},

deleteComment(documentId, commentId) {
deleteComment(documentId, userId, commentId) {
return new Promise((resolve, reject) => {
getAnnotations(documentId);
getAnnotations(documentId, userId);
let index = -1;
let annotations = getAnnotations(documentId);
let annotations = getAnnotations(documentId, userId);
for (let i=0, l=annotations.length; i<l; i++) {
if (annotations[i].uuid === commentId) {
index = i;
Expand All @@ -100,7 +101,7 @@ export default class LocalStoreAdapter extends StoreAdapter {

if (index > -1) {
annotations.splice(index, 1);
updateAnnotations(documentId, annotations);
updateAnnotations(documentId, userId, annotations);
}

resolve(true);
Expand All @@ -110,17 +111,17 @@ export default class LocalStoreAdapter extends StoreAdapter {
}
}

function getAnnotations(documentId) {
return JSON.parse(localStorage.getItem(`${documentId}/annotations`)) || [];
function getAnnotations(documentId, userId) {
return JSON.parse(localStorage.getItem(`${documentId}/${userId}/annotations`)) || [];
}

function updateAnnotations(documentId, annotations) {
localStorage.setItem(`${documentId}/annotations`, JSON.stringify(annotations));
// Note, userId should not be an optional parameter, leaving it here for debugging purposes
function updateAnnotations(documentId, userId='bitdiddle', annotations) {
localStorage.setItem(`${documentId}/${userId}/annotations`, JSON.stringify(annotations));
}

function findAnnotation(documentId, annotationId) {
function findAnnotation(documentId, userId, annotationId) {
let index = -1;
let annotations = getAnnotations(documentId);
let annotations = getAnnotations(documentId, userId);
for (let i=0, l=annotations.length; i<l; i++) {
if (annotations[i].uuid === annotationId) {
index = i;
Expand Down
10 changes: 5 additions & 5 deletions src/adapter/StoreAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export default class StoreAdapter {
* @param {Number} pageNumber The number of the page the annotations belong to
* @return {Promise}
*/
__getAnnotations(documentId, pageNumber) { abstractFunction('getAnnotations'); }
__getAnnotations(documentId, userId, pageNumber) { abstractFunction('getAnnotations'); }
get getAnnotations() { return this.__getAnnotations; }
set getAnnotations(fn) {
this.__getAnnotations = function getAnnotations(documentId, pageNumber) {
this.__getAnnotations = function getAnnotations(documentId, userId, pageNumber) {
return fn(...arguments).then((annotations) => {
// TODO may be best to have this happen on the server
if (annotations.annotations) {
Expand Down Expand Up @@ -58,12 +58,12 @@ export default class StoreAdapter {
* @param {Object} annotation The definition for the new annotation
* @return {Promise}
*/
__addAnnotation(documentId, pageNumber, annotation) { abstractFunction('addAnnotation'); }
__addAnnotation(documentId, userId, pageNumber, annotation) { abstractFunction('addAnnotation'); }
get addAnnotation() { return this.__addAnnotation; }
set addAnnotation(fn) {
this.__addAnnotation = function addAnnotation(documentId, pageNumber, annotation) {
this.__addAnnotation = function addAnnotation(documentId, userId, pageNumber, annotation) {
return fn(...arguments).then((annotation) => {
fireEvent('annotation:add', documentId, pageNumber, annotation);
fireEvent('annotation:add', documentId, userId, pageNumber, annotation);
return annotation;
});
};
Expand Down
1 change: 1 addition & 0 deletions src/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function render(svg, viewport, data) {
}

svg.setAttribute('data-pdf-annotate-document', data.documentId);
svg.setAttribute('data-pdf-annotate-user', data.userId);
svg.setAttribute('data-pdf-annotate-page', data.pageNumber);

// Make sure annotations is an array
Expand Down
3 changes: 2 additions & 1 deletion web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const documentId = 'example.pdf';
let PAGE_HEIGHT;
let RENDER_OPTIONS = {
documentId,
userId: 'aphacker',
pdfDocument: null,
scale: parseFloat(localStorage.getItem(`${documentId}/scale`), 10) || 1.33,
rotate: parseInt(localStorage.getItem(`${documentId}/rotate`), 10) || 0
Expand Down Expand Up @@ -383,7 +384,7 @@ render();
document.querySelector(`div#pageContainer${i+1} svg.annotationLayer`).innerHTML = '';
}

localStorage.removeItem(`${RENDER_OPTIONS.documentId}/annotations`);
localStorage.removeItem(`${RENDER_OPTIONS.documentId}/${RENDER_OPTIONS.userId}/annotations`);
}
}
document.querySelector('a.clear').addEventListener('click', handleClearClick);
Expand Down

0 comments on commit 8a47af9

Please sign in to comment.