Merge branch 'master' of code.0x2620.org:0x2620/openmedialibrary

This commit is contained in:
rlx 2019-01-24 17:32:44 +05:30
commit b5d0a9f136
3 changed files with 61 additions and 39 deletions

View file

@ -3,6 +3,7 @@
oml.ui.viewer = function() { oml.ui.viewer = function() {
var ui = oml.user.ui, var ui = oml.user.ui,
annotations = [],
frame = Ox.Element(), frame = Ox.Element(),
that = Ox.Element() that = Ox.Element()
@ -38,6 +39,24 @@ oml.ui.viewer = function() {
.appendTo(that), .appendTo(that),
$iframe, item; $iframe, item;
function loadAnnotations(callback) {
annotations = JSON.parse(localStorage[item + '.annotations'] || '[]')
callback && callback(annotations)
}
function saveAnnotations(data) {
if (data) {
data.created = data.created || (new Date).toISOString();
annotations.push(data);
}
localStorage[item + '.annotations'] = JSON.stringify(annotations)
}
function removeAnnotation(id) {
annotations = annotations.filter(function(annotation) {
return annotation.id != id
})
saveAnnotations()
}
that.updateElement = function() { that.updateElement = function() {
item = ui.item; item = ui.item;
if (item && item.length) { if (item && item.length) {
@ -55,18 +74,36 @@ oml.ui.viewer = function() {
console.log('got', event, data) console.log('got', event, data)
if (event == 'addAnnotation') { if (event == 'addAnnotation') {
console.log('adding', data.id) console.log('adding', data.id)
saveAnnotations(data);
var $annotation = oml.ui.annotation(data, $iframe) var $annotation = oml.ui.annotation(data, $iframe)
oml.$ui.annotationFolder.append($annotation); oml.$ui.annotationFolder.append($annotation);
$annotation.annotate(); $annotation.annotate();
} else if (event == 'removeAnnotation') { } else if (event == 'removeAnnotation') {
console.log('do it ...', data)
oml.$ui.annotationFolder.find('#a-' + data.id).remove() oml.$ui.annotationFolder.find('#a-' + data.id).remove()
removeAnnotation(data.id)
} else if (event == 'selectAnnotation') { } else if (event == 'selectAnnotation') {
console.log('select', data) console.log('select', data)
} else if (event == 'deselectAnnotation') { } else if (event == 'deselectAnnotation') {
console.log('deselect', data) console.log('deselect', data)
} } else {
that.triggerEvent(event, data); that.triggerEvent(event, data);
}
}).bindEvent({
init: function() {
loadAnnotations(function(annotations) {
annotations.forEach(function(data) {
var $annotation = oml.ui.annotation(data, $iframe)
oml.$ui.annotationFolder.append($annotation);
})
// fixme: trigger loaded event from reader instead?
setTimeout(function() {
that.postMessage('addAnnotations', {
annotations: annotations
})
}, 500)
})
}
}).appendTo(frame); }).appendTo(frame);
$iframe.attr({ $iframe.attr({
src: '/' + item + '/reader/' src: '/' + item + '/reader/'
@ -77,7 +114,7 @@ oml.ui.viewer = function() {
return that; return that;
}; };
that.postMessage = function(event, data) { that.postMessage = function(event, data) {
$iframe && $iframe.postMesage(event, data) $iframe && $iframe.postMessage(event, data)
}; };
return that.updateElement(); return that.updateElement();

View file

@ -1,7 +1,7 @@
"use strict"; "use strict";
var reader; var reader;
var id = document.location.pathname.split('/')[1]; var id = document.location.pathname.split('/')[1];
var annotations = JSON.parse(localStorage[id + '.annotations'] || '[]') var annotations = [];
var currentSelection; var currentSelection;
var fontSize = parseInt(localStorage.epubFontSize || '100', 10) var fontSize = parseInt(localStorage.epubFontSize || '100', 10)
@ -18,23 +18,18 @@ Ox.load({
if (annotation) { if (annotation) {
reader.rendition.display(annotation.cfiRange) reader.rendition.display(annotation.cfiRange)
} }
} else if (event == 'addAnnotations') {
data.annotations.forEach(function(annotation) {
annotations.push(annotation)
renderAnnotation(annotation)
})
} }
}) })
setTimeout(function() {
annotations.forEach(function(a) {
Ox.$parent.postMessage('addAnnotation', a)
}) })
}, 1000)
})
function saveAnnotations() {
localStorage[id + '.annotations'] = JSON.stringify(annotations)
}
function addAnnotation(annotation) { function addAnnotation(annotation) {
annotations.push(annotation) annotations.push(annotation)
Ox.$parent.postMessage('addAnnotation', annotation) Ox.$parent.postMessage('addAnnotation', annotation)
saveAnnotations()
} }
function selectAnnotation(id) { function selectAnnotation(id) {
@ -57,7 +52,11 @@ function removeAnnotation(a) {
return annotation.cfiRange != a.dataset.epubcfi return annotation.cfiRange != a.dataset.epubcfi
}) })
reader.rendition.annotations.remove(a.dataset.epubcfi) reader.rendition.annotations.remove(a.dataset.epubcfi)
saveAnnotations() Ox.$parent.postMessage('removeAnnotation', {id: a.dataset.id})
}
function renderAnnotation(annotation) {
reader.rendition.annotations.highlight(annotation.cfiRange, {id: annotation.id}, onHighlightClicked);
} }
function getText(book, cfiRange, cb) { function getText(book, cfiRange, cb) {
@ -95,16 +94,11 @@ document.onreadystatechange = function () {
book = reader.book; book = reader.book;
rendition.themes.fontSize(fontSize + "%"); rendition.themes.fontSize(fontSize + "%");
annotations.forEach(function(a) {
rendition.annotations.highlight(a.cfiRange, {id: a.id}, onHighlightClicked);
})
reader.rendition.on('keydown', function(event) { reader.rendition.on('keydown', function(event) {
if (event.key == 'Delete') { if (event.key == 'Delete') {
document.querySelectorAll('.epubjs-hl.selected').forEach(function(a) { document.querySelectorAll('.epubjs-hl.selected').forEach(function(a) {
removeAnnotation(a) removeAnnotation(a)
Ox.$parent.postMessage('removeAnnotation', {id: a.dataset.id})
}) })
} }
if (event.key == 'n') { if (event.key == 'n') {
@ -117,8 +111,7 @@ document.onreadystatechange = function () {
) )
//currentSelection.cfiRange = reader.rendition.book.section().cfiFromRange(range).toString() //currentSelection.cfiRange = reader.rendition.book.section().cfiFromRange(range).toString()
*/ */
renderAnnotation(currentSelection)
rendition.annotations.highlight(currentSelection.cfiRange, {id: currentSelection.id}, onHighlightClicked);
currentSelection.contents.window.getSelection().removeAllRanges(); currentSelection.contents.window.getSelection().removeAllRanges();
delete currentSelection.contents delete currentSelection.contents
addAnnotation(currentSelection) addAnnotation(currentSelection)
@ -153,7 +146,6 @@ document.onreadystatechange = function () {
getText(book, cfiRange, function(text) { getText(book, cfiRange, function(text) {
currentSelection = { currentSelection = {
id: Ox.SHA1(cfiRange), id: Ox.SHA1(cfiRange),
created: (new Date).toISOString(),
cfiRange: cfiRange, cfiRange: cfiRange,
text: text, text: text,
contents: contents contents: contents

View file

@ -1,5 +1,5 @@
var id = document.location.pathname.split('/')[1]; var id = document.location.pathname.split('/')[1];
var annotations = JSON.parse(localStorage[id + '.annotations'] || '[]') var annotations = [];
var currentPage = 1, rendered = false var currentPage = 1, rendered = false
Ox.load({ Ox.load({
@ -20,13 +20,13 @@ Ox.load({
PDFViewerApplication.pdfViewer.currentPageNumber = annotation.page; PDFViewerApplication.pdfViewer.currentPageNumber = annotation.page;
} }
selectAnnotation(data.id) selectAnnotation(data.id)
} else if (event == 'addAnnotations') {
data.annotations.forEach(function(annotation) {
annotations.push(annotation)
renderAnnotation(annotation)
})
} }
}) })
setTimeout(function() {
annotations.forEach(function(a) {
Ox.$parent.postMessage('addAnnotation', a)
})
}, 1000)
}) })
window.addEventListener('keydown', function(event) { window.addEventListener('keydown', function(event) {
@ -34,7 +34,6 @@ window.addEventListener('keydown', function(event) {
var selected = document.querySelector('.oml-annotation.selected') var selected = document.querySelector('.oml-annotation.selected')
if (selected) { if (selected) {
removeAnnotation(selected.dataset.id) removeAnnotation(selected.dataset.id)
Ox.$parent.postMessage('removeAnnotation', {id: selected.dataset.id})
} }
} else if (event.key == 'n') { } else if (event.key == 'n') {
if (!window.getSelection().isCollapsed) { if (!window.getSelection().isCollapsed) {
@ -74,7 +73,6 @@ function getHighlight() {
}); });
var text = selection.toString(); var text = selection.toString();
return { return {
created: (new Date).toISOString(),
page: pageNumber, page: pageNumber,
coords: selected, coords: selected,
text: text, text: text,
@ -85,10 +83,10 @@ function getHighlight() {
function renderAnnotation(annotation) { function renderAnnotation(annotation) {
var pageIndex = annotation.page - 1; var pageIndex = annotation.page - 1;
var page = PDFViewerApplication.pdfViewer.getPageView(pageIndex); var page = PDFViewerApplication.pdfViewer.getPageView(pageIndex);
if (!page.canvas) { if (!page || !page.canvas) {
setTimeout(function() { setTimeout(function() {
renderAnnotation(annotation) renderAnnotation(annotation)
}, 10) }, 50)
return return
} }
var pageElement = page.canvas.parentElement.parentElement; var pageElement = page.canvas.parentElement.parentElement;
@ -120,7 +118,6 @@ function renderAnnotation(annotation) {
function addAnnotation(annotation) { function addAnnotation(annotation) {
annotations.push(annotation) annotations.push(annotation)
Ox.$parent.postMessage('addAnnotation', annotation) Ox.$parent.postMessage('addAnnotation', annotation)
saveAnnotations()
} }
function selectAnnotation(id) { function selectAnnotation(id) {
@ -147,11 +144,7 @@ function removeAnnotation(id) {
annotations = annotations.filter(function(annotation) { annotations = annotations.filter(function(annotation) {
return annotation.id != id return annotation.id != id
}) })
saveAnnotations() Ox.$parent.postMessage('removeAnnotation', {id: selected.dataset.id})
}
function saveAnnotations() {
localStorage[id + '.annotations'] = JSON.stringify(annotations)
} }
function loadAnnotations(page) { function loadAnnotations(page) {