tiptap-markdown-react 0.7.4 → 0.8.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.
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,12 @@ 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"
589
594
  };
590
595
  const defaultColorPaletteLabels = {
591
596
  none: "None",
@@ -974,28 +979,36 @@ function insertMarkdown(editor, markdown, sources = []) {
974
979
  const prepared = enrichMarkdownCitations(markdown, sources);
975
980
  editor.chain().focus().insertContent(prepared, { contentType: "markdown" }).run();
976
981
  }
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";
982
+ const toolbar = "_toolbar_8cpy8_2";
983
+ const inner = "_inner_8cpy8_15";
984
+ const btn = "_btn_8cpy8_27";
985
+ const btnActive = "_btnActive_8cpy8_50";
986
+ const tableBubble = "_tableBubble_8cpy8_66";
987
+ const txtIcon = "_txtIcon_8cpy8_78";
988
+ const colorIcon = "_colorIcon_8cpy8_86";
989
+ const colorIconGlyph = "_colorIconGlyph_8cpy8_94";
990
+ const colorIconBar = "_colorIconBar_8cpy8_100";
991
+ const scriptTrigger = "_scriptTrigger_8cpy8_107";
992
+ const scriptTriggerGlyph = "_scriptTriggerGlyph_8cpy8_127";
993
+ const divider = "_divider_8cpy8_135";
994
+ const styleTrigger = "_styleTrigger_8cpy8_144";
995
+ const styleTriggerLabel = "_styleTriggerLabel_8cpy8_173";
996
+ const styleTriggerCaret = "_styleTriggerCaret_8cpy8_181";
997
+ const styleItem = "_styleItem_8cpy8_187";
998
+ const styleIcon = "_styleIcon_8cpy8_193";
999
+ const scriptGlyph = "_scriptGlyph_8cpy8_203";
1000
+ const menu = "_menu_8cpy8_215";
1001
+ const menuItem = "_menuItem_8cpy8_226";
1002
+ const menuItemSelected = "_menuItemSelected_8cpy8_252";
1003
+ const popoverContent$1 = "_popoverContent_8cpy8_266";
1004
+ const mathOverlay = "_mathOverlay_8cpy8_270";
1005
+ const mathPopover = "_mathPopover_8cpy8_280";
1006
+ const mathTitle = "_mathTitle_8cpy8_290";
1007
+ const mathTextarea = "_mathTextarea_8cpy8_297";
1008
+ const mathPreview = "_mathPreview_8cpy8_316";
1009
+ const mathActions = "_mathActions_8cpy8_326";
1010
+ const mathBtn = "_mathBtn_8cpy8_333";
1011
+ const mathBtnPrimary = "_mathBtnPrimary_8cpy8_348";
999
1012
  const styles$3 = {
1000
1013
  toolbar,
1001
1014
  inner,
@@ -1018,7 +1031,15 @@ const styles$3 = {
1018
1031
  menu,
1019
1032
  menuItem,
1020
1033
  menuItemSelected,
1021
- popoverContent: popoverContent$1
1034
+ popoverContent: popoverContent$1,
1035
+ mathOverlay,
1036
+ mathPopover,
1037
+ mathTitle,
1038
+ mathTextarea,
1039
+ mathPreview,
1040
+ mathActions,
1041
+ mathBtn,
1042
+ mathBtnPrimary
1022
1043
  };
1023
1044
  const palette = "_palette_x5lrq_2";
1024
1045
  const noneRow = "_noneRow_x5lrq_11";
@@ -1095,6 +1116,95 @@ function ColorPalette({ value, onPick, labels }) {
1095
1116
  )) })
1096
1117
  ] });
1097
1118
  }
