Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created March 30, 2022 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larscwallin/c15c2e2b93da2eaab48b7adb49093c86 to your computer and use it in GitHub Desktop.
Save larscwallin/c15c2e2b93da2eaab48b7adb49093c86 to your computer and use it in GitHub Desktop.
async function annotateAllWordsInVisibleRange(){
let intRange = await vanillaReader.readingProgressionTimeLine.fetchTimelineRange(vanillaReader._readerView.getVisibleRange().getLocator());
for(let i = intRange.start; i <= intRange.end; i++ ) {
let location = await vanillaReader.readingProgressionTimeLine.fetchContentLocationAsRange(i, i + 1)
vanillaReader.highlightsLayer.addHighlight(location.getLocator().toString(), {
color: 'yellow',
locator: location.getLocator().toString()
}
)
}
/*
document.addEventListener('mouseover', (ev)=>{
let element = document.elementFromPoint(ev.x, ev.y)
if(element) {
let highlight = vanillaReader.annotationLayerHighlights.getReaderViewAnnotations().filter((highlight)=>{
return highlight.getId().toString() === element.getAttribute('data-id');
});
if(highlight.length > 0) {
console.log(highlight[0].getLocator());
}
}
});
*/
}
async function getVisibleRangeTextContent(){
let intRange = await vanillaReader.readingProgressionTimeLine.fetchTimelineRange(vanillaReader._readerView.getVisiblePages().getLocator());
let registeredBlocks = new Map();
let resultSet = [];
let firstBlockCharOffset = undefined
let lastBlockCharOffset = undefined;
for(let i = intRange.start; i <= intRange.end; i++ ) {
let location = await vanillaReader.readingProgressionTimeLine.fetchContentLocationAsRange(i, i + 1)
let targets = await location.fetchContentBlockTargets('INTERSECTING');
if(targets.length > 0) {
let target = targets[0]
let block = target.getContentBlock();
if(i === intRange.start) {
firstBlockCharOffset = target.getCharOffset();
}
if(i === intRange.end) {
lastBlockCharOffset = target.getCharOffset();
}
let blockIsRegistered = registeredBlocks.has(block.getId());
if(!blockIsRegistered) {
registeredBlocks.set(block.getId(), block.getTextContent());
}
} else {
console.log('No block found for locator ', location);
}
}
resultSet = Array.from(registeredBlocks.values());
resultSet[0] = resultSet[0].slice(firstBlockCharOffset, undefined);
resultSet[resultSet.length - 1] = resultSet[resultSet.length - 1].slice(0, lastBlockCharOffset);
console.log(resultSet[resultSet.length - 1].slice(0, lastBlockCharOffset));
return resultSet;
}
function setGazeListener() {
if(webgazer) {
webgazer.setGazeListener((data, t)=>{
if(data){
let element = document.elementFromPoint(data.x, data.y)
if(element) {
let highlight = vanillaReader.annotationLayerHighlights.getReaderViewAnnotations().filter((highlight)=>{
return highlight.getId().toString() === element.getAttribute('data-id');
});
if(highlight.length > 0) {
highlight[0].setOptions({
rangeStyle: { "background-color": "violet" }
});
setTimeout(()=>{
highlight[0].setOptions({
rangeStyle: { "background-color": "yellow" }
},500);
})
//console.log(highlight[0].getLocator());
}
}
} else {
console.log('rec data is null')
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment