tiptap-markdown-react 0.8.0 → 0.8.2

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
@@ -6,8 +6,8 @@ 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
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";
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-CMvSKha8.js";
10
+ import { h, i, j } from "./extractToc-CMvSKha8.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";
@@ -590,7 +590,9 @@ const defaultToolbarLabels = {
590
590
  blockMath: "Block equation",
591
591
  mathPlaceholder: "E = mc^2",
592
592
  mathDone: "Done",
593
- mathCancel: "Cancel"
593
+ mathCancel: "Cancel",
594
+ mathNewInline: "New equation",
595
+ mathNewBlock: "Add a TeX equation"
594
596
  };
595
597
  const defaultColorPaletteLabels = {
596
598
  none: "None",
@@ -979,36 +981,41 @@ function insertMarkdown(editor, markdown, sources = []) {
979
981
  const prepared = enrichMarkdownCitations(markdown, sources);
980
982
  editor.chain().focus().insertContent(prepared, { contentType: "markdown" }).run();
981
983
  }
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";
984
+ const toolbar = "_toolbar_16b09_2";
985
+ const inner = "_inner_16b09_15";
986
+ const btn = "_btn_16b09_27";
987
+ const btnActive = "_btnActive_16b09_50";
988
+ const tableBubble = "_tableBubble_16b09_66";
989
+ const txtIcon = "_txtIcon_16b09_78";
990
+ const colorIcon = "_colorIcon_16b09_86";
991
+ const colorIconGlyph = "_colorIconGlyph_16b09_94";
992
+ const colorIconBar = "_colorIconBar_16b09_100";
993
+ const scriptTrigger = "_scriptTrigger_16b09_107";
994
+ const scriptTriggerGlyph = "_scriptTriggerGlyph_16b09_127";
995
+ const divider = "_divider_16b09_135";
996
+ const styleTrigger = "_styleTrigger_16b09_144";
997
+ const styleTriggerLabel = "_styleTriggerLabel_16b09_173";
998
+ const styleTriggerCaret = "_styleTriggerCaret_16b09_181";
999
+ const styleItem = "_styleItem_16b09_187";
1000
+ const styleIcon = "_styleIcon_16b09_193";
1001
+ const scriptGlyph = "_scriptGlyph_16b09_203";
1002
+ const menu = "_menu_16b09_215";
1003
+ const menuItem = "_menuItem_16b09_226";
1004
+ const menuItemSelected = "_menuItemSelected_16b09_252";
1005
+ const popoverContent$1 = "_popoverContent_16b09_266";
1006
+ const mathLayer = "_mathLayer_16b09_270";
1007
+ const mathStack = "_mathStack_16b09_275";
1008
+ const mathStackBlock = "_mathStackBlock_16b09_282";
1009
+ const mathHint = "_mathHint_16b09_287";
1010
+ const mathHintBlock = "_mathHintBlock_16b09_299";
1011
+ const mathHintIcon = "_mathHintIcon_16b09_305";
1012
+ const mathPreviewInline = "_mathPreviewInline_16b09_311";
1013
+ const mathPreviewBlock = "_mathPreviewBlock_16b09_321";
1014
+ const mathField = "_mathField_16b09_336";
1015
+ const mathFieldBlock = "_mathFieldBlock_16b09_348";
1016
+ const mathInput = "_mathInput_16b09_355";
1017
+ const mathDone = "_mathDone_16b09_373";
1018
+ const mathDoneKbd = "_mathDoneKbd_16b09_398";
1012
1019
  const styles$3 = {
1013
1020
  toolbar,
1014
1021
  inner,
@@ -1032,14 +1039,19 @@ const styles$3 = {
1032
1039
  menuItem,
1033
1040
  menuItemSelected,
1034
1041
  popoverContent: popoverContent$1,
1035
- mathOverlay,
1036
- mathPopover,
1037
- mathTitle,
1038
- mathTextarea,
1039
- mathPreview,
1040
- mathActions,
1041
- mathBtn,
1042
- mathBtnPrimary
1042
+ mathLayer,
1043
+ mathStack,
1044
+ mathStackBlock,
1045
+ mathHint,
1046
+ mathHintBlock,
1047
+ mathHintIcon,
1048
+ mathPreviewInline,
1049
+ mathPreviewBlock,
1050
+ mathField,
1051
+ mathFieldBlock,
1052
+ mathInput,
1053
+ mathDone,
1054
+ mathDoneKbd
1043
1055
  };
1044
1056
  const palette = "_palette_x5lrq_2";
1045
1057
  const noneRow = "_noneRow_x5lrq_11";
@@ -1116,93 +1128,211 @@ function ColorPalette({ value, onPick, labels }) {
1116
1128
  )) })
1117
1129
  ] });
1118
1130
  }
