Skip to content

Commit dd0f16d

Browse files
committed
Add import document operation
1 parent 96248f2 commit dd0f16d

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

web/ui-customization-doc-editor-sidebar/src/DocumentEditor.tsx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
PageRemoveIcon,
1919
RotateClockwiseIcon,
2020
RotateCounterClockwiseIcon,
21+
UploadIcon,
2122
} from "@baseline-ui/icons/24";
2223
import { themes } from "@baseline-ui/tokens";
2324
import type { DocumentOperations, Instance } from "@nutrient-sdk/viewer";
@@ -46,7 +47,8 @@ type DocumentOperation =
4647
| DocumentOperations.AddPageAfterOperation
4748
| DocumentOperations.DuplicatePagesOperation
4849
| DocumentOperations.MovePagesAfterOperation
49-
| DocumentOperations.MovePagesBeforeOperation;
50+
| DocumentOperations.MovePagesBeforeOperation
51+
| DocumentOperations.ImportDocumentAfterOperation;
5052

5153
const DocumentEditor = (props: Props) => {
5254
const { instance } = props;
@@ -259,6 +261,51 @@ const DocumentEditor = (props: Props) => {
259261

260262
return updatePageIndexes(result);
261263
});
264+
} else if (operation === "import-document") {
265+
// Create file input
266+
const input = document.createElement("input");
267+
input.type = "file";
268+
input.accept = "application/pdf";
269+
270+
input.onchange = async (e) => {
271+
const file = (e.target as HTMLInputElement).files?.[0];
272+
if (!file) return;
273+
274+
const selectedPageIndexes = getPageIndexesFromSelectedKeys();
275+
const afterIndex =
276+
selectedPageIndexes.length > 0
277+
? Math.max(...selectedPageIndexes)
278+
: draftPages.length - 1;
279+
280+
const importOperation: DocumentOperations.ImportDocumentAfterOperation =
281+
{
282+
type: "importDocument",
283+
afterPageIndex: afterIndex,
284+
document: file,
285+
treatImportedDocumentAsOnePage: true,
286+
};
287+
288+
// Add a placeholder draft page for the imported document
289+
setDraftPages((current) => {
290+
const newPage: DraftPageData = {
291+
id: `temp-import-${Date.now()}`,
292+
label: file.name,
293+
alt: `Imported: ${file.name}`,
294+
pageIndex: afterIndex + 1,
295+
src: "", // Will be populated after save
296+
rotation: 0,
297+
isNew: true,
298+
};
299+
const result = [...current];
300+
result.splice(afterIndex + 1, 0, newPage);
301+
return updatePageIndexes(result);
302+
});
303+
304+
setOperationQueue((prev) => [...prev, importOperation]);
305+
};
306+
307+
input.click();
308+
return; // Don't queue yet, will be queued in onchange
262309
} else if (operation === "export-selected-pages") {
263310
await handleExportSelectedPages();
264311
return; // Don't queue this operation
@@ -410,6 +457,11 @@ const DocumentEditor = (props: Props) => {
410457
label: "Duplicate Page",
411458
icon: DuplicateIcon,
412459
},
460+
{
461+
id: "import-document",
462+
label: "Import Document",
463+
icon: UploadIcon,
464+
},
413465
{
414466
id: "move-left",
415467
label: "Move Left",

0 commit comments

Comments
 (0)