@hussmartinez/pdf-sdk 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -3
- package/dist/assets/processing/processing.worker.js +1 -1
- package/dist/index.d.ts +33 -47
- package/dist/index.js +1 -1
- package/dist/lazy/{processing-worker-Dy5JQHXJ.js → processing-worker-DIP9ck2l.js} +2 -2
- package/dist/lazy/{processing-worker-Dy5JQHXJ.js.map → processing-worker-DIP9ck2l.js.map} +1 -1
- package/dist/react.d.ts +175 -172
- package/dist/react.js +478 -41
- package/dist/react.js.map +1 -1
- package/dist/shared/{en-CQX26xju.js → en-DKR-ngAZ.js} +56 -46
- package/dist/shared/en-DKR-ngAZ.js.map +1 -0
- package/dist/styles.css +19 -0
- package/package.json +11 -8
- package/dist/shared/en-CQX26xju.js.map +0 -1
package/dist/react.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import { createMessages, createShortcutRegistry, createToolRegistry, defineMessages, defineTool } from "@hussmartinez/sdk-core";
|
|
4
|
-
import { AnnouncerProvider, IconButton, OpenIcon, RedoIcon, RotateLeftIcon, RotateRightIcon, Toolbar, ToolbarGroup, UiMessagesProvider, UndoIcon, useAnnounce, usePlatform } from "@hussmartinez/ui/react";
|
|
5
|
-
import { createContext, forwardRef, useCallback, useContext, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import { T as isPdfError, _ as createPdfJsEngine, b as safeLinkUrl, g as createPdfProcessor, n as createPdfSession, r as cleanFileName, t as pdfMessages, y as linkDestination } from "./shared/en-DKR-ngAZ.js";
|
|
3
|
+
import { createMessages, createShortcutRegistry, createToolRegistry, defineMessages, defineTool, plural } from "@hussmartinez/sdk-core";
|
|
4
|
+
import { AnnouncerProvider, Button, DeleteIcon, DownloadIcon, IconButton, MoveDownIcon, MoveUpIcon, OpenIcon, PageAddIcon, RedoIcon, RotateLeftIcon, RotateRightIcon, Toolbar, ToolbarGroup, UiMessagesProvider, UndoIcon, useAnnounce, usePlatform } from "@hussmartinez/ui/react";
|
|
5
|
+
import { Component, createContext, forwardRef, useCallback, useContext, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
6
6
|
import { uiMessages } from "@hussmartinez/ui";
|
|
7
7
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
8
8
|
// src/features/history/history-tools.ts
|
|
@@ -54,41 +54,217 @@ function rotate({ session }, angle) {
|
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
56
|
var hasTargets = ({ snapshot }) => hasDocument({ snapshot }) && targetPages(snapshot).length > 0;
|
|
57
|
+
var rotateLeftTool = /* @__PURE__ */ defineTool({
|
|
58
|
+
id: "pdf.rotate-left",
|
|
59
|
+
label: "tools.rotate-left",
|
|
60
|
+
icon: "rotate-left",
|
|
61
|
+
group: "pages",
|
|
62
|
+
isEnabled: hasTargets,
|
|
63
|
+
run: (context) => rotate(context, -90),
|
|
64
|
+
shortcuts: ["["]
|
|
65
|
+
});
|
|
66
|
+
var rotateRightTool = /* @__PURE__ */ defineTool({
|
|
67
|
+
id: "pdf.rotate-right",
|
|
68
|
+
label: "tools.rotate-right",
|
|
69
|
+
icon: "rotate-right",
|
|
70
|
+
group: "pages",
|
|
71
|
+
isEnabled: hasTargets,
|
|
72
|
+
run: (context) => rotate(context, 90),
|
|
73
|
+
shortcuts: ["]"]
|
|
74
|
+
});
|
|
75
|
+
// src/features/pages/delete-tool.ts
|
|
76
|
+
var canDelete = ({ snapshot }) => {
|
|
77
|
+
const targets = targetPages(snapshot).length;
|
|
78
|
+
return hasDocument({ snapshot }) && targets > 0 && targets < snapshot.plan.pages.length;
|
|
79
|
+
};
|
|
80
|
+
function deletePages({ session }) {
|
|
81
|
+
const before = session.getSnapshot();
|
|
82
|
+
if (!canDelete({ snapshot: before })) return;
|
|
83
|
+
const pages = targetPages(before);
|
|
84
|
+
const removed = new Set(pages);
|
|
85
|
+
const first = before.plan.pages.findIndex((page) => removed.has(page.key));
|
|
86
|
+
session.execute({
|
|
87
|
+
type: "pdf.deletePages",
|
|
88
|
+
pages
|
|
89
|
+
});
|
|
90
|
+
const remaining = session.getSnapshot().plan.pages;
|
|
91
|
+
const next = remaining[Math.min(first, remaining.length - 1)];
|
|
92
|
+
if (!next) return;
|
|
93
|
+
session.select([next.key]);
|
|
94
|
+
session.goToPage(next.key);
|
|
95
|
+
}
|
|
96
|
+
var deletePagesTool = /* @__PURE__ */ defineTool({
|
|
97
|
+
id: "pdf.delete-pages",
|
|
98
|
+
label: "tools.delete-pages",
|
|
99
|
+
icon: "delete",
|
|
100
|
+
group: "pages",
|
|
101
|
+
isEnabled: canDelete,
|
|
102
|
+
run: deletePages
|
|
103
|
+
});
|
|
104
|
+
// src/features/pages/insert-tool.ts
|
|
105
|
+
var insertPagesTool = /* @__PURE__ */ defineTool({
|
|
106
|
+
id: "pdf.insert-pages",
|
|
107
|
+
label: "tools.insert-pages",
|
|
108
|
+
icon: "page-add",
|
|
109
|
+
group: "file",
|
|
110
|
+
isEnabled: (state) => hasDocument(state) && !state.inserting,
|
|
111
|
+
run: ({ chooseInsertFile }) => chooseInsertFile?.()
|
|
112
|
+
});
|
|
113
|
+
// src/features/pages/move-tools.ts
|
|
114
|
+
function moveOf(snapshot, direction) {
|
|
115
|
+
if (snapshot.status !== "ready") return void 0;
|
|
116
|
+
const chosen = new Set(targetPages(snapshot));
|
|
117
|
+
const order = snapshot.plan.pages.map((page) => page.key);
|
|
118
|
+
const indexes = order.flatMap((key, index) => chosen.has(key) ? [index] : []);
|
|
119
|
+
const first = indexes[0];
|
|
120
|
+
const last = indexes.at(-1);
|
|
121
|
+
if (first === void 0 || last === void 0) return void 0;
|
|
122
|
+
const pages = indexes.map((index) => order[index]);
|
|
123
|
+
const block = last - first + 1 === pages.length;
|
|
124
|
+
const others = order.length - pages.length;
|
|
125
|
+
if (direction === "earlier") {
|
|
126
|
+
if (block && first === 0) return void 0;
|
|
127
|
+
return {
|
|
128
|
+
pages,
|
|
129
|
+
to: Math.max(0, first - 1)
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (block && last === order.length - 1) return void 0;
|
|
133
|
+
return {
|
|
134
|
+
pages,
|
|
135
|
+
to: Math.min(others, last + 2 - pages.length)
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function move({ session }, direction) {
|
|
139
|
+
const before = session.getSnapshot();
|
|
140
|
+
const found = moveOf(before, direction);
|
|
141
|
+
if (!found) return;
|
|
142
|
+
session.execute({
|
|
143
|
+
type: "pdf.movePages",
|
|
144
|
+
pages: found.pages,
|
|
145
|
+
to: found.to
|
|
146
|
+
});
|
|
147
|
+
session.select(found.pages);
|
|
148
|
+
const current = before.currentPage;
|
|
149
|
+
session.goToPage(current !== void 0 && found.pages.includes(current) ? current : found.pages[0]);
|
|
150
|
+
}
|
|
151
|
+
var canMove = (direction) => ({ snapshot }) => hasDocument({ snapshot }) && moveOf(snapshot, direction) !== void 0;
|
|
57
152
|
// src/features/tools.ts
|
|
58
153
|
var pdfTools = [
|
|
59
154
|
openTool,
|
|
155
|
+
insertPagesTool,
|
|
60
156
|
undoTool,
|
|
61
157
|
redoTool,
|
|
158
|
+
rotateLeftTool,
|
|
159
|
+
rotateRightTool,
|
|
62
160
|
/* @__PURE__ */ defineTool({
|
|
63
|
-
id: "pdf.
|
|
64
|
-
label: "tools.
|
|
65
|
-
icon: "
|
|
161
|
+
id: "pdf.move-earlier",
|
|
162
|
+
label: "tools.move-earlier",
|
|
163
|
+
icon: "move-up",
|
|
66
164
|
group: "pages",
|
|
67
|
-
isEnabled:
|
|
68
|
-
run: (context) =>
|
|
69
|
-
shortcuts: ["["]
|
|
165
|
+
isEnabled: canMove("earlier"),
|
|
166
|
+
run: (context) => move(context, "earlier")
|
|
70
167
|
}),
|
|
71
168
|
/* @__PURE__ */ defineTool({
|
|
72
|
-
id: "pdf.
|
|
73
|
-
label: "tools.
|
|
74
|
-
icon: "
|
|
169
|
+
id: "pdf.move-later",
|
|
170
|
+
label: "tools.move-later",
|
|
171
|
+
icon: "move-down",
|
|
75
172
|
group: "pages",
|
|
76
|
-
isEnabled:
|
|
77
|
-
run: (context) =>
|
|
78
|
-
|
|
173
|
+
isEnabled: canMove("later"),
|
|
174
|
+
run: (context) => move(context, "later")
|
|
175
|
+
}),
|
|
176
|
+
deletePagesTool,
|
|
177
|
+
/* @__PURE__ */ defineTool({
|
|
178
|
+
id: "pdf.download",
|
|
179
|
+
label: "tools.download",
|
|
180
|
+
icon: "download",
|
|
181
|
+
group: "save",
|
|
182
|
+
isEnabled: (state) => hasDocument(state) && !state.saving,
|
|
183
|
+
run: ({ save }) => save?.()
|
|
79
184
|
})
|
|
80
185
|
];
|
|
81
186
|
var DEFAULT_TOOLBAR = [
|
|
82
|
-
["pdf.open"],
|
|
187
|
+
["pdf.open", "pdf.insert-pages"],
|
|
83
188
|
["pdf.undo", "pdf.redo"],
|
|
84
|
-
[
|
|
189
|
+
[
|
|
190
|
+
"pdf.rotate-left",
|
|
191
|
+
"pdf.rotate-right",
|
|
192
|
+
"pdf.move-earlier",
|
|
193
|
+
"pdf.move-later",
|
|
194
|
+
"pdf.delete-pages"
|
|
195
|
+
],
|
|
196
|
+
["pdf.download"]
|
|
85
197
|
];
|
|
86
198
|
// src/react/context.ts
|
|
87
199
|
var PdfContext = /* @__PURE__ */ createContext(void 0);
|
|
200
|
+
// src/messages/editor-en.ts
|
|
201
|
+
var pdfEditorText = /* @__PURE__ */ defineMessages({
|
|
202
|
+
"viewer.label": "Document",
|
|
203
|
+
"viewer.empty": "No document is open.",
|
|
204
|
+
"viewer.page": "Page {page} of {count}",
|
|
205
|
+
"viewer.webLink": "Link to {destination}, opens in a new tab",
|
|
206
|
+
"viewer.emailLink": "Email {destination}",
|
|
207
|
+
"thumbnails.label": "Pages",
|
|
208
|
+
"thumbnails.page": "Page {page} of {count}",
|
|
209
|
+
"thumbnails.number": "{page}",
|
|
210
|
+
"editor.label": "PDF editor",
|
|
211
|
+
"editor.start.title": "Open a PDF to get started",
|
|
212
|
+
"editor.start.retryTitle": "Try another PDF",
|
|
213
|
+
"editor.start.choose": "Choose a PDF",
|
|
214
|
+
"editor.start.retry": "Choose another PDF",
|
|
215
|
+
"editor.start.hint": "Or drop one here. Your file stays on this device and is never uploaded.",
|
|
216
|
+
"editor.opening": "Opening the PDF…",
|
|
217
|
+
"editor.opened": /* @__PURE__ */ plural("count", {
|
|
218
|
+
one: "Opened a PDF with {count} page.",
|
|
219
|
+
other: "Opened a PDF with {count} pages."
|
|
220
|
+
}),
|
|
221
|
+
"editor.closed": "The PDF was closed.",
|
|
222
|
+
"editor.undone": "Undone.",
|
|
223
|
+
"editor.redone": "Redone.",
|
|
224
|
+
"editor.rotated": /* @__PURE__ */ plural("count", {
|
|
225
|
+
one: "Rotated {count} page.",
|
|
226
|
+
other: "Rotated {count} pages."
|
|
227
|
+
}),
|
|
228
|
+
"editor.deleted": /* @__PURE__ */ plural("count", {
|
|
229
|
+
one: "Deleted {count} page.",
|
|
230
|
+
other: "Deleted {count} pages."
|
|
231
|
+
}),
|
|
232
|
+
"editor.moved": /* @__PURE__ */ plural("count", {
|
|
233
|
+
one: "Moved {count} page to position {position}.",
|
|
234
|
+
other: "Moved {count} pages to position {position}."
|
|
235
|
+
}),
|
|
236
|
+
"editor.inserted": /* @__PURE__ */ plural("count", {
|
|
237
|
+
one: "Inserted {count} page after page {page}.",
|
|
238
|
+
other: "Inserted {count} pages after page {page}."
|
|
239
|
+
}),
|
|
240
|
+
"editor.historyTrimmed": "To make room for the inserted document, your earliest edits can no longer be undone.",
|
|
241
|
+
"editor.saving": "Preparing the PDF…",
|
|
242
|
+
"editor.cancelSave": "Cancel",
|
|
243
|
+
"editor.saved": "Saved {name}.",
|
|
244
|
+
"editor.ready": "The PDF is ready.",
|
|
245
|
+
"editor.saveFailed": "The PDF couldn't be saved. Please try again.",
|
|
246
|
+
"toolbar.label": "PDF tools",
|
|
247
|
+
"toolbar.groups.file": "File",
|
|
248
|
+
"toolbar.groups.history": "History",
|
|
249
|
+
"toolbar.groups.pages": "Pages",
|
|
250
|
+
"toolbar.groups.save": "Save",
|
|
251
|
+
"toolbar.groups.other": "More tools",
|
|
252
|
+
"tools.open": "Open a PDF",
|
|
253
|
+
"tools.undo": "Undo",
|
|
254
|
+
"tools.redo": "Redo",
|
|
255
|
+
"tools.rotate-left": "Rotate left",
|
|
256
|
+
"tools.rotate-right": "Rotate right",
|
|
257
|
+
"tools.delete-pages": "Delete pages",
|
|
258
|
+
"tools.download": "Download",
|
|
259
|
+
"tools.insert-pages": "Insert pages",
|
|
260
|
+
"tools.move-earlier": "Move earlier",
|
|
261
|
+
"tools.move-later": "Move later"
|
|
262
|
+
});
|
|
88
263
|
// src/react/editor-messages.ts
|
|
89
264
|
var pdfEditorMessages = /* @__PURE__ */ defineMessages({
|
|
90
265
|
...pdfMessages,
|
|
91
|
-
...uiMessages
|
|
266
|
+
...uiMessages,
|
|
267
|
+
...pdfEditorText
|
|
92
268
|
});
|
|
93
269
|
// src/react/hooks.ts
|
|
94
270
|
function usePdfContext(hook) {
|
|
@@ -142,7 +318,7 @@ function useProvidedSession(props) {
|
|
|
142
318
|
function PdfSessionProvider(props) {
|
|
143
319
|
const { messages: overrides, locale = "en", children } = props;
|
|
144
320
|
const session = useProvidedSession(props);
|
|
145
|
-
const messages = useMemo(() => createMessages(
|
|
321
|
+
const messages = useMemo(() => createMessages(pdfEditorMessages, {
|
|
146
322
|
locale,
|
|
147
323
|
...overrides ? { overrides } : {}
|
|
148
324
|
}), [locale, overrides]);
|
|
@@ -336,6 +512,29 @@ var LETTER$1 = {
|
|
|
336
512
|
width: 612,
|
|
337
513
|
height: 792
|
|
338
514
|
};
|
|
515
|
+
function focusedThumbnail(root) {
|
|
516
|
+
if (!root) return void 0;
|
|
517
|
+
const active = document.activeElement;
|
|
518
|
+
if (!(active instanceof HTMLElement) || !root.contains(active)) return void 0;
|
|
519
|
+
return active.dataset["hmPageKey"];
|
|
520
|
+
}
|
|
521
|
+
var FocusKeeper = class extends Component {
|
|
522
|
+
getSnapshotBeforeUpdate() {
|
|
523
|
+
return focusedThumbnail(this.props.root.current) ?? null;
|
|
524
|
+
}
|
|
525
|
+
componentDidUpdate(_props, _state, focused) {
|
|
526
|
+
const element = this.props.root.current;
|
|
527
|
+
if (!focused || !element) return;
|
|
528
|
+
const now = document.activeElement;
|
|
529
|
+
if (now !== null && now !== document.body) return;
|
|
530
|
+
const target = this.props.keys.includes(focused) ? focused : this.props.fallback;
|
|
531
|
+
if (!target) return;
|
|
532
|
+
element.querySelector(`[data-hm-page-key="${CSS.escape(target)}"]`)?.focus();
|
|
533
|
+
}
|
|
534
|
+
render() {
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
};
|
|
339
538
|
var join$3 = (...names) => names.filter(Boolean).join(" ");
|
|
340
539
|
function Thumbnail(props) {
|
|
341
540
|
const { page, index, count, id, selected, current, active, displayWidth, classNames } = props;
|
|
@@ -386,7 +585,7 @@ function Thumbnail(props) {
|
|
|
386
585
|
});
|
|
387
586
|
}
|
|
388
587
|
function PdfThumbnails(props) {
|
|
389
|
-
const { className, classNames = {}, unstyled = false, concurrency = 2 } = props;
|
|
588
|
+
const { className, classNames = {}, unstyled = false, concurrency = 2, onDelete, onMove } = props;
|
|
390
589
|
const session = usePdfSession();
|
|
391
590
|
const snapshot = usePdfDocument();
|
|
392
591
|
const messages = usePdfMessages();
|
|
@@ -451,6 +650,13 @@ function PdfThumbnails(props) {
|
|
|
451
650
|
const onKeyDown = (event) => {
|
|
452
651
|
if (keys.length === 0) return;
|
|
453
652
|
const modifier = event.ctrlKey || event.metaKey;
|
|
653
|
+
if (event.altKey && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
|
|
654
|
+
if (onMove && !modifier && !event.shiftKey) {
|
|
655
|
+
event.preventDefault();
|
|
656
|
+
onMove(event.key === "ArrowUp" ? "earlier" : "later");
|
|
657
|
+
}
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
454
660
|
const move = (index) => {
|
|
455
661
|
event.preventDefault();
|
|
456
662
|
const target = Math.max(0, Math.min(keys.length - 1, index));
|
|
@@ -478,6 +684,13 @@ function PdfThumbnails(props) {
|
|
|
478
684
|
case "Escape":
|
|
479
685
|
if (active) session.select([active]);
|
|
480
686
|
break;
|
|
687
|
+
case "Delete":
|
|
688
|
+
case "Backspace":
|
|
689
|
+
if (onDelete && !event.altKey && !modifier && !event.shiftKey) {
|
|
690
|
+
event.preventDefault();
|
|
691
|
+
onDelete();
|
|
692
|
+
}
|
|
693
|
+
break;
|
|
481
694
|
case "a":
|
|
482
695
|
case "A": if (modifier) {
|
|
483
696
|
event.preventDefault();
|
|
@@ -488,7 +701,7 @@ function PdfThumbnails(props) {
|
|
|
488
701
|
const onPointer = (index, event) => {
|
|
489
702
|
choose(index, event.shiftKey ? "range" : event.ctrlKey || event.metaKey ? "toggle" : "only");
|
|
490
703
|
};
|
|
491
|
-
return /* @__PURE__ */
|
|
704
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
492
705
|
ref: root,
|
|
493
706
|
role: "listbox",
|
|
494
707
|
"aria-label": messages.t("thumbnails.label"),
|
|
@@ -498,7 +711,7 @@ function PdfThumbnails(props) {
|
|
|
498
711
|
onKeyDown,
|
|
499
712
|
className: join$3("hm-pdf-thumbnails", className, classNames.root),
|
|
500
713
|
...unstyled ? { "data-hm-unstyled": "" } : {},
|
|
501
|
-
children: plan.pages.map((page, index) => /* @__PURE__ */ jsx(Thumbnail, {
|
|
714
|
+
children: [plan.pages.map((page, index) => /* @__PURE__ */ jsx(Thumbnail, {
|
|
502
715
|
page,
|
|
503
716
|
index,
|
|
504
717
|
count: keys.length,
|
|
@@ -514,7 +727,11 @@ function PdfThumbnails(props) {
|
|
|
514
727
|
onMeasured,
|
|
515
728
|
onPointer,
|
|
516
729
|
classNames
|
|
517
|
-
}, page.key))
|
|
730
|
+
}, page.key)), /* @__PURE__ */ jsx(FocusKeeper, {
|
|
731
|
+
root,
|
|
732
|
+
keys,
|
|
733
|
+
fallback: active
|
|
734
|
+
})]
|
|
518
735
|
});
|
|
519
736
|
}
|
|
520
737
|
// src/react/toolbar/icons.ts
|
|
@@ -523,13 +740,28 @@ var pdfIcons = {
|
|
|
523
740
|
undo: UndoIcon,
|
|
524
741
|
redo: RedoIcon,
|
|
525
742
|
"rotate-left": RotateLeftIcon,
|
|
526
|
-
"rotate-right": RotateRightIcon
|
|
743
|
+
"rotate-right": RotateRightIcon,
|
|
744
|
+
delete: DeleteIcon,
|
|
745
|
+
download: DownloadIcon,
|
|
746
|
+
"page-add": PageAddIcon,
|
|
747
|
+
"move-up": MoveUpIcon,
|
|
748
|
+
"move-down": MoveDownIcon
|
|
749
|
+
};
|
|
750
|
+
// src/features/pages/thumbnail-keys.ts
|
|
751
|
+
var THUMBNAIL_KEYS = {
|
|
752
|
+
"pdf.delete-pages": "Delete Backspace",
|
|
753
|
+
"pdf.move-earlier": "Alt+ArrowUp",
|
|
754
|
+
"pdf.move-later": "Alt+ArrowDown"
|
|
527
755
|
};
|
|
528
756
|
// src/react/toolbar/PdfToolbar.tsx
|
|
529
757
|
function PdfToolbar(props) {
|
|
530
|
-
const { registry, shortcuts, context, icons, onRun, className } = props;
|
|
758
|
+
const { registry, shortcuts, context, icons, onRun, className, saving = false, inserting = false } = props;
|
|
531
759
|
const messages = usePdfMessages();
|
|
532
|
-
const state = {
|
|
760
|
+
const state = {
|
|
761
|
+
snapshot: usePdfDocument(),
|
|
762
|
+
saving,
|
|
763
|
+
inserting
|
|
764
|
+
};
|
|
533
765
|
const t = messages.t;
|
|
534
766
|
const run = onRun ?? ((tool) => void tool.run(context));
|
|
535
767
|
if (registry.toolbar.length === 0) return null;
|
|
@@ -540,12 +772,13 @@ function PdfToolbar(props) {
|
|
|
540
772
|
label: t(`toolbar.groups.${group[0]?.group ?? "other"}`),
|
|
541
773
|
children: group.filter((tool) => tool.isAvailable?.(state) ?? true).map((tool) => {
|
|
542
774
|
const Icon = icons[tool.icon];
|
|
543
|
-
const keys = shortcuts?.ariaKeyShortcuts(tool.id);
|
|
775
|
+
const keys = shortcuts?.ariaKeyShortcuts(tool.id) ?? THUMBNAIL_KEYS[tool.id];
|
|
544
776
|
return /* @__PURE__ */ jsx(IconButton, {
|
|
545
777
|
"data-hm-tool": tool.id,
|
|
546
778
|
label: t(tool.label),
|
|
547
779
|
icon: Icon ? /* @__PURE__ */ jsx(Icon, {}) : null,
|
|
548
780
|
disabled: !(tool.isEnabled?.(state) ?? true),
|
|
781
|
+
focusableWhenDisabled: true,
|
|
549
782
|
...keys ? { "aria-keyshortcuts": keys } : {},
|
|
550
783
|
onClick: () => run(tool)
|
|
551
784
|
}, tool.id);
|
|
@@ -891,26 +1124,146 @@ function PdfEditorStart(props) {
|
|
|
891
1124
|
]
|
|
892
1125
|
});
|
|
893
1126
|
}
|
|
1127
|
+
// src/features/save/download-name.ts
|
|
1128
|
+
var DEFAULT_DOWNLOAD_NAME = "document.pdf";
|
|
1129
|
+
var EXTENSION = ".pdf";
|
|
1130
|
+
function downloadFileName(name) {
|
|
1131
|
+
const clean = cleanFileName(name);
|
|
1132
|
+
if (clean === void 0) return DEFAULT_DOWNLOAD_NAME;
|
|
1133
|
+
if (clean.toLowerCase().endsWith(EXTENSION)) return clean;
|
|
1134
|
+
const base = Array.from(clean).slice(0, 124).join("").trimEnd();
|
|
1135
|
+
return base === "" ? DEFAULT_DOWNLOAD_NAME : `${base}${EXTENSION}`;
|
|
1136
|
+
}
|
|
1137
|
+
function saveFile(bytes, name) {
|
|
1138
|
+
const url = URL.createObjectURL(new Blob([bytes], { type: "application/pdf" }));
|
|
1139
|
+
const link = document.createElement("a");
|
|
1140
|
+
link.href = url;
|
|
1141
|
+
link.download = name;
|
|
1142
|
+
link.rel = "noopener";
|
|
1143
|
+
link.click();
|
|
1144
|
+
setTimeout(() => URL.revokeObjectURL(url), REVOKE_AFTER);
|
|
1145
|
+
}
|
|
1146
|
+
var REVOKE_AFTER = 4e4;
|
|
1147
|
+
// src/react/editor/use-save.ts
|
|
1148
|
+
var CANCEL_AFTER = 1e3;
|
|
1149
|
+
var IDLE = {
|
|
1150
|
+
saving: false,
|
|
1151
|
+
cancellable: false,
|
|
1152
|
+
failure: void 0
|
|
1153
|
+
};
|
|
1154
|
+
function useSave(options) {
|
|
1155
|
+
const { session, messages, announce, props } = options;
|
|
1156
|
+
const [state, setState] = useState(IDLE);
|
|
1157
|
+
const running = useRef(void 0);
|
|
1158
|
+
const cancel = useCallback(() => running.current?.abort(), []);
|
|
1159
|
+
const save = useCallback(() => {
|
|
1160
|
+
if (running.current) return;
|
|
1161
|
+
const controller = new AbortController();
|
|
1162
|
+
running.current = controller;
|
|
1163
|
+
setState({
|
|
1164
|
+
saving: true,
|
|
1165
|
+
cancellable: false,
|
|
1166
|
+
failure: void 0
|
|
1167
|
+
});
|
|
1168
|
+
const timer = setTimeout(() => setState((current) => current.saving ? {
|
|
1169
|
+
...current,
|
|
1170
|
+
cancellable: true
|
|
1171
|
+
} : current), CANCEL_AFTER);
|
|
1172
|
+
let failure;
|
|
1173
|
+
const finish = () => {
|
|
1174
|
+
clearTimeout(timer);
|
|
1175
|
+
if (running.current === controller) running.current = void 0;
|
|
1176
|
+
setState({
|
|
1177
|
+
saving: false,
|
|
1178
|
+
cancellable: false,
|
|
1179
|
+
failure
|
|
1180
|
+
});
|
|
1181
|
+
};
|
|
1182
|
+
(async () => {
|
|
1183
|
+
const snapshot = session.getSnapshot();
|
|
1184
|
+
const info = {
|
|
1185
|
+
pageCount: snapshot.plan.pages.length,
|
|
1186
|
+
...snapshot.sources[0]?.name ? { sourceName: snapshot.sources[0].name } : {}
|
|
1187
|
+
};
|
|
1188
|
+
let bytes;
|
|
1189
|
+
try {
|
|
1190
|
+
bytes = await session.export({ signal: controller.signal });
|
|
1191
|
+
} catch (error) {
|
|
1192
|
+
if (!controller.signal.aborted && !isPdfError(error, "ABORTED")) failure = messages.error(error);
|
|
1193
|
+
return;
|
|
1194
|
+
}
|
|
1195
|
+
if (controller.signal.aborted) return;
|
|
1196
|
+
const { onExport, downloadName } = props.current;
|
|
1197
|
+
try {
|
|
1198
|
+
const name = downloadFileName(downloadName ? downloadName(info) : info.sourceName);
|
|
1199
|
+
if (onExport) {
|
|
1200
|
+
await onExport(bytes, {
|
|
1201
|
+
fileName: name,
|
|
1202
|
+
pageCount: info.pageCount
|
|
1203
|
+
});
|
|
1204
|
+
announce(messages.t("editor.ready"));
|
|
1205
|
+
} else {
|
|
1206
|
+
saveFile(bytes, name);
|
|
1207
|
+
announce(messages.t("editor.saved", { name }));
|
|
1208
|
+
}
|
|
1209
|
+
} catch {
|
|
1210
|
+
failure = messages.t("editor.saveFailed");
|
|
1211
|
+
}
|
|
1212
|
+
})().finally(finish);
|
|
1213
|
+
}, [
|
|
1214
|
+
session,
|
|
1215
|
+
messages,
|
|
1216
|
+
announce,
|
|
1217
|
+
props
|
|
1218
|
+
]);
|
|
1219
|
+
const opened = session.getSnapshot().sources[0]?.id;
|
|
1220
|
+
useEffect(() => {
|
|
1221
|
+
running.current?.abort();
|
|
1222
|
+
setState((current) => current.failure ? {
|
|
1223
|
+
...current,
|
|
1224
|
+
failure: void 0
|
|
1225
|
+
} : current);
|
|
1226
|
+
}, [opened]);
|
|
1227
|
+
useEffect(() => () => running.current?.abort(), []);
|
|
1228
|
+
return {
|
|
1229
|
+
...state,
|
|
1230
|
+
save,
|
|
1231
|
+
cancel
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
894
1234
|
// src/react/editor/PdfEditor.tsx
|
|
895
1235
|
function warningText(messages, warning) {
|
|
896
1236
|
const t = messages.t;
|
|
897
1237
|
return t(`warnings.${warning.limit}`, { actual: warning.actual });
|
|
898
1238
|
}
|
|
899
1239
|
var join = (...names) => names.filter(Boolean).join(" ");
|
|
900
|
-
function resultOf(tool, messages, before) {
|
|
1240
|
+
function resultOf(tool, messages, before, after) {
|
|
901
1241
|
switch (tool.id) {
|
|
902
1242
|
case "pdf.undo": return messages.t("editor.undone");
|
|
903
1243
|
case "pdf.redo": return messages.t("editor.redone");
|
|
904
1244
|
case "pdf.rotate-left":
|
|
905
1245
|
case "pdf.rotate-right": return messages.t("editor.rotated", { count: targetPages(before).length });
|
|
1246
|
+
case "pdf.delete-pages": return messages.t("editor.deleted", { count: targetPages(before).length });
|
|
1247
|
+
case "pdf.move-earlier":
|
|
1248
|
+
case "pdf.move-later": {
|
|
1249
|
+
if (after.plan === before.plan) return void 0;
|
|
1250
|
+
const [first] = after.selection;
|
|
1251
|
+
const position = after.plan.pages.findIndex((page) => page.key === first) + 1;
|
|
1252
|
+
return messages.t("editor.moved", {
|
|
1253
|
+
count: after.selection.length,
|
|
1254
|
+
position
|
|
1255
|
+
});
|
|
1256
|
+
}
|
|
906
1257
|
default: return;
|
|
907
1258
|
}
|
|
908
1259
|
}
|
|
909
1260
|
function changeBetween(before, after) {
|
|
910
1261
|
const pageCount = after.plan.pages.length;
|
|
911
|
-
|
|
1262
|
+
const opened = after.sources[0];
|
|
1263
|
+
if (after.status === "ready" && opened?.id !== before.sources[0]?.id) return {
|
|
912
1264
|
reason: "open",
|
|
913
|
-
pageCount
|
|
1265
|
+
pageCount,
|
|
1266
|
+
...opened?.name ? { fileName: opened.name } : {}
|
|
914
1267
|
};
|
|
915
1268
|
if (before.sources.length > 0 && after.sources.length === 0) return {
|
|
916
1269
|
reason: "close",
|
|
@@ -939,8 +1292,18 @@ function PdfEditorBody(props) {
|
|
|
939
1292
|
const root = useRef(null);
|
|
940
1293
|
const startButton = useRef(null);
|
|
941
1294
|
const input = useRef(null);
|
|
1295
|
+
const insertInput = useRef(null);
|
|
1296
|
+
const [inserting, setInserting] = useState(false);
|
|
942
1297
|
const latestOnChange = useRef(onChange);
|
|
943
1298
|
latestOnChange.current = onChange;
|
|
1299
|
+
const saveProps = useRef(props);
|
|
1300
|
+
saveProps.current = props;
|
|
1301
|
+
const saving = useSave({
|
|
1302
|
+
session,
|
|
1303
|
+
messages,
|
|
1304
|
+
announce,
|
|
1305
|
+
props: saveProps
|
|
1306
|
+
});
|
|
944
1307
|
const registry = useMemo(() => createToolRegistry(pdfTools, {
|
|
945
1308
|
...props.tools ? { tools: props.tools } : {},
|
|
946
1309
|
toolbar: props.toolbar ?? DEFAULT_TOOLBAR.map((group) => group.filter((id) => !props.tools || props.tools.includes(id))).filter((group) => group.length > 0)
|
|
@@ -951,17 +1314,29 @@ function PdfEditorBody(props) {
|
|
|
951
1314
|
}), [platform, registry]);
|
|
952
1315
|
const context = useMemo(() => ({
|
|
953
1316
|
session,
|
|
954
|
-
chooseFile: () => input.current?.click()
|
|
955
|
-
|
|
1317
|
+
chooseFile: () => input.current?.click(),
|
|
1318
|
+
chooseInsertFile: () => insertInput.current?.click(),
|
|
1319
|
+
save: saving.save
|
|
1320
|
+
}), [session, saving.save]);
|
|
956
1321
|
const icons = useMemo(() => ({
|
|
957
1322
|
...pdfIcons,
|
|
958
1323
|
...props.icons
|
|
959
1324
|
}), [props.icons]);
|
|
1325
|
+
const deleteTool = registry.get("pdf.delete-pages");
|
|
1326
|
+
const moveTools = {
|
|
1327
|
+
earlier: registry.get("pdf.move-earlier"),
|
|
1328
|
+
later: registry.get("pdf.move-later")
|
|
1329
|
+
};
|
|
960
1330
|
const runTool = (tool) => {
|
|
961
1331
|
const before = session.getSnapshot();
|
|
962
|
-
|
|
1332
|
+
const state = {
|
|
1333
|
+
snapshot: before,
|
|
1334
|
+
saving: saving.saving,
|
|
1335
|
+
inserting
|
|
1336
|
+
};
|
|
1337
|
+
if (!(tool.isEnabled?.(state) ?? true)) return;
|
|
963
1338
|
Promise.resolve(tool.run(context)).then(() => {
|
|
964
|
-
const result = resultOf(tool, messages, before);
|
|
1339
|
+
const result = resultOf(tool, messages, before, session.getSnapshot());
|
|
965
1340
|
if (result) announce(result);
|
|
966
1341
|
});
|
|
967
1342
|
};
|
|
@@ -974,7 +1349,7 @@ function PdfEditorBody(props) {
|
|
|
974
1349
|
};
|
|
975
1350
|
useImperativeHandle(handle, () => ({
|
|
976
1351
|
session,
|
|
977
|
-
open: (next) => session.open(next),
|
|
1352
|
+
open: (next, options) => session.open(next, options),
|
|
978
1353
|
export: (options) => session.export(options),
|
|
979
1354
|
undo: () => session.undo(),
|
|
980
1355
|
redo: () => session.redo(),
|
|
@@ -1019,12 +1394,40 @@ function PdfEditorBody(props) {
|
|
|
1019
1394
|
const open = (chosen) => {
|
|
1020
1395
|
session.open(chosen).catch(() => {});
|
|
1021
1396
|
};
|
|
1397
|
+
const insert = async (chosen) => {
|
|
1398
|
+
const before = session.getSnapshot();
|
|
1399
|
+
const current = before.plan.pages.findIndex((page) => page.key === before.currentPage);
|
|
1400
|
+
const at = current >= 0 ? current + 1 : before.plan.pages.length;
|
|
1401
|
+
setInserting(true);
|
|
1402
|
+
try {
|
|
1403
|
+
await session.insert(chosen, { at });
|
|
1404
|
+
} catch {
|
|
1405
|
+
return;
|
|
1406
|
+
} finally {
|
|
1407
|
+
setInserting(false);
|
|
1408
|
+
}
|
|
1409
|
+
const had = new Set(before.plan.pages.map((page) => page.key));
|
|
1410
|
+
const added = session.getSnapshot().plan.pages.filter((page) => !had.has(page.key)).map((page) => page.key);
|
|
1411
|
+
const [first] = added;
|
|
1412
|
+
if (!first) return;
|
|
1413
|
+
session.select(added);
|
|
1414
|
+
session.goToPage(first);
|
|
1415
|
+
announce(messages.t("editor.inserted", {
|
|
1416
|
+
count: added.length,
|
|
1417
|
+
page: at
|
|
1418
|
+
}));
|
|
1419
|
+
};
|
|
1022
1420
|
const { error, warnings } = snapshot;
|
|
1023
1421
|
const showError = error !== void 0 && error.code !== "ABORTED";
|
|
1024
1422
|
let content;
|
|
1025
1423
|
if (status === "ready") content = /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(PdfThumbnails, {
|
|
1026
1424
|
className: join("hm-pdf-editor-sidebar", classNames.sidebar),
|
|
1027
|
-
unstyled
|
|
1425
|
+
unstyled,
|
|
1426
|
+
...deleteTool ? { onDelete: () => runTool(deleteTool) } : {},
|
|
1427
|
+
...moveTools.earlier || moveTools.later ? { onMove: (direction) => {
|
|
1428
|
+
const tool = moveTools[direction];
|
|
1429
|
+
if (tool) runTool(tool);
|
|
1430
|
+
} } : {}
|
|
1028
1431
|
}), /* @__PURE__ */ jsx(PdfViewer, { unstyled })] });
|
|
1029
1432
|
else if (status === "opening") content = /* @__PURE__ */ jsxs("div", {
|
|
1030
1433
|
role: "status",
|
|
@@ -1045,7 +1448,7 @@ function PdfEditorBody(props) {
|
|
|
1045
1448
|
ref: root,
|
|
1046
1449
|
role: "group",
|
|
1047
1450
|
"aria-label": messages.t("editor.label"),
|
|
1048
|
-
"aria-busy": status === "opening" || snapshot.busy,
|
|
1451
|
+
"aria-busy": status === "opening" || snapshot.busy || saving.saving,
|
|
1049
1452
|
className: join("hm-pdf-editor", className, classNames.root),
|
|
1050
1453
|
"data-hm-status": status,
|
|
1051
1454
|
onKeyDown,
|
|
@@ -1064,6 +1467,20 @@ function PdfEditorBody(props) {
|
|
|
1064
1467
|
if (chosen) open(chosen);
|
|
1065
1468
|
}
|
|
1066
1469
|
}),
|
|
1470
|
+
/* @__PURE__ */ jsx("input", {
|
|
1471
|
+
ref: insertInput,
|
|
1472
|
+
type: "file",
|
|
1473
|
+
accept: "application/pdf,.pdf",
|
|
1474
|
+
tabIndex: -1,
|
|
1475
|
+
"aria-hidden": "true",
|
|
1476
|
+
hidden: true,
|
|
1477
|
+
"data-hm-insert": "",
|
|
1478
|
+
onChange: (event) => {
|
|
1479
|
+
const [chosen] = event.target.files ?? [];
|
|
1480
|
+
event.target.value = "";
|
|
1481
|
+
if (chosen) insert(chosen);
|
|
1482
|
+
}
|
|
1483
|
+
}),
|
|
1067
1484
|
/* @__PURE__ */ jsxs("div", {
|
|
1068
1485
|
className: join("hm-pdf-editor-header", classNames.header),
|
|
1069
1486
|
children: [
|
|
@@ -1073,17 +1490,35 @@ function PdfEditorBody(props) {
|
|
|
1073
1490
|
context,
|
|
1074
1491
|
icons,
|
|
1075
1492
|
onRun: runTool,
|
|
1493
|
+
saving: saving.saving,
|
|
1494
|
+
inserting,
|
|
1076
1495
|
className: join("hm-pdf-editor-toolbar", classNames.toolbar)
|
|
1077
1496
|
}),
|
|
1497
|
+
saving.saving ? /* @__PURE__ */ jsxs("div", {
|
|
1498
|
+
className: join("hm-pdf-editor-saving", classNames.status),
|
|
1499
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
1500
|
+
role: "status",
|
|
1501
|
+
children: messages.t("editor.saving")
|
|
1502
|
+
}), saving.cancellable ? /* @__PURE__ */ jsx(Button, {
|
|
1503
|
+
size: "small",
|
|
1504
|
+
onClick: saving.cancel,
|
|
1505
|
+
children: messages.t("editor.cancelSave")
|
|
1506
|
+
}) : null]
|
|
1507
|
+
}) : null,
|
|
1508
|
+
saving.failure ? /* @__PURE__ */ jsx("p", {
|
|
1509
|
+
role: "alert",
|
|
1510
|
+
className: join("hm-pdf-editor-alert", classNames.alert),
|
|
1511
|
+
children: saving.failure
|
|
1512
|
+
}) : null,
|
|
1078
1513
|
status === "ready" && showError ? /* @__PURE__ */ jsx("p", {
|
|
1079
1514
|
role: "alert",
|
|
1080
1515
|
className: join("hm-pdf-editor-alert", classNames.alert),
|
|
1081
1516
|
children: messages.error(error)
|
|
1082
1517
|
}) : null,
|
|
1083
|
-
status === "ready" && warnings.length > 0 ? /* @__PURE__ */ jsx("div", {
|
|
1518
|
+
status === "ready" && (warnings.length > 0 || snapshot.historyTrimmed) ? /* @__PURE__ */ jsx("div", {
|
|
1084
1519
|
role: "status",
|
|
1085
1520
|
className: join("hm-pdf-editor-warnings", classNames.status),
|
|
1086
|
-
children: /* @__PURE__ */
|
|
1521
|
+
children: /* @__PURE__ */ jsxs("ul", { children: [warnings.map((warning) => /* @__PURE__ */ jsx("li", { children: warningText(messages, warning) }, warning.limit)), snapshot.historyTrimmed ? /* @__PURE__ */ jsx("li", { children: messages.t("editor.historyTrimmed") }) : null] })
|
|
1087
1522
|
}) : null
|
|
1088
1523
|
]
|
|
1089
1524
|
}),
|
|
@@ -1096,7 +1531,7 @@ function PdfEditorBody(props) {
|
|
|
1096
1531
|
});
|
|
1097
1532
|
}
|
|
1098
1533
|
var PdfEditor = /* @__PURE__ */ forwardRef(function PdfEditor(props, handle) {
|
|
1099
|
-
const { file, onChange, className, classNames, unstyled, tools, toolbar, icons, messages: overrides, locale = "en", ...source } = props;
|
|
1534
|
+
const { file, onChange, onExport, downloadName, className, classNames, unstyled, tools, toolbar, icons, messages: overrides, locale = "en", ...source } = props;
|
|
1100
1535
|
const session = useProvidedSession(source);
|
|
1101
1536
|
const messages = useMemo(() => createMessages(pdfEditorMessages, {
|
|
1102
1537
|
locale,
|
|
@@ -1114,6 +1549,8 @@ var PdfEditor = /* @__PURE__ */ forwardRef(function PdfEditor(props, handle) {
|
|
|
1114
1549
|
handle,
|
|
1115
1550
|
...file !== void 0 ? { file } : {},
|
|
1116
1551
|
...onChange ? { onChange } : {},
|
|
1552
|
+
...onExport ? { onExport } : {},
|
|
1553
|
+
...downloadName ? { downloadName } : {},
|
|
1117
1554
|
...className !== void 0 ? { className } : {},
|
|
1118
1555
|
...classNames ? { classNames } : {},
|
|
1119
1556
|
...unstyled !== void 0 ? { unstyled } : {},
|