2019-01-22 13:35:01 +00:00
|
|
|
var id = document.location.pathname.split('/')[1];
|
2019-01-24 11:52:44 +00:00
|
|
|
var annotations = [];
|
2019-01-22 13:35:01 +00:00
|
|
|
var currentPage = 1, rendered = false
|
|
|
|
|
2019-01-23 15:14:59 +00:00
|
|
|
Ox.load({
|
|
|
|
'UI': {
|
|
|
|
loadCSS: false
|
2019-01-22 13:35:01 +00:00
|
|
|
}
|
2019-01-23 15:14:59 +00:00
|
|
|
}, function() {
|
|
|
|
Ox.$parent.bindMessage(function(data, event) {
|
|
|
|
if (event == 'selectAnnotation') {
|
|
|
|
var annotation = annotations.filter(function(a) { return a.id == data.id })[0]
|
2019-01-25 11:08:45 +00:00
|
|
|
var delay = 0
|
2019-01-23 15:14:59 +00:00
|
|
|
if (
|
|
|
|
annotation &&
|
|
|
|
annotation.page &&
|
|
|
|
PDFViewerApplication.pdfViewer.currentPageNumber != annotation.page
|
|
|
|
) {
|
|
|
|
PDFViewerApplication.pdfViewer.currentPageNumber = annotation.page;
|
2019-01-25 11:08:45 +00:00
|
|
|
delay = 250
|
2019-01-23 15:14:59 +00:00
|
|
|
}
|
2019-02-10 12:16:35 +00:00
|
|
|
annotation && setTimeout(function() {
|
2019-01-25 11:08:45 +00:00
|
|
|
var el = document.querySelector('.a' + annotation.id);
|
2019-02-01 08:57:28 +00:00
|
|
|
if (el && !isInView(el)) {
|
2019-01-25 11:08:45 +00:00
|
|
|
document.querySelector('#viewerContainer').scrollTop = el.offsetTop + el.parentElement.offsetTop - 64;
|
|
|
|
}
|
|
|
|
}, delay)
|
2019-01-23 15:14:59 +00:00
|
|
|
selectAnnotation(data.id)
|
2019-01-31 19:32:55 +00:00
|
|
|
} else if (event == 'addAnnotation') {
|
|
|
|
createAnnotation()
|
2019-01-24 11:52:44 +00:00
|
|
|
} else if (event == 'addAnnotations') {
|
2019-02-10 12:16:35 +00:00
|
|
|
if (data.replace) {
|
|
|
|
document.querySelectorAll('.oml-annotation').forEach(function(a) {
|
|
|
|
a.remove()
|
|
|
|
})
|
|
|
|
annotations = []
|
|
|
|
}
|
2019-01-24 11:52:44 +00:00
|
|
|
data.annotations.forEach(function(annotation) {
|
|
|
|
annotations.push(annotation)
|
|
|
|
renderAnnotation(annotation)
|
|
|
|
})
|
2019-01-31 06:22:19 +00:00
|
|
|
} else if (event == 'removeAnnotation') {
|
|
|
|
removeAnnotation(data.id)
|
2019-02-10 12:16:35 +00:00
|
|
|
} else {
|
|
|
|
console.log('got', event, 'data', data)
|
2019-01-23 15:14:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-01-22 13:35:01 +00:00
|
|
|
|
|
|
|
window.addEventListener('keydown', function(event) {
|
|
|
|
if (event.key == 'Delete') {
|
|
|
|
var selected = document.querySelector('.oml-annotation.selected')
|
|
|
|
if (selected) {
|
|
|
|
removeAnnotation(selected.dataset.id)
|
|
|
|
}
|
2019-01-31 18:42:52 +00:00
|
|
|
} else if (event.key == 'n' || event.keyCode == 13) {
|
2019-02-09 18:10:14 +00:00
|
|
|
if (event.target.nodeName != 'INPUT') {
|
|
|
|
var selected = document.querySelector('.oml-annotation.selected')
|
|
|
|
if (!window.getSelection().isCollapsed) {
|
|
|
|
createAnnotation()
|
|
|
|
} else if (selected) {
|
|
|
|
console.log('editNote?', selected.dataset.id)
|
|
|
|
}
|
|
|
|
event.stopPropagation()
|
|
|
|
event.preventDefault()
|
2019-01-22 13:35:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-01-31 19:32:55 +00:00
|
|
|
window.addEventListener('mouseup', function(event) {
|
|
|
|
var selection = window.getSelection()
|
|
|
|
if (selection.isCollapsed) {
|
2019-02-01 08:49:16 +00:00
|
|
|
Ox.$parent.postMessage('selectText', false)
|
2019-01-31 19:32:55 +00:00
|
|
|
} else {
|
2019-02-01 08:49:16 +00:00
|
|
|
Ox.$parent.postMessage('selectText', true)
|
2019-01-31 19:32:55 +00:00
|
|
|
}
|
|
|
|
})
|
2019-01-22 13:35:01 +00:00
|
|
|
|
2019-01-23 09:14:36 +00:00
|
|
|
function bindEvents() {
|
|
|
|
if (!window.PDFViewerApplication || !window.PDFViewerApplication.eventBus) {
|
|
|
|
setTimeout(bindEvents, 10)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PDFViewerApplication.eventBus.on('pagerendered', function(event) {
|
2019-01-23 15:14:59 +00:00
|
|
|
loadAnnotations(event.pageNumber)
|
2019-01-23 09:14:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents()
|
2019-01-22 13:35:01 +00:00
|
|
|
|
|
|
|
function getHighlight() {
|
2019-01-23 15:14:59 +00:00
|
|
|
var pageNumber = PDFViewerApplication.pdfViewer.currentPageNumber;
|
|
|
|
var pageIndex = pageNumber - 1;
|
2019-01-22 13:35:01 +00:00
|
|
|
var page = PDFViewerApplication.pdfViewer.getPageView(pageIndex);
|
|
|
|
var pageRect = page.canvas.getClientRects()[0];
|
2019-01-23 15:14:59 +00:00
|
|
|
var selection = window.getSelection()
|
|
|
|
var selectionRects = selection.getRangeAt(0).getClientRects();
|
2019-01-22 13:35:01 +00:00
|
|
|
var viewport = page.viewport;
|
|
|
|
var selected = Array.from(selectionRects).map(function (r) {
|
|
|
|
return viewport.convertToPdfPoint(r.left - pageRect.x, r.top - pageRect.y).concat(
|
|
|
|
viewport.convertToPdfPoint(r.right - pageRect.x, r.bottom - pageRect.y));
|
|
|
|
});
|
2019-01-23 15:14:59 +00:00
|
|
|
var text = selection.toString();
|
2019-02-09 18:10:14 +00:00
|
|
|
var position = [pageNumber].concat(Ox.sort(selected.map(function(c) { return [c[1], c[0]]}))[0]);
|
2019-01-23 15:14:59 +00:00
|
|
|
return {
|
|
|
|
page: pageNumber,
|
2019-02-10 12:16:35 +00:00
|
|
|
pageLabel: PDFViewerApplication.pdfViewer.currentPageLabel,
|
2019-02-09 11:43:00 +00:00
|
|
|
position: position,
|
2019-01-23 15:14:59 +00:00
|
|
|
coords: selected,
|
|
|
|
text: text,
|
|
|
|
id: Ox.SHA1(pageNumber.toString() + JSON.stringify(selected))
|
|
|
|
};
|
2019-01-22 13:35:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 19:32:55 +00:00
|
|
|
function createAnnotation() {
|
|
|
|
var selected = document.querySelector('.oml-annotation.selected')
|
|
|
|
if (selected) {
|
|
|
|
deselectAllAnnotations()
|
|
|
|
}
|
|
|
|
var annot = getHighlight()
|
|
|
|
renderAnnotation(annot)
|
|
|
|
addAnnotation(annot)
|
|
|
|
window.getSelection().removeAllRanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-22 13:35:01 +00:00
|
|
|
function renderAnnotation(annotation) {
|
2019-01-23 15:14:59 +00:00
|
|
|
var pageIndex = annotation.page - 1;
|
2019-01-22 13:35:01 +00:00
|
|
|
var page = PDFViewerApplication.pdfViewer.getPageView(pageIndex);
|
2019-01-24 11:52:44 +00:00
|
|
|
if (!page || !page.canvas) {
|
2019-01-22 13:35:01 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
renderAnnotation(annotation)
|
2019-01-24 11:52:44 +00:00
|
|
|
}, 50)
|
2019-01-22 13:35:01 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-22 14:06:48 +00:00
|
|
|
var pageElement = page.canvas.parentElement.parentElement;
|
2019-01-22 13:35:01 +00:00
|
|
|
var viewport = page.viewport;
|
|
|
|
annotation.coords.forEach(function (rect) {
|
|
|
|
var bounds = viewport.convertToViewportRectangle(rect);
|
|
|
|
var el = document.createElement('div');
|
|
|
|
el.classList.add('oml-annotation')
|
|
|
|
el.classList.add('a' + annotation.id)
|
|
|
|
el.classList.add('page' + annotation.page)
|
|
|
|
el.dataset.id = annotation.id
|
|
|
|
el.setAttribute('style', 'position: absolute; background-color: yellow;opacity:0.3;' +
|
|
|
|
'left:' + Math.min(bounds[0], bounds[2]) + 'px; top:' + Math.min(bounds[1], bounds[3]) + 'px;' +
|
|
|
|
'width:' + Math.abs(bounds[0] - bounds[2]) + 'px; height:' + Math.abs(bounds[1] - bounds[3]) + 'px;');
|
2019-01-22 14:06:48 +00:00
|
|
|
|
2019-01-22 13:35:01 +00:00
|
|
|
el.addEventListener('click', function() {
|
2019-02-01 08:57:28 +00:00
|
|
|
if (!el.classList.contains('selected')) {
|
2019-01-22 13:35:01 +00:00
|
|
|
selectAnnotation(annotation.id)
|
2019-01-23 15:14:59 +00:00
|
|
|
Ox.$parent.postMessage('selectAnnotation', {id: annotation.id})
|
2019-01-22 13:35:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
pageElement.appendChild(el);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addAnnotation(annotation) {
|
|
|
|
annotations.push(annotation)
|
2019-01-23 15:14:59 +00:00
|
|
|
Ox.$parent.postMessage('addAnnotation', annotation)
|
2019-01-22 13:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function selectAnnotation(id) {
|
2019-01-23 15:14:59 +00:00
|
|
|
document.querySelectorAll('.oml-annotation.selected').forEach(function(g) {
|
|
|
|
g.classList.remove('selected')
|
|
|
|
g.style.backgroundColor = 'yellow'
|
|
|
|
})
|
2019-01-22 13:35:01 +00:00
|
|
|
document.querySelectorAll('.oml-annotation.a' + id).forEach(function(g) {
|
|
|
|
g.classList.add('selected')
|
|
|
|
g.style.backgroundColor = 'blue'
|
|
|
|
})
|
|
|
|
}
|
2019-01-31 18:42:52 +00:00
|
|
|
|
2019-01-22 13:35:01 +00:00
|
|
|
function deselectAnnotation(id) {
|
|
|
|
document.querySelectorAll('.oml-annotation.a' + id).forEach(function(g) {
|
|
|
|
g.classList.remove('selected')
|
|
|
|
g.style.backgroundColor = 'yellow'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:42:52 +00:00
|
|
|
function deselectAllAnnotations() {
|
|
|
|
var ids = []
|
|
|
|
document.querySelectorAll('.oml-annotation.selected').forEach(function(g) {
|
|
|
|
g.classList.remove('selected')
|
|
|
|
g.style.backgroundColor = 'yellow'
|
|
|
|
var id = $(g).parents('.oml-annotation').data('id')
|
2019-02-10 12:16:35 +00:00
|
|
|
//console.log('deselect', g, id)
|
2019-01-31 18:42:52 +00:00
|
|
|
if (!Ox.contains(ids, id)) {
|
|
|
|
ids.push(id)
|
2019-02-01 08:49:16 +00:00
|
|
|
Ox.$parent.postMessage('selectAnnotation', {id: null})
|
2019-01-31 18:42:52 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-22 13:35:01 +00:00
|
|
|
function removeAnnotation(id) {
|
|
|
|
document.querySelectorAll('.oml-annotation.a' + id).forEach(function(a) {
|
|
|
|
a.remove()
|
|
|
|
})
|
|
|
|
annotations = annotations.filter(function(annotation) {
|
|
|
|
return annotation.id != id
|
|
|
|
})
|
2019-01-24 12:57:26 +00:00
|
|
|
Ox.$parent.postMessage('removeAnnotation', {id: id})
|
2019-01-22 13:35:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 15:14:59 +00:00
|
|
|
function loadAnnotations(page) {
|
|
|
|
document.querySelectorAll('.oml-annotation.page' + page).forEach(function(e) {
|
2019-01-22 13:35:01 +00:00
|
|
|
e.remove()
|
|
|
|
})
|
|
|
|
annotations.filter(function(a) {
|
2019-01-23 15:14:59 +00:00
|
|
|
return a.page == page
|
2019-01-22 13:35:01 +00:00
|
|
|
}).forEach(function(annot) {
|
|
|
|
renderAnnotation(annot)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-01 08:57:28 +00:00
|
|
|
function isInView(element) {
|
|
|
|
var docViewTop = $(window).scrollTop();
|
|
|
|
var docViewBottom = docViewTop + $(window).height();
|
|
|
|
var elementTop = $(element).offset().top;
|
|
|
|
var elementBottom = elementTop + $(element).height();
|
|
|
|
return elementTop < docViewBottom && elementBottom > docViewTop;
|
|
|
|
}
|