UNPKG

epubjs

Version:
249 lines (209 loc) 6.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // Manage annotations for a book? /* let a = rendition.annotations.highlight(cfiRange, data) a.on("added", () => console.log("added")) a.on("removed", () => console.log("removed")) a.on("clicked", () => console.log("clicked")) a.update(data) a.remove(); a.text(); rendition.annotations.show() rendition.annotations.hide() rendition.annotations.highlights.show() rendition.annotations.highlights.hide() */ var _eventEmitter = require("event-emitter"); var _eventEmitter2 = _interopRequireDefault(_eventEmitter); var _epubcfi = require("./epubcfi"); var _epubcfi2 = _interopRequireDefault(_epubcfi); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Handles managing adding & removing Annotations * @class */ var Annotations = function () { function Annotations(rendition) { _classCallCheck(this, Annotations); this.rendition = rendition; this.highlights = []; this.underlines = []; this.marks = []; this._annotations = {}; this._annotationsBySectionIndex = {}; this.rendition.hooks.content.register(this.inject.bind(this)); this.rendition.hooks.unloaded.register(this.clear.bind(this)); } _createClass(Annotations, [{ key: "add", value: function add(type, cfiRange, data, cb) { var hash = encodeURI(cfiRange); var cfi = new _epubcfi2.default(cfiRange); var sectionIndex = cfi.spinePos; var annotation = new Annotation({ type: type, cfiRange: cfiRange, data: data, sectionIndex: sectionIndex, cb: cb }); this._annotations[hash] = annotation; if (sectionIndex in this._annotationsBySectionIndex) { this._annotationsBySectionIndex[sectionIndex].push(hash); } else { this._annotationsBySectionIndex[sectionIndex] = [hash]; } var contents = this.rendition.getContents(); contents.forEach(function (content) { if (annotation.sectionIndex === content.sectionIndex) { annotation.attach(content); } }); return annotation; } }, { key: "remove", value: function remove(cfiRange) { var hash = decodeURI(cfiRange); var result = void 0; if (hash in this._annotations) { var annotation = this._annotations[hash]; var contents = this.rendition.getContents(); contents.forEach(function (content) { if (annotation.sectionIndex === content.sectionIndex) { annotation.detach(content); } }); delete this._annotations[hash]; } return result; } }, { key: "highlight", value: function highlight(cfiRange, data, cb) { this.add("highlight", cfiRange, data, cb); } }, { key: "underline", value: function underline(cfiRange, data, cb) { this.add("underline", cfiRange, data, cb); } }, { key: "mark", value: function mark(cfiRange, data, cb) { this.add("mark", cfiRange, data, cb); } }, { key: "each", value: function each() { return this._annotations.forEach.apply(this._annotations, arguments); } }, { key: "inject", value: function inject(contents) { var _this = this; var sectionIndex = contents.index; if (sectionIndex in this._annotationsBySectionIndex) { var annotations = this._annotationsBySectionIndex[sectionIndex]; annotations.forEach(function (hash) { var annotation = _this._annotations[hash]; annotation.attach(contents); }); } } }, { key: "clear", value: function clear(contents) { var _this2 = this; var sectionIndex = contents.index; if (sectionIndex in this._annotationsBySectionIndex) { var annotations = this._annotationsBySectionIndex[sectionIndex]; annotations.forEach(function (hash) { var annotation = _this2._annotations[hash]; annotation.detach(contents); }); } } }, { key: "show", value: function show() {} }, { key: "hide", value: function hide() {} }]); return Annotations; }(); var Annotation = function () { function Annotation(_ref) { var type = _ref.type, cfiRange = _ref.cfiRange, data = _ref.data, sectionIndex = _ref.sectionIndex; _classCallCheck(this, Annotation); this.type = type; this.cfiRange = cfiRange; this.data = data; this.sectionIndex = sectionIndex; this.mark = undefined; } _createClass(Annotation, [{ key: "update", value: function update(data) { this.data = data; } }, { key: "attach", value: function attach(contents) { var cfiRange = this.cfiRange, data = this.data, type = this.type, mark = this.mark, cb = this.cb; var result = void 0; /* if (mark) { return; // already added } */ if (type === "highlight") { result = contents.highlight(cfiRange, data, cb); } else if (type === "underline") { result = contents.underline(cfiRange, data, cb); } else if (type === "mark") { result = contents.mark(cfiRange, data, cb); } this.mark = result; return result; } }, { key: "detach", value: function detach(contents) { var cfiRange = this.cfiRange, type = this.type; if (contents) { if (type === "highlight") { result = contents.unhighlight(cfiRange); } else if (type === "underline") { result = contents.ununderline(cfiRange); } else if (type === "mark") { result = contents.unmark(cfiRange); } } this.mark = undefined; return result; } }, { key: "text", value: function text() { // TODO: needs implementation in contents } }]); return Annotation; }(); (0, _eventEmitter2.default)(Annotation.prototype); exports.default = Annotations; module.exports = exports["default"];