1131
+ function resolveNodeEl(editor, pos) {
1132
+ const dom = editor.view.nodeDOM(pos);
1133
+ if (dom instanceof HTMLElement) return dom;
1134
+ if (dom instanceof Node) return dom.parentElement;
1135
+ return null;
1136
+ }
1137
+ function getMathLayerStyle(editor, kind, mode, pos) {
1138
+ const host = editor.view.dom.parentElement;
1139
+ if (!host) return {};
1140
+ const hostRect = host.getBoundingClientRect();
1141
+ const editorRect = editor.view.dom.getBoundingClientRect();
1142
+ const gap = mode === "edit" ? 0 : 6;
1143
+ const blockWidth = editor.view.dom.clientWidth;
1144
+ if (pos != null) {
1145
+ const el = resolveNodeEl(editor, pos);
1146
+ if (el) {
1147
+ const r = el.getBoundingClientRect();
1148
+ if (kind === "block") {
1149
+ return {
1150
+ left: editorRect.left - hostRect.left,
1151
+ top: r.top - hostRect.top + gap,
1152
+ width: blockWidth
1153
+ };
1154
+ }
1155
+ return {
1156
+ left: r.left - hostRect.left,
1157
+ top: r.top - hostRect.top + gap
1158
+ };
1159
+ }
1160
+ }
1161
+ const coords = editor.view.coordsAtPos(editor.state.selection.from);
1162
+ if (kind === "block") {
1163
+ return {
1164
+ left: editorRect.left - hostRect.left,
1165
+ top: coords.bottom - hostRect.top + gap,
1166
+ width: blockWidth
1167
+ };
1168
+ }
1169
+ return {
1170
+ left: coords.left - hostRect.left,
1171
+ top: coords.bottom - hostRect.top + gap
1172
+ };
1173
+ }
1119
1174
  function MathEditorPopover({
1175
+ editor,
1120
1176
  kind,
1177
+ mode,
1178
+ pos,
1121
1179
  latex,
1122
- title,
1180
+ newLabel,
1123
1181
  placeholder,
1124
1182
  doneLabel,
1125
- cancelLabel,
1126
1183
  onConfirm,
1127
1184
  onCancel
1128
1185
  }) {
1129
1186
  const [draft, setDraft] = useState(latex);
1187
+ const [layerStyle, setLayerStyle] = useState(
1188
+ () => getMathLayerStyle(editor, kind, mode, pos)
1189
+ );
1130
1190
  const titleId = useId();
1191
+ const inputRef = useRef(null);
1192
+ const rootRef = useRef(null);
1193
+ const isBlock = kind === "block";
1194
+ const host = editor.view.dom.parentElement;
1131
1195
  useEffect(() => {
1132
1196
  setDraft(latex);
1133
1197
  }, [latex]);
1134
1198
  useEffect(() => {
1135
- const onKey = (e) => {
1136
- if (e.key === "Escape") onCancel();
1199
+ const update = () => setLayerStyle(getMathLayerStyle(editor, kind, mode, pos));
1200
+ update();
1201
+ window.addEventListener("resize", update);
1202
+ return () => window.removeEventListener("resize", update);
1203
+ }, [editor, kind, mode, pos]);
1204
+ useEffect(() => {
1205
+ var _a;
1206
+ (_a = inputRef.current) == null ? void 0 : _a.focus();
1207
+ }, []);
1208
+ useEffect(() => {
1209
+ if (pos == null) return;
1210
+ const el = resolveNodeEl(editor, pos);
1211
+ if (!el) return;
1212
+ el.classList.add("tmr-math-editing");
1213
+ return () => {
1214
+ el.classList.remove("tmr-math-editing");
1137
1215
  };
1138
- document.addEventListener("keydown", onKey, true);
1139
- return () => document.removeEventListener("keydown", onKey, true);
1216
+ }, [editor, pos]);
1217
+ useEffect(() => {
1218
+ const onPtr = (e) => {
1219
+ var _a;
1220
+ const target = e.target;
1221
+ if (!(target instanceof Node)) return;
1222
+ if ((_a = rootRef.current) == null ? void 0 : _a.contains(target)) return;
1223
+ onCancel();
1224
+ };
1225
+ document.addEventListener("pointerdown", onPtr);
1226
+ return () => document.removeEventListener("pointerdown", onPtr);
1140
1227
  }, [onCancel]);
1141
- const preview = renderMathHtml(draft.trim(), kind === "block");
1228
+ const preview = useMemo(
1229
+ () => renderMathHtml(draft.trim(), isBlock),
1230
+ [draft, isBlock]
1231
+ );
1142
1232
  const canSubmit = draft.trim().length > 0;
1233
+ const showHint = !canSubmit && mode === "insert";
1234
+ const submit = () => {
1235
+ if (!canSubmit) return;
1236
+ onConfirm(draft.trim());
1237
+ };
1238
+ const onKeyDown = (e) => {
1239
+ if (e.key === "Escape") {
1240
+ e.preventDefault();
1241
+ onCancel();
1242
+ return;
1243
+ }
1244
+ if (e.key !== "Enter") return;
1245
+ if (isBlock && !(e.metaKey || e.ctrlKey)) return;
1246
+ e.preventDefault();
1247
+ submit();
1248
+ };
1249
+ const field = isBlock ? /* @__PURE__ */ jsx(
1250
+ "textarea",
1251
+ {
1252
+ ref: inputRef,
1253
+ className: styles$3.mathInput,
1254
+ value: draft,
1255
+ placeholder,
1256
+ rows: Math.max(3, draft.split("\n").length),
1257
+ "aria-labelledby": titleId,
1258
+ onChange: (e) => setDraft(e.target.value),
1259
+ onKeyDown
1260
+ }
1261
+ ) : /* @__PURE__ */ jsx(
1262
+ "input",
1263
+ {
1264
+ ref: inputRef,
1265
+ className: styles$3.mathInput,
1266
+ value: draft,
1267
+ placeholder,
1268
+ "aria-labelledby": titleId,
1269
+ onChange: (e) => setDraft(e.target.value),
1270
+ onKeyDown
1271
+ }
1272
+ );
1273
+ if (!host) return null;
1143
1274
  return createPortal(
1144
1275
  /* @__PURE__ */ jsx(
1145
1276
  "div",
1146
1277
  {
1147
- className: styles$3.mathOverlay,
1148
- onMouseDown: (e) => {
1149
- if (e.target === e.currentTarget) onCancel();
1150
- },
1278
+ ref: rootRef,
1279
+ className: styles$3.mathLayer,
1280
+ style: layerStyle,
1281
+ role: "dialog",
1282
+ "aria-modal": "false",
1283
+ "aria-labelledby": titleId,
1151
1284
  children: /* @__PURE__ */ jsxs(
1152
1285
  "div",
1153
1286
  {
1154
- className: styles$3.mathPopover,
1155
- role: "dialog",
1156
- "aria-modal": "true",
1157
- "aria-labelledby": titleId,
1158
- onMouseDown: (e) => e.stopPropagation(),
1287
+ className: isBlock ? `${styles$3.mathStack} ${styles$3.mathStackBlock}` : styles$3.mathStack,
1159
1288
  children: [
1160
- /* @__PURE__ */ jsx("div", { id: titleId, className: styles$3.mathTitle, children: title }),
1161
- /* @__PURE__ */ jsx(
1162
- "textarea",
1289
+ showHint ? /* @__PURE__ */ jsxs(
1290
+ "div",
1163
1291
  {
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)
1292
+ id: titleId,
1293
+ className: isBlock ? `${styles$3.mathHint} ${styles$3.mathHintBlock}` : styles$3.mathHint,
1294
+ children: [
1295
+ /* @__PURE__ */ jsx("span", { className: styles$3.mathHintIcon, "aria-hidden": true, children: isBlock ? "TeX" : "√x" }),
1296
+ newLabel
1297
+ ]
1170
1298
  }
1171
- ),
1172
- /* @__PURE__ */ jsx(
1299
+ ) : /* @__PURE__ */ jsx(
1173
1300
  "div",
1174
1301
  {
1175
- className: styles$3.mathPreview,
1302
+ id: titleId,
1303
+ className: isBlock ? styles$3.mathPreviewBlock : styles$3.mathPreviewInline,
1176
1304
  dangerouslySetInnerHTML: { __html: preview }
1177
1305
  }
1178
1306
  ),
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
- ] })
1307
+ /* @__PURE__ */ jsxs(
1308
+ "div",
1309
+ {
1310
+ className: isBlock ? `${styles$3.mathField} ${styles$3.mathFieldBlock}` : styles$3.mathField,
1311
+ children: [
1312
+ field,
1313
+ /* @__PURE__ */ jsxs(
1314
+ "button",
1315
+ {
1316
+ type: "button",
1317
+ className: styles$3.mathDone,
1318
+ disabled: !canSubmit,
1319
+ title: isBlock ? `${doneLabel} (⌘/Ctrl+Enter)` : `${doneLabel} (Enter)`,
1320
+ onClick: submit,
1321
+ children: [
1322
+ doneLabel,
1323
+ /* @__PURE__ */ jsx("span", { className: styles$3.mathDoneKbd, "aria-hidden": true, children: "↵" })
1324
+ ]
1325
+ }
1326
+ )
1327
+ ]
1328
+ }
1329
+ )
1200
1330
  ]
1201
1331
  }
1202
1332
  )
1203
1333
  }
1204
1334
  ),
1205
- document.body
1335
+ host
1206
1336
  );
1207
1337
  }
1208
1338
  function ToolbarButton({
@@ -1888,12 +2018,14 @@ function EditorToolbar({
1888
2018
  mathEdit ? /* @__PURE__ */ jsx(
1889
2019
  MathEditorPopover,
1890
2020
  {
2021
+ editor,
1891
2022
  kind: mathEdit.kind,
2023
+ mode: mathEdit.mode,
2024
+ pos: mathEdit.pos,
1892
2025
  latex: mathEdit.latex,
1893
- title: mathEdit.kind === "inline" ? t.inlineMath : t.blockMath,
2026
+ newLabel: mathEdit.kind === "inline" ? t.mathNewInline : t.mathNewBlock,
1894
2027
  placeholder: t.mathPlaceholder,
1895
2028
  doneLabel: t.mathDone,
1896
- cancelLabel: t.mathCancel,
1897
2029
  onConfirm: applyMath,
1898
2030
  onCancel: () => setMathEdit(null)
1899
2031
  }