1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00

Fix passing events from Contents to Renderer

This commit is contained in:
Fred Chasen 2016-12-13 13:39:37 +01:00
parent fb0e52d5cd
commit b21a35258b
4 changed files with 36 additions and 28 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "epubjs", "name": "epubjs",
"version": "0.3.5", "version": "0.3.6",
"description": "Parse and Render Epubs", "description": "Parse and Render Epubs",
"main": "lib/index.js", "main": "lib/index.js",
"jsnext:main": "src/index.js", "jsnext:main": "src/index.js",

View file

@ -4,6 +4,9 @@ import EpubCFI from "./epubcfi";
import Mapping from "./mapping"; import Mapping from "./mapping";
import {replaceLinks} from "./utils/replacements"; import {replaceLinks} from "./utils/replacements";
// Dom events to listen for
const EVENTS = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];
class Contents { class Contents {
constructor(doc, content, cfiBase) { constructor(doc, content, cfiBase) {
// Blank Cfi for Parsing // Blank Cfi for Parsing
@ -13,8 +16,6 @@ class Contents {
this.documentElement = this.document.documentElement; this.documentElement = this.document.documentElement;
this.content = content || this.document.body; this.content = content || this.document.body;
this.window = this.document.defaultView; this.window = this.document.defaultView;
// Dom events to listen for
this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];
this._size = { this._size = {
width: 0, width: 0,
@ -26,6 +27,10 @@ class Contents {
this.listeners(); this.listeners();
} }
static get listenedEvents() {
return EVENTS;
}
width(w) { width(w) {
// var frame = this.documentElement; // var frame = this.documentElement;
var frame = this.content; var frame = this.content;
@ -263,6 +268,7 @@ class Contents {
this.resizeListeners(); this.resizeListeners();
this.linksHandler();
} }
removeListeners() { removeListeners() {
@ -549,7 +555,8 @@ class Contents {
if(!this.document) { if(!this.document) {
return; return;
} }
this.listenedEvents.forEach(function(eventName){
EVENTS.forEach(function(eventName){
this.document.addEventListener(eventName, this.triggerEvent.bind(this), false); this.document.addEventListener(eventName, this.triggerEvent.bind(this), false);
}, this); }, this);
@ -559,7 +566,7 @@ class Contents {
if(!this.document) { if(!this.document) {
return; return;
} }
this.listenedEvents.forEach(function(eventName){ EVENTS.forEach(function(eventName){
this.document.removeEventListener(eventName, this.triggerEvent, false); this.document.removeEventListener(eventName, this.triggerEvent, false);
}, this); }, this);
@ -698,12 +705,10 @@ class Contents {
return mapping.page(this, cfiBase, start, end); return mapping.page(this, cfiBase, start, end);
} }
linksHandler(fn) { linksHandler() {
replaceLinks(this.content, fn); replaceLinks(this.content, (href) => {
} this.emit("link", href);
});
passEvents() {
} }
destroy() { destroy() {

View file

@ -6,6 +6,7 @@ import Queue from "./utils/queue";
import Layout from "./layout"; import Layout from "./layout";
import Mapping from "./mapping"; import Mapping from "./mapping";
import Themes from "./themes"; import Themes from "./themes";
import Contents from "./contents";
/** /**
* [Rendition description] * [Rendition description]
@ -67,7 +68,8 @@ class Rendition {
this.hooks.show = new Hook(this); this.hooks.show = new Hook(this);
this.hooks.content.register(this.handleLinks.bind(this)); this.hooks.content.register(this.handleLinks.bind(this));
this.hooks.content.register(this.passViewEvents.bind(this)); this.hooks.content.register(this.passEvents.bind(this));
this.hooks.content.register(this.adjustImages.bind(this));
// this.hooks.display.register(this.afterDisplay.bind(this)); // this.hooks.display.register(this.afterDisplay.bind(this));
this.themes = new Themes(this); this.themes = new Themes(this);
@ -306,7 +308,7 @@ class Rendition {
* @param {*} view * @param {*} view
*/ */
afterDisplayed(view){ afterDisplayed(view){
this.hooks.content.trigger(view, this); this.hooks.content.trigger(view.contents, this);
this.emit("rendered", view.section); this.emit("rendered", view.section);
this.reportLocation(); this.reportLocation();
} }
@ -521,12 +523,14 @@ class Rendition {
* @private * @private
* @param {View} view * @param {View} view
*/ */
passViewEvents(view){ passEvents(contents){
view.contents.listenedEvents.forEach(function(e){ var listenedEvents = Contents.listenedEvents;
view.on(e, this.triggerViewEvent.bind(this));
listenedEvents.forEach(function(e){
contents.on(e, this.triggerViewEvent.bind(this));
}.bind(this)); }.bind(this));
view.on("selected", this.triggerSelectedEvent.bind(this)); contents.on("selected", this.triggerSelectedEvent.bind(this));
} }
/** /**
@ -569,12 +573,12 @@ class Rendition {
* Hook to adjust images to fit in columns * Hook to adjust images to fit in columns
* @param {View} view * @param {View} view
*/ */
adjustImages(view) { adjustImages(contents) {
view.addStylesheetRules([ contents.addStylesheetRules([
["img", ["img",
["max-width", (view.layout.spreadWidth) + "px"], ["max-width", (this._layout.spreadWidth) + "px"],
["max-height", (view.layout.height) + "px"] ["max-height", (this._layout.height) + "px"]
] ]
]); ]);
return new Promise(function(resolve, reject){ return new Promise(function(resolve, reject){
@ -589,8 +593,8 @@ class Rendition {
return this.manager ? this.manager.getContents() : []; return this.manager ? this.manager.getContents() : [];
} }
handleLinks(view) { handleLinks(contents) {
view.contents.linksHandler((href) => { contents.on("link", (href) => {
let relative = this.book.path.relative(href); let relative = this.book.path.relative(href);
this.display(relative); this.display(relative);
}); });

View file

@ -91,7 +91,7 @@ class Themes {
}); });
} }
inject (view) { inject (contents) {
var links = []; var links = [];
var themes = this._themes; var themes = this._themes;
var theme; var theme;
@ -100,13 +100,13 @@ class Themes {
if (themes.hasOwnProperty(name)) { if (themes.hasOwnProperty(name)) {
theme = themes[name]; theme = themes[name];
if(theme.rules || (theme.url && links.indexOf(theme.url) === -1)) { if(theme.rules || (theme.url && links.indexOf(theme.url) === -1)) {
this.add(name, view.contents); this.add(name, contents);
} }
} }
} }
if(this._current) { if(this._current) {
view.contents.addClass(this._current); contents.addClass(this._current);
} }
} }
@ -137,8 +137,7 @@ class Themes {
}); });
} }
overrides (view) { overrides (contents) {
var contents = view.contents;
var overrides = this._overrides; var overrides = this._overrides;
for (var rule in overrides) { for (var rule in overrides) {