-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreorder.js
More file actions
372 lines (335 loc) · 13.2 KB
/
Copy pathreorder.js
File metadata and controls
372 lines (335 loc) · 13.2 KB
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
369
370
371
372
let _pdfLibPromise = null;
function getPdfLib() {
if (!_pdfLibPromise) _pdfLibPromise = import("./vendor/pdf-lib.esm.min.js");
return _pdfLibPromise;
}
const dropzone = document.getElementById("dropzone");
const fileInput = document.getElementById("fileInput");
const fileListEl = document.getElementById("fileList");
const actionsEl = document.getElementById("actions");
const reorderBtn = document.getElementById("reorderBtn");
const reorderBtnLabel = document.getElementById("reorderBtnLabel");
const clearBtn = document.getElementById("clearBtn");
const resultEl = document.getElementById("result");
const reorderPanel = document.getElementById("reorderPanel");
const reorderMeta = document.getElementById("reorderMeta");
/** @type {{file: File, order: number[]} | null} */
let loaded = null;
let dragIndex = null;
// Live, honest proof: count real network requests made after page load.
const proofLiveText = document.getElementById("proofLiveText");
let requestsSinceLoad = 0;
if ("PerformanceObserver" in window) {
try {
new PerformanceObserver((list) => {
requestsSinceLoad += list.getEntries().length;
if (proofLiveText) {
proofLiveText.textContent = `${requestsSinceLoad} network request${
requestsSinceLoad === 1 ? "" : "s"
} since page load · verified live, not our word for it`;
}
}).observe({ type: "resource", buffered: false });
} catch (e) {
// observer unsupported — static claim stays as-is
}
}
function escapeHtml(str) {
const div = document.createElement("div");
div.textContent = str;
return div.innerHTML.replace(/"/g, """).replace(/'/g, "'");
}
function baseName(fileName) {
return fileName.replace(/\.pdf$/i, "");
}
/** Marks an error as a known, user-facing validation message (safe to show verbatim),
* as opposed to a raw pdf-lib/parser exception (which must stay hidden from users). */
class ValidationError extends Error {}
/**
* Reads the source PDF's own top-level /Outlines linked list (if any) as a
* flat list of {title, page} entries (1-based page numbers), so a bookmark
* can be carried over into the reordered output instead of silently dropped
* — building `out` via PDFDocument.create()+copyPages() never brings
* /Outlines along on its own. Only direct [pageRef, ...] /Dest arrays are
* resolved (not named destinations, not /A GoTo actions) — an entry that
* can't be resolved to a page is dropped rather than guessed at.
*/
async function readExistingBookmarks(doc) {
const { PDFName } = await getPdfLib();
const context = doc.context;
const outlinesRef = doc.catalog.get(PDFName.of("Outlines"));
if (!outlinesRef) return [];
const outlines = context.lookup(outlinesRef);
if (!outlines) return [];
const pageRefs = doc.getPages().map((p) => p.ref);
function resolvePage(dest) {
if (!dest || typeof dest.get !== "function") return -1;
try {
const pageRef = dest.get(0);
if (!pageRef) return -1;
return pageRefs.findIndex(
(r) => r.tag === pageRef.tag && r.objectNumber === pageRef.objectNumber
);
} catch (e) {
return -1;
}
}
const items = [];
let cur = outlines.get(PDFName.of("First"));
let guard = 0;
while (cur && guard++ < 10000) {
const item = context.lookup(cur);
if (!item) break;
const titleObj = item.get(PDFName.of("Title"));
const title = titleObj && titleObj.decodeText ? titleObj.decodeText() : "";
const dest = item.get(PDFName.of("Dest"));
const pageIndex = resolvePage(dest);
if (title && pageIndex >= 0) items.push({ title, page: pageIndex + 1 });
cur = item.get(PDFName.of("Next"));
}
return items;
}
/**
* Rebuilds a flat PDF outline (bookmark) tree on `doc` from a list of
* {title, page} entries (1-based) via pdf-lib's low-level context API —
* pdf-lib has no high-level outline API, so /Outlines and each item dict
* are constructed and linked (Parent/First/Last/Next/Prev/Count) by hand.
* Mirrors bookmarks.js's addOutline() exactly.
*/
async function addOutline(doc, entries) {
if (entries.length === 0) return;
const { PDFName, PDFString, PDFNumber } = await getPdfLib();
const context = doc.context;
const sorted = [...entries].sort((a, b) => a.page - b.page);
const outlineRef = context.nextRef();
const itemRefs = sorted.map(() => context.nextRef());
sorted.forEach((entry, i) => {
const page = doc.getPage(entry.page - 1);
const dict = {
Title: PDFString.of(entry.title),
Parent: outlineRef,
Dest: context.obj([page.ref, PDFName.of("Fit")]),
};
if (i > 0) dict.Prev = itemRefs[i - 1];
if (i < itemRefs.length - 1) dict.Next = itemRefs[i + 1];
context.assign(itemRefs[i], context.obj(dict));
});
context.assign(
outlineRef,
context.obj({
Type: PDFName.of("Outlines"),
First: itemRefs[0],
Last: itemRefs[itemRefs.length - 1],
Count: PDFNumber.of(itemRefs.length),
})
);
doc.catalog.set(PDFName.of("Outlines"), outlineRef);
}
function focusRow(position, selector, fallback) {
const row = fileListEl.querySelector(`li[data-position="${position}"]`);
const el = row ? row.querySelector(selector) : null;
(el || fallback || clearBtn)?.focus();
}
function renderPages() {
fileListEl.innerHTML = "";
if (!loaded) return;
loaded.order.forEach((pageIndex, position) => {
const li = document.createElement("li");
li.className = "file-row";
li.draggable = true;
li.dataset.position = String(position);
li.innerHTML = `
<span class="handle" aria-hidden="true">⠿</span>
<span class="name">${position + 1}. Page ${pageIndex + 1} of ${escapeHtml(loaded.file.name)}</span>
<button class="move-up" type="button" aria-label="Move page ${pageIndex + 1} up" ${position === 0 ? "disabled" : ""}>↑</button>
<button class="move-down" type="button" aria-label="Move page ${pageIndex + 1} down" ${position === loaded.order.length - 1 ? "disabled" : ""}>↓</button>
<button class="remove" type="button" aria-label="Remove page ${pageIndex + 1}">✕</button>
`;
li.querySelector(".remove").addEventListener("click", () => {
loaded.order.splice(position, 1);
renderPages();
updateActions();
focusRow(Math.min(position, loaded.order.length - 1), ".remove", dropzone);
});
li.querySelector(".move-up").addEventListener("click", () => {
if (position === 0) return;
const [moved] = loaded.order.splice(position, 1);
loaded.order.splice(position - 1, 0, moved);
renderPages();
focusRow(position - 1, ".move-up");
});
li.querySelector(".move-down").addEventListener("click", () => {
if (position === loaded.order.length - 1) return;
const [moved] = loaded.order.splice(position, 1);
loaded.order.splice(position + 1, 0, moved);
renderPages();
focusRow(position + 1, ".move-down");
});
li.addEventListener("dragstart", () => {
dragIndex = position;
li.classList.add("dragging");
});
li.addEventListener("dragend", () => li.classList.remove("dragging"));
li.addEventListener("dragover", (e) => e.preventDefault());
li.addEventListener("drop", (e) => {
e.preventDefault();
if (dragIndex === null || dragIndex === position) return;
const [moved] = loaded.order.splice(dragIndex, 1);
loaded.order.splice(position, 0, moved);
dragIndex = null;
renderPages();
});
fileListEl.appendChild(li);
});
}
function updateActions() {
const hasPages = !!loaded && loaded.order.length > 0;
actionsEl.hidden = !loaded;
reorderPanel.hidden = !loaded;
reorderBtn.disabled = !hasPages;
resultEl.hidden = true;
if (!loaded) return;
reorderMeta.textContent = `${loaded.order.length} of ${loaded.pageCount} page${
loaded.pageCount === 1 ? "" : "s"
} kept — drag to reorder, ✕ to drop a page`;
}
async function loadFile(file) {
try {
const { PDFDocument } = await getPdfLib();
const bytes = await file.arrayBuffer();
const doc = await PDFDocument.load(bytes, { ignoreEncryption: true });
const pageCount = doc.getPageCount();
loaded = {
file,
pageCount,
order: Array.from({ length: pageCount }, (_, i) => i),
};
renderPages();
updateActions();
} catch (err) {
loaded = null;
fileInput.value = "";
renderPages();
updateActions();
resultEl.hidden = false;
resultEl.className = "result result-error";
resultEl.setAttribute("role", "alert");
resultEl.setAttribute("aria-live", "assertive");
resultEl.innerHTML = `<span><strong>Couldn't load file.</strong> ${escapeHtml(
"This file may be corrupted or password-protected."
)}</span>`;
}
}
dropzone.addEventListener("click", () => fileInput.click());
dropzone.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
fileInput.click();
}
});
fileInput.addEventListener("change", (e) => {
const file = e.target.files[0];
if (file) loadFile(file);
});
["dragenter", "dragover"].forEach((evt) =>
dropzone.addEventListener(evt, (e) => {
e.preventDefault();
dropzone.classList.add("dragover");
})
);
["dragleave", "drop"].forEach((evt) =>
dropzone.addEventListener(evt, (e) => {
e.preventDefault();
dropzone.classList.remove("dragover");
})
);
dropzone.addEventListener("drop", (e) => {
const file = e.dataTransfer?.files?.[0];
if (file) loadFile(file);
});
clearBtn.addEventListener("click", () => {
loaded = null;
fileInput.value = "";
renderPages();
updateActions();
});
reorderBtn.addEventListener("click", async () => {
reorderBtn.disabled = true;
const originalLabel = reorderBtnLabel.textContent;
reorderBtnLabel.textContent = "Rebuilding…";
resultEl.hidden = true;
const startedAt = performance.now();
const requestsBefore = requestsSinceLoad;
try {
if (loaded.order.length === 0) throw new ValidationError("Keep at least one page.");
const { PDFDocument } = await getPdfLib();
const src = await PDFDocument.load(await loaded.file.arrayBuffer(), { ignoreEncryption: true });
const existingBookmarks = await readExistingBookmarks(src).catch(() => []);
// Remap each bookmark's old (1-based) page number to its new position
// under the reordered page list.
const oldToNewIndex = new Map();
loaded.order.forEach((oldIndex, newIndex) => oldToNewIndex.set(oldIndex, newIndex));
const remappedBookmarks = existingBookmarks
.filter((b) => oldToNewIndex.has(b.page - 1))
.map((b) => ({ title: b.title, page: oldToNewIndex.get(b.page - 1) + 1 }));
const out = await PDFDocument.create();
const pages = await out.copyPages(src, loaded.order);
pages.forEach((page) => out.addPage(page));
await addOutline(out, remappedBookmarks);
const bytes = await out.save();
const blob = new Blob([bytes], { type: "application/pdf" });
const url = URL.createObjectURL(blob);
setTimeout(() => URL.revokeObjectURL(url), 30000);
const fileName = `${baseName(loaded.file.name)}-reordered.pdf`;
const elapsedMs = Math.round(performance.now() - startedAt);
const requestsDuring = requestsSinceLoad - requestsBefore;
resultEl.hidden = false;
resultEl.className = "result";
resultEl.setAttribute("role", "status");
resultEl.setAttribute("aria-live", "polite");
resultEl.innerHTML = `
<span><strong>Done.</strong> ${pages.length} page${pages.length === 1 ? "" : "s"} rebuilt in new order —
${elapsedMs}ms, ${requestsDuring} network requests, entirely on this device.</span>
<a class="btn btn-primary" href="${url}" download="${escapeHtml(fileName)}">Download ${escapeHtml(fileName)}</a>
`;
} catch (err) {
resultEl.hidden = false;
resultEl.className = "result result-error";
resultEl.setAttribute("role", "alert");
resultEl.setAttribute("aria-live", "assertive");
resultEl.innerHTML = `<span><strong>Reorder failed.</strong> ${escapeHtml(
err instanceof ValidationError ? err.message : "This file may be corrupted or password-protected."
)}</span>`;
} finally {
reorderBtn.disabled = loaded ? loaded.order.length === 0 : true;
reorderBtn.focus();
reorderBtnLabel.textContent = originalLabel;
}
});
const proForm = document.getElementById("proForm");
const proNote = document.getElementById("proNote");
proForm.addEventListener("submit", async (e) => {
e.preventDefault();
const email = document.getElementById("proEmail").value.trim();
if (!email) return;
const proSubmitBtn = proForm.querySelector('button[type="submit"]');
proSubmitBtn.disabled = true;
// Keep a local copy so the signal is not lost if the network request fails.
const saved = JSON.parse(localStorage.getItem("clientpdf_waitlist") || "[]");
saved.push({ email, at: new Date().toISOString() });
localStorage.setItem("clientpdf_waitlist", JSON.stringify(saved));
try {
await fetch("https://formsubmit.co/ajax/analytics@antikode.com", {
method: "POST",
headers: { "Content-Type": "application/json", Accept: "application/json" },
body: JSON.stringify({
email,
_subject: "ClientPDF Pro waitlist signup",
source: location.pathname,
}),
});
} catch (err) {
// Local copy above already preserves the signup even if this fails.
}
proForm.hidden = true;
proNote.hidden = false;
});