add annotation event from annotation panel

This commit is contained in:
j 2019-02-01 01:02:55 +05:30
commit fe9b0618ed
4 changed files with 72 additions and 24 deletions

View file

@ -18,6 +18,8 @@ Ox.load({
if (annotation) {
reader.rendition.display(annotation.cfiRange)
}
} else if (event == 'addAnnotation') {
createAnnotation()
} else if (event == 'addAnnotations') {
data.annotations.forEach(function(annotation) {
annotations.push(annotation)
@ -29,6 +31,29 @@ Ox.load({
})
})
function createAnnotation() {
console.log('createAnnotation', currentSelection)
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()
*/
renderAnnotation(currentSelection)
currentSelection.contents.window.getSelection().removeAllRanges();
delete currentSelection.contents
addAnnotation(currentSelection)
document.querySelectorAll('.epubjs-hl.selected').forEach(function(other) {
other.classList.remove('selected')
})
currentSelection = null
}
}
function addAnnotation(annotation) {
annotations.push(annotation)
Ox.$parent.postMessage('addAnnotation', annotation)
@ -125,22 +150,7 @@ document.onreadystatechange = function () {
if (selected) {
deselectAllAnnotations()
}
/*
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()
*/
renderAnnotation(currentSelection)
currentSelection.contents.window.getSelection().removeAllRanges();
delete currentSelection.contents
addAnnotation(currentSelection)
document.querySelectorAll('.epubjs-hl.selected').forEach(function(other) {
other.classList.remove('selected')
})
currentSelection = null
createAnnotation()
} else if (selected) {
console.log('editNote?', selected.dataset.id)
@ -162,7 +172,16 @@ document.onreadystatechange = function () {
localStorage.epubFontSize = fontSize
event.preventDefault()
}
console.log(fontSize)
}).on('mouseup', function(event) {
if (currentSelection) {
var selection = window.getSelection()
if (selection.isCollapsed) {
currentSelection = null
}
if (!currentSelection) {
Ox.$parent.postMessage('selection', false)
}
}
})
rendition.on("mark", function(cfiRange, contents) {
console.log('!! mark', cfiRange)
@ -175,6 +194,7 @@ document.onreadystatechange = function () {
text: text,
contents: contents
}
Ox.$parent.postMessage('selection', text ? true : false)
})
});
}