2019-01-21 11:40:45 +00:00
|
|
|
"use strict";
|
|
|
|
var reader;
|
|
|
|
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 currentSelection;
|
2019-01-22 15:01:24 +00:00
|
|
|
var fontSize = parseInt(localStorage.epubFontSize || '100', 10)
|
2019-01-21 11:40:45 +00:00
|
|
|
|
2019-01-23 15:14:59 +00:00
|
|
|
Ox.load({
|
|
|
|
'UI': {
|
|
|
|
loadCSS: false
|
|
|
|
}
|
|
|
|
}, function() {
|
|
|
|
Ox.$parent.bindMessage(function(data, event) {
|
|
|
|
console.log('got', event, 'data', data)
|
|
|
|
if (event == 'selectAnnotation') {
|
|
|
|
selectAnnotation(data.id)
|
|
|
|
var annotation = annotations.filter(function(a) { return a.id == data.id })[0]
|
|
|
|
if (annotation) {
|
|
|
|
reader.rendition.display(annotation.cfiRange)
|
|
|
|
}
|
2019-01-24 11:52:44 +00:00
|
|
|
} else if (event == 'addAnnotations') {
|
|
|
|
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-01-23 15:14:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function addAnnotation(annotation) {
|
|
|
|
annotations.push(annotation)
|
|
|
|
Ox.$parent.postMessage('addAnnotation', annotation)
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectAnnotation(id) {
|
|
|
|
$('.epubjs-hl.selected').each(function(i, g) {
|
|
|
|
g.classList.remove('selected')
|
|
|
|
})
|
|
|
|
$('.epubjs-hl[data-id='+id+']').each(function(i, g) {
|
|
|
|
g.classList.add('selected')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function deselectAnnotation(id) {
|
|
|
|
$('.epubjs-hl[data-id='+id+']').each(function(i, g) {
|
|
|
|
g.classList.remove('selected')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-31 06:22:19 +00:00
|
|
|
function removeAnnotation(id) {
|
|
|
|
var a = annotations.filter(function(a) { return a.id == id })[0]
|
|
|
|
if (a) {
|
|
|
|
annotations = annotations.filter(function(annotation) {
|
|
|
|
return annotation.id != id
|
|
|
|
})
|
|
|
|
reader.rendition.annotations.remove(a.cfiRange)
|
|
|
|
}
|
|
|
|
Ox.$parent.postMessage('removeAnnotation', {id: id})
|
2019-01-24 11:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderAnnotation(annotation) {
|
|
|
|
reader.rendition.annotations.highlight(annotation.cfiRange, {id: annotation.id}, onHighlightClicked);
|
2019-01-23 15:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 11:40:45 +00:00
|
|
|
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;
|
|
|
|
|
2019-01-22 15:01:24 +00:00
|
|
|
rendition.themes.fontSize(fontSize + "%");
|
2019-01-22 13:35:01 +00:00
|
|
|
|
2019-01-21 11:40:45 +00:00
|
|
|
reader.rendition.on('keydown', function(event) {
|
|
|
|
if (event.key == 'Delete') {
|
|
|
|
document.querySelectorAll('.epubjs-hl.selected').forEach(function(a) {
|
2019-01-31 06:22:19 +00:00
|
|
|
removeAnnotation(a.dataset.id)
|
2019-01-21 11:40:45 +00:00
|
|
|
})
|
|
|
|
}
|
2019-01-22 13:35:01 +00:00
|
|
|
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()
|
|
|
|
*/
|
2019-01-24 11:52:44 +00:00
|
|
|
renderAnnotation(currentSelection)
|
2019-01-22 13:35:01 +00:00
|
|
|
currentSelection.contents.window.getSelection().removeAllRanges();
|
|
|
|
delete currentSelection.contents
|
2019-01-23 15:14:59 +00:00
|
|
|
addAnnotation(currentSelection)
|
2019-01-22 13:35:01 +00:00
|
|
|
document.querySelectorAll('.epubjs-hl.selected').forEach(function(other) {
|
|
|
|
other.classList.remove('selected')
|
|
|
|
})
|
|
|
|
currentSelection = null
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 15:01:24 +00:00
|
|
|
if (event.keyCode == 61 && event.shiftKey) {
|
|
|
|
fontSize += 10
|
|
|
|
rendition.themes.fontSize(fontSize + "%");
|
|
|
|
localStorage.epubFontSize = fontSize
|
|
|
|
event.preventDefault()
|
|
|
|
} else if (event.keyCode == 173 && event.shiftKey) {
|
|
|
|
fontSize -= 10
|
|
|
|
rendition.themes.fontSize(fontSize + "%");
|
|
|
|
localStorage.epubFontSize = fontSize
|
|
|
|
event.preventDefault()
|
|
|
|
} else if (event.keyCode == 48 && event.shiftKey) {
|
|
|
|
fontSize = 100
|
|
|
|
rendition.themes.fontSize(fontSize + "%");
|
|
|
|
localStorage.epubFontSize = fontSize
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
console.log(fontSize)
|
2019-01-22 13:35:01 +00:00
|
|
|
})
|
|
|
|
rendition.on("mark", function(cfiRange, contents) {
|
|
|
|
console.log('!! mark', cfiRange)
|
2019-01-21 11:40:45 +00:00
|
|
|
})
|
|
|
|
rendition.on("selected", function(cfiRange, contents) {
|
2019-01-23 15:14:59 +00:00
|
|
|
getText(book, cfiRange, function(text) {
|
2019-01-22 13:35:01 +00:00
|
|
|
currentSelection = {
|
2019-01-23 15:14:59 +00:00
|
|
|
id: Ox.SHA1(cfiRange),
|
2019-01-22 13:35:01 +00:00
|
|
|
cfiRange: cfiRange,
|
2019-01-23 15:14:59 +00:00
|
|
|
text: text,
|
2019-01-22 13:35:01 +00:00
|
|
|
contents: contents
|
2019-01-21 11:40:45 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|