1119
+ function MathEditorPopover({
1120
+ kind,
1121
+ latex,
1122
+ title,
1123
+ placeholder,
1124
+ doneLabel,
1125
+ cancelLabel,
1126
+ onConfirm,
1127
+ onCancel
1128
+ }) {
1129
+ const [draft, setDraft] = useState(latex);
1130
+ const titleId = useId();
1131
+ useEffect(() => {
1132
+ setDraft(latex);
1133
+ }, [latex]);
1134
+ useEffect(() => {
1135
+ const onKey = (e) => {
1136
+ if (e.key === "Escape") onCancel();
1137
+ };
1138
+ document.addEventListener("keydown", onKey, true);
1139
+ return () => document.removeEventListener("keydown", onKey, true);
1140
+ }, [onCancel]);
1141
+ const preview = renderMathHtml(draft.trim(), kind === "block");
1142
+ const canSubmit = draft.trim().length > 0;
1143
+ return createPortal(
1144
+ /* @__PURE__ */ jsx(
1145
+ "div",
1146
+ {
1147
+ className: styles$3.mathOverlay,
1148
+ onMouseDown: (e) => {
1149
+ if (e.target === e.currentTarget) onCancel();
1150
+ },
1151
+ children: /* @__PURE__ */ jsxs(
1152
+ "div",
1153
+ {
1154
+ className: styles$3.mathPopover,
1155
+ role: "dialog",
1156
+ "aria-modal": "true",
1157
+ "aria-labelledby": titleId,
1158
+ onMouseDown: (e) => e.stopPropagation(),
1159
+ children: [
1160
+ /* @__PURE__ */ jsx("div", { id: titleId, className: styles$3.mathTitle, children: title }),
1161
+ /* @__PURE__ */ jsx(
1162
+ "textarea",
1163
+ {
1164
+ className: styles$3.mathTextarea,
1165
+ value: draft,
1166
+ placeholder,
1167
+ autoFocus: true,
1168
+ rows: kind === "block" ? 5 : 3,
1169
+ onChange: (e) => setDraft(e.target.value)
1170
+ }
1171
+ ),
1172
+ /* @__PURE__ */ jsx(
1173
+ "div",
1174
+ {
1175
+ className: styles$3.mathPreview,
1176
+ dangerouslySetInnerHTML: { __html: preview }
1177
+ }
1178
+ ),
1179
+ /* @__PURE__ */ jsxs("div", { className: styles$3.mathActions, children: [
1180
+ /* @__PURE__ */ jsx(
1181
+ "button",
1182
+ {
1183
+ type: "button",
1184
+ className: styles$3.mathBtn,
1185
+ onClick: onCancel,
1186
+ children: cancelLabel
1187
+ }
1188
+ ),
1189
+ /* @__PURE__ */ jsx(
1190
+ "button",
1191
+ {
1192
+ type: "button",
1193
+ className: `${styles$3.mathBtn} ${styles$3.mathBtnPrimary}`,
1194
+ disabled: !canSubmit,
1195
+ onClick: () => onConfirm(draft.trim()),
1196
+ children: doneLabel
1197
+ }
1198
+ )
1199
+ ] })
1200
+ ]
1201
+ }
1202
+ )
1203
+ }
1204
+ ),
1205
+ document.body
1206
+ );
1207
+ }
1098
1208
  function ToolbarButton({
1099
1209
  title,
1100
1210
  active: active2,
@@ -1369,6 +1479,38 @@ function EditorToolbar({
1369
1479
  else chain().unsetFontSize().run();
1370
1480
  };
1371
1481
  const canEditFontSize = editorFocused && state.isParagraph;
1482
+ const [mathEdit, setMathEdit] = useState(null);
1483
+ useEffect(() => subscribeMathClick((payload) => {
1484
+ setMathEdit({
1485
+ kind: payload.kind,
1486
+ mode: "edit",
1487
+ pos: payload.pos,
1488
+ latex: payload.latex
1489
+ });
1490
+ }), []);
1491
+ const openInsertMath = (kind) => {
1492
+ const { from, to } = editor.state.selection;
1493
+ const selected = from === to ? "" : editor.state.doc.textBetween(from, to, " ");
1494
+ setMathEdit({ kind, mode: "insert", latex: selected });
1495
+ };
1496
+ const applyMath = (latex) => {
1497
+ if (!mathEdit) return;
1498
+ const { kind, mode, pos } = mathEdit;
1499
+ setMathEdit(null);
1500
+ if (mode === "edit" && pos != null) {
1501
+ if (kind === "inline") {
1502
+ chain().updateInlineMath({ latex, pos }).run();
1503
+ } else {
1504
+ chain().updateBlockMath({ latex, pos }).run();
1505
+ }
1506
+ return;
1507
+ }
1508
+ if (kind === "inline") {
1509
+ chain().insertContent({ type: "inlineMath", attrs: { latex } }).run();
1510
+ } else {
1511
+ chain().insertContent({ type: "blockMath", attrs: { latex } }).run();
1512
+ }
1513
+ };
1372
1514
  const scriptActive = state.superscript ? "superscript" : state.subscript ? "subscript" : "normal";
1373
1515
  return /* @__PURE__ */ jsxs(Fragment$1, { children: [
1374
1516
  /* @__PURE__ */ jsx(
@@ -1685,6 +1827,14 @@ function EditorToolbar({
1685
1827
  /* @__PURE__ */ jsx("span", { className: styles$3.styleIcon, children: "{ }" }),
1686
1828
  t.codeBlock
1687
1829
  ] }) }),
1830
+ /* @__PURE__ */ jsx(MenuItem, { onSelect: () => openInsertMath("inline"), children: /* @__PURE__ */ jsxs("span", { className: styles$3.styleItem, children: [
1831
+ /* @__PURE__ */ jsx("span", { className: styles$3.styleIcon, children: "√x" }),
1832
+ t.inlineMath
1833
+ ] }) }),
1834
+ /* @__PURE__ */ jsx(MenuItem, { onSelect: () => openInsertMath("block"), children: /* @__PURE__ */ jsxs("span", { className: styles$3.styleItem, children: [
1835
+ /* @__PURE__ */ jsx("span", { className: styles$3.styleIcon, children: "∑" }),
1836
+ t.blockMath
1837
+ ] }) }),
1688
1838
  /* @__PURE__ */ jsx(MenuItem, { onSelect: () => chain().setHorizontalRule().run(), children: /* @__PURE__ */ jsxs("span", { className: styles$3.styleItem, children: [
1689
1839
  /* @__PURE__ */ jsx("span", { className: styles$3.styleIcon, children: "—" }),
1690
1840
  t.hr
@@ -1735,6 +1885,19 @@ function EditorToolbar({
1735
1885
  onChange: handleMdFileChange
1736
1886
  }
1737
1887
  ),
1888
+ mathEdit ? /* @__PURE__ */ jsx(
1889
+ MathEditorPopover,
1890
+ {
1891
+ kind: mathEdit.kind,
1892
+ latex: mathEdit.latex,
1893
+ title: mathEdit.kind === "inline" ? t.inlineMath : t.blockMath,
1894
+ placeholder: t.mathPlaceholder,
1895
+ doneLabel: t.mathDone,
1896
+ cancelLabel: t.mathCancel,
1897
+ onConfirm: applyMath,
1898
+ onCancel: () => setMathEdit(null)
1899
+ }
1900
+ ) : null,
1738
1901
  tableMenu && createPortal(
1739
1902
  /* @__PURE__ */ jsxs(
1740
1903
  "div",
@@ -2154,10 +2317,10 @@ export {
2154
2317
  TrashIcon,
2155
2318
  UnderlineIcon,
2156
2319
  UndoIcon,
2157
- g as applyCitationSources,
2320
+ h as applyCitationSources,
2158
2321
  applyCommentAnchorsToEditor,
2159
2322
  baseExtensions,
2160
- h as blockTextHash,
2323
+ i as blockTextHash,
2161
2324
  collectCommentIds,
2162
2325
  commentAnchorExtension,
2163
2326
  commentAnchorPlugin,
@@ -2170,7 +2333,7 @@ export {
2170
2333
  defaultTocLabels,
2171
2334
  defaultToolbarLabels,
2172
2335
  enrichMarkdownCitations,
2173
- i as extractToc,
2336
+ j as extractToc,
2174
2337
  findCitationRefElement,
2175
2338
  focusComment,
2176
2339
  insertMarkdown,