tiptap-markdown-react 0.8.1 → 0.8.3

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";
@@ -981,42 +981,41 @@ function insertMarkdown(editor, markdown, sources = []) {
981
981
  const prepared = enrichMarkdownCitations(markdown, sources);
982
982
  editor.chain().focus().insertContent(prepared, { contentType: "markdown" }).run();
983
983
  }
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";
984
+ const toolbar = "_toolbar_w3tos_2";
985
+ const inner = "_inner_w3tos_15";
986
+ const btn = "_btn_w3tos_27";
987
+ const btnActive = "_btnActive_w3tos_50";
988
+ const tableBubble = "_tableBubble_w3tos_66";
989
+ const txtIcon = "_txtIcon_w3tos_78";
990
+ const colorIcon = "_colorIcon_w3tos_86";
991
+ const colorIconGlyph = "_colorIconGlyph_w3tos_94";
992
+ const colorIconBar = "_colorIconBar_w3tos_100";
993
+ const scriptTrigger = "_scriptTrigger_w3tos_107";
994
+ const scriptTriggerGlyph = "_scriptTriggerGlyph_w3tos_127";
995
+ const divider = "_divider_w3tos_135";
996
+ const styleTrigger = "_styleTrigger_w3tos_144";
997
+ const styleTriggerLabel = "_styleTriggerLabel_w3tos_173";
998
+ const styleTriggerCaret = "_styleTriggerCaret_w3tos_181";
999
+ const styleItem = "_styleItem_w3tos_187";
1000
+ const styleIcon = "_styleIcon_w3tos_193";
1001
+ const scriptGlyph = "_scriptGlyph_w3tos_203";
1002
+ const menu = "_menu_w3tos_215";
1003
+ const menuItem = "_menuItem_w3tos_226";
1004
+ const menuItemSelected = "_menuItemSelected_w3tos_252";
1005
+ const popoverContent$1 = "_popoverContent_w3tos_266";
1006
+ const mathLayer = "_mathLayer_w3tos_270";
1007
+ const mathStack = "_mathStack_w3tos_275";
1008
+ const mathStackBlock = "_mathStackBlock_w3tos_282";
1009
+ const mathHint = "_mathHint_w3tos_287";
1010
+ const mathHintBlock = "_mathHintBlock_w3tos_299";
1011
+ const mathHintIcon = "_mathHintIcon_w3tos_305";
1012
+ const mathPreviewInline = "_mathPreviewInline_w3tos_311";
1013
+ const mathPreviewBlock = "_mathPreviewBlock_w3tos_337";
1014
+ const mathField = "_mathField_w3tos_367";
1015
+ const mathFieldBlock = "_mathFieldBlock_w3tos_379";
1016
+ const mathInput = "_mathInput_w3tos_386";
1017
+ const mathDone = "_mathDone_w3tos_404";
1018
+ const mathDoneKbd = "_mathDoneKbd_w3tos_429";
1020
1019
  const styles$3 = {
1021
1020
  toolbar,
1022
1021
  inner,
@@ -1040,8 +1039,7 @@ const styles$3 = {
1040
1039
  menuItem,
1041
1040
  menuItemSelected,
1042
1041
  popoverContent: popoverContent$1,
1043
- mathAnchor,
1044
- mathContent,
1042
+ mathLayer,
1045
1043
  mathStack,
1046
1044
  mathStackBlock,
1047
1045
  mathHint,
@@ -1130,24 +1128,52 @@ function ColorPalette({ value, onPick, labels }) {
1130
1128
  )) })
1131
1129
  ] });
1132
1130
  }
