@drghaliasri/butex 7.0.3 → 7.0.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.
@@ -792,6 +792,10 @@ function defaultFloatMeta() {
792
792
  centered: true,
793
793
  captionEnabled: false,
794
794
  caption: "",
795
+ captionField: {
796
+ id: document2Id("field"),
797
+ tokens: [{ id: document2Id("text"), kind: "text", text: "" }]
798
+ },
795
799
  labelEnabled: false,
796
800
  label: ""
797
801
  };
@@ -850,15 +854,20 @@ function collectDocument2Labels(document2) {
850
854
  }
851
855
  function walk(blocks) {
852
856
  for (const block of blocks) {
853
- if (block.kind === "image" && block.labelEnabled && block.label.trim().length > 0) {
854
- counters.fig += 1;
855
- entries.push({
856
- key: block.label.trim(),
857
- kind: "fig",
858
- caption: block.captionEnabled ? block.caption : "",
859
- ownerId: block.id,
860
- number: counters.fig
861
- });
857
+ if (block.kind === "image") {
858
+ if (block.labelEnabled && block.label.trim().length > 0) {
859
+ counters.fig += 1;
860
+ entries.push({
861
+ key: block.label.trim(),
862
+ kind: "fig",
863
+ caption: block.captionEnabled ? block.caption : "",
864
+ ownerId: block.id,
865
+ number: counters.fig
866
+ });
867
+ }
868
+ if (block.captionEnabled) {
869
+ pushEqFromField(block.captionField.tokens);
870
+ }
862
871
  }
863
872
  if (block.kind === "table") {
864
873
  if (block.labelEnabled && block.label.trim().length > 0) {
@@ -871,6 +880,9 @@ function collectDocument2Labels(document2) {
871
880
  number: counters.tab
872
881
  });
873
882
  }
883
+ if (block.captionEnabled) {
884
+ pushEqFromField(block.captionField.tokens);
885
+ }
874
886
  for (const row of block.rows) {
875
887
  for (const cell of row) {
876
888
  pushEqFromField(cell.tokens);
@@ -963,6 +975,14 @@ function applyFloatMetaPatch(block, patch) {
963
975
  }
964
976
  if (typeof patch.caption === "string") {
965
977
  block.caption = patch.caption;
978
+ const existingText = block.captionField.tokens.find((token) => token.kind === "text");
979
+ block.captionField.tokens = [
980
+ {
981
+ id: existingText?.id ?? document2Id("text"),
982
+ kind: "text",
983
+ text: patch.caption
984
+ }
985
+ ];
966
986
  }
967
987
  if (typeof patch.labelEnabled === "boolean") {
968
988
  block.labelEnabled = patch.labelEnabled;
@@ -1482,6 +1502,21 @@ function parseListBlock(json, options, path, diagnostics, identities) {
1482
1502
  ...metadataFromJson(json.metadata)
1483
1503
  };
1484
1504
  }
1505
+ function captionFieldFromJson(json, options, path, diagnostics, identities) {
1506
+ const caption = typeof json.caption === "string" ? json.caption : "";
1507
+ return applyImportedInlineIds(
1508
+ createInlineField2(
1509
+ caption,
1510
+ json.caption_math_objects ?? [],
1511
+ options,
1512
+ `${path}.caption`,
1513
+ diagnostics,
1514
+ json.caption_formats ?? []
1515
+ ),
1516
+ json.caption_inline_ids,
1517
+ identities
1518
+ );
1519
+ }
1485
1520
  function parseTableBlock(json, options, path, diagnostics, identities) {
1486
1521
  if (!Array.isArray(json.rows)) {
1487
1522
  throw new Error("\\begin{tabular} requires rows array");
@@ -1508,6 +1543,7 @@ function parseTableBlock(json, options, path, diagnostics, identities) {
1508
1543
  if (mathObjects.length > 0 && mathObjects.length !== mathObjectIndex) {
1509
1544
  pushDiagnostic(diagnostics, options, path, `math_objects count mismatch: detected ${String(mathObjectIndex)}, got ${String(mathObjects.length)}`);
1510
1545
  }
1546
+ const floatMeta = parseFloatMetaFromJson(json);
1511
1547
  return {
1512
1548
  id: blockIdFromJson(json, identities),
1513
1549
  kind: "table",
@@ -1515,13 +1551,15 @@ function parseTableBlock(json, options, path, diagnostics, identities) {
1515
1551
  closing: "\\end{tabular}",
1516
1552
  columns: typeof json.columns === "string" ? json.columns : "",
1517
1553
  rows,
1518
- ...parseFloatMetaFromJson(json),
1554
+ ...floatMeta,
1555
+ captionField: captionFieldFromJson(json, options, path, diagnostics, identities),
1519
1556
  ...metadataFromJson(json.metadata)
1520
1557
  };
1521
1558
  }
1522
- function parseImageBlock(json, identities) {
1559
+ function parseImageBlock(json, options, path, diagnostics, identities) {
1523
1560
  const assetId = typeof json.asset_id === "string" && json.asset_id.length > 0 ? json.asset_id : void 0;
1524
1561
  const value = assetId !== void 0 ? typeof json.value === "string" ? json.value : "" : requireString(json.value, "\\includegraphics requires string value");
1562
+ const floatMeta = parseFloatMetaFromJson(json);
1525
1563
  return {
1526
1564
  id: blockIdFromJson(json, identities),
1527
1565
  kind: "image",
@@ -1529,7 +1567,8 @@ function parseImageBlock(json, identities) {
1529
1567
  value,
1530
1568
  ...assetId !== void 0 ? { assetId } : {},
1531
1569
  options: isRecordOfStrings(json.options) ? json.options : {},
1532
- ...parseFloatMetaFromJson(json),
1570
+ ...floatMeta,
1571
+ captionField: captionFieldFromJson(json, options, path, diagnostics, identities),
1533
1572
  ...metadataFromJson(json.metadata)
1534
1573
  };
1535
1574
  }
@@ -1562,7 +1601,7 @@ function parseBlock(json, options, path, diagnostics, identities) {
1562
1601
  return parseTableBlock(json, options, path, diagnostics, identities);
1563
1602
  }
1564
1603
  if (json.command === "\\includegraphics") {
1565
- return parseImageBlock(json, identities);
1604
+ return parseImageBlock(json, options, path, diagnostics, identities);
1566
1605
  }
1567
1606
  if (json.command === "\\begin{thebibliography}" || json.command === "\\bibliography") {
1568
1607
  return parseBibliographyBlock(json, identities);
@@ -8510,6 +8549,20 @@ function cloneToken(token) {
8510
8549
  function cloneField(field) {
8511
8550
  return { id: field.id, tokens: field.tokens.map(cloneToken) };
8512
8551
  }
8552
+ function inlineFieldSource(field) {
8553
+ return field.tokens.map((token) => {
8554
+ if (token.kind === "text") {
8555
+ return token.text;
8556
+ }
8557
+ if (token.kind === "cite") {
8558
+ return citeTokenLatex(token.keys);
8559
+ }
8560
+ if (token.kind === "ref") {
8561
+ return refTokenLatex(token.keys, token.refCommand);
8562
+ }
8563
+ return token.source;
8564
+ }).join("");
8565
+ }
8513
8566
  function cloneBlock(block) {
8514
8567
  const metadata = block.metadata ? { metadata: { ...block.metadata } } : {};
8515
8568
  if (block.kind === "textBlock") {
@@ -8527,10 +8580,20 @@ function cloneBlock(block) {
8527
8580
  };
8528
8581
  }
8529
8582
  if (block.kind === "table") {
8530
- return { ...block, ...metadata, rows: block.rows.map((row) => row.map(cloneField)) };
8583
+ return {
8584
+ ...block,
8585
+ ...metadata,
8586
+ rows: block.rows.map((row) => row.map(cloneField)),
8587
+ captionField: cloneField(block.captionField)
8588
+ };
8531
8589
  }
8532
8590
  if (block.kind === "image") {
8533
- return { ...block, ...metadata, options: { ...block.options } };
8591
+ return {
8592
+ ...block,
8593
+ ...metadata,
8594
+ options: { ...block.options },
8595
+ captionField: cloneField(block.captionField)
8596
+ };
8534
8597
  }
8535
8598
  return { ...block, ...metadata };
8536
8599
  }
@@ -8543,6 +8606,13 @@ function cloneDocument(document2) {
8543
8606
  diagnostics: document2.diagnostics.map((diagnostic) => ({ ...diagnostic }))
8544
8607
  };
8545
8608
  }
8609
+ function visitCaptionField(block, visitor) {
8610
+ if (!visitor(block.captionField)) {
8611
+ return false;
8612
+ }
8613
+ block.caption = inlineFieldSource(block.captionField);
8614
+ return true;
8615
+ }
8546
8616
  function visitFields(blocks, visitor) {
8547
8617
  for (const block of blocks) {
8548
8618
  if (block.kind === "textBlock") {
@@ -8563,6 +8633,11 @@ function visitFields(blocks, visitor) {
8563
8633
  }
8564
8634
  }
8565
8635
  }
8636
+ if (visitCaptionField(block, visitor)) {
8637
+ return true;
8638
+ }
8639
+ } else if (block.kind === "image" && visitCaptionField(block, visitor)) {
8640
+ return true;
8566
8641
  }
8567
8642
  }
8568
8643
  return false;
@@ -9362,6 +9437,15 @@ function fieldJson(field) {
9362
9437
  ...serialized.hasPersistedMath ? { math_objects: serialized.mathObjects } : {}
9363
9438
  };
9364
9439
  }
9440
+ function captionJson(field) {
9441
+ const serialized = serializeField(field);
9442
+ return {
9443
+ caption: serialized.value,
9444
+ caption_inline_ids: serialized.inlineIds,
9445
+ ...serialized.formats.length > 0 ? { caption_formats: serialized.formats } : {},
9446
+ ...serialized.hasPersistedMath ? { caption_math_objects: serialized.mathObjects } : {}
9447
+ };
9448
+ }
9365
9449
  function listItemJson(item) {
9366
9450
  const field = fieldJson(item.field);
9367
9451
  return {
@@ -9406,7 +9490,7 @@ function blockJson(block) {
9406
9490
  ...hasPersistedMath ? { math_objects: fields.flatMap((row) => row.flatMap((field) => field.mathObjects)) } : {},
9407
9491
  centered: block.centered,
9408
9492
  caption_enabled: block.captionEnabled,
9409
- caption: block.caption,
9493
+ ...captionJson(block.captionField),
9410
9494
  label_enabled: block.labelEnabled,
9411
9495
  label: block.label,
9412
9496
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
@@ -9421,7 +9505,7 @@ function blockJson(block) {
9421
9505
  options: { ...block.options },
9422
9506
  centered: block.centered,
9423
9507
  caption_enabled: block.captionEnabled,
9424
- caption: block.caption,
9508
+ ...captionJson(block.captionField),
9425
9509
  label_enabled: block.labelEnabled,
9426
9510
  label: block.label,
9427
9511
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
@@ -9837,11 +9921,12 @@ ${block.closing}`;
9837
9921
  ${rows.join(" \\\\\n")}
9838
9922
  ${block.closing}`;
9839
9923
  }
9840
- function floatCaptionLabelLines(block, counter) {
9924
+ function floatCaptionLabelLines(block, counter, labelKinds) {
9841
9925
  const lines = [];
9842
- const hasCaption = block.captionEnabled && block.caption.trim().length > 0;
9926
+ const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds).trim();
9927
+ const hasCaption = block.captionEnabled && caption.length > 0;
9843
9928
  if (hasCaption) {
9844
- lines.push(` \\caption{${block.caption.trim()}}`);
9929
+ lines.push(` \\caption{${caption}}`);
9845
9930
  }
9846
9931
  if (block.labelEnabled && block.label.trim().length > 0) {
9847
9932
  if (!hasCaption) {
@@ -9857,7 +9942,7 @@ function tableBlockLatex(block, labelKinds, fixedFloats) {
9857
9942
  if (block.centered) {
9858
9943
  lines.push(" \\centering");
9859
9944
  }
9860
- lines.push(...floatCaptionLabelLines(block, "table"));
9945
+ lines.push(...floatCaptionLabelLines(block, "table", labelKinds));
9861
9946
  lines.push(indent(tabularInnerLatex(block, labelKinds)));
9862
9947
  lines.push("\\end{table}");
9863
9948
  return lines.join("\n");
@@ -9877,14 +9962,14 @@ function imageOptionsLatex(block, constrainToColumn) {
9877
9962
  }
9878
9963
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
9879
9964
  }
9880
- function imageBlockLatex(block, fixedFloats) {
9965
+ function imageBlockLatex(block, labelKinds, fixedFloats) {
9881
9966
  const placement = fixedFloats ? "H" : "htbp";
9882
9967
  const lines = [`\\begin{figure}[${placement}]`];
9883
9968
  if (block.centered) {
9884
9969
  lines.push(" \\centering");
9885
9970
  }
9886
9971
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
9887
- lines.push(...floatCaptionLabelLines(block, "figure"));
9972
+ lines.push(...floatCaptionLabelLines(block, "figure", labelKinds));
9888
9973
  lines.push("\\end{figure}");
9889
9974
  return lines.join("\n");
9890
9975
  }
@@ -9899,7 +9984,7 @@ function blockLatex(block, references, labelKinds, fixedFloats) {
9899
9984
  return tableBlockLatex(block, labelKinds, fixedFloats);
9900
9985
  }
9901
9986
  if (block.kind === "image") {
9902
- return imageBlockLatex(block, fixedFloats);
9987
+ return imageBlockLatex(block, labelKinds, fixedFloats);
9903
9988
  }
9904
9989
  if (block.kind === "bibliography") {
9905
9990
  const width = String(Math.max(references.length, 9));
@@ -10092,7 +10177,7 @@ function validateBlock(context, block, path) {
10092
10177
  );
10093
10178
  });
10094
10179
  });
10095
- validateTextValue(context, block.caption, `${path}.caption`, block.id);
10180
+ validateInlineField(context, block.captionField, `${path}.caption`, block.id);
10096
10181
  if (block.labelEnabled) {
10097
10182
  registerKey(context, block.label, `${path}.label`, "tab", block.id);
10098
10183
  }
@@ -10105,7 +10190,7 @@ function validateBlock(context, block, path) {
10105
10190
  addIssue(context, "empty_image", path, block.id);
10106
10191
  }
10107
10192
  validateTextValue(context, block.value, `${path}.value`, block.id);
10108
- validateTextValue(context, block.caption, `${path}.caption`, block.id);
10193
+ validateInlineField(context, block.captionField, `${path}.caption`, block.id);
10109
10194
  if (block.labelEnabled) {
10110
10195
  registerKey(context, block.label, `${path}.label`, "fig", block.id);
10111
10196
  }
@@ -10430,6 +10515,15 @@ function floatNumberLabel(document2, key, previewOptions) {
10430
10515
  digitForm: previewOptions.digitForm
10431
10516
  });
10432
10517
  }
10518
+ function captionPreview(block, islands, output, equationSide, document2, previewOptions) {
10519
+ if (!block.captionEnabled || block.caption.trim().length === 0) {
10520
+ return {};
10521
+ }
10522
+ return {
10523
+ caption: block.caption.trim(),
10524
+ captionInlines: previewInlines(block.captionField, islands, output, equationSide, document2, previewOptions)
10525
+ };
10526
+ }
10433
10527
  function blockPreview(block, islands, output, equationSide, document2, previewOptions) {
10434
10528
  if (block.kind === "textBlock") {
10435
10529
  return textBlockPreview(block, islands, output, equationSide, document2, previewOptions);
@@ -10453,7 +10547,7 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
10453
10547
  kind: "table",
10454
10548
  id: block.id,
10455
10549
  rows: block.rows.map((row) => row.map((cell) => previewInlines(cell, islands, output, equationSide, document2, previewOptions))),
10456
- ...block.captionEnabled && block.caption.trim().length > 0 ? { caption: block.caption.trim() } : {},
10550
+ ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
10457
10551
  ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
10458
10552
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
10459
10553
  };
@@ -10467,7 +10561,7 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
10467
10561
  ...block.assetId !== void 0 ? { assetId: block.assetId } : {},
10468
10562
  options: block.options,
10469
10563
  centered: block.centered,
10470
- ...block.captionEnabled && block.caption.trim().length > 0 ? { caption: block.caption.trim() } : {},
10564
+ ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
10471
10565
  ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
10472
10566
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
10473
10567
  };
@@ -12248,6 +12342,7 @@ function FloatMetaEditor({
12248
12342
  centered,
12249
12343
  captionEnabled,
12250
12344
  caption,
12345
+ captionEditor,
12251
12346
  labelEnabled,
12252
12347
  label,
12253
12348
  labelPlaceholder,
@@ -12291,9 +12386,9 @@ function FloatMetaEditor({
12291
12386
  ),
12292
12387
  /* @__PURE__ */ jsx7("span", { children: messages.captionToggle })
12293
12388
  ] }),
12294
- captionEnabled ? /* @__PURE__ */ jsxs6("label", { className: "butex-document2-widget__refs-field", children: [
12389
+ captionEnabled ? /* @__PURE__ */ jsxs6("div", { className: "butex-document2-widget__refs-field", children: [
12295
12390
  /* @__PURE__ */ jsx7("span", { children: messages.floatCaption }),
12296
- /* @__PURE__ */ jsx7(
12391
+ captionEditor ?? /* @__PURE__ */ jsx7(
12297
12392
  "input",
12298
12393
  {
12299
12394
  value: caption,
@@ -12701,9 +12796,12 @@ function BlockEditor({
12701
12796
  onDeleteMath,
12702
12797
  onOpenCite,
12703
12798
  onDeleteCite,
12799
+ onOpenRef,
12800
+ onDeleteRef,
12704
12801
  onFieldFocus,
12705
12802
  onMathFocus,
12706
12803
  onCiteFocus,
12804
+ onRefFocus,
12707
12805
  onFieldBlur,
12708
12806
  onImageSrcChange,
12709
12807
  onImageAssetChange,
@@ -12712,6 +12810,8 @@ function BlockEditor({
12712
12810
  onRequestImagePick,
12713
12811
  listImageAssets,
12714
12812
  renderImageBlockEditor,
12813
+ onFloatMetaChange,
12814
+ onParagraphCenteredChange,
12715
12815
  onAddListItem,
12716
12816
  onRemoveListItem,
12717
12817
  onManageReferences
@@ -12736,6 +12836,7 @@ function BlockEditor({
12736
12836
  {
12737
12837
  captionEnabled: block.captionEnabled,
12738
12838
  caption: block.caption,
12839
+ captionEditor: /* @__PURE__ */ jsx7(InlineField, { field: block.captionField, ...fieldProps }),
12739
12840
  labelEnabled: block.labelEnabled,
12740
12841
  label: block.label,
12741
12842
  labelPlaceholder: messages.tableLabelPlaceholder,
@@ -12774,6 +12875,7 @@ function BlockEditor({
12774
12875
  centered: block.centered,
12775
12876
  captionEnabled: block.captionEnabled,
12776
12877
  caption: block.caption,
12878
+ captionEditor: /* @__PURE__ */ jsx7(InlineField, { field: block.captionField, ...fieldProps }),
12777
12879
  labelEnabled: block.labelEnabled,
12778
12880
  label: block.label,
12779
12881
  labelPlaceholder: messages.figureLabelPlaceholder,
@@ -13699,16 +13801,21 @@ function PreviewBlock({
13699
13801
  const messages = document2Messages(uiLocale);
13700
13802
  const floatCaptionSeparator = uiLocale === "ar" ? ": " : ". ";
13701
13803
  const source = "metadata" in block ? block.metadata?.source : void 0;
13702
- function FloatCaption({ numberLabel, caption }) {
13703
- if (!numberLabel && !caption) {
13804
+ function FloatCaption({
13805
+ numberLabel,
13806
+ caption,
13807
+ captionInlines
13808
+ }) {
13809
+ const hasCaption = Boolean(captionInlines?.length) || Boolean(caption);
13810
+ if (!numberLabel && !hasCaption) {
13704
13811
  return null;
13705
13812
  }
13706
13813
  return /* @__PURE__ */ jsxs11("figcaption", { dir: documentDirection, children: [
13707
13814
  numberLabel ? /* @__PURE__ */ jsxs11("span", { className: "butex-document2-widget__preview-float-number", children: [
13708
13815
  numberLabel,
13709
- caption ? floatCaptionSeparator : ""
13816
+ hasCaption ? floatCaptionSeparator : ""
13710
13817
  ] }) : null,
13711
- caption ?? ""
13818
+ captionInlines ? /* @__PURE__ */ jsx12(PreviewInlines, { inlines: captionInlines, output }) : caption ?? ""
13712
13819
  ] });
13713
13820
  }
13714
13821
  if (block.kind === "basmala" || block.kind === "closing") {
@@ -13767,19 +13874,21 @@ function PreviewBlock({
13767
13874
  if (block.kind === "table") {
13768
13875
  return /* @__PURE__ */ jsxs11("figure", { className: "butex-document2-widget__preview-float", "data-butex-block-source": source, children: [
13769
13876
  /* @__PURE__ */ jsx12("div", { className: "butex-document2-widget__preview-table-scroll", children: /* @__PURE__ */ jsx12("table", { children: /* @__PURE__ */ jsx12("tbody", { children: block.rows.map((row, rowIndex) => /* @__PURE__ */ jsx12("tr", { children: row.map((cell, columnIndex) => /* @__PURE__ */ jsx12("td", { children: /* @__PURE__ */ jsx12(PreviewInlines, { inlines: cell, output }) }, columnIndex)) }, rowIndex)) }) }) }),
13770
- /* @__PURE__ */ jsx12(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption })
13877
+ /* @__PURE__ */ jsx12(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption, captionInlines: block.captionInlines })
13771
13878
  ] });
13772
13879
  }
13773
13880
  if (block.kind === "image") {
13774
- const src = resolveImageUrl ? resolveImageUrl({ assetId: block.assetId, value: block.src }) : block.src;
13881
+ const resolvedSrc = resolveImageUrl ? resolveImageUrl({ assetId: block.assetId, value: block.src }) : block.src;
13882
+ const src = resolvedSrc?.trim() ?? "";
13775
13883
  return /* @__PURE__ */ jsxs11(
13776
13884
  "figure",
13777
13885
  {
13778
13886
  className: `butex-document2-widget__preview-float${block.centered === false ? " butex-document2-widget__preview-float--start" : ""}`,
13779
13887
  "data-butex-block-source": source,
13888
+ "data-butex-image-pending": src.length === 0 ? "true" : void 0,
13780
13889
  children: [
13781
- /* @__PURE__ */ jsx12("img", { src, alt: block.caption ?? "" }),
13782
- /* @__PURE__ */ jsx12(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption })
13890
+ src.length > 0 ? /* @__PURE__ */ jsx12("img", { src, alt: block.caption ?? "" }) : null,
13891
+ /* @__PURE__ */ jsx12(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption, captionInlines: block.captionInlines })
13783
13892
  ]
13784
13893
  }
13785
13894
  );
@@ -17854,7 +17963,7 @@ function LabelsPanel({
17854
17963
  digitForm,
17855
17964
  documentDirection
17856
17965
  });
17857
- const captionPreview = entry.kind === "eq" ? title : formatFloatCaptionPreview(title, entry.caption, uiLocale);
17966
+ const captionPreview2 = entry.kind === "eq" ? title : formatFloatCaptionPreview(title, entry.caption, uiLocale);
17858
17967
  const overrideKey = `${entry.kind}:${entry.ownerId}`;
17859
17968
  const displayedKey = keyOverrides[overrideKey] ?? entry.key;
17860
17969
  const entryKeyError = document2KeyError(displayedKey, {
@@ -17873,7 +17982,7 @@ function LabelsPanel({
17873
17982
  className: "butex-document2-widget__modal-preview",
17874
17983
  dir: uiLocaleDirection(uiLocale),
17875
17984
  "aria-label": messages.captionPreview,
17876
- children: captionPreview
17985
+ children: captionPreview2
17877
17986
  }
17878
17987
  ),
17879
17988
  /* @__PURE__ */ jsxs15("div", { className: "butex-document2-widget__refs-grid", children: [
@@ -18335,6 +18444,9 @@ function findBlockIdForField(blocks, fieldId) {
18335
18444
  if (block.kind === "textBlock" && block.field.id === fieldId) {
18336
18445
  return block.id;
18337
18446
  }
18447
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
18448
+ return block.id;
18449
+ }
18338
18450
  if (block.kind === "list") {
18339
18451
  for (const item of block.items) {
18340
18452
  if (item.field.id === fieldId) {
@@ -20676,6 +20788,9 @@ function findInlineFieldById(document2, fieldId) {
20676
20788
  if (block.kind === "textBlock" && block.field.id === fieldId) {
20677
20789
  return block.field;
20678
20790
  }
20791
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
20792
+ return block.captionField;
20793
+ }
20679
20794
  if (block.kind === "list") {
20680
20795
  for (const item of block.items) {
20681
20796
  if (item.field.id === fieldId) {
@@ -20707,6 +20822,9 @@ function findInlineFieldContainingToken(document2, tokenId) {
20707
20822
  if (block.kind === "textBlock" && block.field.tokens.some((token) => token.id === tokenId)) {
20708
20823
  return block.field;
20709
20824
  }
20825
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.tokens.some((token) => token.id === tokenId)) {
20826
+ return block.captionField;
20827
+ }
20710
20828
  if (block.kind === "list") {
20711
20829
  for (const item of block.items) {
20712
20830
  if (item.field.tokens.some((token) => token.id === tokenId)) {
@@ -20781,6 +20899,9 @@ function blockHasField(block, fieldId) {
20781
20899
  if (block.kind === "textBlock") {
20782
20900
  return block.field.id === fieldId;
20783
20901
  }
20902
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
20903
+ return true;
20904
+ }
20784
20905
  if (block.kind === "list") {
20785
20906
  return block.items.some((item) => item.field.id === fieldId || item.blocks.some((child) => blockHasField(child, fieldId)));
20786
20907
  }
@@ -20793,6 +20914,9 @@ function blockHasToken(block, tokenId) {
20793
20914
  if (block.kind === "textBlock") {
20794
20915
  return block.field.tokens.some((token) => token.id === tokenId);
20795
20916
  }
20917
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.tokens.some((token) => token.id === tokenId)) {
20918
+ return true;
20919
+ }
20796
20920
  if (block.kind === "list") {
20797
20921
  return block.items.some(
20798
20922
  (item) => item.field.tokens.some((token) => token.id === tokenId) || item.blocks.some((child) => blockHasToken(child, tokenId))
@@ -20839,6 +20963,10 @@ function findFormatTextToken(document2, focus) {
20839
20963
  }
20840
20964
  }
20841
20965
  } else if (block.kind === "table") {
20966
+ const captionToken = textTokenFromField(block.captionField);
20967
+ if (captionToken) {
20968
+ return captionToken;
20969
+ }
20842
20970
  for (const row of block.rows) {
20843
20971
  for (const cell of row) {
20844
20972
  const token = textTokenFromField(cell);
@@ -20847,6 +20975,11 @@ function findFormatTextToken(document2, focus) {
20847
20975
  }
20848
20976
  }
20849
20977
  }
20978
+ } else if (block.kind === "image") {
20979
+ const captionToken = textTokenFromField(block.captionField);
20980
+ if (captionToken) {
20981
+ return captionToken;
20982
+ }
20850
20983
  }
20851
20984
  }
20852
20985
  return null;
@@ -22143,8 +22276,111 @@ var ButexDocumentEditor23 = forwardRef3(
22143
22276
  }
22144
22277
  );
22145
22278
  ButexDocumentEditor23.displayName = "ButexDocumentEditor2";
22279
+
22280
+ // src/react-document2/ButexDocumentPreview.tsx
22281
+ import { useEffect as useEffect14, useMemo as useMemo3 } from "react";
22282
+ import { jsx as jsx21, jsxs as jsxs20 } from "react/jsx-runtime";
22283
+ function isRecord2(value) {
22284
+ return typeof value === "object" && value !== null;
22285
+ }
22286
+ function isDocument2Node2(value) {
22287
+ return isRecord2(value) && value.nodeType === "DocumentObject" && isRecord2(value.meta) && Array.isArray(value.references) && Array.isArray(value.blocks) && Array.isArray(value.diagnostics);
22288
+ }
22289
+ function hasRuntimeDocumentMarker(value) {
22290
+ return isRecord2(value) && value.nodeType === "DocumentObject";
22291
+ }
22292
+ function resolvePreviewDocument2(input, uiLocale = "ar") {
22293
+ if (isDocument2Node2(input)) {
22294
+ return { document: input, error: null };
22295
+ }
22296
+ const messages = document2Messages(uiLocale);
22297
+ if (hasRuntimeDocumentMarker(input)) {
22298
+ return {
22299
+ document: createEmptyDocument2(),
22300
+ error: `${messages.loadDocumentError}: ${messages.runtimePersistenceError}`
22301
+ };
22302
+ }
22303
+ try {
22304
+ return { document: fromDocumentJson2(input), error: null };
22305
+ } catch (error) {
22306
+ const detail = error instanceof Error ? error.message : String(error);
22307
+ return {
22308
+ document: createEmptyDocument2(),
22309
+ error: `${messages.loadDocumentError}: ${detail}`
22310
+ };
22311
+ }
22312
+ }
22313
+ function ButexDocumentPreview({
22314
+ document: document2,
22315
+ className,
22316
+ documentDirection = "rtl",
22317
+ uiLocale = "ar",
22318
+ digitForm,
22319
+ equationSide = "arabic",
22320
+ mathOutput = "svg",
22321
+ resolveImageUrl
22322
+ }) {
22323
+ const messages = document2Messages(uiLocale);
22324
+ const resolvedDigitForm = digitForm ?? (documentDirection === "rtl" ? "arabicIndic" : "western");
22325
+ const resolved = useMemo3(() => resolvePreviewDocument2(document2, uiLocale), [document2, uiLocale]);
22326
+ const previewState = useMemo3(() => {
22327
+ try {
22328
+ return {
22329
+ preview: document2Preview(resolved.document, mathOutput, equationSide, {
22330
+ documentDirection,
22331
+ digitForm: resolvedDigitForm,
22332
+ uiLocale
22333
+ }),
22334
+ error: resolved.error
22335
+ };
22336
+ } catch (error) {
22337
+ const detail = error instanceof Error ? error.message : String(error);
22338
+ return {
22339
+ preview: document2Preview(createEmptyDocument2(), mathOutput, equationSide, {
22340
+ documentDirection,
22341
+ digitForm: resolvedDigitForm,
22342
+ uiLocale
22343
+ }),
22344
+ error: `${messages.loadDocumentError}: ${detail}`
22345
+ };
22346
+ }
22347
+ }, [documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
22348
+ useEffect14(() => {
22349
+ injectBuTeXDocument2Styles();
22350
+ }, []);
22351
+ return /* @__PURE__ */ jsx21(
22352
+ "div",
22353
+ {
22354
+ className: ["butex-document2-widget", "butex-document2-preview", className].filter(Boolean).join(" "),
22355
+ dir: uiLocaleDirection(uiLocale),
22356
+ lang: uiLocale,
22357
+ children: /* @__PURE__ */ jsxs20("div", { className: "butex-document2-widget__shell", children: [
22358
+ previewState.error ? /* @__PURE__ */ jsx21("p", { className: "butex-document2-widget__error", children: previewState.error }) : null,
22359
+ /* @__PURE__ */ jsx21("div", { className: "butex-document2-widget__layout butex-document2-widget__layout--editor-closed butex-document2-widget__layout--preview-open", children: /* @__PURE__ */ jsx21(
22360
+ "section",
22361
+ {
22362
+ className: "butex-document2-widget__panel butex-document2-widget__preview-panel",
22363
+ "aria-label": messages.documentPreview,
22364
+ children: /* @__PURE__ */ jsx21(
22365
+ DocumentPreview,
22366
+ {
22367
+ blocks: previewState.preview.blocks,
22368
+ output: mathOutput,
22369
+ documentDirection,
22370
+ uiLocale,
22371
+ digitForm: resolvedDigitForm,
22372
+ resolveImageUrl
22373
+ }
22374
+ )
22375
+ }
22376
+ ) })
22377
+ ] })
22378
+ }
22379
+ );
22380
+ }
22146
22381
  export {
22147
22382
  ButexDocumentEditor23 as ButexDocumentEditor2,
22383
+ ButexDocumentPreview,
22148
22384
  DOCUMENT2_WIDGET_CSS,
22149
22385
  DocumentInsertToolbar,
22150
22386
  DocumentPreview,
@@ -22153,6 +22389,7 @@ export {
22153
22389
  MathChip,
22154
22390
  MathIsland,
22155
22391
  injectBuTeXDocument2Styles,
22156
- resolveInitialDocument2
22392
+ resolveInitialDocument2,
22393
+ resolvePreviewDocument2
22157
22394
  };
22158
22395
  //# sourceMappingURL=react-document2.mjs.map