@drghaliasri/butex 7.0.3 → 7.1.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.
@@ -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 } } : {}
@@ -9470,6 +9554,9 @@ function toDocumentJson2(document2) {
9470
9554
  };
9471
9555
  }
9472
9556
 
9557
+ // src/document2/diff.ts
9558
+ import { diffJson } from "diff";
9559
+
9473
9560
  // src/document2/arabicPreamble.ts
9474
9561
  function fontOptions(digitsMapping, includeScript = true) {
9475
9562
  const parts = [];
@@ -9837,11 +9924,12 @@ ${block.closing}`;
9837
9924
  ${rows.join(" \\\\\n")}
9838
9925
  ${block.closing}`;
9839
9926
  }
9840
- function floatCaptionLabelLines(block, counter) {
9927
+ function floatCaptionLabelLines(block, counter, labelKinds) {
9841
9928
  const lines = [];
9842
- const hasCaption = block.captionEnabled && block.caption.trim().length > 0;
9929
+ const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds).trim();
9930
+ const hasCaption = block.captionEnabled && caption.length > 0;
9843
9931
  if (hasCaption) {
9844
- lines.push(` \\caption{${block.caption.trim()}}`);
9932
+ lines.push(` \\caption{${caption}}`);
9845
9933
  }
9846
9934
  if (block.labelEnabled && block.label.trim().length > 0) {
9847
9935
  if (!hasCaption) {
@@ -9857,7 +9945,7 @@ function tableBlockLatex(block, labelKinds, fixedFloats) {
9857
9945
  if (block.centered) {
9858
9946
  lines.push(" \\centering");
9859
9947
  }
9860
- lines.push(...floatCaptionLabelLines(block, "table"));
9948
+ lines.push(...floatCaptionLabelLines(block, "table", labelKinds));
9861
9949
  lines.push(indent(tabularInnerLatex(block, labelKinds)));
9862
9950
  lines.push("\\end{table}");
9863
9951
  return lines.join("\n");
@@ -9877,14 +9965,14 @@ function imageOptionsLatex(block, constrainToColumn) {
9877
9965
  }
9878
9966
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
9879
9967
  }
9880
- function imageBlockLatex(block, fixedFloats) {
9968
+ function imageBlockLatex(block, labelKinds, fixedFloats) {
9881
9969
  const placement = fixedFloats ? "H" : "htbp";
9882
9970
  const lines = [`\\begin{figure}[${placement}]`];
9883
9971
  if (block.centered) {
9884
9972
  lines.push(" \\centering");
9885
9973
  }
9886
9974
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
9887
- lines.push(...floatCaptionLabelLines(block, "figure"));
9975
+ lines.push(...floatCaptionLabelLines(block, "figure", labelKinds));
9888
9976
  lines.push("\\end{figure}");
9889
9977
  return lines.join("\n");
9890
9978
  }
@@ -9899,7 +9987,7 @@ function blockLatex(block, references, labelKinds, fixedFloats) {
9899
9987
  return tableBlockLatex(block, labelKinds, fixedFloats);
9900
9988
  }
9901
9989
  if (block.kind === "image") {
9902
- return imageBlockLatex(block, fixedFloats);
9990
+ return imageBlockLatex(block, labelKinds, fixedFloats);
9903
9991
  }
9904
9992
  if (block.kind === "bibliography") {
9905
9993
  const width = String(Math.max(references.length, 9));
@@ -10092,7 +10180,7 @@ function validateBlock(context, block, path) {
10092
10180
  );
10093
10181
  });
10094
10182
  });
10095
- validateTextValue(context, block.caption, `${path}.caption`, block.id);
10183
+ validateInlineField(context, block.captionField, `${path}.caption`, block.id);
10096
10184
  if (block.labelEnabled) {
10097
10185
  registerKey(context, block.label, `${path}.label`, "tab", block.id);
10098
10186
  }
@@ -10105,7 +10193,7 @@ function validateBlock(context, block, path) {
10105
10193
  addIssue(context, "empty_image", path, block.id);
10106
10194
  }
10107
10195
  validateTextValue(context, block.value, `${path}.value`, block.id);
10108
- validateTextValue(context, block.caption, `${path}.caption`, block.id);
10196
+ validateInlineField(context, block.captionField, `${path}.caption`, block.id);
10109
10197
  if (block.labelEnabled) {
10110
10198
  registerKey(context, block.label, `${path}.label`, "fig", block.id);
10111
10199
  }
@@ -10430,6 +10518,15 @@ function floatNumberLabel(document2, key, previewOptions) {
10430
10518
  digitForm: previewOptions.digitForm
10431
10519
  });
