tiptap-markdown-react 0.8.1 → 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";
@@ -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_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";
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,48 @@ 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 getMathLayerStyle(editor, kind, mode, pos) {
1138
+ const host = editor.view.dom.parentElement;
1139
+ if (!host) return {};
1140
+ const hostRect = host.getBoundingClientRect();
1134
1141
  const editorRect = editor.view.dom.getBoundingClientRect();
1142
+ const gap = mode === "edit" ? 0 : 6;
1143
+ const blockWidth = editor.view.dom.clientWidth;
1135
1144
  if (pos != null) {
1136
- const dom = editor.view.nodeDOM(pos);
1137
- const el = dom instanceof HTMLElement ? dom : dom instanceof Node ? dom.parentElement : null;
1145
+ const el = resolveNodeEl(editor, pos);
1138
1146
  if (el) {
1139
1147
  const r = el.getBoundingClientRect();
1140
1148
  if (kind === "block") {
1141
- return new DOMRect(editorRect.left, r.top, editorRect.width, r.height);
1149
+ return {
1150
+ left: editorRect.left - hostRect.left,
1151
+ top: r.top - hostRect.top + gap,
1152
+ width: blockWidth
1153
+ };
1142
1154
  }
1143
- return r;
1155
+ return {
1156
+ left: r.left - hostRect.left,
1157
+ top: r.top - hostRect.top + gap
1158
+ };
1144
1159
  }
1145
1160
  }
1146
1161
  const coords = editor.view.coordsAtPos(editor.state.selection.from);
1147
1162
  if (kind === "block") {
1148
- return new DOMRect(editorRect.left, coords.bottom, editorRect.width, 0);
1163
+ return {
1164
+ left: editorRect.left - hostRect.left,
1165
+ top: coords.bottom - hostRect.top + gap,
1166
+ width: blockWidth
1167
+ };
1149
1168
  }
1150
- return new DOMRect(coords.left, coords.bottom, 0, 0);
1169
+ return {
1170
+ left: coords.left - hostRect.left,
1171
+ top: coords.bottom - hostRect.top + gap
1172
+ };
1151
1173
  }
1152
1174
  function MathEditorPopover({
1153
1175
  editor,
@@ -1162,38 +1184,47 @@ function MathEditorPopover({
1162
1184
  onCancel
1163
1185
  }) {
1164
1186
  const [draft, setDraft] = useState(latex);
1165
- const [rect, setRect] = useState(() => getMathAnchorRect(editor, kind, pos));
1187
+ const [layerStyle, setLayerStyle] = useState(
1188
+ () => getMathLayerStyle(editor, kind, mode, pos)
1189
+ );
1166
1190
  const titleId = useId();
1167
1191
  const inputRef = useRef(null);
1168
- const confirmedRef = useRef(false);
1192
+ const rootRef = useRef(null);
1169
1193
  const isBlock = kind === "block";
1194
+ const host = editor.view.dom.parentElement;
1170
1195
  useEffect(() => {
1171
1196
  setDraft(latex);
1172
1197
  }, [latex]);
1173
1198
  useEffect(() => {
1174
- const update = () => setRect(getMathAnchorRect(editor, kind, pos));
1199
+ const update = () => setLayerStyle(getMathLayerStyle(editor, kind, mode, pos));
1175
1200
  update();
1176
1201
  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]);
1202
+ return () => window.removeEventListener("resize", update);
1203
+ }, [editor, kind, mode, pos]);
1183
1204
  useEffect(() => {
1184
1205
  var _a;
1185
1206
  (_a = inputRef.current) == null ? void 0 : _a.focus();
1186
1207
  }, []);
1187
1208
  useEffect(() => {
1188
1209
  if (pos == null) return;
1189
- const dom = editor.view.nodeDOM(pos);
1190
- const el = dom instanceof HTMLElement ? dom : dom instanceof Node ? dom.parentElement : null;
1210
+ const el = resolveNodeEl(editor, pos);
1191
1211
  if (!el) return;
1192
1212
  el.classList.add("tmr-math-editing");
1193
1213
  return () => {
1194
1214
  el.classList.remove("tmr-math-editing");
1195
1215
  };
1196
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);
1227
+ }, [onCancel]);
1197
1228
  const preview = useMemo(
1198
1229
  () => renderMathHtml(draft.trim(), isBlock),
1199
1230
  [draft, isBlock]
@@ -1202,7 +1233,6 @@ function MathEditorPopover({
1202
1233
  const showHint = !canSubmit && mode === "insert";
1203
1234
  const submit = () => {
1204
1235
  if (!canSubmit) return;
1205
- confirmedRef.current = true;
1206
1236
  onConfirm(draft.trim());
1207
1237
  };
1208
1238
  const onKeyDown = (e) => {
@@ -1240,88 +1270,69 @@ function MathEditorPopover({
1240
1270
  onKeyDown
1241
1271
  }
1242
1272
  );
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,
1273
+ if (!host) return null;
1274
+ return createPortal(
1275
+ /* @__PURE__ */ jsx(
1276
+ "div",
1277
+ {
1278
+ ref: rootRef,
1279
+ className: styles$3.mathLayer,
1280
+ style: layerStyle,
1281
+ role: "dialog",
1282
+ "aria-modal": "false",
1283
+ "aria-labelledby": titleId,
1284
+ children: /* @__PURE__ */ jsxs(
1285
+ "div",
1260
1286
  {
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
- )
1287
+ className: isBlock ? `${styles$3.mathStack} ${styles$3.mathStackBlock}` : styles$3.mathStack,
1288
+ children: [
1289
+ showHint ? /* @__PURE__ */ jsxs(
1290
+ "div",
1291
+ {
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
+ ]
1298
+ }
1299
+ ) : /* @__PURE__ */ jsx(
1300
+ "div",
1301
+ {
1302
+ id: titleId,
1303
+ className: isBlock ? styles$3.mathPreviewBlock : styles$3.mathPreviewInline,
1304
+ dangerouslySetInnerHTML: { __html: preview }
1305
+ }
1306
+ ),
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
+ )
1330
+ ]
1321
1331
  }
1322
- ) })
1323
- ]
1324
- }
1332
+ )
1333
+ }
1334
+ ),
1335
+ host
1325
1336
  );
1326
1337
  }
1327
1338
  function ToolbarButton({