1133
- function getMathAnchorRect(editor, kind, pos) {
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 getMathPreviewFontSize(editor, pos) {
1138
+ const el = pos != null ? resolveNodeEl(editor, pos) : null;
1139
+ return getComputedStyle(el ?? editor.view.dom).fontSize;
1140
+ }
1141
+ function getMathLayerStyle(editor, kind, mode, pos) {
1142
+ const host = editor.view.dom.parentElement;
1143
+ if (!host) return {};
1144
+ const hostRect = host.getBoundingClientRect();
1134
1145
  const editorRect = editor.view.dom.getBoundingClientRect();
1146
+ const gap = mode === "edit" ? 0 : 6;
1147
+ const blockWidth = editor.view.dom.clientWidth;
1135
1148
  if (pos != null) {
1136
- const dom = editor.view.nodeDOM(pos);
1137
- const el = dom instanceof HTMLElement ? dom : dom instanceof Node ? dom.parentElement : null;
1149
+ const el = resolveNodeEl(editor, pos);
1138
1150
  if (el) {
1139
1151
  const r = el.getBoundingClientRect();
1140
1152
  if (kind === "block") {
1141
- return new DOMRect(editorRect.left, r.top, editorRect.width, r.height);
1153
+ return {
1154
+ left: editorRect.left - hostRect.left,
1155
+ top: r.top - hostRect.top + gap,
1156
+ width: blockWidth
1157
+ };
1142
1158
  }
1143
- return r;
1159
+ return {
1160
+ left: r.left - hostRect.left,
1161
+ top: r.top - hostRect.top + gap
1162
+ };
1144
1163
  }
1145
1164
  }
1146
1165
  const coords = editor.view.coordsAtPos(editor.state.selection.from);
1147
1166
  if (kind === "block") {
1148
- return new DOMRect(editorRect.left, coords.bottom, editorRect.width, 0);
1167
+ return {
1168
+ left: editorRect.left - hostRect.left,
1169
+ top: coords.bottom - hostRect.top + gap,
1170
+ width: blockWidth
1171
+ };
1149
1172
  }
1150
- return new DOMRect(coords.left, coords.bottom, 0, 0);
1173
+ return {
1174
+ left: coords.left - hostRect.left,
1175
+ top: coords.bottom - hostRect.top + gap
1176
+ };
1151
1177
  }
1152
1178
  function MathEditorPopover({
1153
1179
  editor,
@@ -1162,38 +1188,53 @@ function MathEditorPopover({
1162
1188
  onCancel
1163
1189
  }) {
1164
1190
  const [draft, setDraft] = useState(latex);
1165
- const [rect, setRect] = useState(() => getMathAnchorRect(editor, kind, pos));
1191
+ const [layerStyle, setLayerStyle] = useState(
1192
+ () => getMathLayerStyle(editor, kind, mode, pos)
1193
+ );
1194
+ const [previewFontSize, setPreviewFontSize] = useState(
1195
+ () => getMathPreviewFontSize(editor, pos)
1196
+ );
1166
1197
  const titleId = useId();
1167
1198
  const inputRef = useRef(null);
1168
- const confirmedRef = useRef(false);
1199
+ const rootRef = useRef(null);
1169
1200
  const isBlock = kind === "block";
1201
+ const host = editor.view.dom.parentElement;
1170
1202
  useEffect(() => {
1171
1203
  setDraft(latex);
1172
1204
  }, [latex]);
1173
1205
  useEffect(() => {
1174
- const update = () => setRect(getMathAnchorRect(editor, kind, pos));
1206
+ const update = () => {
1207
+ setLayerStyle(getMathLayerStyle(editor, kind, mode, pos));
1208
+ setPreviewFontSize(getMathPreviewFontSize(editor, pos));
1209
+ };
1175
1210
  update();
1176
1211
  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]);
1212
+ return () => window.removeEventListener("resize", update);
1213
+ }, [editor, kind, mode, pos]);
1183
1214
  useEffect(() => {
1184
1215
  var _a;
1185
1216
  (_a = inputRef.current) == null ? void 0 : _a.focus();
1186
1217
  }, []);
1187
1218
  useEffect(() => {
1188
1219
  if (pos == null) return;
1189
- const dom = editor.view.nodeDOM(pos);
1190
- const el = dom instanceof HTMLElement ? dom : dom instanceof Node ? dom.parentElement : null;
1220
+ const el = resolveNodeEl(editor, pos);
1191
1221
  if (!el) return;
1192
1222
  el.classList.add("tmr-math-editing");
1193
1223
  return () => {
1194
1224
  el.classList.remove("tmr-math-editing");
1195
1225
  };
1196
1226
  }, [editor, pos]);
1227
+ useEffect(() => {
1228
+ const onPtr = (e) => {
1229
+ var _a;
1230
+ const target = e.target;
1231
+ if (!(target instanceof Node)) return;
1232
+ if ((_a = rootRef.current) == null ? void 0 : _a.contains(target)) return;
1233
+ onCancel();
1234
+ };
1235
+ document.addEventListener("pointerdown", onPtr);
1236
+ return () => document.removeEventListener("pointerdown", onPtr);
1237
+ }, [onCancel]);
1197
1238
  const preview = useMemo(
1198
1239
  () => renderMathHtml(draft.trim(), isBlock),
1199
1240
  [draft, isBlock]
@@ -1202,7 +1243,6 @@ function MathEditorPopover({
1202
1243
  const showHint = !canSubmit && mode === "insert";
1203
1244
  const submit = () => {
1204
1245
  if (!canSubmit) return;
1205
- confirmedRef.current = true;
1206
1246
  onConfirm(draft.trim());
1207
1247
  };
1208
1248
  const onKeyDown = (e) => {
@@ -1240,88 +1280,70 @@ function MathEditorPopover({
1240
1280
  onKeyDown
1241
1281
  }
1242
1282
  );
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,
1283
+ if (!host) return null;
1284
+ return createPortal(
1285
+ /* @__PURE__ */ jsx(
1286
+ "div",
1287
+ {
1288
+ ref: rootRef,
1289
+ className: styles$3.mathLayer,
1290
+ style: layerStyle,
1291
+ role: "dialog",
1292
+ "aria-modal": "false",
1293
+ "aria-labelledby": titleId,
1294
+ children: /* @__PURE__ */ jsxs(
1295
+ "div",
1260
1296
  {
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
- )
1297
+ className: isBlock ? `${styles$3.mathStack} ${styles$3.mathStackBlock}` : styles$3.mathStack,
1298
+ children: [
1299
+ showHint ? /* @__PURE__ */ jsxs(
1300
+ "div",
1301
+ {
1302
+ id: titleId,
1303
+ className: isBlock ? `${styles$3.mathHint} ${styles$3.mathHintBlock}` : styles$3.mathHint,
1304
+ children: [
1305
+ /* @__PURE__ */ jsx("span", { className: styles$3.mathHintIcon, "aria-hidden": true, children: isBlock ? "TeX" : "√x" }),
1306
+ newLabel
1307
+ ]
1308
+ }
1309
+ ) : /* @__PURE__ */ jsx(
1310
+ "div",
1311
+ {
1312
+ id: titleId,
1313
+ className: isBlock ? styles$3.mathPreviewBlock : styles$3.mathPreviewInline,
1314
+ style: { fontSize: previewFontSize },
1315
+ dangerouslySetInnerHTML: { __html: preview }
1316
+ }
1317
+ ),
1318
+ /* @__PURE__ */ jsxs(
1319
+ "div",
1320
+ {
1321
+ className: isBlock ? `${styles$3.mathField} ${styles$3.mathFieldBlock}` : styles$3.mathField,
1322
+ children: [
1323
+ field,
1324
+ /* @__PURE__ */ jsxs(
1325
+ "button",
1326
+ {
1327
+ type: "button",
1328
+ className: styles$3.mathDone,
1329
+ disabled: !canSubmit,
1330
+ title: isBlock ? `${doneLabel} (⌘/Ctrl+Enter)` : `${doneLabel} (Enter)`,
1331
+ onClick: submit,
1332
+ children: [
1333
+ doneLabel,
1334
+ /* @__PURE__ */ jsx("span", { className: styles$3.mathDoneKbd, "aria-hidden": true, children: "↵" })
1335
+ ]
1336
+ }
1337
+ )
1338
+ ]
1339
+ }
1340
+ )
1341
+ ]
1321
1342
  }
1322
- ) })
1323
- ]
1324
- }
1343
+ )
1344
+ }
1345
+ ),
1346
+ host
1325
1347
  );
1326
1348
  }
1327
1349
  function ToolbarButton({