@extend-ai/react-docx 0.6.2 → 0.6.4

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/dist/index.cjs CHANGED
@@ -12288,76 +12288,7 @@ var defaultStarterModel = {
12288
12288
  nodes: [
12289
12289
  {
12290
12290
  type: "paragraph",
12291
- style: { headingLevel: 1, styleId: "Heading1", styleName: "Heading 1" },
12292
- children: [
12293
- { type: "text", text: "React DOCX WYSIWYG", style: { bold: true } }
12294
- ]
12295
- },
12296
- {
12297
- type: "paragraph",
12298
- children: [
12299
- {
12300
- type: "text",
12301
- text: "Import a .docx, edit styles from the toolbar, and export.",
12302
- style: { highlight: "yellow" }
12303
- }
12304
- ]
12305
- },
12306
- {
12307
- type: "table",
12308
- style: {
12309
- borders: createDefaultEditorTableBorders()
12310
- },
12311
- rows: [
12312
- {
12313
- type: "table-row",
12314
- cells: [
12315
- {
12316
- type: "table-cell",
12317
- style: { backgroundColor: "#eef2ff" },
12318
- nodes: [
12319
- {
12320
- type: "paragraph",
12321
- children: [{ type: "text", text: "Header A" }]
12322
- }
12323
- ]
12324
- },
12325
- {
12326
- type: "table-cell",
12327
- style: { backgroundColor: "#eef2ff" },
12328
- nodes: [
12329
- {
12330
- type: "paragraph",
12331
- children: [{ type: "text", text: "Header B" }]
12332
- }
12333
- ]
12334
- }
12335
- ]
12336
- },
12337
- {
12338
- type: "table-row",
12339
- cells: [
12340
- {
12341
- type: "table-cell",
12342
- nodes: [
12343
- {
12344
- type: "paragraph",
12345
- children: [{ type: "text", text: "Row 1" }]
12346
- }
12347
- ]
12348
- },
12349
- {
12350
- type: "table-cell",
12351
- nodes: [
12352
- {
12353
- type: "paragraph",
12354
- children: [{ type: "text", text: "Value" }]
12355
- }
12356
- ]
12357
- }
12358
- ]
12359
- }
12360
- ]
12291
+ children: [{ type: "text", text: "" }]
12361
12292
  }
12362
12293
  ],
12363
12294
  metadata: {
@@ -12440,6 +12371,9 @@ var defaultStarterModel = {
12440
12371
  defaultParagraphStyleId: "Normal"
12441
12372
  }
12442
12373
  };
12374
+ function createBlankDocumentModel() {
12375
+ return cloneDocModel(defaultStarterModel);
12376
+ }
12443
12377
  function textRuns2(paragraph) {
12444
12378
  return paragraph.children.filter(
12445
12379
  (child) => child.type === "text"
@@ -24998,6 +24932,7 @@ function useDocxEditor(options = {}) {
24998
24932
  const [status, setStatus] = React.useState(
24999
24933
  options.initialStatus ?? "Ready"
25000
24934
  );
24935
+ const [importError, setImportError] = React.useState();
25001
24936
  const [isImporting, setIsImporting] = React.useState(false);
25002
24937
  const [documentTheme, setDocumentThemeState] = React.useState(options.initialDocumentTheme ?? "light");
25003
24938
  const [showTrackedChanges, setShowTrackedChangesState] = React.useState(options.initialShowTrackedChanges ?? false);
@@ -25515,13 +25450,35 @@ function useDocxEditor(options = {}) {
25515
25450
  },
25516
25451
  [dispatchEditorTransaction]
25517
25452
  );
25453
+ const replaceDocumentWithImportError = React.useCallback(
25454
+ (fileName2, error) => {
25455
+ unloadEmbeddedFonts();
25456
+ setModel(createBlankDocumentModel());
25457
+ setDocumentLoadNonce((current) => current + 1);
25458
+ setHistory({ past: [], future: [] });
25459
+ setHistoryRestoreRequest(void 0);
25460
+ setBasePackage(void 0);
25461
+ setFileName(fileName2);
25462
+ setSelection({ kind: "paragraph", nodeIndex: 0 });
25463
+ setActiveTextRangeState(void 0);
25464
+ setPendingRunStyle(void 0);
25465
+ setSelectedFormFieldLocation(void 0);
25466
+ setImportError(error);
25467
+ setStatus(`Failed to load file: ${error.message}`);
25468
+ },
25469
+ [unloadEmbeddedFonts]
25470
+ );
25518
25471
  const importDocxFile = React.useCallback(
25519
25472
  async (file) => {
25520
25473
  if (!/\.docx$/i.test(file.name)) {
25521
- setStatus("Only .docx files are supported");
25474
+ replaceDocumentWithImportError(
25475
+ file.name,
25476
+ new Error("Only .docx files are supported")
25477
+ );
25522
25478
  return;
25523
25479
  }
25524
25480
  setIsImporting(true);
25481
+ setImportError(void 0);
25525
25482
  setStatus(`Loading ${file.name}...`);
25526
25483
  try {
25527
25484
  const buffer = await file.arrayBuffer();
@@ -25538,16 +25495,16 @@ function useDocxEditor(options = {}) {
25538
25495
  setActiveTextRangeState(void 0);
25539
25496
  setPendingRunStyle(void 0);
25540
25497
  setSelectedFormFieldLocation(void 0);
25498
+ setImportError(void 0);
25541
25499
  setStatus(`Loaded ${file.name}`);
25542
25500
  } catch (error) {
25543
- setStatus(
25544
- `Failed to load file: ${error instanceof Error ? error.message : "Unknown error"}`
25545
- );
25501
+ const nextError = error instanceof Error ? error : new Error("Unknown error");
25502
+ replaceDocumentWithImportError(file.name, nextError);
25546
25503
  } finally {
25547
25504
  setIsImporting(false);
25548
25505
  }
25549
25506
  },
25550
- [loadEmbeddedFontsFromPackage]
25507
+ [loadEmbeddedFontsFromPackage, replaceDocumentWithImportError]
25551
25508
  );
25552
25509
  const newDocument = React.useCallback(() => {
25553
25510
  unloadEmbeddedFonts();
@@ -25561,6 +25518,7 @@ function useDocxEditor(options = {}) {
25561
25518
  setSelectedFormFieldLocation(void 0);
25562
25519
  setHistory({ past: [], future: [] });
25563
25520
  setHistoryRestoreRequest(void 0);
25521
+ setImportError(void 0);
25564
25522
  setStatus("Created new document");
25565
25523
  }, [unloadEmbeddedFonts]);
25566
25524
  const exportDocx = React.useCallback(() => {
@@ -28469,6 +28427,7 @@ function useDocxEditor(options = {}) {
28469
28427
  documentLoadNonce,
28470
28428
  fileName,
28471
28429
  status,
28430
+ importError,
28472
28431
  isImporting,
28473
28432
  documentTheme,
28474
28433
  trackedChanges,
@@ -45448,6 +45407,35 @@ ${currentText.slice(end)}`;
45448
45407
  children: "Drop .docx anywhere to replace current document"
45449
45408
  }
45450
45409
  ) : null,
45410
+ editor.importError ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
45411
+ "div",
45412
+ {
45413
+ role: "alert",
45414
+ "data-docx-import-error": "true",
45415
+ style: {
45416
+ position: "absolute",
45417
+ top: 24,
45418
+ left: "50%",
45419
+ transform: "translateX(-50%)",
45420
+ zIndex: 80,
45421
+ width: "min(520px, calc(100% - 32px))",
45422
+ display: "grid",
45423
+ gap: 6,
45424
+ padding: "12px 14px",
45425
+ borderRadius: 8,
45426
+ border: `1px solid ${editor.documentTheme === "dark" ? "rgba(248, 113, 113, 0.42)" : "rgba(185, 28, 28, 0.26)"}`,
45427
+ backgroundColor: editor.documentTheme === "dark" ? "rgba(69, 10, 10, 0.96)" : "rgba(254, 242, 242, 0.98)",
45428
+ color: editor.documentTheme === "dark" ? "#fee2e2" : "#7f1d1d",
45429
+ boxShadow: editor.documentTheme === "dark" ? "0 12px 28px rgba(2, 6, 23, 0.44)" : "0 12px 28px rgba(127, 29, 29, 0.12)",
45430
+ fontSize: 13,
45431
+ lineHeight: 1.35
45432
+ },
45433
+ children: [
45434
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { style: { fontWeight: 600 }, children: "Failed to load DOCX" }),
45435
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { style: { overflowWrap: "anywhere" }, children: editor.importError.message })
45436
+ ]
45437
+ }
45438
+ ) : null,
45451
45439
  tableMoveDropPreview && !isReadOnly ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
45452
45440
  "div",
45453
45441
  {