tiptap-markdown-react 0.7.5 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -5,9 +5,9 @@ import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
5
5
  import Image from "@tiptap/extension-image";
6
6
  import { TableOfContents } from "@tiptap/extension-table-of-contents";
7
7
  import { NodeViewWrapper, ReactNodeViewRenderer, NodeViewContent, useEditor, EditorContent, useEditorState } from "@tiptap/react";
8
- import { forwardRef, useEffect, useRef, useImperativeHandle, useMemo, useState, useCallback, useContext, createContext } from "react";
9
- import { C as CitationRef, m as mergeCommentIntervals, a as mapCommentAnchors, e as enrichMarkdownCitations, s as styles$5, b as baseExtensions, c as CommentMark, l as lowlight, d as makeTocGetId, p as pureCodeBlock, f as pureImage, R as ReportContent } from "./extractToc-DPWZ7Oby.js";
10
- import { g, h, i } from "./extractToc-DPWZ7Oby.js";
8
+ import { forwardRef, useEffect, useRef, useImperativeHandle, useMemo, useState, useId, useCallback, useContext, createContext } from "react";
9
+ import { C as CitationRef, m as mergeCommentIntervals, a as mapCommentAnchors, e as enrichMarkdownCitations, s as styles$5, b as baseExtensions, c as CommentMark, l as lowlight, d as makeTocGetId, p as pureCodeBlock, f as pureImage, r as renderMathHtml, g as subscribeMathClick, R as ReportContent } from "./extractToc-C0E_xYTj.js";
10
+ import { h, i, j } from "./extractToc-C0E_xYTj.js";
11
11
  import { Slice, Fragment } from "@tiptap/pm/model";
12
12
  import { Extension } from "@tiptap/core";
13
13
  import { PluginKey, Plugin } from "@tiptap/pm/state";
@@ -585,7 +585,14 @@ const defaultToolbarLabels = {
585
585
  tableDeleteColumn: "Delete column",
586
586
  tableAddRowBefore: "Add row before",
587
587
  tableAddRowAfter: "Add row after",
588
- tableDeleteRow: "Delete row"
588
+ tableDeleteRow: "Delete row",
589
+ inlineMath: "Inline equation",
590
+ blockMath: "Block equation",
591
+ mathPlaceholder: "E = mc^2",
592
+ mathDone: "Done",
593
+ mathCancel: "Cancel",
594
+ mathNewInline: "New equation",
595
+ mathNewBlock: "Add a TeX equation"
589
596
  };
