vue-book-reader
Version:
<div align="center"> <img width=250 src="https://raw.githubusercontent.com/jinhuan138/vue--book-reader/master/public/logo.png" /> <h1>VueReader</h1> </div>
49 lines (48 loc) • 1.83 kB
JavaScript
const makeComicBook = ({ entries, loadBlob, getSize }, file) => {
const cache = /* @__PURE__ */ new Map();
const urls = /* @__PURE__ */ new Map();
const load = async (name) => {
if (cache.has(name))
return cache.get(name);
const src = URL.createObjectURL(await loadBlob(name));
const page = URL.createObjectURL(
new Blob([`<!DOCTYPE html><html><head><meta charset="utf-8"></head><body style="margin: 0"><img src="${src}"></body></html>`], { type: "text/html" })
);
urls.set(name, [src, page]);
cache.set(name, page);
return page;
};
const unload = (name) => {
var _a, _b;
(_b = (_a = urls.get(name)) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(_a, (url) => URL.revokeObjectURL(url));
urls.delete(name);
cache.delete(name);
};
const exts = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg", ".jxl", ".avif"];
const files = entries.map((entry) => entry.filename).filter((name) => exts.some((ext) => name.endsWith(ext))).sort();
if (!files.length)
throw new Error("No supported image files in archive");
const book = {};
book.getCover = () => loadBlob(files[0]);
book.metadata = { title: file.name };
book.sections = files.map((name) => ({
id: name,
load: () => load(name),
unload: () => unload(name),
size: getSize(name)
}));
book.toc = files.map((name) => ({ label: name, href: name }));
book.rendition = { layout: "pre-paginated" };
book.resolveHref = (href) => ({ index: book.sections.findIndex((s) => s.id === href) });
book.splitTOCHref = (href) => [href, null];
book.getTOCFragment = (doc) => doc.documentElement;
book.destroy = () => {
for (const arr of urls.values())
for (const url of arr)
URL.revokeObjectURL(url);
};
return book;
};
export {
makeComicBook
};