epub.html 14.9 KB
Newer Older
Fred Chasen's avatar
Fred Chasen committed
1 2 3 4 5 6 7 8 9 10 11
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>Chunking Test - Epub Parser</title>
    <script>this.ready=new Promise(function($){document.addEventListener('DOMContentLoaded',$,{once:true})})</script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.1/jszip.min.js"></script>
    <script src="https://unpkg.com/epubjs@0.4.2/dist/epub.min.js"></script>
    <script src="../dist/paged.js"></script>

Fred Chasen's avatar
Fred Chasen committed
12 13 14 15
    <link rel="stylesheet" type="text/css" href="assets/styles/examples.css">

    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Fred Chasen's avatar
Fred Chasen committed
16 17 18 19
</head>
<body>
  <script>
    ready.then(async function () {
Fred Chasen's avatar
Fred Chasen committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
      let scale;
      // Sidebar
      var nav = document.getElementById("navigation");
	    var opener = document.getElementById("opener");
      opener.style.visibility = "hidden";
	    opener.addEventListener("click", function(e){
	      nav.classList.add("open");
	    }, false);

	    var closer = document.getElementById("closer");
	    closer.addEventListener("click", function(e){
	      nav.classList.remove("open");
	    }, false);

      // Dropper
      let form = document.getElementById("form");

      form.addEventListener("dragenter", dragenter, false);
      form.addEventListener("dragover", dragover, false);
      form.addEventListener("dragleave", dragleave, false);
      form.addEventListener("drop", drop, false);

      function dragenter(e) {
        e.stopPropagation();
        e.preventDefault();
        form.classList.add("is-dragover");
      }

      function dragover(e) {
        e.stopPropagation();
        e.preventDefault();
      }

      function dragleave(e) {
        e.stopPropagation();
        e.preventDefault();
        form.classList.remove("is-dragover");
      }

      function drop(e) {
        e.stopPropagation();
        e.preventDefault();

        var dt = e.dataTransfer;
        var files = dt.files;
        upload(files[0]);

        form.style.display = "none";
      }
Fred Chasen's avatar
Fred Chasen committed
69 70 71

      let inputElement = document.getElementById("input");

Fred Chasen's avatar
Fred Chasen committed
72 73 74 75 76 77 78
      let choose = document.getElementById("choose");
      choose.addEventListener("click", () => {
        inputElement.click();
      }, false);


      inputElement.addEventListener('change', (e) => {
Fred Chasen's avatar
Fred Chasen committed
79
        var file = e.target.files[0];
Fred Chasen's avatar
Fred Chasen committed
80 81 82 83 84 85
        upload(file);
        form.style.display = "none";
      });

      function upload(file) {
        // var file = e.target.files[0];
Fred Chasen's avatar
Fred Chasen committed
86 87 88 89 90 91 92 93 94 95
        if (window.FileReader) {
          var reader = new FileReader();
          reader.onload = openBook;
          reader.readAsArrayBuffer(file);

          let pages = document.querySelectorAll(".section");
          for (var i = 0; i < pages.length; i++) {
            pages[i].remove();
          }
        }
Fred Chasen's avatar
Fred Chasen committed
96
      }
Fred Chasen's avatar
Fred Chasen committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

      async function sections(spine) {
        let text = "";
        let pattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
        let links = /<link[^>]*rel\s*=\s*["']\s*stylesheet\s*["'][^>]*>/im;

        for (let section of spine) {
          let href = section.href;
          let styles = "";
          let html = await fetch(href)
            .then((response) => {
              return response.text();
            }).then((t) => {
              let matches = pattern.exec(t);
              styles = links.exec(t);
              return matches && matches.length && matches[1];
            });
Fred Chasen's avatar
Fred Chasen committed
114
          let id = encodeURIComponent(section.href);
Fred Chasen's avatar
Fred Chasen committed
115
          text += "<article id='"+id+"' class='section'>";
Fred Chasen's avatar
Fred Chasen committed
116 117 118 119 120 121 122

          // if (styles) {
          //   for (let i = 0; i < styles.length; i++) {
          //     text += styles[i];
          //   }
          // }

Fred Chasen's avatar
Fred Chasen committed
123
          text += html + "</article>";
Fred Chasen's avatar
Fred Chasen committed
124 125 126 127
        }
        return text;
      }

Fred Chasen's avatar
Fred Chasen committed
128
      function addStyles(polisher, resources) {
Fred Chasen's avatar
Fred Chasen committed
129
        let head = document.querySelector("head");
130
        let stylesArray = resources.filter((r) => {
Fred Chasen's avatar
Fred Chasen committed
131 132
          return r.type === "text/css";
        });
Fred Chasen's avatar
Fred Chasen committed
133
        let toAdd = ["assets/styles/epub.css"];
134 135 136 137 138 139 140
        for (let style of stylesArray) {
          // let link = document.createElement("link");
          // link.rel = "stylesheet";
          // link.type = "text/css";
          // link.href = style.href;
          // head.appendChild(link);
          toAdd.push(style.href);
Fred Chasen's avatar
Fred Chasen committed
141
        }
Fred Chasen's avatar
Fred Chasen committed
142
        return polisher.add(...toAdd) //.apply(styles, toAdd);
Fred Chasen's avatar
Fred Chasen committed
143 144
      }

145
      async function openBook(e){
Fred Chasen's avatar
Fred Chasen committed
146
        var bookData = e.target.result;
Fred Chasen's avatar
Fred Chasen committed
147
        let chunker, polisher;
Fred Chasen's avatar
Fred Chasen committed
148 149
        let epub = ePub(bookData, {replacements: true} ).then((book) => {
          this.book = book;
Fred Chasen's avatar
Fred Chasen committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

            let toc = book.navigation.toc;

            var $nav = document.getElementById("toc"),
                docfrag = document.createDocumentFragment();

            var processTocItem = function(chapter, parent) {
              var item = document.createElement("li");
              var link = document.createElement("a");
              link.id = "chap-" + chapter.id;
              link.textContent = chapter.title;
              link.href = "#" + encodeURIComponent(chapter.href);
              item.appendChild(link);
              parent.appendChild(item);

              link.onclick = function(){
                nav.classList.remove("open");
              };

              if (chapter.subitems && chapter.subitems.length) {
                var ul = document.createElement("ul");
                item.appendChild(ul);
                chapter.subitems.forEach(function(subchapter) {
                  processTocItem(subchapter, ul);
                });
              }

              // link.onclick = function(){
              //   var url = link.getAttribute("href");
              //   console.log(url)
              //   rendition.display(url);
              //   return false;
              // };

            }

            toc.forEach(function(chapter) {
              processTocItem(chapter, docfrag);
            });

            $nav.appendChild(docfrag);

            opener.style.visibility = "visible";



          let meta = book.metadata;

            var $title = document.getElementById("title");
            var $author = document.getElementById("author");
            var $cover = document.getElementById("cover");
            var $nav = document.getElementById('navigation');

            $title.textContent = meta.title;
            $author.textContent = meta.creator;

            if (book.cover) {
              $cover.src = book.cover;
            } else {
              $cover.style.display = "none";
            }



214 215
          return book;
        }).then((book) => {
Fred Chasen's avatar
Fred Chasen committed
216 217 218 219 220 221
          chunker = new Paged.Chunker()
          polisher = new Paged.Polisher();

          Paged.initializeHandlers(chunker, polisher);

          return addStyles(polisher, this.book.resources);
222
        }).then(() => {
Fred Chasen's avatar
Fred Chasen committed
223 224 225
          return sections(this.book.spine);
        }).then((text) => {
          let t0 = performance.now();
Fred Chasen's avatar
Fred Chasen committed
226
          let viewer = document.querySelector("#viewer");
227

Fred Chasen's avatar
Fred Chasen committed
228
          let flow = chunker.flow(text, viewer).then((flow) => {
Fred Chasen's avatar
Fred Chasen committed
229 230 231 232 233
            let t1 = performance.now();

            console.log("Rendering " + flow.total + " pages took " + (t1 - t0) + " milliseconds.");
          });

Fred Chasen's avatar
Fred Chasen committed
234
          let resizer = () => {
235
            let pages = document.querySelector(".pagedjs_pages");
Fred Chasen's avatar
Fred Chasen committed
236 237 238 239 240 241 242 243
            scale = ((window.innerWidth ) / pages.offsetWidth) * .75;

            if (scale < 1) {
              pages.style.transform = `scale(${scale})`;
              pages.style.marginLeft = `${(window.innerWidth / 2) - ((pages.offsetWidth/ 2) )  * scale }px`;
            } else {
              pages.style.transform = "none";
              pages.style.marginLeft = "0";
Fred Chasen's avatar
Fred Chasen committed
244

Fred Chasen's avatar
Fred Chasen committed
245
            }
Fred Chasen's avatar
Fred Chasen committed
246 247 248 249 250
          };
          resizer();

          window.addEventListener("resize", resizer, false);

Fred Chasen's avatar
Fred Chasen committed
251 252

          setTimeout(() => {
253
            let page = document.querySelector(".pagedjs_page");
Fred Chasen's avatar
Fred Chasen committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
            let height = (page.offsetHeight + 51) * scale;
            var next = document.getElementById("next");

            next.style.visibility = "visible";
            next.addEventListener("click", function(e){
              window.scrollBy(0, height);
              e.preventDefault();
            }, false);

            var prev = document.getElementById("prev");
            prev.style.visibility = "visible";

            prev.addEventListener("click", function(e){
              window.scrollBy(0, -height);
              e.preventDefault();
            }, false);

            var keyListener = function(e){

              // Left Key
              if ((e.keyCode || e.which) == 37) {
                window.scrollBy(0, -height);
              }

              // Right Key
              if ((e.keyCode || e.which) == 39) {
                window.scrollBy(0, height);
              }

            };

            document.addEventListener("keyup", keyListener, false);
          }, 200);


Fred Chasen's avatar
Fred Chasen committed
289 290 291 292 293
          epub = undefined;
        });
      }
    });
  </script>
Fred Chasen's avatar
Fred Chasen committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  <style>

  :root {
      /* --width: 8.5in; */
      /* --height: 11in; */
      --margin: 100px;
  }

  /* section {
    break-before: column;
  } */

    @media screen {

      body {
        background-color: whitesmoke;
      }

      #main {
        display: block;
      }

      #viewer {
        /* width: calc(var(--width) * 2);
        height: var(--height);
        overflow: scroll;
        margin-top: 80px; */
      }
      /* .scaled {
        transform: scale(.6);
        transform-origin: 0 0;
      } */

