aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/nuldoc/public/slides/slide.js
blob: 72f14bbaf6dbc300c128c5ae8993a7d68347aa12 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
289
290
291
292
293
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import {
  AnnotationLayer,
  getDocument,
  GlobalWorkerOptions,
  TextLayer,
} from "./pdf.min.mjs";

const PAGE_HASH_PATTERN = /^#p(\d+)$/;
// How long to wait before re-rendering, so a drag-resize does not redraw on every frame.
const RESIZE_DEBOUNCE_MS = 150;
// How long to wait before showing the indicator, so quick renders do not flash it.
const RENDER_INDICATOR_DELAY_MS = 200;
// How many pages around the current one to render ahead of time.
const PREFETCH_RADIUS = 1;

function pageFromHash(hash) {
  const matched = PAGE_HASH_PATTERN.exec(hash);
  if (matched === null) return null;
  const num = Number(matched[1]);
  return num >= 1 ? num : null;
}

async function init() {
  GlobalWorkerOptions.workerSrc = "/pdf.worker.min.mjs";

  const container = document.getElementById("slide-container");
  const stage = document.getElementById("slide-stage");
  const canvas = document.getElementById("slide");
  const ctx = canvas.getContext("2d");
  const textLayerDiv = document.getElementById("text-layer");
  const annotationLayerDiv = document.getElementById("annotation-layer");
  const status = document.getElementById("slide-status");
  const prev = document.getElementById("prev");
  const next = document.getElementById("next");
  const pageInput = document.getElementById("page-input");
  const pageCount = document.getElementById("page-count");
  const url = container.dataset.slideLink;

  let doc = null;
  let pageNum = 1;
  let renderToken = 0;
  let textLayer = null;
  let stageWidth = 0;
  // `${pageNumber}@${width}` -> { image, layers }. Both are promises.
  const cache = new Map();

  const hideStatus = () => {
    status.hidden = true;
    status.replaceChildren();
    delete status.dataset.state;
  };

  const showStatus = (state, message) => {
    status.dataset.state = state;
    status.replaceChildren(message);
    status.hidden = false;
  };

  const showFailure = (message) => {
    const description = document.createElement("p");
    description.textContent = message;
    const fallback = document.createElement("a");
    fallback.href = url;
    fallback.textContent = "PDF を直接開く";
    status.dataset.state = "error";
    status.replaceChildren(description, fallback);
    status.hidden = false;
  };

  const renderToOffscreenCanvas = async (page, width) => {
    const scale = width / page.getViewport({ scale: 1 }).width;
    const viewport = page.getViewport({ scale });
    const outputScale = globalThis.devicePixelRatio || 1;

    const target = document.createElement("canvas");
    target.width = Math.floor(viewport.width * outputScale);
    target.height = Math.floor(viewport.height * outputScale);
    const transform = outputScale !== 1
      ? [outputScale, 0, 0, outputScale, 0, 0]
      : null;

    await page.render({
      canvasContext: target.getContext("2d"),
      viewport,
      transform,
    }).promise;

    return {
      canvas: target,
      viewport,
      cssWidth: Math.floor(viewport.width),
      cssHeight: Math.floor(viewport.height),
      // Factor the text/annotation layers use to convert PDF units into CSS px.
      totalScaleFactor: viewport.width / viewport.rawDims.pageWidth,
    };
  };

  // Cache the rendered result itself; same page at the same width returns instantly.
  const entryFor = (num, width) => {
    const key = `${num}@${width}`;
    const cached = cache.get(key);
    if (cached !== undefined) return cached;

    const page = doc.getPage(num);
    const entry = {
      image: page.then((p) => renderToOffscreenCanvas(p, width)),
      layers: page.then(async (p) => ({
        page: p,
        textContent: await p.getTextContent(),
        annotations: await p.getAnnotations({ intent: "display" }),
      })),
    };
    // Drop rejected promises, otherwise a failure would never be retried.
    const forget = () => {
      if (cache.get(key) === entry) cache.delete(key);
    };
    entry.image.catch(forget);
    entry.layers.catch(forget);

    cache.set(key, entry);
    return entry;
  };

  const sweepCache = () => {
    for (const key of cache.keys()) {
      const [num, width] = key.split("@").map(Number);
      if (width !== stageWidth || Math.abs(num - pageNum) > PREFETCH_RADIUS) {
        cache.delete(key);
      }
    }
  };

  const prefetchNeighbors = () => {
    for (let d = 1; d <= PREFETCH_RADIUS; d++) {
      for (const num of [pageNum + d, pageNum - d]) {
        if (num < 1 || num > doc.numPages) continue;
        entryFor(num, stageWidth);
      }
    }
  };

  const drawImage = ({ canvas: source, cssWidth, cssHeight }) => {
    if (canvas.width !== source.width || canvas.height !== source.height) {
      canvas.width = source.width;
      canvas.height = source.height;
    }
    canvas.style.width = `${cssWidth}px`;
    canvas.style.height = `${cssHeight}px`;
    ctx.drawImage(source, 0, 0);
  };

  const renderOverlays = async (entry, viewport, token) => {
    if (textLayer !== null) {
      textLayer.cancel();
      textLayer = null;
    }
    textLayerDiv.replaceChildren();
    annotationLayerDiv.replaceChildren();

    const { page, textContent, annotations } = await entry.layers;
    if (token !== renderToken) return;

    const layer = new TextLayer({
      textContentSource: textContent,
      container: textLayerDiv,
      viewport,
    });
    textLayer = layer;
    try {
      await layer.render();
    } catch {
      // Cancelled by a move to another page; leave it to the newer render.
      return;
    }
    if (token !== renderToken) return;

    if (annotations.length === 0) return;
    await new AnnotationLayer({
      div: annotationLayerDiv,
      page,
      viewport: viewport.clone({ dontFlip: true }),
      linkService,
    }).render({ annotations, renderForms: false });
  };

  const renderPage = async (num) => {
    // No measurable width (e.g. hidden); leave it to the ResizeObserver re-render.
    if (stageWidth === 0) return;

    const token = ++renderToken;
    const entry = entryFor(num, stageWidth);

    const indicator = globalThis.setTimeout(() => {
      if (token === renderToken) showStatus("loading", "描画中…");
    }, RENDER_INDICATOR_DELAY_MS);

    try {
      const image = await entry.image;
      if (token !== renderToken) return;

      globalThis.clearTimeout(indicator);
      hideStatus();
      stage.style.setProperty("--total-scale-factor", image.totalScaleFactor);
      drawImage(image);
      await renderOverlays(entry, image.viewport, token);
    } catch (e) {
      if (token !== renderToken) return;
      console.error(e);
      showFailure("スライドの描画に失敗しました。");
    } finally {
      globalThis.clearTimeout(indicator);
      if (token === renderToken) {
        sweepCache();
        prefetchNeighbors();
      }
    }
  };

  const syncControls = () => {
    prev.disabled = pageNum <= 1;
    next.disabled = doc === null || pageNum >= doc.numPages;
    if (document.activeElement !== pageInput) {
      pageInput.value = String(pageNum);
    }
  };

  const writeHash = (num) => {
    const hash = `#p${num}`;
    if (globalThis.location.hash === hash) return;
    // Pushing an entry per page would make the back button useless.
    globalThis.history.replaceState(null, "", hash);
  };

  const goToPage = (num, { updateHash = true } = {}) => {
    if (doc === null || !Number.isFinite(num)) return;
    pageNum = Math.min(Math.max(Math.trunc(num), 1), doc.numPages);
    syncControls();
    if (updateHash) writeHash(pageNum);
    renderPage(pageNum);
  };

  const linkService = {
    addLinkAttributes(link, href, newWindow) {
      link.href = href;
      link.rel = "noopener noreferrer";
      if (newWindow) link.target = "_blank";
    },
    getDestinationHash() {
      return "#";
    },
    getAnchorUrl(hash) {
      return hash;
    },
    async goToDestination(dest) {
      if (doc === null) return;
      const explicit = typeof dest === "string"
        ? await doc.getDestination(dest)
        : dest;
      if (!Array.isArray(explicit) || explicit.length === 0) return;
      const ref = explicit[0];
      const index = typeof ref === "object" && ref !== null
        ? await doc.getPageIndex(ref)
        : Number(ref);
      goToPage(index + 1);
    },
    executeNamedAction(action) {
      switch (action) {
        case "GoBack":
        case "PrevPage":
          goToPage(pageNum - 1);
          break;
        case "GoForward":
        case "NextPage":
          goToPage(pageNum + 1);
          break;
        case "FirstPage":
          goToPage(1);
          break;
        case "LastPage":
          goToPage(doc.numPages);
          break;
        default:
          break;
      }
    },
    executeSetOCGState() {},
  };

  prev.addEventListener("click", () => goToPage(pageNum - 1));
  next.addEventListener("click", () => goToPage(pageNum + 1));

  document.addEventListener("keydown", (e) => {
    if (e.target === pageInput) return;
    if (e.altKey || e.ctrlKey || e.metaKey) return;
    if (e.key === "ArrowLeft" || e.key === "h") {
      goToPage(pageNum - 1);
    } else if (e.key === "ArrowRight" || e.key === "l") {
      goToPage(pageNum + 1);
    } else {
      return;
    }
    e.preventDefault();
  });

  const commitPageInput = () => {
    const value = Number(pageInput.value);
    if (Number.isFinite(value) && pageInput.value !== "") {
      goToPage(value);
    }
    pageInput.value = String(pageNum);
  };
  pageInput.addEventListener("change", commitPageInput);
  pageInput.addEventListener("keydown", (e) => {
    if (e.key !== "Enter") return;
    e.preventDefault();
    commitPageInput();
  });

  globalThis.addEventListener("hashchange", () => {
    const num = pageFromHash(globalThis.location.hash);
    if (num !== null && num !== pageNum) goToPage(num, { updateHash: false });
  });

  let resizeTimer = 0;
  const measure = () => Math.floor(container.clientWidth);
  new ResizeObserver(() => {
    globalThis.clearTimeout(resizeTimer);
    resizeTimer = globalThis.setTimeout(() => {
      const width = measure();
      if (width === 0 || width === stageWidth) return;
      stageWidth = width;
      cache.clear();
      if (doc !== null) renderPage(pageNum);
    }, RESIZE_DEBOUNCE_MS);
  }).observe(container);

  stageWidth = measure();
  syncControls();
  showStatus("loading", "読み込み中……");

  const loadingTask = getDocument(url);
  loadingTask.onProgress = ({ loaded, total }) => {
    if (doc !== null) return;
    const percent = total > 0
      ? Math.min(100, Math.round((loaded / total) * 100))
      : null;
    showStatus(
      "loading",
      percent === null ? "読み込み中……" : `読み込み中…… ${percent}%`,
    );
  };

  try {
    doc = await loadingTask.promise;
  } catch (e) {
    console.error(e);
    showFailure("スライドの読み込みに失敗しました。");
    return;
  }

  pageCount.textContent = String(doc.numPages);
  pageInput.max = String(doc.numPages);
  pageNum = Math.min(pageFromHash(globalThis.location.hash) ?? 1, doc.numPages);
  syncControls();
  renderPage(pageNum);
}

document.addEventListener("DOMContentLoaded", init);