@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.
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var react_document2_entry_exports = {};
22
22
  __export(react_document2_entry_exports, {
23
23
  ButexDocumentEditor2: () => ButexDocumentEditor23,
24
+ ButexDocumentPreview: () => ButexDocumentPreview,
24
25
  DOCUMENT2_WIDGET_CSS: () => DOCUMENT2_WIDGET_CSS,
25
26
  DocumentInsertToolbar: () => DocumentInsertToolbar,
26
27
  DocumentPreview: () => DocumentPreview,
@@ -29,7 +30,8 @@ __export(react_document2_entry_exports, {
29
30
  MathChip: () => MathChip,
30
31
  MathIsland: () => MathIsland,
31
32
  injectBuTeXDocument2Styles: () => injectBuTeXDocument2Styles,
32
- resolveInitialDocument2: () => resolveInitialDocument2
33
+ resolveInitialDocument2: () => resolveInitialDocument2,
34
+ resolvePreviewDocument2: () => resolvePreviewDocument2
33
35
  });
34
36
  module.exports = __toCommonJS(react_document2_entry_exports);
35
37
 
@@ -827,6 +829,10 @@ function defaultFloatMeta() {
827
829
  centered: true,
828
830
  captionEnabled: false,
829
831
  caption: "",
832
+ captionField: {
833
+ id: document2Id("field"),
834
+ tokens: [{ id: document2Id("text"), kind: "text", text: "" }]
835
+ },
830
836
  labelEnabled: false,
831
837
  label: ""
832
838
  };
@@ -885,15 +891,20 @@ function collectDocument2Labels(document2) {
885
891
  }
886
892
  function walk(blocks) {
887
893
  for (const block of blocks) {
888
- if (block.kind === "image" && block.labelEnabled && block.label.trim().length > 0) {
889
- counters.fig += 1;
890
- entries.push({
891
- key: block.label.trim(),
892
- kind: "fig",
893
- caption: block.captionEnabled ? block.caption : "",
894
- ownerId: block.id,
895
- number: counters.fig
896
- });
894
+ if (block.kind === "image") {
895
+ if (block.labelEnabled && block.label.trim().length > 0) {
896
+ counters.fig += 1;
897
+ entries.push({
898
+ key: block.label.trim(),
899
+ kind: "fig",
900
+ caption: block.captionEnabled ? block.caption : "",
901
+ ownerId: block.id,
902
+ number: counters.fig
903
+ });
904
+ }
905
+ if (block.captionEnabled) {
906
+ pushEqFromField(block.captionField.tokens);
907
+ }
897
908
  }
898
909
  if (block.kind === "table") {
899
910
  if (block.labelEnabled && block.label.trim().length > 0) {
@@ -906,6 +917,9 @@ function collectDocument2Labels(document2) {
906
917
  number: counters.tab
907
918
  });
908
919
  }
920
+ if (block.captionEnabled) {
921
+ pushEqFromField(block.captionField.tokens);
922
+ }
909
923
  for (const row of block.rows) {
910
924
  for (const cell of row) {
911
925
  pushEqFromField(cell.tokens);
@@ -998,6 +1012,14 @@ function applyFloatMetaPatch(block, patch) {
998
1012
  }
999
1013
  if (typeof patch.caption === "string") {
1000
1014
  block.caption = patch.caption;
1015
+ const existingText = block.captionField.tokens.find((token) => token.kind === "text");
1016
+ block.captionField.tokens = [
1017
+ {
1018
+ id: existingText?.id ?? document2Id("text"),
1019
+ kind: "text",
1020
+ text: patch.caption
1021
+ }
1022
+ ];
1001
1023
  }
1002
1024
  if (typeof patch.labelEnabled === "boolean") {
1003
1025
  block.labelEnabled = patch.labelEnabled;
@@ -1517,6 +1539,21 @@ function parseListBlock(json, options, path, diagnostics, identities) {
1517
1539
  ...metadataFromJson(json.metadata)
1518
1540
  };
1519
1541
  }
1542
+ function captionFieldFromJson(json, options, path, diagnostics, identities) {
1543
+ const caption = typeof json.caption === "string" ? json.caption : "";
1544
+ return applyImportedInlineIds(
1545
+ createInlineField2(
1546
+ caption,
1547
+ json.caption_math_objects ?? [],
1548
+ options,
1549
+ `${path}.caption`,
1550
+ diagnostics,
1551
+ json.caption_formats ?? []
1552
+ ),
1553
+ json.caption_inline_ids,
1554
+ identities
1555
+ );
1556
+ }
1520
1557
  function parseTableBlock(json, options, path, diagnostics, identities) {
1521
1558
  if (!Array.isArray(json.rows)) {
1522
1559
  throw new Error("\\begin{tabular} requires rows array");
@@ -1543,6 +1580,7 @@ function parseTableBlock(json, options, path, diagnostics, identities) {
1543
1580
  if (mathObjects.length > 0 && mathObjects.length !== mathObjectIndex) {
1544
1581
  pushDiagnostic(diagnostics, options, path, `math_objects count mismatch: detected ${String(mathObjectIndex)}, got ${String(mathObjects.length)}`);
1545
1582
  }
1583
+ const floatMeta = parseFloatMetaFromJson(json);
1546
1584
  return {
1547
1585
  id: blockIdFromJson(json, identities),
1548
1586
  kind: "table",
@@ -1550,13 +1588,15 @@ function parseTableBlock(json, options, path, diagnostics, identities) {
1550
1588
  closing: "\\end{tabular}",
1551
1589
  columns: typeof json.columns === "string" ? json.columns : "",
1552
1590
  rows,
1553
- ...parseFloatMetaFromJson(json),
1591
+ ...floatMeta,
1592
+ captionField: captionFieldFromJson(json, options, path, diagnostics, identities),
1554
1593
  ...metadataFromJson(json.metadata)
1555
1594
  };
1556
1595
  }
1557
- function parseImageBlock(json, identities) {
1596
+ function parseImageBlock(json, options, path, diagnostics, identities) {
1558
1597
  const assetId = typeof json.asset_id === "string" && json.asset_id.length > 0 ? json.asset_id : void 0;
1559
1598
  const value = assetId !== void 0 ? typeof json.value === "string" ? json.value : "" : requireString(json.value, "\\includegraphics requires string value");
1599
+ const floatMeta = parseFloatMetaFromJson(json);
1560
1600
  return {
1561
1601
  id: blockIdFromJson(json, identities),
1562
1602
  kind: "image",
@@ -1564,7 +1604,8 @@ function parseImageBlock(json, identities) {
1564
1604
  value,
1565
1605
  ...assetId !== void 0 ? { assetId } : {},
1566
1606
  options: isRecordOfStrings(json.options) ? json.options : {},
1567
- ...parseFloatMetaFromJson(json),
1607
+ ...floatMeta,
1608
+ captionField: captionFieldFromJson(json, options, path, diagnostics, identities),
1568
1609
  ...metadataFromJson(json.metadata)
1569
1610
  };
1570
1611
  }
@@ -1597,7 +1638,7 @@ function parseBlock(json, options, path, diagnostics, identities) {
1597
1638
  return parseTableBlock(json, options, path, diagnostics, identities);
1598
1639
  }
1599
1640
  if (json.command === "\\includegraphics") {
1600
- return parseImageBlock(json, identities);
1641
+ return parseImageBlock(json, options, path, diagnostics, identities);
1601
1642
  }
1602
1643
  if (json.command === "\\begin{thebibliography}" || json.command === "\\bibliography") {
1603
1644
  return parseBibliographyBlock(json, identities);
@@ -8545,6 +8586,20 @@ function cloneToken(token) {
8545
8586
  function cloneField(field) {
8546
8587
  return { id: field.id, tokens: field.tokens.map(cloneToken) };
8547
8588
  }
8589
+ function inlineFieldSource(field) {
8590
+ return field.tokens.map((token) => {
8591
+ if (token.kind === "text") {
8592
+ return token.text;
8593
+ }
8594
+ if (token.kind === "cite") {
8595
+ return citeTokenLatex(token.keys);
8596
+ }
8597
+ if (token.kind === "ref") {
8598
+ return refTokenLatex(token.keys, token.refCommand);
8599
+ }
8600
+ return token.source;
8601
+ }).join("");
8602
+ }
8548
8603
  function cloneBlock(block) {
8549
8604
  const metadata = block.metadata ? { metadata: { ...block.metadata } } : {};
8550
8605
  if (block.kind === "textBlock") {
@@ -8562,10 +8617,20 @@ function cloneBlock(block) {
8562
8617
  };
8563
8618
  }
8564
8619
  if (block.kind === "table") {
8565
- return { ...block, ...metadata, rows: block.rows.map((row) => row.map(cloneField)) };
8620
+ return {
8621
+ ...block,
8622
+ ...metadata,
8623
+ rows: block.rows.map((row) => row.map(cloneField)),
8624
+ captionField: cloneField(block.captionField)
8625
+ };
8566
8626
  }
8567
8627
  if (block.kind === "image") {
8568
- return { ...block, ...metadata, options: { ...block.options } };
8628
+ return {
8629
+ ...block,
8630
+ ...metadata,
8631
+ options: { ...block.options },
8632
+ captionField: cloneField(block.captionField)
8633
+ };
8569
8634
  }
8570
8635
  return { ...block, ...metadata };
8571
8636
  }
@@ -8578,6 +8643,13 @@ function cloneDocument(document2) {
8578
8643
  diagnostics: document2.diagnostics.map((diagnostic) => ({ ...diagnostic }))
8579
8644
  };
8580
8645
  }
8646
+ function visitCaptionField(block, visitor) {
8647
+ if (!visitor(block.captionField)) {
8648
+ return false;
8649
+ }
8650
+ block.caption = inlineFieldSource(block.captionField);
8651
+ return true;
8652
+ }
8581
8653
  function visitFields(blocks, visitor) {
8582
8654
  for (const block of blocks) {
8583
8655
  if (block.kind === "textBlock") {
@@ -8598,6 +8670,11 @@ function visitFields(blocks, visitor) {
8598
8670
  }
8599
8671
  }
8600
8672
  }
8673
+ if (visitCaptionField(block, visitor)) {
8674
+ return true;
8675
+ }
8676
+ } else if (block.kind === "image" && visitCaptionField(block, visitor)) {
8677
+ return true;
8601
8678
  }
8602
8679
  }
8603
8680
  return false;
@@ -9397,6 +9474,15 @@ function fieldJson(field) {
9397
9474
  ...serialized.hasPersistedMath ? { math_objects: serialized.mathObjects } : {}
9398
9475
  };
9399
9476
  }
9477
+ function captionJson(field) {
9478
+ const serialized = serializeField(field);
9479
+ return {
9480
+ caption: serialized.value,
9481
+ caption_inline_ids: serialized.inlineIds,
9482
+ ...serialized.formats.length > 0 ? { caption_formats: serialized.formats } : {},
9483
+ ...serialized.hasPersistedMath ? { caption_math_objects: serialized.mathObjects } : {}
9484
+ };
9485
+ }
9400
9486
  function listItemJson(item) {
9401
9487
  const field = fieldJson(item.field);
9402
9488
  return {
@@ -9441,7 +9527,7 @@ function blockJson(block) {
9441
9527
  ...hasPersistedMath ? { math_objects: fields.flatMap((row) => row.flatMap((field) => field.mathObjects)) } : {},
9442
9528
  centered: block.centered,
9443
9529
  caption_enabled: block.captionEnabled,
9444
- caption: block.caption,
9530
+ ...captionJson(block.captionField),
9445
9531
  label_enabled: block.labelEnabled,
9446
9532
  label: block.label,
9447
9533
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
@@ -9456,7 +9542,7 @@ function blockJson(block) {
9456
9542
  options: { ...block.options },
9457
9543
  centered: block.centered,
9458
9544
  caption_enabled: block.captionEnabled,
9459
- caption: block.caption,
9545
+ ...captionJson(block.captionField),
9460
9546
  label_enabled: block.labelEnabled,
9461
9547
  label: block.label,
9462
9548
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
@@ -9872,11 +9958,12 @@ ${block.closing}`;
9872
9958
  ${rows.join(" \\\\\n")}
9873
9959
  ${block.closing}`;
9874
9960
  }
9875
- function floatCaptionLabelLines(block, counter) {
9961
+ function floatCaptionLabelLines(block, counter, labelKinds) {
9876
9962
  const lines = [];
9877
- const hasCaption = block.captionEnabled && block.caption.trim().length > 0;
9963
+ const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds).trim();
9964
+ const hasCaption = block.captionEnabled && caption.length > 0;
9878
9965
  if (hasCaption) {
9879
- lines.push(` \\caption{${block.caption.trim()}}`);
9966
+ lines.push(` \\caption{${caption}}`);
9880
9967
  }
9881
9968
  if (block.labelEnabled && block.label.trim().length > 0) {
9882
9969
  if (!hasCaption) {
@@ -9892,7 +9979,7 @@ function tableBlockLatex(block, labelKinds, fixedFloats) {
9892
9979
  if (block.centered) {
9893
9980
  lines.push(" \\centering");
9894
9981
  }
9895
- lines.push(...floatCaptionLabelLines(block, "table"));
9982
+ lines.push(...floatCaptionLabelLines(block, "table", labelKinds));
9896
9983
  lines.push(indent(tabularInnerLatex(block, labelKinds)));
9897
9984
  lines.push("\\end{table}");
9898
9985
  return lines.join("\n");
@@ -9912,14 +9999,14 @@ function imageOptionsLatex(block, constrainToColumn) {
9912
9999
  }
9913
10000
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
9914
10001
  }
9915
- function imageBlockLatex(block, fixedFloats) {
10002
+ function imageBlockLatex(block, labelKinds, fixedFloats) {
9916
10003
  const placement = fixedFloats ? "H" : "htbp";
9917
10004
  const lines = [`\\begin{figure}[${placement}]`];
9918
10005
  if (block.centered) {
9919
10006
  lines.push(" \\centering");
9920
10007
  }
9921
10008
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
9922
- lines.push(...floatCaptionLabelLines(block, "figure"));
10009
+ lines.push(...floatCaptionLabelLines(block, "figure", labelKinds));
9923
10010
  lines.push("\\end{figure}");
9924
10011
  return lines.join("\n");
9925
10012
  }
@@ -9934,7 +10021,7 @@ function blockLatex(block, references, labelKinds, fixedFloats) {
9934
10021
  return tableBlockLatex(block, labelKinds, fixedFloats);
9935
10022
  }
9936
10023
  if (block.kind === "image") {
9937
- return imageBlockLatex(block, fixedFloats);
10024
+ return imageBlockLatex(block, labelKinds, fixedFloats);
9938
10025
  }
9939
10026
  if (block.kind === "bibliography") {
9940
10027
  const width = String(Math.max(references.length, 9));
@@ -10127,7 +10214,7 @@ function validateBlock(context, block, path) {
10127
10214
  );
10128
10215
  });
10129
10216
  });
10130
- validateTextValue(context, block.caption, `${path}.caption`, block.id);
10217
+ validateInlineField(context, block.captionField, `${path}.caption`, block.id);
10131
10218
  if (block.labelEnabled) {
10132
10219
  registerKey(context, block.label, `${path}.label`, "tab", block.id);
10133
10220
  }
@@ -10140,7 +10227,7 @@ function validateBlock(context, block, path) {
10140
10227
  addIssue(context, "empty_image", path, block.id);
10141
10228
  }
10142
10229
  validateTextValue(context, block.value, `${path}.value`, block.id);
10143
- validateTextValue(context, block.caption, `${path}.caption`, block.id);
10230
+ validateInlineField(context, block.captionField, `${path}.caption`, block.id);
10144
10231
  if (block.labelEnabled) {
10145
10232
  registerKey(context, block.label, `${path}.label`, "fig", block.id);
10146
10233
  }
@@ -10465,6 +10552,15 @@ function floatNumberLabel(document2, key, previewOptions) {
10465
10552
  digitForm: previewOptions.digitForm
10466
10553
  });
10467
10554
  }
10555
+ function captionPreview(block, islands, output, equationSide, document2, previewOptions) {
10556
+ if (!block.captionEnabled || block.caption.trim().length === 0) {
10557
+ return {};
10558
+ }
10559
+ return {
10560
+ caption: block.caption.trim(),
10561
+ captionInlines: previewInlines(block.captionField, islands, output, equationSide, document2, previewOptions)
10562
+ };
10563
+ }
10468
10564
  function blockPreview(block, islands, output, equationSide, document2, previewOptions) {
10469
10565
  if (block.kind === "textBlock") {
10470
10566
  return textBlockPreview(block, islands, output, equationSide, document2, previewOptions);
@@ -10488,7 +10584,7 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
10488
10584
  kind: "table",
10489
10585
  id: block.id,
10490
10586
  rows: block.rows.map((row) => row.map((cell) => previewInlines(cell, islands, output, equationSide, document2, previewOptions))),
10491
- ...block.captionEnabled && block.caption.trim().length > 0 ? { caption: block.caption.trim() } : {},
10587
+ ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
10492
10588
  ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
10493
10589
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
10494
10590
  };
@@ -10502,7 +10598,7 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
10502
10598
  ...block.assetId !== void 0 ? { assetId: block.assetId } : {},
10503
10599
  options: block.options,
10504
10600
  centered: block.centered,
10505
- ...block.captionEnabled && block.caption.trim().length > 0 ? { caption: block.caption.trim() } : {},
10601
+ ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
10506
10602
  ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
10507
10603
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
10508
10604
  };
@@ -12283,6 +12379,7 @@ function FloatMetaEditor({
12283
12379
  centered,
12284
12380
  captionEnabled,
12285
12381
  caption,
12382
+ captionEditor,
12286
12383
  labelEnabled,
12287
12384
  label,
12288
12385
  labelPlaceholder,
@@ -12326,9 +12423,9 @@ function FloatMetaEditor({
12326
12423
  ),
12327
12424
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { children: messages.captionToggle })
12328
12425
  ] }),
12329
- captionEnabled ? /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("label", { className: "butex-document2-widget__refs-field", children: [
12426
+ captionEnabled ? /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "butex-document2-widget__refs-field", children: [
12330
12427
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { children: messages.floatCaption }),
12331
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
12428
+ captionEditor ?? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
12332
12429
  "input",
12333
12430
  {
12334
12431
  value: caption,
@@ -12736,9 +12833,12 @@ function BlockEditor({
12736
12833
  onDeleteMath,
12737
12834
  onOpenCite,
12738
12835
  onDeleteCite,
12836
+ onOpenRef,
12837
+ onDeleteRef,
12739
12838
  onFieldFocus,
12740
12839
  onMathFocus,
12741
12840
  onCiteFocus,
12841
+ onRefFocus,
12742
12842
  onFieldBlur,
12743
12843
  onImageSrcChange,
12744
12844
  onImageAssetChange,
@@ -12747,6 +12847,8 @@ function BlockEditor({
12747
12847
  onRequestImagePick,
12748
12848
  listImageAssets,
12749
12849
  renderImageBlockEditor,
12850
+ onFloatMetaChange,
12851
+ onParagraphCenteredChange,
12750
12852
  onAddListItem,
12751
12853
  onRemoveListItem,
12752
12854
  onManageReferences
@@ -12771,6 +12873,7 @@ function BlockEditor({
12771
12873
  {
12772
12874
  captionEnabled: block.captionEnabled,
12773
12875
  caption: block.caption,
12876
+ captionEditor: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(InlineField, { field: block.captionField, ...fieldProps }),
12774
12877
  labelEnabled: block.labelEnabled,
12775
12878
  label: block.label,
12776
12879
  labelPlaceholder: messages.tableLabelPlaceholder,
@@ -12809,6 +12912,7 @@ function BlockEditor({
12809
12912
  centered: block.centered,
12810
12913
  captionEnabled: block.captionEnabled,
12811
12914
  caption: block.caption,
12915
+ captionEditor: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(InlineField, { field: block.captionField, ...fieldProps }),
12812
12916
  labelEnabled: block.labelEnabled,
12813
12917
  label: block.label,
12814
12918
  labelPlaceholder: messages.figureLabelPlaceholder,
@@ -13734,16 +13838,21 @@ function PreviewBlock({
13734
13838
  const messages = document2Messages(uiLocale);
13735
13839
  const floatCaptionSeparator = uiLocale === "ar" ? ": " : ". ";
13736
13840
  const source = "metadata" in block ? block.metadata?.source : void 0;
13737
- function FloatCaption({ numberLabel, caption }) {
13738
- if (!numberLabel && !caption) {
13841
+ function FloatCaption({
13842
+ numberLabel,
13843
+ caption,
13844
+ captionInlines
13845
+ }) {
13846
+ const hasCaption = Boolean(captionInlines?.length) || Boolean(caption);
13847
+ if (!numberLabel && !hasCaption) {
13739
13848
  return null;
13740
13849
  }
13741
13850
  return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)("figcaption", { dir: documentDirection, children: [
13742
13851
  numberLabel ? /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)("span", { className: "butex-document2-widget__preview-float-number", children: [
13743
13852
  numberLabel,
13744
- caption ? floatCaptionSeparator : ""
13853
+ hasCaption ? floatCaptionSeparator : ""
13745
13854
  ] }) : null,
13746
- caption ?? ""
13855
+ captionInlines ? /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(PreviewInlines, { inlines: captionInlines, output }) : caption ?? ""
13747
13856
  ] });
13748
13857
  }
13749
13858
  if (block.kind === "basmala" || block.kind === "closing") {
@@ -13802,19 +13911,21 @@ function PreviewBlock({
13802
13911
  if (block.kind === "table") {
13803
13912
  return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)("figure", { className: "butex-document2-widget__preview-float", "data-butex-block-source": source, children: [
13804
13913
  /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("div", { className: "butex-document2-widget__preview-table-scroll", children: /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("table", { children: /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("tbody", { children: block.rows.map((row, rowIndex) => /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("tr", { children: row.map((cell, columnIndex) => /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("td", { children: /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(PreviewInlines, { inlines: cell, output }) }, columnIndex)) }, rowIndex)) }) }) }),
13805
- /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption })
13914
+ /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption, captionInlines: block.captionInlines })
13806
13915
  ] });
13807
13916
  }
13808
13917
  if (block.kind === "image") {
13809
- const src = resolveImageUrl ? resolveImageUrl({ assetId: block.assetId, value: block.src }) : block.src;
13918
+ const resolvedSrc = resolveImageUrl ? resolveImageUrl({ assetId: block.assetId, value: block.src }) : block.src;
13919
+ const src = resolvedSrc?.trim() ?? "";
13810
13920
  return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)(
13811
13921
  "figure",
13812
13922
  {
13813
13923
  className: `butex-document2-widget__preview-float${block.centered === false ? " butex-document2-widget__preview-float--start" : ""}`,
13814
13924
  "data-butex-block-source": source,
13925
+ "data-butex-image-pending": src.length === 0 ? "true" : void 0,
13815
13926
  children: [
13816
- /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("img", { src, alt: block.caption ?? "" }),
13817
- /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption })
13927
+ src.length > 0 ? /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("img", { src, alt: block.caption ?? "" }) : null,
13928
+ /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(FloatCaption, { numberLabel: block.numberLabel, caption: block.caption, captionInlines: block.captionInlines })
13818
13929
  ]
13819
13930
  }
13820
13931
  );
@@ -17881,7 +17992,7 @@ function LabelsPanel({
17881
17992
  digitForm,
17882
17993
  documentDirection
17883
17994
  });
17884
- const captionPreview = entry.kind === "eq" ? title : formatFloatCaptionPreview(title, entry.caption, uiLocale);
17995
+ const captionPreview2 = entry.kind === "eq" ? title : formatFloatCaptionPreview(title, entry.caption, uiLocale);
17885
17996
  const overrideKey = `${entry.kind}:${entry.ownerId}`;
17886
17997
  const displayedKey = keyOverrides[overrideKey] ?? entry.key;
17887
17998
  const entryKeyError = document2KeyError(displayedKey, {
@@ -17900,7 +18011,7 @@ function LabelsPanel({
17900
18011
  className: "butex-document2-widget__modal-preview",
17901
18012
  dir: uiLocaleDirection(uiLocale),
17902
18013
  "aria-label": messages.captionPreview,
17903
- children: captionPreview
18014
+ children: captionPreview2
17904
18015
  }
17905
18016
  ),
17906
18017
  /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)("div", { className: "butex-document2-widget__refs-grid", children: [
@@ -18362,6 +18473,9 @@ function findBlockIdForField(blocks, fieldId) {
18362
18473
  if (block.kind === "textBlock" && block.field.id === fieldId) {
18363
18474
  return block.id;
18364
18475
  }
18476
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
18477
+ return block.id;
18478
+ }
18365
18479
  if (block.kind === "list") {
18366
18480
  for (const item of block.items) {
18367
18481
  if (item.field.id === fieldId) {
@@ -20703,6 +20817,9 @@ function findInlineFieldById(document2, fieldId) {
20703
20817
  if (block.kind === "textBlock" && block.field.id === fieldId) {
20704
20818
  return block.field;
20705
20819
  }
20820
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
20821
+ return block.captionField;
20822
+ }
20706
20823
  if (block.kind === "list") {
20707
20824
  for (const item of block.items) {
20708
20825
  if (item.field.id === fieldId) {
@@ -20734,6 +20851,9 @@ function findInlineFieldContainingToken(document2, tokenId) {
20734
20851
  if (block.kind === "textBlock" && block.field.tokens.some((token) => token.id === tokenId)) {
20735
20852
  return block.field;
20736
20853
  }
20854
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.tokens.some((token) => token.id === tokenId)) {
20855
+ return block.captionField;
20856
+ }
20737
20857
  if (block.kind === "list") {
20738
20858
  for (const item of block.items) {
20739
20859
  if (item.field.tokens.some((token) => token.id === tokenId)) {
@@ -20808,6 +20928,9 @@ function blockHasField(block, fieldId) {
20808
20928
  if (block.kind === "textBlock") {
20809
20929
  return block.field.id === fieldId;
20810
20930
  }
20931
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
20932
+ return true;
20933
+ }
20811
20934
  if (block.kind === "list") {
20812
20935
  return block.items.some((item) => item.field.id === fieldId || item.blocks.some((child) => blockHasField(child, fieldId)));
20813
20936
  }
@@ -20820,6 +20943,9 @@ function blockHasToken(block, tokenId) {
20820
20943
  if (block.kind === "textBlock") {
20821
20944
  return block.field.tokens.some((token) => token.id === tokenId);
20822
20945
  }
20946
+ if ((block.kind === "image" || block.kind === "table") && block.captionField.tokens.some((token) => token.id === tokenId)) {
20947
+ return true;
20948
+ }
20823
20949
  if (block.kind === "list") {
20824
20950
  return block.items.some(
20825
20951
  (item) => item.field.tokens.some((token) => token.id === tokenId) || item.blocks.some((child) => blockHasToken(child, tokenId))
@@ -20866,6 +20992,10 @@ function findFormatTextToken(document2, focus) {
20866
20992
  }
20867
20993
  }
20868
20994
  } else if (block.kind === "table") {
20995
+ const captionToken = textTokenFromField(block.captionField);
20996
+ if (captionToken) {
20997
+ return captionToken;
20998
+ }
20869
20999
  for (const row of block.rows) {
20870
21000
  for (const cell of row) {
20871
21001
  const token = textTokenFromField(cell);
@@ -20874,6 +21004,11 @@ function findFormatTextToken(document2, focus) {
20874
21004
  }
20875
21005
  }
20876
21006
  }
21007
+ } else if (block.kind === "image") {
21008
+ const captionToken = textTokenFromField(block.captionField);
21009
+ if (captionToken) {
21010
+ return captionToken;
21011
+ }
20877
21012
  }
20878
21013
  }
20879
21014
  return null;
@@ -22170,9 +22305,112 @@ var ButexDocumentEditor23 = (0, import_react18.forwardRef)(
22170
22305
  }
22171
22306
  );
22172
22307
  ButexDocumentEditor23.displayName = "ButexDocumentEditor2";
22308
+
22309
+ // src/react-document2/ButexDocumentPreview.tsx
22310
+ var import_react19 = require("react");
22311
+ var import_jsx_runtime21 = require("react/jsx-runtime");
22312
+ function isRecord2(value) {
22313
+ return typeof value === "object" && value !== null;
22314
+ }
22315
+ function isDocument2Node2(value) {
22316
+ return isRecord2(value) && value.nodeType === "DocumentObject" && isRecord2(value.meta) && Array.isArray(value.references) && Array.isArray(value.blocks) && Array.isArray(value.diagnostics);
22317
+ }
22318
+ function hasRuntimeDocumentMarker(value) {
22319
+ return isRecord2(value) && value.nodeType === "DocumentObject";
22320
+ }
22321
+ function resolvePreviewDocument2(input, uiLocale = "ar") {
22322
+ if (isDocument2Node2(input)) {
22323
+ return { document: input, error: null };
22324
+ }
22325
+ const messages = document2Messages(uiLocale);
22326
+ if (hasRuntimeDocumentMarker(input)) {
22327
+ return {
22328
+ document: createEmptyDocument2(),
22329
+ error: `${messages.loadDocumentError}: ${messages.runtimePersistenceError}`
22330
+ };
22331
+ }
22332
+ try {
22333
+ return { document: fromDocumentJson2(input), error: null };
22334
+ } catch (error) {
22335
+ const detail = error instanceof Error ? error.message : String(error);
22336
+ return {
22337
+ document: createEmptyDocument2(),
22338
+ error: `${messages.loadDocumentError}: ${detail}`
22339
+ };
22340
+ }
22341
+ }
22342
+ function ButexDocumentPreview({
22343
+ document: document2,
22344
+ className,
22345
+ documentDirection = "rtl",
22346
+ uiLocale = "ar",
22347
+ digitForm,
22348
+ equationSide = "arabic",
22349
+ mathOutput = "svg",
22350
+ resolveImageUrl
22351
+ }) {
22352
+ const messages = document2Messages(uiLocale);
22353
+ const resolvedDigitForm = digitForm ?? (documentDirection === "rtl" ? "arabicIndic" : "western");
22354
+ const resolved = (0, import_react19.useMemo)(() => resolvePreviewDocument2(document2, uiLocale), [document2, uiLocale]);
22355
+ const previewState = (0, import_react19.useMemo)(() => {
22356
+ try {
22357
+ return {
22358
+ preview: document2Preview(resolved.document, mathOutput, equationSide, {
22359
+ documentDirection,
22360
+ digitForm: resolvedDigitForm,
22361
+ uiLocale
22362
+ }),
22363
+ error: resolved.error
22364
+ };
22365
+ } catch (error) {
22366
+ const detail = error instanceof Error ? error.message : String(error);
22367
+ return {
22368
+ preview: document2Preview(createEmptyDocument2(), mathOutput, equationSide, {
22369
+ documentDirection,
22370
+ digitForm: resolvedDigitForm,
22371
+ uiLocale
22372
+ }),
22373
+ error: `${messages.loadDocumentError}: ${detail}`
22374
+ };
22375
+ }
22376
+ }, [documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
22377
+ (0, import_react19.useEffect)(() => {
22378
+ injectBuTeXDocument2Styles();
22379
+ }, []);
22380
+ return /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
22381
+ "div",
22382
+ {
22383
+ className: ["butex-document2-widget", "butex-document2-preview", className].filter(Boolean).join(" "),
22384
+ dir: uiLocaleDirection(uiLocale),
22385
+ lang: uiLocale,
22386
+ children: /* @__PURE__ */ (0, import_jsx_runtime21.jsxs)("div", { className: "butex-document2-widget__shell", children: [
22387
+ previewState.error ? /* @__PURE__ */ (0, import_jsx_runtime21.jsx)("p", { className: "butex-document2-widget__error", children: previewState.error }) : null,
22388
+ /* @__PURE__ */ (0, import_jsx_runtime21.jsx)("div", { className: "butex-document2-widget__layout butex-document2-widget__layout--editor-closed butex-document2-widget__layout--preview-open", children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
22389
+ "section",
22390
+ {
22391
+ className: "butex-document2-widget__panel butex-document2-widget__preview-panel",
22392
+ "aria-label": messages.documentPreview,
22393
+ children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
22394
+ DocumentPreview,
22395
+ {
22396
+ blocks: previewState.preview.blocks,
22397
+ output: mathOutput,
22398
+ documentDirection,
22399
+ uiLocale,
22400
+ digitForm: resolvedDigitForm,
22401
+ resolveImageUrl
22402
+ }
22403
+ )
22404
+ }
22405
+ ) })
22406
+ ] })
22407
+ }
22408
+ );
22409
+ }
22173
22410
  // Annotate the CommonJS export names for ESM import in node:
22174
22411
  0 && (module.exports = {
22175
22412
  ButexDocumentEditor2,
22413
+ ButexDocumentPreview,
22176
22414
  DOCUMENT2_WIDGET_CSS,
22177
22415
  DocumentInsertToolbar,
22178
22416
  DocumentPreview,
@@ -22181,6 +22419,7 @@ ButexDocumentEditor23.displayName = "ButexDocumentEditor2";
22181
22419
  MathChip,
22182
22420
  MathIsland,
22183
22421
  injectBuTeXDocument2Styles,
22184
- resolveInitialDocument2
22422
+ resolveInitialDocument2,
22423
+ resolvePreviewDocument2
22185
22424
  });
22186
22425
  //# sourceMappingURL=react-document2.js.map