327
      .pagedjs_page {
Fred Chasen's avatar
Fred Chasen committed
328 329 330 331 332
        background-color: #fdfdfd;
        margin-bottom: calc(var(--margin) / 2);
        flex: none;
      }

333
      .pagedjs_pages {
Fred Chasen's avatar
Fred Chasen committed
334 335 336 337 338 339 340 341 342
        width: calc(var(--width) * 2);
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: flex-start;
        transform-origin: 0 0;
        margin: 0 auto;
      }

343
      .pagedjs_pages > .section {
Fred Chasen's avatar
Fred Chasen committed
344 345 346 347 348 349 350
        width: calc(var(--width) * 2);
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: flex-start;
      }

351
      .pagedjs_first_page {
352 353 354
        margin-left: 50%;
      }

Fred Chasen's avatar
Fred Chasen committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
      #controls {
        margin: 20px 0;
        text-align: center;
      }
    }

    .box {
      font-size: 1.25rem; /* 20 */
      background-color: #ddd;
      position: relative;
      padding: 100px 20px;

      outline: 2px dashed #ccc;
      outline-offset: -10px;

      -webkit-transition: outline-offset .15s ease-in-out, background-color .15s linear;
      transition: outline-offset .15s ease-in-out, background-color .15s linear;

      width: 400px;
      margin: 80px auto;
    }

    .box.is-dragover {
      outline-offset: -20px;
      outline-color: #000;
      background-color: #ccc;
    }

    .box .box__dragndrop {
      display: inline;
    }

    .box .box__icon {
      width: 100%;
      height: 80px;
      fill: #92b0b3;
      display: block;
      margin-bottom: 40px;
      font-size: 100px;
    }

    .box.is-uploading .box__input,
    .box.is-success .box__input,
    .box.is-error .box__input
    {
      visibility: hidden;
    }

    .box__uploading,
    .box__success,
    .box__error
    {
      display: none;
    }

    .box.is-uploading .box__uploading,
    .box.is-success .box__success,
    .box.is-error .box__error
    {
      display: block;
      position: absolute;
      top: 50%;
      right: 0;
      left: 0;

      -webkit-transform: translateY( -50% );
      transform: translateY( -50% );
    }

    .box__uploading
    {
      font-style: italic;
    }
    .box__success
    {
      -webkit-animation: appear-from-inside .25s ease-in-out;
      animation: appear-from-inside .25s ease-in-out;
    }

    @-webkit-keyframes appear-from-inside
    {
      from	{ -webkit-transform: translateY( -50% ) scale( 0 ); }
      75%		{ -webkit-transform: translateY( -50% ) scale( 1.1 ); }
      to		{ -webkit-transform: translateY( -50% ) scale( 1 ); }
    }
    @keyframes appear-from-inside
    {
      from	{ transform: translateY( -50% ) scale( 0 ); }
      75%		{ transform: translateY( -50% ) scale( 1.1 ); }
      to		{ transform: translateY( -50% ) scale( 1 ); }
    }

    .box__file + label
		{
			max-width: 80%;
			text-overflow: ellipsis;
			white-space: nowrap;
			cursor: pointer;
			display: inline-block;
			overflow: hidden;
		}

    #input {
      display: none;
    }

    .arrow {
      position: fixed;
      top: 50%;
      margin-top: -32px;
      font-size: 64px;
      color: #E2E2E2;
      font-family: arial, sans-serif;
      font-weight: bold;
      cursor: pointer;
      -webkit-user-select: none;
      -moz-user-select: none;
      user-select: none;
      text-decoration: none;
    }

    #prev {
      left: 0;
    }

    #next {
      right: 0;
    }

    .arrow .material-icons {
      font-size: 64px;
    }

