summaryrefslogtreecommitdiffstats
path: root/assets/reader.js
blob: 6f04c2b677187f2af018bfb5d786feb0c549fd59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
69
70
71
72
73
74
75
// In-page reader for the WP-001 PDF. PDF.js loads only when the reader opens, then pages render
// one at a time into canvases so the first page shows before the rest are done.
(function(){
"use strict";
var PDF = "/papers/WP-001-can-an-llm-dream.pdf";
var CDN = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/";
var btn = document.getElementById("readBtn"), box = document.getElementById("reader");
var pages = document.getElementById("readerPages"), load = document.getElementById("readerLoad");
var msg = document.getElementById("readerMsg"), bar = document.getElementById("readerBar");
var reduce = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
var started = false;

function track(name){ if (window.umami) window.umami.track(name, {page: location.pathname}); }
function progress(text, frac){
  load.hidden = false; msg.textContent = text;
  if (frac == null) bar.classList.add("ind");
  else { bar.classList.remove("ind"); bar.firstChild.style.width = Math.round(frac*100) + "%"; }
}
function fail(){
  bar.hidden = true;
  msg.innerHTML = 'The reader could not load the paper. <a href="' + PDF + '" download>Download the PDF</a> instead.';
}
function script(src){
  return new Promise(function(ok, no){ var s = document.createElement("script"); s.src = src; s.onload = ok; s.onerror = no; document.head.appendChild(s); });
}

function start(){
  started = true;
  progress("Loading the PDF reader", null);
  script(CDN + "pdf.min.js").then(function(){
    var lib = window.pdfjsLib;
    // A worker from another origin is blocked, so start it from a same-origin blob that imports the CDN file.
    var blob = new Blob(['importScripts("' + CDN + 'pdf.worker.min.js");'], {type:"application/javascript"});
    lib.GlobalWorkerOptions.workerPort = new Worker(URL.createObjectURL(blob));
    progress("Loading the paper", null);
    return lib.getDocument({url: PDF}).promise;
  }).then(function(doc){
    var n = doc.numPages, slots = [];
    return doc.getPage(1).then(function(first){
      var vp = first.getViewport({scale:1});
      for (var i=1; i<=n; i++){
        var d = document.createElement("div"); d.className = "pg"; d.style.aspectRatio = vp.width + " / " + vp.height;
        d.setAttribute("role", "img"); d.setAttribute("aria-label", "Page " + i + " of " + n);
        pages.appendChild(d); slots.push(d);
      }
      var i = 0;
      function next(){
        if (i >= n){ load.hidden = true; return; }
        progress("Loading page " + (i+1) + " of " + n, i/n);
        return doc.getPage(i+1).then(function(page){
          var slot = slots[i], w = slot.clientWidth || 800, dpr = Math.min(3, Math.max(2, window.devicePixelRatio || 1));
          var base = page.getViewport({scale:1}), vp2 = page.getViewport({scale: w*dpr/base.width});
          var cv = document.createElement("canvas"); cv.width = Math.round(vp2.width); cv.height = Math.round(vp2.height);
          return page.render({canvasContext: cv.getContext("2d"), viewport: vp2}).promise.then(function(){
            slot.appendChild(cv); i++; return next();
          });
        });
      }
      return next();
    });
  }).catch(fail);
}

btn.addEventListener("click", function(){
  var open = btn.getAttribute("aria-expanded") !== "true";
  btn.setAttribute("aria-expanded", String(open));
  btn.firstChild.nodeValue = open ? "Hide the paper" : "Read the paper here";
  box.hidden = !open;
  if (open){
    track("paper-reader-open");
    if (!started) start();
    box.scrollIntoView({behavior: reduce ? "auto" : "smooth", block: "nearest"});
  }
});
})();