99 lines
3.6 KiB
JavaScript
99 lines
3.6 KiB
JavaScript
"use strict";
|
|
var reader;
|
|
var id = document.location.pathname.split('/')[1];
|
|
var annotations = JSON.parse(localStorage[id + '.annotations'] || '[]')
|
|
var currentSelection;
|
|
|
|
function saveAnnotations() {
|
|
localStorage[id + '.annotations'] = JSON.stringify(annotations)
|
|
}
|
|
|
|
function getText(book, cfiRange, cb) {
|
|
book.getRange(cfiRange).then(function (range) {
|
|
var text;
|
|
if (range) {
|
|
text = range.toString();
|
|
}
|
|
cb(text)
|
|
})
|
|
}
|
|
|
|
function onHighlightClicked(e) {
|
|
console.log("highlight clicked", e.target.dataset.epubcfi);
|
|
if(e.target.classList.contains('selected')) {
|
|
e.target.classList.remove('selected')
|
|
} else {
|
|
document.querySelectorAll('.epubjs-hl.selected').forEach(function(other) {
|
|
other.classList.remove('selected')
|
|
})
|
|
e.target.classList.add('selected')
|
|
}
|
|
}
|
|
|
|
document.onreadystatechange = function () {
|
|
if (document.readyState == "complete") {
|
|
EPUBJS.filePath = "/static/epub.js/js/libs/";
|
|
EPUBJS.cssPath = "/static/epub.js/css/";
|
|
EPUBJS.core.addCss('/static/css/epub.css')
|
|
// fileStorage.filePath = EPUBJS.filePath;
|
|
reader = ePubReader(document.location.pathname.replace(/\/reader\//, '/epub/'), {
|
|
restore: true
|
|
});
|
|
var rendition = reader.rendition,
|
|
book = reader.book;
|
|
|
|
annotations.forEach(function(a) {
|
|
rendition.annotations.highlight(a.cfiRange, a.data || {}, onHighlightClicked);
|
|
})
|
|
|
|
|
|
reader.rendition.on('keydown', function(event) {
|
|
if (event.key == 'Delete') {
|
|
document.querySelectorAll('.epubjs-hl.selected').forEach(function(a) {
|
|
annotations = annotations.filter(function(annotation) {
|
|
return annotation.cfiRange != a.dataset.epubcfi
|
|
})
|
|
rendition.annotations.remove(a.dataset.epubcfi)
|
|
saveAnnotations()
|
|
})
|
|
}
|
|
if (event.key == 'n') {
|
|
if (currentSelection) {
|
|
/*
|
|
var range = currentSelection.contents.window.getSelection().getRangeAt(0)
|
|
console.log(
|
|
currentSelection.cfiRange,
|
|
reader.rendition.book.section().cfiFromRange(range).toString()
|
|
)
|
|
//currentSelection.cfiRange = reader.rendition.book.section().cfiFromRange(range).toString()
|
|
*/
|
|
|
|
rendition.annotations.highlight(currentSelection.cfiRange, currentSelection.data, onHighlightClicked);
|
|
currentSelection.contents.window.getSelection().removeAllRanges();
|
|
delete currentSelection.contents
|
|
annotations.push(currentSelection)
|
|
saveAnnotations()
|
|
document.querySelectorAll('.epubjs-hl.selected').forEach(function(other) {
|
|
other.classList.remove('selected')
|
|
})
|
|
currentSelection = null
|
|
}
|
|
}
|
|
})
|
|
rendition.on("mark", function(cfiRange, contents) {
|
|
console.log('!! mark', cfiRange)
|
|
})
|
|
rendition.on("selected", function(cfiRange, contents) {
|
|
console.log('!! selected', cfiRange)
|
|
getText(book, cfiRange, function(quote) {
|
|
currentSelection = {
|
|
cfiRange: cfiRange,
|
|
data: {
|
|
quote: quote
|
|
},
|
|
contents: contents
|
|
}
|
|
})
|
|
});
|
|
}
|
|
};
|