.navlink {
  margin: 14px;
  display: block;
  text-align: center;
  text-decoration: none;
  color: #ccc;
}

.arrow:hover, .navlink:hover {
  color: #777;
}

.arrow:active, .navlink:hover {
  color: #000;
}

#toc {
  display: block;
  margin: 10px auto;
}

#navigation h1 {
  width: 200px;
  font-size: 16px;
  font-weight: normal;
  color: #232323;
  margin-bottom: 10px;
  margin-left: 24px;
}

#navigation h2 {
  font-size: 14px;
  font-weight: normal;
  color: #B0B0B0;
  margin-bottom: 20px;
  text-align: left;
  margin-left: 24px;
}

#navigation ul {
  padding-left: 36px;
  margin-left: 0;
  margin-top: 12px;
  margin-bottom: 12px;
  width: 340px;
}

#navigation ul li {
  list-style: decimal;
  margin-bottom: 10px;
  color: #cccddd;
  font-size: 12px;
  padding-left: 0;
  margin-left: 0;
}

#navigation ul li a {
  color: #ccc;
  text-decoration: none;
}

#navigation ul li a:hover {
  color: #fff;
  text-decoration: underline;
}

#navigation ul li a.active {
  color: #fff;
}

#navigation #cover {
  display: block;
  margin: 24px auto;
}