590
597
  const defaultColorPaletteLabels = {
591
598
  none: "None",
@@ -974,28 +981,42 @@ function insertMarkdown(editor, markdown, sources = []) {
974
981
  const prepared = enrichMarkdownCitations(markdown, sources);
975
982
  editor.chain().focus().insertContent(prepared, { contentType: "markdown" }).run();
976
983
  }
977
- const toolbar = "_toolbar_i2emf_2";
978
- const inner = "_inner_i2emf_15";
979
- const btn = "_btn_i2emf_27";
980
- const btnActive = "_btnActive_i2emf_50";
981
- const tableBubble = "_tableBubble_i2emf_66";
982
- const txtIcon = "_txtIcon_i2emf_78";
983
- const colorIcon = "_colorIcon_i2emf_86";
984
- const colorIconGlyph = "_colorIconGlyph_i2emf_94";
985
- const colorIconBar = "_colorIconBar_i2emf_100";
986
- const scriptTrigger = "_scriptTrigger_i2emf_107";
987
- const scriptTriggerGlyph = "_scriptTriggerGlyph_i2emf_127";
988
- const divider = "_divider_i2emf_135";
989
- const styleTrigger = "_styleTrigger_i2emf_144";
990
- const styleTriggerLabel = "_styleTriggerLabel_i2emf_173";
991
- const styleTriggerCaret = "_styleTriggerCaret_i2emf_181";
992
- const styleItem = "_styleItem_i2emf_187";
993
- const styleIcon = "_styleIcon_i2emf_193";
994
- const scriptGlyph = "_scriptGlyph_i2emf_203";
995
- const menu = "_menu_i2emf_215";
996
- const menuItem = "_menuItem_i2emf_226";
997
- const menuItemSelected = "_menuItemSelected_i2emf_252";
998
- const popoverContent$1 = "_popoverContent_i2emf_266";
984
+ const toolbar = "_toolbar_z6tes_2";
985
+ const inner = "_inner_z6tes_15";
986
+ const btn = "_btn_z6tes_27";
987
+ const btnActive = "_btnActive_z6tes_50";
988
+ const tableBubble = "_tableBubble_z6tes_66";
989
+ const txtIcon = "_txtIcon_z6tes_78";
990
+ const colorIcon = "_colorIcon_z6tes_86";
991
+ const colorIconGlyph = "_colorIconGlyph_z6tes_94";
992
+ const colorIconBar = "_colorIconBar_z6tes_100";
993
+ const scriptTrigger = "_scriptTrigger_z6tes_107";
994
+ const scriptTriggerGlyph = "_scriptTriggerGlyph_z6tes_127";
995
+ const divider = "_divider_z6tes_135";
996
+ const styleTrigger = "_styleTrigger_z6tes_144";
997
+ const styleTriggerLabel = "_styleTriggerLabel_z6tes_173";
998
+ const styleTriggerCaret = "_styleTriggerCaret_z6tes_181";
999
+ const styleItem = "_styleItem_z6tes_187";
1000
+ const styleIcon = "_styleIcon_z6tes_193";
1001
+ const scriptGlyph = "_scriptGlyph_z6tes_203";
1002
+ const menu = "_menu_z6tes_215";
1003
+ const menuItem = "_menuItem_z6tes_226";
1004
+ const menuItemSelected = "_menuItemSelected_z6tes_252";
1005
+ const popoverContent$1 = "_popoverContent_z6tes_266";
1006
+ const mathAnchor = "_mathAnchor_z6tes_270";
1007
+ const mathContent = "_mathContent_z6tes_277";
1008
+ const mathStack = "_mathStack_z6tes_286";
1009
+ const mathStackBlock = "_mathStackBlock_z6tes_293";
1010
+ const mathHint = "_mathHint_z6tes_298";
1011
+ const mathHintBlock = "_mathHintBlock_z6tes_310";
1012
+ const mathHintIcon = "_mathHintIcon_z6tes_316";
1013
+ const mathPreviewInline = "_mathPreviewInline_z6tes_322";
1014
+ const mathPreviewBlock = "_mathPreviewBlock_z6tes_332";
1015
+ const mathField = "_mathField_z6tes_347";
1016
+ const mathFieldBlock = "_mathFieldBlock_z6tes_359";
1017
+ const mathInput = "_mathInput_z6tes_366";
1018
+ const mathDone = "_mathDone_z6tes_384";
1019
+ const mathDoneKbd = "_mathDoneKbd_z6tes_409";
999
1020
  const styles$3 = {
1000
1021
  toolbar,
1001
1022
  inner,
@@ -1018,7 +1039,21 @@ const styles$3 = {
1018
1039
  menu,
1019
1040
  menuItem,
1020
1041
  menuItemSelected,
1021
- popoverContent: popoverContent$1
1042
+ popoverContent: popoverContent$1,
1043
+ mathAnchor,
1044
+ mathContent,
1045
+ mathStack,
1046
+ mathStackBlock,
1047
+ mathHint,
1048
+ mathHintBlock,
1049
+ mathHintIcon,
1050
+ mathPreviewInline,
1051
+ mathPreviewBlock,
1052
+ mathField,
1053
+ mathFieldBlock,
1054
+ mathInput,
1055
+ mathDone,
1056
+ mathDoneKbd
1022
1057
  };
1023
1058
  const palette = "_palette_x5lrq_2";
1024
1059
  const noneRow = "_noneRow_x5lrq_11";
@@ -1095,6 +1130,200 @@ function ColorPalette({ value, onPick, labels }) {
1095
1130
  )) })
1096
1131
  ] });
1097
1132
  }
