1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-03 14:59:18 +02:00
This commit is contained in:
code-Shabbir 2023-09-22 19:29:27 +05:30 committed by GitHub
commit 3330f9f49f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 885 additions and 17212 deletions

17299
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,11 @@
{ {
"name": "epubjs", "name": "@makiwin/epubjs",
"version": "0.3.93", "version": "0.3.94",
"description": "Parse and Render Epubs", "description": "Parse and Render Epubs",
"main": "lib/index.js", "main": "lib/index.js",
"module": "src/index.js", "module": "src/index.js",
"types": "types/index.d.ts", "types": "types/index.d.ts",
"repository": "https://github.com/futurepress/epub.js", "repository": "https://github.com/makiwin/epub.js",
"directories": { "directories": {
"test": "test" "test": "test"
}, },
@ -24,7 +24,7 @@
"watch": "babel --watch -d lib/ src/", "watch": "babel --watch -d lib/ src/",
"prepare": "npm run compile && npm run build && npm run minify && npm run legacy && npm run productionLegacy" "prepare": "npm run compile && npm run build && npm run minify && npm run legacy && npm run productionLegacy"
}, },
"author": "fchasen@gmail.com", "author": "fchasen@gmail.com, massimiliano.vinciprova@gmail.com",
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.15.7", "@babel/cli": "^7.15.7",
@ -36,6 +36,7 @@
"babel-loader": "^8.2.3", "babel-loader": "^8.2.3",
"documentation": "^13.2.5", "documentation": "^13.2.5",
"eslint": "^8.0.1", "eslint": "^8.0.1",
"file-loader": "^6.2.0",
"jsdoc": "^3.6.7", "jsdoc": "^3.6.7",
"karma": "^5.0.9", "karma": "^5.0.9",
"karma-chrome-launcher": "^3.1.0", "karma-chrome-launcher": "^3.1.0",

View file

@ -1,363 +1,368 @@
import {uuid, isNumber, isElement, windowBounds, extend} from "../../utils/core"; import {
import throttle from 'lodash/throttle' uuid,
isNumber,
isElement,
windowBounds,
extend,
} from '../../utils/core';
import throttle from 'lodash/throttle';
class Stage { class Stage {
constructor(_options) { constructor(_options) {
this.settings = _options || {}; this.settings = _options || {};
this.id = "epubjs-container-" + uuid(); this.id = 'epubjs-container-' + uuid();
this.container = this.create(this.settings); this.container = this.create(this.settings);
if(this.settings.hidden) { if (this.settings.hidden) {
this.wrapper = this.wrap(this.container); this.wrapper = this.wrap(this.container);
} }
}
}
/*
/* * Creates an element to render to.
* Creates an element to render to. * Resizes to passed width and height or to the elements size
* Resizes to passed width and height or to the elements size */
*/ create(options) {
create(options){ let height = options.height; // !== false ? options.height : "100%";
let height = options.height;// !== false ? options.height : "100%"; let width = options.width; // !== false ? options.width : "100%";
let width = options.width;// !== false ? options.width : "100%"; let overflow = options.overflow || false;
let overflow = options.overflow || false; let axis = options.axis || 'vertical';
let axis = options.axis || "vertical"; let direction = options.direction;
let direction = options.direction;
extend(this.settings, options);
extend(this.settings, options);
if (options.height && isNumber(options.height)) {
if(options.height && isNumber(options.height)) { height = options.height + 'px';
height = options.height + "px"; }
}
if (options.width && isNumber(options.width)) {
if(options.width && isNumber(options.width)) { width = options.width + 'px';
width = options.width + "px"; }
}
// Create new container element
// Create new container element let container = document.createElement('div');
let container = document.createElement("div");
container.id = this.id;
container.id = this.id; container.classList.add('epub-container');
container.classList.add("epub-container");
// Style Element
// Style Element // container.style.fontSize = "0";
// container.style.fontSize = "0"; container.style.wordSpacing = '0';
container.style.wordSpacing = "0"; container.style.lineHeight = '0';
container.style.lineHeight = "0"; container.style.verticalAlign = 'top';
container.style.verticalAlign = "top"; container.style.position = 'relative';
container.style.position = "relative"; container.style.overflowAnchor = 'none';
if(axis === "horizontal") { if (axis === 'horizontal') {
// container.style.whiteSpace = "nowrap"; // container.style.whiteSpace = "nowrap";
container.style.display = "flex"; container.style.display = 'flex';
container.style.flexDirection = "row"; container.style.flexDirection = 'row';
container.style.flexWrap = "nowrap"; container.style.flexWrap = 'nowrap';
} }
if(width){ if (width) {
container.style.width = width; container.style.width = width;
} }
if(height){ if (height) {
container.style.height = height; container.style.height = height;
} }
if (overflow) { if (overflow) {
if (overflow === "scroll" && axis === "vertical") { if (overflow === 'scroll' && axis === 'vertical') {
container.style["overflow-y"] = overflow; container.style['overflow-y'] = overflow;
container.style["overflow-x"] = "hidden"; container.style['overflow-x'] = 'hidden';
} else if (overflow === "scroll" && axis === "horizontal") { } else if (overflow === 'scroll' && axis === 'horizontal') {
container.style["overflow-y"] = "hidden"; container.style['overflow-y'] = 'hidden';
container.style["overflow-x"] = overflow; container.style['overflow-x'] = overflow;
} else { } else {
container.style["overflow"] = overflow; container.style['overflow'] = overflow;
} }
} }
if (direction) { if (direction) {
container.dir = direction; container.dir = direction;
container.style["direction"] = direction; container.style['direction'] = direction;
} }
if (direction && this.settings.fullsize) { if (direction && this.settings.fullsize) {
document.body.style["direction"] = direction; document.body.style['direction'] = direction;
} }
return container; return container;
} }
wrap(container) { wrap(container) {
var wrapper = document.createElement("div"); var wrapper = document.createElement('div');
wrapper.style.visibility = "hidden"; wrapper.style.visibility = 'hidden';
wrapper.style.overflow = "hidden"; wrapper.style.overflow = 'hidden';
wrapper.style.width = "0"; wrapper.style.width = '0';
wrapper.style.height = "0"; wrapper.style.height = '0';
wrapper.appendChild(container); wrapper.appendChild(container);
return wrapper; return wrapper;
} }
getElement(_element) {
getElement(_element){ var element;
var element;
if (isElement(_element)) {
if(isElement(_element)) { element = _element;
element = _element; } else if (typeof _element === 'string') {
} else if (typeof _element === "string") { element = document.getElementById(_element);
element = document.getElementById(_element); }
}
if (!element) {
if(!element){ throw new Error('Not an Element');
throw new Error("Not an Element"); }
}
return element;
return element; }
}
attachTo(what) {
attachTo(what){ var element = this.getElement(what);
var base;
var element = this.getElement(what);
var base; if (!element) {
return;
if(!element){ }
return;
} if (this.settings.hidden) {
base = this.wrapper;
if(this.settings.hidden) { } else {
base = this.wrapper; base = this.container;
} else { }
base = this.container;
} element.appendChild(base);
element.appendChild(base); this.element = element;
this.element = element; return element;
}
return element;
getContainer() {
} return this.container;
}
getContainer() {
return this.container; onResize(func) {
} // Only listen to window for resize event if width and height are not fixed.
// This applies if it is set to a percent or auto.
onResize(func){ if (!isNumber(this.settings.width) || !isNumber(this.settings.height)) {
// Only listen to window for resize event if width and height are not fixed. this.resizeFunc = throttle(func, 50);
// This applies if it is set to a percent or auto. window.addEventListener('resize', this.resizeFunc, false);
if(!isNumber(this.settings.width) || }
!isNumber(this.settings.height) ) { }
this.resizeFunc = throttle(func, 50);
window.addEventListener("resize", this.resizeFunc, false); onOrientationChange(func) {
} this.orientationChangeFunc = func;
window.addEventListener(
} 'orientationchange',
this.orientationChangeFunc,
onOrientationChange(func){ false
this.orientationChangeFunc = func; );
window.addEventListener("orientationchange", this.orientationChangeFunc, false); }
}
size(width, height) {
size(width, height){ var bounds;
var bounds; let _width = width || this.settings.width;
let _width = width || this.settings.width; let _height = height || this.settings.height;
let _height = height || this.settings.height;
// If width or height are set to false, inherit them from containing element
// If width or height are set to false, inherit them from containing element if (width === null) {
if(width === null) { bounds = this.element.getBoundingClientRect();
bounds = this.element.getBoundingClientRect();
if (bounds.width) {
if(bounds.width) { width = Math.floor(bounds.width);
width = Math.floor(bounds.width); this.container.style.width = width + 'px';
this.container.style.width = width + "px"; }
} } else {
} else { if (isNumber(width)) {
if (isNumber(width)) { this.container.style.width = width + 'px';
this.container.style.width = width + "px"; } else {
} else { this.container.style.width = width;
this.container.style.width = width; }
} }
}
if (height === null) {
if(height === null) { bounds = bounds || this.element.getBoundingClientRect();
bounds = bounds || this.element.getBoundingClientRect();
if (bounds.height) {
if(bounds.height) { height = bounds.height;
height = bounds.height; this.container.style.height = height + 'px';
this.container.style.height = height + "px"; }
} } else {
if (isNumber(height)) {
} else { this.container.style.height = height + 'px';
if (isNumber(height)) { } else {
this.container.style.height = height + "px"; this.container.style.height = height;
} else { }
this.container.style.height = height; }
}
} if (!isNumber(width)) {
width = this.container.clientWidth;
if(!isNumber(width)) { }
width = this.container.clientWidth;
} if (!isNumber(height)) {
height = this.container.clientHeight;
if(!isNumber(height)) { }
height = this.container.clientHeight;
} this.containerStyles = window.getComputedStyle(this.container);
this.containerStyles = window.getComputedStyle(this.container); this.containerPadding = {
left: parseFloat(this.containerStyles['padding-left']) || 0,
this.containerPadding = { right: parseFloat(this.containerStyles['padding-right']) || 0,
left: parseFloat(this.containerStyles["padding-left"]) || 0, top: parseFloat(this.containerStyles['padding-top']) || 0,
right: parseFloat(this.containerStyles["padding-right"]) || 0, bottom: parseFloat(this.containerStyles['padding-bottom']) || 0,
top: parseFloat(this.containerStyles["padding-top"]) || 0, };
bottom: parseFloat(this.containerStyles["padding-bottom"]) || 0
}; // Bounds not set, get them from window
let _windowBounds = windowBounds();
// Bounds not set, get them from window let bodyStyles = window.getComputedStyle(document.body);
let _windowBounds = windowBounds(); let bodyPadding = {
let bodyStyles = window.getComputedStyle(document.body); left: parseFloat(bodyStyles['padding-left']) || 0,
let bodyPadding = { right: parseFloat(bodyStyles['padding-right']) || 0,
left: parseFloat(bodyStyles["padding-left"]) || 0, top: parseFloat(bodyStyles['padding-top']) || 0,
right: parseFloat(bodyStyles["padding-right"]) || 0, bottom: parseFloat(bodyStyles['padding-bottom']) || 0,
top: parseFloat(bodyStyles["padding-top"]) || 0, };
bottom: parseFloat(bodyStyles["padding-bottom"]) || 0
}; if (!_width) {
width = _windowBounds.width - bodyPadding.left - bodyPadding.right;
if (!_width) { }
width = _windowBounds.width -
bodyPadding.left - if ((this.settings.fullsize && !_height) || !_height) {
bodyPadding.right; height =
} _windowBounds.height - bodyPadding.top - bodyPadding.bottom;
}
if ((this.settings.fullsize && !_height) || !_height) {
height = _windowBounds.height - return {
bodyPadding.top - width:
bodyPadding.bottom; width -
} this.containerPadding.left -
this.containerPadding.right,
return { height:
width: width - height -
this.containerPadding.left - this.containerPadding.top -
this.containerPadding.right, this.containerPadding.bottom,
height: height - };
this.containerPadding.top - }
this.containerPadding.bottom
}; bounds() {
let box;
} if (this.container.style.overflow !== 'visible') {
box = this.container && this.container.getBoundingClientRect();
bounds(){ }
let box;
if (this.container.style.overflow !== "visible") { if (!box || !box.width || !box.height) {
box = this.container && this.container.getBoundingClientRect(); return windowBounds();
} } else {
return box;
if(!box || !box.width || !box.height) { }
return windowBounds(); }
} else {
return box; getSheet() {
} var style = document.createElement('style');
} // WebKit hack --> https://davidwalsh.name/add-rules-stylesheets
style.appendChild(document.createTextNode(''));
getSheet(){
var style = document.createElement("style"); document.head.appendChild(style);
// WebKit hack --> https://davidwalsh.name/add-rules-stylesheets return style.sheet;
style.appendChild(document.createTextNode("")); }
document.head.appendChild(style); addStyleRules(selector, rulesArray) {
var scope = '#' + this.id + ' ';
return style.sheet; var rules = '';
}
if (!this.sheet) {
addStyleRules(selector, rulesArray){ this.sheet = this.getSheet();
var scope = "#" + this.id + " "; }
var rules = "";
rulesArray.forEach(function (set) {
if(!this.sheet){ for (var prop in set) {
this.sheet = this.getSheet(); if (set.hasOwnProperty(prop)) {
} rules += prop + ':' + set[prop] + ';';
}
rulesArray.forEach(function(set) { }
for (var prop in set) { });
if(set.hasOwnProperty(prop)) {
rules += prop + ":" + set[prop] + ";"; this.sheet.insertRule(scope + selector + ' {' + rules + '}', 0);
} }
}
}); axis(axis) {
if (axis === 'horizontal') {
this.sheet.insertRule(scope + selector + " {" + rules + "}", 0); this.container.style.display = 'flex';
} this.container.style.flexDirection = 'row';
this.container.style.flexWrap = 'nowrap';
axis(axis) { } else {
if(axis === "horizontal") { this.container.style.display = 'block';
this.container.style.display = "flex"; }
this.container.style.flexDirection = "row"; this.settings.axis = axis;
this.container.style.flexWrap = "nowrap"; }
} else {
this.container.style.display = "block"; // orientation(orientation) {
} // if (orientation === "landscape") {
this.settings.axis = axis; //
} // } else {
//
// orientation(orientation) { // }
// if (orientation === "landscape") { //
// // this.orientation = orientation;
// } else { // }
//
// } direction(dir) {
// if (this.container) {
// this.orientation = orientation; this.container.dir = dir;
// } this.container.style['direction'] = dir;
}
direction(dir) {
if (this.container) { if (this.settings.fullsize) {
this.container.dir = dir; document.body.style['direction'] = dir;
this.container.style["direction"] = dir; }
} this.settings.dir = dir;
}
if (this.settings.fullsize) {
document.body.style["direction"] = dir; overflow(overflow) {
} if (this.container) {
this.settings.dir = dir; if (overflow === 'scroll' && this.settings.axis === 'vertical') {
} this.container.style['overflow-y'] = overflow;
this.container.style['overflow-x'] = 'hidden';
overflow(overflow) { } else if (
if (this.container) { overflow === 'scroll' &&
if (overflow === "scroll" && this.settings.axis === "vertical") { this.settings.axis === 'horizontal'
this.container.style["overflow-y"] = overflow; ) {
this.container.style["overflow-x"] = "hidden"; this.container.style['overflow-y'] = 'hidden';
} else if (overflow === "scroll" && this.settings.axis === "horizontal") { this.container.style['overflow-x'] = overflow;
this.container.style["overflow-y"] = "hidden"; } else {
this.container.style["overflow-x"] = overflow; this.container.style['overflow'] = overflow;
} else { }
this.container.style["overflow"] = overflow; }
} this.settings.overflow = overflow;
} }
this.settings.overflow = overflow;
} destroy() {
var base;
destroy() {
var base; if (this.element) {
if (this.settings.hidden) {
if (this.element) { base = this.wrapper;
} else {
if(this.settings.hidden) { base = this.container;
base = this.wrapper; }
} else {
base = this.container; if (this.element.contains(this.container)) {
} this.element.removeChild(this.container);
}
if(this.element.contains(this.container)) {
this.element.removeChild(this.container); window.removeEventListener('resize', this.resizeFunc);
} window.removeEventListener(
'orientationChange',
window.removeEventListener("resize", this.resizeFunc); this.orientationChangeFunc
window.removeEventListener("orientationChange", this.orientationChangeFunc); );
}
} }
}
} }
export default Stage; export default Stage;

1
types/epub.min.d.ts vendored Normal file
View file

@ -0,0 +1 @@
declare module '@makiwin/epubjs/dist/epub.min.js';

View file

@ -1,8 +1,8 @@
var webpack = require("webpack"); var webpack = require("webpack");
var path = require("path"); var path = require("path");
var PROD = (process.env.NODE_ENV === "production") var PROD = process.env.NODE_ENV === "production";
var LEGACY = (process.env.LEGACY) var LEGACY = process.env.LEGACY;
var MINIMIZE = (process.env.MINIMIZE === "true") var MINIMIZE = process.env.MINIMIZE === "true";
var hostname = "localhost"; var hostname = "localhost";
var port = 8080; var port = 8080;
@ -22,9 +22,9 @@ if (MINIMIZE) {
module.exports = { module.exports = {
mode: process.env.NODE_ENV, mode: process.env.NODE_ENV,
entry: { entry: {
"epub": "./src/epub.js", epub: ["./src/epub.js", "./types/epub.min.d.ts"],
}, },
devtool: MINIMIZE ? false : 'source-map', devtool: MINIMIZE ? false : "source-map",
output: { output: {
path: path.resolve("./dist"), path: path.resolve("./dist"),
filename: filename, filename: filename,
@ -32,20 +32,20 @@ module.exports = {
library: "ePub", library: "ePub",
libraryTarget: "umd", libraryTarget: "umd",
libraryExport: "default", libraryExport: "default",
publicPath: "/dist/" publicPath: "/dist/",
}, },
optimization: { optimization: {
minimize: MINIMIZE minimize: MINIMIZE,
}, },
externals: { externals: {
"jszip/dist/jszip": "JSZip", "jszip/dist/jszip": "JSZip",
"xmldom": "xmldom" xmldom: "xmldom",
}, },
plugins: [], plugins: [],
resolve: { resolve: {
alias: { alias: {
path: "path-webpack" path: "path-webpack",
} },
}, },
devServer: { devServer: {
host: hostname, host: hostname,
@ -54,30 +54,45 @@ module.exports = {
headers: { headers: {
"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE", "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE",
"Access-Control-Allow-Headers": "Content-Type" "Access-Control-Allow-Headers": "Content-Type",
} },
}, },
module: { module: {
rules: [ rules: [
{
test: /\.d\.ts$/,
loader: "file-loader",
options: {
name: "[name].[ext]",
outputPath: "",
},
},
{ {
test: /\.js$/, test: /\.js$/,
exclude: /node_modules/, exclude: /node_modules/,
use: { use: {
loader: "babel-loader", loader: "babel-loader",
options: { options: {
presets: [["@babel/preset-env", { presets: [
targets: LEGACY ? "defaults" : "last 2 Chrome versions, last 2 Safari versions, last 2 ChromeAndroid versions, last 2 iOS versions, last 2 Firefox versions, last 2 Edge versions", [
corejs: 3, "@babel/preset-env",
useBuiltIns: "usage", {
bugfixes: true, targets: LEGACY
modules: false ? "defaults"
}]] : "last 2 Chrome versions, last 2 Safari versions, last 2 ChromeAndroid versions, last 2 iOS versions, last 2 Firefox versions, last 2 Edge versions",
} corejs: 3,
} useBuiltIns: "usage",
} bugfixes: true,
] modules: false,
},
],
],
},
},
},
],
}, },
performance: { performance: {
hints: false hints: false,
} },
} };