1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00

Fix webkit hash target, add annotation events

This commit is contained in:
Fred Chasen 2018-12-05 15:56:47 -08:00
parent 186c4fa395
commit 56722c7726
3 changed files with 22 additions and 11 deletions

View file

@ -1,5 +1,6 @@
import EventEmitter from "event-emitter"; import EventEmitter from "event-emitter";
import EpubCFI from "./epubcfi"; import EpubCFI from "./epubcfi";
import { EVENTS } from "./utils/constants";
/** /**
* Handles managing adding & removing Annotations * Handles managing adding & removing Annotations
@ -111,34 +112,34 @@ class Annotations {
* Add a highlight to the store * Add a highlight to the store
* @param {EpubCFI} cfiRange EpubCFI range to attach annotation to * @param {EpubCFI} cfiRange EpubCFI range to attach annotation to
* @param {object} data Data to assign to annotation * @param {object} data Data to assign to annotation
* @param {function} cb Callback after annotation is added * @param {function} cb Callback after annotation is clicked
* @param {string} className CSS class to assign to annotation * @param {string} className CSS class to assign to annotation
* @param {object} styles CSS styles to assign to annotation * @param {object} styles CSS styles to assign to annotation
*/ */
highlight (cfiRange, data, cb, className, styles) { highlight (cfiRange, data, cb, className, styles) {
this.add("highlight", cfiRange, data, cb, className, styles); return this.add("highlight", cfiRange, data, cb, className, styles);
} }
/** /**
* Add a underline to the store * Add a underline to the store
* @param {EpubCFI} cfiRange EpubCFI range to attach annotation to * @param {EpubCFI} cfiRange EpubCFI range to attach annotation to
* @param {object} data Data to assign to annotation * @param {object} data Data to assign to annotation
* @param {function} cb Callback after annotation is added * @param {function} cb Callback after annotation is clicked
* @param {string} className CSS class to assign to annotation * @param {string} className CSS class to assign to annotation
* @param {object} styles CSS styles to assign to annotation * @param {object} styles CSS styles to assign to annotation
*/ */
underline (cfiRange, data, cb, className, styles) { underline (cfiRange, data, cb, className, styles) {
this.add("underline", cfiRange, data, cb, className, styles); return this.add("underline", cfiRange, data, cb, className, styles);
} }
/** /**
* Add a mark to the store * Add a mark to the store
* @param {EpubCFI} cfiRange EpubCFI range to attach annotation to * @param {EpubCFI} cfiRange EpubCFI range to attach annotation to
* @param {object} data Data to assign to annotation * @param {object} data Data to assign to annotation
* @param {function} cb Callback after annotation is added * @param {function} cb Callback after annotation is clicked
*/ */
mark (cfiRange, data, cb) { mark (cfiRange, data, cb) {
this.add("mark", cfiRange, data, cb); return this.add("mark", cfiRange, data, cb);
} }
/** /**
@ -206,7 +207,7 @@ class Annotations {
* @param {EpubCFI} options.cfiRange EpubCFI range to attach annotation to * @param {EpubCFI} options.cfiRange EpubCFI range to attach annotation to
* @param {object} options.data Data to assign to annotation * @param {object} options.data Data to assign to annotation
* @param {int} options.sectionIndex Index in the Spine of the Section annotation belongs to * @param {int} options.sectionIndex Index in the Spine of the Section annotation belongs to
* @param {function} [options.cb] Callback after annotation is added * @param {function} [options.cb] Callback after annotation is clicked
* @param {string} className CSS class to assign to annotation * @param {string} className CSS class to assign to annotation
* @param {object} styles CSS styles to assign to annotation * @param {object} styles CSS styles to assign to annotation
* @returns {Annotation} annotation * @returns {Annotation} annotation
@ -257,7 +258,7 @@ class Annotation {
} }
this.mark = result; this.mark = result;
this.emit(EVENTS.ANNOTATION.ATTACH, result);
return result; return result;
} }
@ -280,7 +281,7 @@ class Annotation {
} }
this.mark = undefined; this.mark = undefined;
this.emit(EVENTS.ANNOTATION.DETACH, result);
return result; return result;
} }

View file

@ -617,9 +617,15 @@ class Contents {
let id = target.substring(target.indexOf("#")+1); let id = target.substring(target.indexOf("#")+1);
let el = this.document.getElementById(id); let el = this.document.getElementById(id);
if(el) { if(el) {
position = el.getBoundingClientRect(); if (isWebkit) {
// Webkit reports incorrect bounding rects in Columns
let newRange = new Range();
newRange.selectNode(el);
position = newRange.getBoundingClientRect();
} else {
position = el.getBoundingClientRect();
}
} }
} }

View file

@ -53,5 +53,9 @@ export const EVENTS = {
}, },
LAYOUT : { LAYOUT : {
UPDATED : "updated" UPDATED : "updated"
},
ANNOTATION : {
ATTACH : "attach",
DETACH : "detach"
} }
} }