1133
+ function getMathAnchorRect(editor, kind, pos) {
1134
+ const editorRect = editor.view.dom.getBoundingClientRect();
1135
+ if (pos != null) {
1136
+ const dom = editor.view.nodeDOM(pos);
1137
+ const el = dom instanceof HTMLElement ? dom : dom instanceof Node ? dom.parentElement : null;
1138
+ if (el) {
1139
+ const r = el.getBoundingClientRect();
1140
+ if (kind === "block") {
1141
+ return new DOMRect(editorRect.left, r.top, editorRect.width, r.height);
1142
+ }
1143
+ return r;
1144
+ }
1145
+ }
1146
+ const coords = editor.view.coordsAtPos(editor.state.selection.from);
1147
+ if (kind === "block") {
1148
+ return new DOMRect(editorRect.left, coords.bottom, editorRect.width, 0);
1149
+ }
1150
+ return new DOMRect(coords.left, coords.bottom, 0, 0);
1151
+ }
1152
+ function MathEditorPopover({
1153
+ editor,
1154
+ kind,
1155
+ mode,
1156
+ pos,
1157
+ latex,
1158
+ newLabel,
1159
+ placeholder,
1160
+ doneLabel,
1161
+ onConfirm,
1162
+ onCancel
1163
+ }) {
1164
+ const [draft, setDraft] = useState(latex);
1165
+ const [rect, setRect] = useState(() => getMathAnchorRect(editor, kind, pos));
1166
+ const titleId = useId();
1167
+ const inputRef = useRef(null);
1168
+ const confirmedRef = useRef(false);
1169
+ const isBlock = kind === "block";
1170
+ useEffect(() => {
1171
+ setDraft(latex);
1172
+ }, [latex]);
1173
+ useEffect(() => {
1174
+ const update = () => setRect(getMathAnchorRect(editor, kind, pos));
1175
+ update();
1176
+ window.addEventListener("resize", update);
1177
+ window.addEventListener("scroll", update, true);
1178
+ return () => {
1179
+ window.removeEventListener("resize", update);
1180
+ window.removeEventListener("scroll", update, true);
1181
+ };
1182
+ }, [editor, kind, pos]);
1183
+ useEffect(() => {
1184
+ var _a;
1185
+ (_a = inputRef.current) == null ? void 0 : _a.focus();
1186
+ }, []);
1187
+ useEffect(() => {
1188
+ if (pos == null) return;
1189
+ const dom = editor.view.nodeDOM(pos);
1190
+ const el = dom instanceof HTMLElement ? dom : dom instanceof Node ? dom.parentElement : null;
1191
+ if (!el) return;
1192
+ el.classList.add("tmr-math-editing");
1193
+ return () => {
1194
+ el.classList.remove("tmr-math-editing");
1195
+ };
1196
+ }, [editor, pos]);
1197
+ const preview = useMemo(
1198
+ () => renderMathHtml(draft.trim(), isBlock),
1199
+ [draft, isBlock]
1200
+ );
1201
+ const canSubmit = draft.trim().length > 0;
1202
+ const showHint = !canSubmit && mode === "insert";
1203
+ const submit = () => {
1204
+ if (!canSubmit) return;
1205
+ confirmedRef.current = true;
1206
+ onConfirm(draft.trim());
1207
+ };
1208
+ const onKeyDown = (e) => {
1209
+ if (e.key === "Escape") {
1210
+ e.preventDefault();
1211
+ onCancel();
1212
+ return;
1213
+ }
1214
+ if (e.key !== "Enter") return;
1215
+ if (isBlock && !(e.metaKey || e.ctrlKey)) return;
1216
+ e.preventDefault();
1217
+ submit();
1218
+ };
1219
+ const field = isBlock ? /* @__PURE__ */ jsx(
1220
+ "textarea",
1221
+ {
1222
+ ref: inputRef,
1223
+ className: styles$3.mathInput,
1224
+ value: draft,
1225
+ placeholder,
1226
+ rows: Math.max(3, draft.split("\n").length),
1227
+ "aria-labelledby": titleId,
1228
+ onChange: (e) => setDraft(e.target.value),
1229
+ onKeyDown
1230
+ }
1231
+ ) : /* @__PURE__ */ jsx(
1232
+ "input",
1233
+ {
1234
+ ref: inputRef,
1235
+ className: styles$3.mathInput,
1236
+ value: draft,
1237
+ placeholder,
1238
+ "aria-labelledby": titleId,
1239
+ onChange: (e) => setDraft(e.target.value),
1240
+ onKeyDown
1241
+ }
1242
+ );
1243
+ return /* @__PURE__ */ jsxs(
1244
+ Popover.Root,
1245
+ {
1246
+ open: true,
1247
+ onOpenChange: (open) => {
1248
+ if (!open && !confirmedRef.current) onCancel();
1249
+ },
1250
+ children: [
1251
+ /* @__PURE__ */ jsx(Popover.Anchor, { asChild: true, children: /* @__PURE__ */ jsx(
1252
+ "span",
1253
+ {
1254
+ className: styles$3.mathAnchor,
1255
+ style: { left: rect.left, top: rect.top }
1256
+ }
1257
+ ) }),
1258
+ /* @__PURE__ */ jsx(Popover.Portal, { children: /* @__PURE__ */ jsx(
1259
+ Popover.Content,
1260
+ {
1261
+ className: styles$3.mathContent,
1262
+ side: "bottom",
1263
+ align: "start",
1264
+ sideOffset: mode === "edit" ? 0 : 6,
1265
+ collisionPadding: 12,
1266
+ style: isBlock ? { width: Math.max(rect.width, 280) } : void 0,
1267
+ onOpenAutoFocus: (e) => e.preventDefault(),
1268
+ onCloseAutoFocus: (e) => e.preventDefault(),
1269
+ children: /* @__PURE__ */ jsxs(
1270
+ "div",
1271
+ {
1272
+ className: isBlock ? `${styles$3.mathStack} ${styles$3.mathStackBlock}` : styles$3.mathStack,
1273
+ role: "dialog",
1274
+ "aria-modal": "false",
1275
+ "aria-labelledby": titleId,
1276
+ children: [
1277
+ showHint ? /* @__PURE__ */ jsxs(
1278
+ "div",
1279
+ {
1280
+ id: titleId,
1281
+ className: isBlock ? `${styles$3.mathHint} ${styles$3.mathHintBlock}` : styles$3.mathHint,
1282
+ children: [
1283
+ /* @__PURE__ */ jsx("span", { className: styles$3.mathHintIcon, "aria-hidden": true, children: isBlock ? "TeX" : "√x" }),
1284
+ newLabel
1285
+ ]
1286
+ }
1287
+ ) : /* @__PURE__ */ jsx(
1288
+ "div",
1289
+ {
1290
+ id: titleId,
1291
+ className: isBlock ? styles$3.mathPreviewBlock : styles$3.mathPreviewInline,
1292
+ dangerouslySetInnerHTML: { __html: preview }
1293
+ }
1294
+ ),
1295
+ /* @__PURE__ */ jsxs(
1296
+ "div",
1297
+ {
1298
+ className: isBlock ? `${styles$3.mathField} ${styles$3.mathFieldBlock}` : styles$3.mathField,
1299
+ children: [
1300
+ field,
1301
+ /* @__PURE__ */ jsxs(
1302
+ "button",
1303
+ {
1304
+ type: "button",
1305
+ className: styles$3.mathDone,
1306
+ disabled: !canSubmit,
1307
+ title: isBlock ? `${doneLabel} (⌘/Ctrl+Enter)` : `${doneLabel} (Enter)`,
1308
+ onClick: submit,
1309
+ children: [
1310
+ doneLabel,
1311
+ /* @__PURE__ */ jsx("span", { className: styles$3.mathDoneKbd, "aria-hidden": true, children: "↵" })
1312
+ ]
1313
+ }
1314
+ )
1315
+ ]
1316
+ }
1317
+ )
1318
+ ]
1319
+ }
1320
+ )
1321
+ }
1322
+ ) })
1323
+ ]
1324
+ }
1325
+ );
1326
+ }
1098
1327
  function ToolbarButton({
1099
1328
  title,
1100
1329
  active: active2,
@@ -1369,6 +1598,38 @@ function EditorToolbar({
1369
1598
  else chain().unsetFontSize().run();
1370
1599
  };
1371
1600
  const canEditFontSize = editorFocused && state.isParagraph;
1601
+ const [mathEdit, setMathEdit] = useState(null);
1602
+ useEffect(() => subscribeMathClick((payload) => {
1603
+ setMathEdit({
1604
+ kind: payload.kind,
1605
+ mode: "edit",
1606
+ pos: payload.pos,
1607
+ latex: payload.latex
1608
+ });
1609
+ }), []);
1610
+ const openInsertMath = (kind) => {
1611
+ const { from, to } = editor.state.selection;
1612
+ const selected = from === to ? "" : editor.state.doc.textBetween(from, to, " ");
1613
+ setMathEdit({ kind, mode: "insert", latex: selected });
1614
+ };
1615
+ const applyMath = (latex) => {
1616
+ if (!mathEdit) return;
1617
+ const { kind, mode, pos } = mathEdit;
1618
+ setMathEdit(null);
1619
+ if (mode === "edit" && pos != null) {
1620
+ if (kind === "inline") {
1621
+ chain().updateInlineMath({ latex, pos }).run();
1622
+ } else {
1623
+ chain().updateBlockMath({ latex, pos }).run();
1624
+ }
1625
+ return;
1626
+ }
1627
+ if (kind === "inline") {
1628
+ chain().insertContent({ type: "inlineMath", attrs: { latex } }).run();
1629
+ } else {
1630
+ chain().insertContent({ type: "blockMath", attrs: { latex } }).run();
1631
+ }
1632
+ };
1372
1633
  const scriptActive = state.superscript ? "superscript" : state.subscript ? "subscript" : "normal";
1373
1634
  return /* @__PURE__ */ jsxs(Fragment$1, { children: [
1374
1635
  /* @__PURE__ */ jsx(
@@ -1685,6 +1946,14 @@ function EditorToolbar({
1685
1946
  /* @__PURE__ */ jsx("span", { className: styles$3.styleIcon, children: "{ }" }),
1686
1947
  t.codeBlock
1687
1948
  ] }) }),
1949
+ /* @__PURE__ */ jsx(MenuItem, { onSelect: () => openInsertMath("inline"), children: /* @__PURE__ */ jsxs("span", { className: styles$3.styleItem, children: [
1950
+ /* @__PURE__ */ jsx("span", { className: styles$3.styleIcon, children: "√x" }),
1951
+ t.inlineMath
1952
+ ] }) }),
1953
+ /* @__PURE__ */ jsx(MenuItem, { onSelect: () => openInsertMath("block"), children: /* @__PURE__ */ jsxs("span", { className: styles$3.styleItem, children: [
1954
+ /* @__PURE__ */ jsx("span", { className: styles$3.styleIcon, children: "∑" }),
1955
+ t.blockMath
1956
+ ] }) }),
1688
1957
  /* @__PURE__ */ jsx(MenuItem, { onSelect: () => chain().setHorizontalRule().run(), children: /* @__PURE__ */ jsxs("span", { className: styles$3.styleItem, children: [
1689
1958
  /* @__PURE__ */ jsx("span", { className: styles$3.styleIcon, children: "—" }),
1690
1959
  t.hr
@@ -1735,6 +2004,21 @@ function EditorToolbar({
1735
2004
  onChange: handleMdFileChange
1736
2005
  }
1737
2006
  ),
2007
+ mathEdit ? /* @__PURE__ */ jsx(
2008
+ MathEditorPopover,
2009
+ {
2010
+ editor,
2011
+ kind: mathEdit.kind,
2012
+ mode: mathEdit.mode,
2013
+ pos: mathEdit.pos,
2014
+ latex: mathEdit.latex,
2015
+ newLabel: mathEdit.kind === "inline" ? t.mathNewInline : t.mathNewBlock,
2016
+ placeholder: t.mathPlaceholder,
2017
+ doneLabel: t.mathDone,
2018
+ onConfirm: applyMath,
2019
+ onCancel: () => setMathEdit(null)
2020
+ }
2021
+ ) : null,
1738
2022
  tableMenu && createPortal(
1739
2023
  /* @__PURE__ */ jsxs(
1740
2024
  "div",
@@ -2154,10 +2438,10 @@ export {
2154
2438
  TrashIcon,
2155
2439
  UnderlineIcon,
2156
2440
  UndoIcon,
2157
- g as applyCitationSources,
2441
+ h as applyCitationSources,
2158
2442
  applyCommentAnchorsToEditor,
2159
2443
  baseExtensions,
2160
- h as blockTextHash,
2444
+ i as blockTextHash,
2161
2445
  collectCommentIds,
2162
2446
  commentAnchorExtension,
2163
2447
  commentAnchorPlugin,
@@ -2170,7 +2454,7 @@ export {
2170
2454
  defaultTocLabels,
2171
2455
  defaultToolbarLabels,
2172
2456
  enrichMarkdownCitations,
2173
- i as extractToc,
2457
+ j as extractToc,
2174
2458
  findCitationRefElement,
2175
2459
  focusComment,
2176
2460
  insertMarkdown,