10432
10520
  }
10521
+ function captionPreview(block, islands, output, equationSide, document2, previewOptions) {
10522
+ if (!block.captionEnabled || block.caption.trim().length === 0) {
10523
+ return {};
10524
+ }
10525
+ return {
10526
+ caption: block.caption.trim(),
10527
+ captionInlines: previewInlines(block.captionField, islands, output, equationSide, document2, previewOptions)
10528
+ };
10529
+ }
10433
10530
  function blockPreview(block, islands, output, equationSide, document2, previewOptions) {
10434
10531
  if (block.kind === "textBlock") {
10435
10532
  return textBlockPreview(block, islands, output, equationSide, document2, previewOptions);
@@ -10453,7 +10550,7 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
10453
10550
  kind: "table",
10454
10551
  id: block.id,
10455
10552
  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() } : {},
10553
+ ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
10457
10554
  ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
10458
10555
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
10459
10556
  };
@@ -10467,7 +10564,7 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
10467
10564
  ...block.assetId !== void 0 ? { assetId: block.assetId } : {},
10468
10565
  options: block.options,
10469
10566
  centered: block.centered,
10470
- ...block.captionEnabled && block.caption.trim().length > 0 ? { caption: block.caption.trim() } : {},
10567
+ ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
10471
10568
  ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
10472
10569
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
10473
10570
  };
@@ -12248,6 +12345,7 @@ function FloatMetaEditor({
12248
12345
  centered,
12249
12346
  captionEnabled,
12250
12347
  caption,
12348
+ captionEditor,
12251
12349
  labelEnabled,
12252
12350
  label,
12253
12351
  labelPlaceholder,
@@ -12291,9 +12389,9 @@ function FloatMetaEditor({
12291
12389
  ),
12292
12390
  /* @__PURE__ */ jsx7("span", { children: messages.captionToggle })
12293
12391
  ] }),