#opener {
  position: fixed;
}

#next, #prev {
  visibility: hidden;
}
  </style>
Fred Chasen's avatar
Fred Chasen committed
571 572
<body>

Fred Chasen's avatar
Fred Chasen committed
573 574 575 576 577 578 579 580 581 582 583 584 585
  <a id="opener">
    <i class="material-icons">menu</i>
  </a>
	<div id="navigation">
		<div id="header">
	    <a id="closer">
	      <i class="material-icons">close</i>
	    </a>
		</div>
    <h1 id="title">...</h1>
    <image id="cover" width="150px"/>
    <h2 id="author">...</h2>
    <ul id="toc"></ul>
Fred Chasen's avatar
Fred Chasen committed
586 587
	</div>

Fred Chasen's avatar
Fred Chasen committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	<div id="main">
  <div id="controls">
    <!-- <input type="file" id="input"> -->

    <form id="form" class="box" method="post" action="" enctype="multipart/form-data">
      <div class="box__input">
        <i class="material-icons box__icon">file_download</i>

        <input class="box__file" type="file" name="files[]" id="input" />
        <label for="file"><a id="choose"><strong>Choose an ePub</strong></a><span class="box__dragndrop"> or drag it here</span>.</label>

      </div>
      <div class="box__uploading">Uploading&hellip;</div>
      <div class="box__success">Done!</div>
      <div class="box__error">Error! <span></span>.</div>
    </form>
	 </div>

Fred Chasen's avatar
Fred Chasen committed
606
  <div id="viewer"></div>
Fred Chasen's avatar
Fred Chasen committed
607 608 609 610 611 612 613 614

  <a id="prev" href="#prev" class="arrow">
    <i class="material-icons">chevron_left</i>
  </a>
  <a id="next" href="#next" class="arrow">
    <i class="material-icons">chevron_right</i>
  </a>
  </div>
Fred Chasen's avatar
Fred Chasen committed
615 616
</body>
</html>