@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.d.cts CHANGED
@@ -712,6 +712,7 @@ interface DocxEditorController {
712
712
  documentLoadNonce: number;
713
713
  fileName: string;
714
714
  status: string;
715
+ importError?: Error;
715
716
  isImporting: boolean;
716
717
  documentTheme: DocxDocumentTheme;
717
718
  selection: DocxEditorSelection;
package/dist/index.d.ts CHANGED
@@ -712,6 +712,7 @@ interface DocxEditorController {
712
712
  documentLoadNonce: number;
713
713
  fileName: string;
714
714
  status: string;
715
+ importError?: Error;
715
716
  isImporting: boolean;
716
717
  documentTheme: DocxDocumentTheme;
717
718
  selection: DocxEditorSelection;
package/dist/index.js CHANGED
@@ -12178,76 +12178,7 @@ var defaultStarterModel = {
12178
12178
  nodes: [
12179
12179
  {
12180
12180
  type: "paragraph",
12181
- style: { headingLevel: 1, styleId: "Heading1", styleName: "Heading 1" },
12182
- children: [
12183
- { type: "text", text: "React DOCX WYSIWYG", style: { bold: true } }
12184
- ]
12185
- },
12186
- {
12187
- type: "paragraph",
12188
- children: [
12189
- {
12190
- type: "text",
12191
- text: "Import a .docx, edit styles from the toolbar, and export.",
12192
- style: { highlight: "yellow" }
12193
- }
12194
- ]
12195
- },
12196
- {
12197
- type: "table",
12198
- style: {
12199
- borders: createDefaultEditorTableBorders()
12200
- },
12201
- rows: [
12202
- {
12203
- type: "table-row",
12204
- cells: [
12205
- {
12206
- type: "table-cell",
12207
- style: { backgroundColor: "#eef2ff" },
12208
- nodes: [
12209
- {
12210
- type: "paragraph",
12211
- children: [{ type: "text", text: "Header A" }]
12212
- }
12213
- ]
12214
- },
12215
- {
12216
- type: "table-cell",
12217
- style: { backgroundColor: "#eef2ff" },
12218
- nodes: [
12219
- {
12220
- type: "paragraph",
12221
- children: [{ type: "text", text: "Header B" }]
12222
- }
12223
- ]
12224
- }
12225
- ]
12226
- },
12227
- {
12228
- type: "table-row",
12229
- cells: [
12230
- {
12231
- type: "table-cell",
12232
- nodes: [
12233
- {
12234
- type: "paragraph",
12235
- children: [{ type: "text", text: "Row 1" }]
12236
- }
12237
- ]
12238
- },
12239
- {
12240
- type: "table-cell",
12241
- nodes: [
12242
- {
12243
- type: "paragraph",
12244
- children: [{ type: "text", text: "Value" }]
12245
- }
12246
- ]
12247
- }
12248
- ]
12249
- }
12250
- ]
12181
+ children: [{ type: "text", text: "" }]
12251
12182
  }
12252
12183
  ],
12253
12184
  metadata: {
@@ -12330,6 +12261,9 @@ var defaultStarterModel = {
12330
12261
  defaultParagraphStyleId: "Normal"
12331
12262
  }
12332
12263
  };
12264
+ function createBlankDocumentModel() {
12265
+ return cloneDocModel(defaultStarterModel);
12266
+ }
12333
12267
  function textRuns2(paragraph) {
12334
12268
  return paragraph.children.filter(
12335
12269
  (child) => child.type === "text"
@@ -24888,6 +24822,7 @@ function useDocxEditor(options = {}) {
24888
24822
  const [status, setStatus] = React.useState(
24889
24823
  options.initialStatus ?? "Ready"
24890
24824
  );
24825
+ const [importError, setImportError] = React.useState();
24891
24826
  const [isImporting, setIsImporting] = React.useState(false);
24892
24827
  const [documentTheme, setDocumentThemeState] = React.useState(options.initialDocumentTheme ?? "light");
24893
24828
  const [showTrackedChanges, setShowTrackedChangesState] = React.useState(options.initialShowTrackedChanges ?? false);
@@ -25405,13 +25340,35 @@ function useDocxEditor(options = {}) {
25405
25340
  },
25406
25341
  [dispatchEditorTransaction]
25407
25342
  );
25343
+ const replaceDocumentWithImportError = React.useCallback(
25344
+ (fileName2, error) => {
25345
+ unloadEmbeddedFonts();
25346
+ setModel(createBlankDocumentModel());
25347
+ setDocumentLoadNonce((current) => current + 1);
25348
+ setHistory({ past: [], future: [] });
25349
+ setHistoryRestoreRequest(void 0);
25350
+ setBasePackage(void 0);
25351
+ setFileName(fileName2);
25352
+ setSelection({ kind: "paragraph", nodeIndex: 0 });
25353
+ setActiveTextRangeState(void 0);
25354
+ setPendingRunStyle(void 0);
25355
+ setSelectedFormFieldLocation(void 0);
25356
+ setImportError(error);
25357
+ setStatus(`Failed to load file: ${error.message}`);
25358
+ },
25359
+ [unloadEmbeddedFonts]
25360
+ );
25408
25361
  const importDocxFile = React.useCallback(
25409
25362
  async (file) => {
25410
25363
  if (!/\.docx$/i.test(file.name)) {
25411
- setStatus("Only .docx files are supported");
25364
+ replaceDocumentWithImportError(
25365
+ file.name,
25366
+ new Error("Only .docx files are supported")
25367
+ );
25412
25368
  return;
25413
25369
  }
25414
25370
  setIsImporting(true);
25371
+ setImportError(void 0);
25415
25372
  setStatus(`Loading ${file.name}...`);
25416
25373
  try {
25417
25374
  const buffer = await file.arrayBuffer();
@@ -25428,16 +25385,16 @@ function useDocxEditor(options = {}) {
25428
25385
  setActiveTextRangeState(void 0);
25429
25386
  setPendingRunStyle(void 0);
25430
25387
  setSelectedFormFieldLocation(void 0);
25388
+ setImportError(void 0);
25431
25389
  setStatus(`Loaded ${file.name}`);
25432
25390
  } catch (error) {
25433
- setStatus(
25434
- `Failed to load file: ${error instanceof Error ? error.message : "Unknown error"}`
25435
- );
25391
+ const nextError = error instanceof Error ? error : new Error("Unknown error");
25392
+ replaceDocumentWithImportError(file.name, nextError);
25436
25393
  } finally {
25437
25394
  setIsImporting(false);
25438
25395
  }
25439
25396
  },
25440
- [loadEmbeddedFontsFromPackage]
25397
+ [loadEmbeddedFontsFromPackage, replaceDocumentWithImportError]
25441
25398
  );
25442
25399
  const newDocument = React.useCallback(() => {
25443
25400
  unloadEmbeddedFonts();
@@ -25451,6 +25408,7 @@ function useDocxEditor(options = {}) {
25451
25408
  setSelectedFormFieldLocation(void 0);
25452
25409
  setHistory({ past: [], future: [] });
25453
25410
  setHistoryRestoreRequest(void 0);
25411
+ setImportError(void 0);
25454
25412
  setStatus("Created new document");
25455
25413
  }, [unloadEmbeddedFonts]);
25456
25414
  const exportDocx = React.useCallback(() => {
@@ -28359,6 +28317,7 @@ function useDocxEditor(options = {}) {
28359
28317
  documentLoadNonce,
28360
28318
  fileName,
28361
28319
  status,
28320
+ importError,
28362
28321
  isImporting,
28363
28322
  documentTheme,
28364
28323
  trackedChanges,
@@ -45338,6 +45297,35 @@ ${currentText.slice(end)}`;
45338
45297
  children: "Drop .docx anywhere to replace current document"
45339
45298
  }
45340
45299
  ) : null,
45300
+ editor.importError ? /* @__PURE__ */ jsxs(
45301
+ "div",
45302
+ {
45303
+ role: "alert",
45304
+ "data-docx-import-error": "true",
45305
+ style: {
45306
+ position: "absolute",
45307
+ top: 24,
45308
+ left: "50%",
45309
+ transform: "translateX(-50%)",
45310
+ zIndex: 80,
45311
+ width: "min(520px, calc(100% - 32px))",
45312
+ display: "grid",
45313
+ gap: 6,
45314
+ padding: "12px 14px",
45315
+ borderRadius: 8,
45316
+ border: `1px solid ${editor.documentTheme === "dark" ? "rgba(248, 113, 113, 0.42)" : "rgba(185, 28, 28, 0.26)"}`,
45317
+ backgroundColor: editor.documentTheme === "dark" ? "rgba(69, 10, 10, 0.96)" : "rgba(254, 242, 242, 0.98)",
45318
+ color: editor.documentTheme === "dark" ? "#fee2e2" : "#7f1d1d",
45319
+ boxShadow: editor.documentTheme === "dark" ? "0 12px 28px rgba(2, 6, 23, 0.44)" : "0 12px 28px rgba(127, 29, 29, 0.12)",
45320
+ fontSize: 13,
45321
+ lineHeight: 1.35
45322
+ },
45323
+ children: [
45324
+ /* @__PURE__ */ jsx("strong", { style: { fontWeight: 600 }, children: "Failed to load DOCX" }),
45325
+ /* @__PURE__ */ jsx("span", { style: { overflowWrap: "anywhere" }, children: editor.importError.message })
45326
+ ]
45327
+ }
45328
+ ) : null,
45341
45329
  tableMoveDropPreview && !isReadOnly ? /* @__PURE__ */ jsx(
45342
45330
  "div",
45343
45331
  {