12294
- captionEnabled ? /* @__PURE__ */ jsxs6("label", { className: "butex-document2-widget__refs-field", children: [
12392
+ captionEnabled ? /* @__PURE__ */ jsxs6("div", { className: "butex-document2-widget__refs-field", children: [
12295
12393
  /* @__PURE__ */ jsx7("span", { children: messages.floatCaption }),
12296
- /* @__PURE__ */ jsx7(
12394
+ captionEditor ?? /* @__PURE__ */ jsx7(
12297
12395
  "input",
12298
12396
  {
12299
12397
  value: caption,
@@ -12701,9 +12799,12 @@ function BlockEditor({
12701
12799
  onDeleteMath,
12702
12800
  onOpenCite,
12703
12801
  onDeleteCite,
12802
+ onOpenRef,
12803
+ onDeleteRef,
12704
12804
  onFieldFocus,
12705
12805
  onMathFocus,
12706
12806
  onCiteFocus,
12807
+ onRefFocus,
12707
12808
  onFieldBlur,
12708
12809
  onImageSrcChange,
12709
12810
  onImageAssetChange,
@@ -12712,6 +12813,8 @@ function BlockEditor({
12712
12813
  onRequestImagePick,
12713
12814
  listImageAssets,
12714
12815
  renderImageBlockEditor,
12816
+ onFloatMetaChange,
12817
+ onParagraphCenteredChange,
12715
12818
  onAddListItem,
12716
12819
  onRemoveListItem,
12717
12820
  onManageReferences
@@ -12736,6 +12839,7 @@ function BlockEditor({
12736
12839
  {
12737
12840
  captionEnabled: block.captionEnabled,
12738
12841
  caption: block.caption,
12842
+ captionEditor: /* @__PURE__ */ jsx7(InlineField, { field: block.captionField, ...fieldProps }),
12739
12843
  labelEnabled: block.labelEnabled,
12740
12844
  label: block.label,
12741
12845
  labelPlaceholder: messages.tableLabelPlaceholder,
@@ -12774,6 +12878,7 @@ function BlockEditor({
12774
12878
  centered: block.centered,
12775
12879
  captionEnabled: block.captionEnabled,
12776
12880
  caption: block.caption,
12881
+ captionEditor: /* @__PURE__ */ jsx7(InlineField, { field: block.captionField, ...fieldProps }),
12777
12882
  labelEnabled: block.labelEnabled,
12778
12883
  label: block.label,
12779
12884
  labelPlaceholder: messages.figureLabelPlaceholder,
@@ -13699,16 +13804,21 @@ function PreviewBlock({
13699
13804
  const messages = document2Messages(uiLocale);
13700
13805
  const floatCaptionSeparator = uiLocale === "ar" ? ": " : ". ";
13701
13806
  const source = "metadata" in block ? block.metadata?.source : void 0;
13702
- function FloatCaption({ numberLabel, caption }) {
13703
- if (!numberLabel && !caption) {
13807
+ function FloatCaption({
13808
+ numberLabel,
13809
+ caption,
13810
+ captionInlines
13811
+ }) {
13812
+ const hasCaption = Boolean(captionInlines?.length) || Boolean(caption);
13813
+ if (!numberLabel && !hasCaption) {
13704
13814
  return null;
13705
13815
  }
13706
13816
  return /* @__PURE__ */ jsxs11("figcaption", { dir: documentDirection, children: [
13707
13817
  numberLabel ? /* @__PURE__ */ jsxs11("span", { className: "butex-document2-widget__preview-float-number", children: [
13708
13818
  numberLabel,
13709
- caption ? floatCaptionSeparator : ""
13819
+ hasCaption ? floatCaptionSeparator : ""
13710
13820
  ] }) : null,
13711
- caption ?? ""
13821
+ captionInlines ? /* @__PURE__ */ jsx12(PreviewInlines, { inlines: captionInlines, output }) : caption ?? ""
13712
13822
  ] });
13713
13823
  }
13714
13824
  if (block.kind === "basmala" || block.kind === "closing") {
@@ -13767,19 +13877,21 @@ function PreviewBlock({
13767
13877
  if (block.kind === "table") {
13768
13878
  return /* @__PURE__ */ jsxs11("figure", { className: "butex-document2-widget__preview-float", "data-butex-block-source": source, children: [
13769
13879
  /* @__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 })
13880
+ /* @__PURE__ */ jsx12(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption, captionInlines: block.captionInlines })
13771
13881
  ] });
13772
13882
  }
13773
13883
  if (block.kind === "image") {
13774
- const src = resolveImageUrl ? resolveImageUrl({ assetId: block.assetId, value: block.src }) : block.src;
13884
+ const resolvedSrc = resolveImageUrl ? resolveImageUrl({ assetId: block.assetId, value: block.src }) : block.src;
13885
+ const src = resolvedSrc?.trim() ?? "";
13775
13886
  return /* @__PURE__ */ jsxs11(
13776
13887
  "figure",
13777
13888
  {
13778
13889
  className: `butex-document2-widget__preview-float${block.centered === false ? " butex-document2-widget__preview-float--start" : ""}`,
13779
13890
  "data-butex-block-source": source,
13891
+ "data-butex-image-pending": src.length === 0 ? "true" : void 0,
13780
13892
  children: [
13781
- /* @__PURE__ */ jsx12("img", { src, alt: block.caption ?? "" }),
13782
- /* @__PURE__ */ jsx12(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption })
13893
+ src.length > 0 ? /* @__PURE__ */ jsx12("img", { src, alt: block.caption ?? "" }) : null,
13894
+ /* @__PURE__ */ jsx12(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption, captionInlines: block.captionInlines })
13783
13895
  ]
13784
13896
  }
13785
13897
  );
@@ -17854,7 +17966,7 @@ function LabelsPanel({
17854
17966
  digitForm,
17855
17967
  documentDirection
17856
17968
  });
17857
- const captionPreview = entry.kind === "eq" ? title : formatFloatCaptionPreview(title, entry.caption, uiLocale);
17969
+ const captionPreview2 = entry.kind === "eq" ? title : formatFloatCaptionPreview(title, entry.caption, uiLocale);
17858
17970
  const overrideKey = `${entry.kind}:${entry.ownerId}`;
17859
17971
  const displayedKey = keyOverrides[overrideKey] ?? entry.key;
17860
17972
  const entryKeyError = document2KeyError(displayedKey, {
@@ -17873,7 +17985,7 @@ function LabelsPanel({
17873
17985
  className: "butex-document2-widget__modal-preview",
17874
17986
  dir: uiLocaleDirection(uiLocale),
17875
17987
  "aria-label": messages.captionPreview,
17876
- children: captionPreview
17988
+ children: captionPreview2
17877
17989
  }
17878
17990
  ),
17879
17991
  /* @__PURE__ */ jsxs15("div", { className: "butex-document2-widget__refs-grid", children: [
@@ -18335,6 +18447,9 @@ function findBlockIdForField(blocks, fieldId) {
18335
18447
  if (block.kind === "textBlock" && block.field.id === fieldId) {
18336
18448
  return block.id;
18337
18449
  }
18450
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
18451
+ return block.id;
18452
+ }
18338
18453
  if (block.kind === "list") {
18339
18454
  for (const item of block.items) {
18340
18455
  if (item.field.id === fieldId) {
@@ -20676,6 +20791,9 @@ function findInlineFieldById(document2, fieldId) {
20676
20791
  if (block.kind === "textBlock" && block.field.id === fieldId) {
20677
20792
  return block.field;
20678
20793
  }
20794
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
20795
+ return block.captionField;
20796
+ }
20679
20797
  if (block.kind === "list") {
20680
20798
  for (const item of block.items) {
20681
20799
  if (item.field.id === fieldId) {
@@ -20707,6 +20825,9 @@ function findInlineFieldContainingToken(document2, tokenId) {
20707
20825
  if (block.kind === "textBlock" && block.field.tokens.some((token) => token.id === tokenId)) {
20708
20826
  return block.field;
20709
20827
  }
20828
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.tokens.some((token) => token.id === tokenId)) {
20829
+ return block.captionField;
20830
+ }
20710
20831
  if (block.kind === "list") {
20711
20832
  for (const item of block.items) {
20712
20833
  if (item.field.tokens.some((token) => token.id === tokenId)) {
@@ -20781,6 +20902,9 @@ function blockHasField(block, fieldId) {
20781
20902
  if (block.kind === "textBlock") {
20782
20903
  return block.field.id === fieldId;
20783
20904
  }
20905
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
20906
+ return true;
20907
+ }
20784
20908
  if (block.kind === "list") {
20785
20909
  return block.items.some((item) => item.field.id === fieldId || item.blocks.some((child) => blockHasField(child, fieldId)));
20786
20910
  }
@@ -20793,6 +20917,9 @@ function blockHasToken(block, tokenId) {
20793
20917
  if (block.kind === "textBlock") {
20794
20918
  return block.field.tokens.some((token) => token.id === tokenId);
20795
20919
  }
20920
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.tokens.some((token) => token.id === tokenId)) {
20921
+ return true;
20922
+ }
20796
20923
  if (block.kind === "list") {
20797
20924
  return block.items.some(
20798
20925
  (item) => item.field.tokens.some((token) => token.id === tokenId) || item.blocks.some((child) => blockHasToken(child, tokenId))
@@ -20839,6 +20966,10 @@ function findFormatTextToken(document2, focus) {
20839
20966
  }
20840
20967
  }
20841
20968
  } else if (block.kind === "table") {
20969
+ const captionToken = textTokenFromField(block.captionField);
20970
+ if (captionToken) {
20971
+ return captionToken;
20972
+ }
20842
20973
  for (const row of block.rows) {
20843
20974
  for (const cell of row) {
20844
20975
  const token = textTokenFromField(cell);
@@ -20847,6 +20978,11 @@ function findFormatTextToken(document2, focus) {
20847
20978
  }
20848
20979
  }
20849
20980
  }
20981
+ } else if (block.kind === "image") {
20982
+ const captionToken = textTokenFromField(block.captionField);
20983
+ if (captionToken) {
20984
+ return captionToken;
20985
+ }
20850
20986
  }
20851
20987
  }
20852
20988
  return null;
@@ -22143,8 +22279,111 @@ var ButexDocumentEditor23 = forwardRef3(
22143
22279
  }
22144
22280
  );
22145
22281
  ButexDocumentEditor23.displayName = "ButexDocumentEditor2";
22282
+
22283
+ // src/react-document2/ButexDocumentPreview.tsx
22284
+ import { useEffect as useEffect14, useMemo as useMemo3 } from "react";
22285
+ import { jsx as jsx21, jsxs as jsxs20 } from "react/jsx-runtime";
22286
+ function isRecord2(value) {
22287
+ return typeof value === "object" && value !== null;
22288
+ }
22289
+ function isDocument2Node2(value) {
22290
+ return isRecord2(value) && value.nodeType === "DocumentObject" && isRecord2(value.meta) && Array.isArray(value.references) && Array.isArray(value.blocks) && Array.isArray(value.diagnostics);
22291
+ }
22292
+ function hasRuntimeDocumentMarker(value) {
22293
+ return isRecord2(value) && value.nodeType === "DocumentObject";
22294
+ }
22295
+ function resolvePreviewDocument2(input, uiLocale = "ar") {
22296
+ if (isDocument2Node2(input)) {
22297
+ return { document: input, error: null };
22298
+ }
22299
+ const messages = document2Messages(uiLocale);
22300
+ if (hasRuntimeDocumentMarker(input)) {
22301
+ return {
22302
+ document: createEmptyDocument2(),
22303
+ error: `${messages.loadDocumentError}: ${messages.runtimePersistenceError}`
22304
+ };
22305
+ }
22306
+ try {
22307
+ return { document: fromDocumentJson2(input), error: null };
22308
+ } catch (error) {
22309
+ const detail = error instanceof Error ? error.message : String(error);
22310
+ return {
22311
+ document: createEmptyDocument2(),
22312
+ error: `${messages.loadDocumentError}: ${detail}`
22313
+ };
22314
+ }
22315
+ }
22316
+ function ButexDocumentPreview({
22317
+ document: document2,
22318
+ className,
22319
+ documentDirection = "rtl",
22320
+ uiLocale = "ar",
22321
+ digitForm,
22322
+ equationSide = "arabic",
22323
+ mathOutput = "svg",
22324
+ resolveImageUrl
22325
+ }) {
22326
+ const messages = document2Messages(uiLocale);
22327
+ const resolvedDigitForm = digitForm ?? (documentDirection === "rtl" ? "arabicIndic" : "western");
22328
+ const resolved = useMemo3(() => resolvePreviewDocument2(document2, uiLocale), [document2, uiLocale]);
22329
+ const previewState = useMemo3(() => {
22330
+ try {
22331
+ return {
22332
+ preview: document2Preview(resolved.document, mathOutput, equationSide, {
22333
+ documentDirection,
22334
+ digitForm: resolvedDigitForm,
22335
+ uiLocale
22336
+ }),
22337
+ error: resolved.error
22338
+ };
22339
+ } catch (error) {
22340
+ const detail = error instanceof Error ? error.message : String(error);
22341
+ return {
22342
+ preview: document2Preview(createEmptyDocument2(), mathOutput, equationSide, {
22343
+ documentDirection,
22344
+ digitForm: resolvedDigitForm,
22345
+ uiLocale
22346
+ }),
22347
+ error: `${messages.loadDocumentError}: ${detail}`
22348
+ };
22349
+ }
22350
+ }, [documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
22351
+ useEffect14(() => {
22352
+ injectBuTeXDocument2Styles();
22353
+ }, []);
22354
+ return /* @__PURE__ */ jsx21(
22355
+ "div",
22356
+ {
22357
+ className: ["butex-document2-widget", "butex-document2-preview", className].filter(Boolean).join(" "),
22358
+ dir: uiLocaleDirection(uiLocale),
22359
+ lang: uiLocale,
22360
+ children: /* @__PURE__ */ jsxs20("div", { className: "butex-document2-widget__shell", children: [
22361
+ previewState.error ? /* @__PURE__ */ jsx21("p", { className: "butex-document2-widget__error", children: previewState.error }) : null,
22362
+ /* @__PURE__ */ jsx21("div", { className: "butex-document2-widget__layout butex-document2-widget__layout--editor-closed butex-document2-widget__layout--preview-open", children: /* @__PURE__ */ jsx21(
22363
+ "section",
22364
+ {
22365
+ className: "butex-document2-widget__panel butex-document2-widget__preview-panel",
22366
+ "aria-label": messages.documentPreview,
22367
+ children: /* @__PURE__ */ jsx21(
22368
+ DocumentPreview,
22369
+ {
22370
+ blocks: previewState.preview.blocks,
22371
+ output: mathOutput,
22372
+ documentDirection,
22373
+ uiLocale,
22374
+ digitForm: resolvedDigitForm,
22375
+ resolveImageUrl
22376
+ }
22377
+ )
22378
+ }
22379
+ ) })
22380
+ ] })
22381
+ }
22382
+ );
22383
+ }
22146
22384
  export {
22147
22385
  ButexDocumentEditor23 as ButexDocumentEditor2,
22386
+ ButexDocumentPreview,
22148
22387
  DOCUMENT2_WIDGET_CSS,
22149
22388
  DocumentInsertToolbar,
22150
22389
  DocumentPreview,
@@ -22153,6 +22392,7 @@ export {
22153
22392
  MathChip,
22154
22393
  MathIsland,
22155
22394
  injectBuTeXDocument2Styles,
22156
- resolveInitialDocument2
22395
+ resolveInitialDocument2,
22396
+ resolvePreviewDocument2
22157
22397
  };
22158
22398
  //# sourceMappingURL=react-document2.mjs.map