@ctzhian/tiptap 2.6.1 → 2.7.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.
Files changed (30) hide show
  1. package/dist/Editor/demo.js +1 -1
  2. package/dist/EditorMarkdown/demo.js +1 -1
  3. package/dist/extension/component/TableExtendButton/index.d.ts +1 -3
  4. package/dist/extension/component/TableExtendButton/index.js +16 -7
  5. package/dist/extension/component/TableExtendButton/use-table-extend-row-column.d.ts +1 -3
  6. package/dist/extension/component/TableExtendButton/use-table-extend-row-column.js +5 -10
  7. package/dist/extension/component/TableHandle/TableHandleAddButton.js +19 -11
  8. package/dist/extension/component/TableHandle/TableHandleMenu.js +159 -83
  9. package/dist/extension/component/TableHandle/index.d.ts +2 -7
  10. package/dist/extension/component/TableHandle/index.js +1 -4
  11. package/dist/extension/component/TableHandle/use-table-handle-positioning.d.ts +2 -6
  12. package/dist/extension/component/TableHandle/use-table-handle-positioning.js +28 -55
  13. package/dist/extension/component/TableHandle/use-table-handle-state.d.ts +4 -13
  14. package/dist/extension/component/TableHandle/use-table-handle-state.js +1 -1
  15. package/dist/extension/component/UploadProgress/index.d.ts +1 -1
  16. package/dist/extension/index.js +2 -2
  17. package/dist/extension/node/Table.js +112 -151
  18. package/dist/extension/node/TableHandler/plugin.d.ts +5 -9
  19. package/dist/extension/node/TableHandler/plugin.js +177 -124
  20. package/dist/extension/node/TableOfContents/index.d.ts +4 -7
  21. package/dist/extension/node/TableOfContents/index.js +23 -419
  22. package/dist/util/table-utils.d.ts +28 -61
  23. package/dist/util/table-utils.js +157 -124
  24. package/package.json +32 -32
  25. package/dist/extension/node/TableOfContents/plugin.d.ts +0 -6
  26. package/dist/extension/node/TableOfContents/plugin.js +0 -58
  27. package/dist/extension/node/TableOfContents/types.d.ts +0 -45
  28. package/dist/extension/node/TableOfContents/types.js +0 -1
  29. package/dist/extension/node/TableOfContents/util.d.ts +0 -6
  30. package/dist/extension/node/TableOfContents/util.js +0 -70
@@ -1,25 +1,22 @@
1
1
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
2
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
3
3
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
5
4
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
6
5
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
7
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
8
6
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
9
7
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
10
8
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
11
9
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
12
10
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
11
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
12
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
13
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
13
14
  import { Selection } from '@tiptap/pm/state';
14
15
  import { cellAround, CellSelection, findTable, selectedRect, selectionCell, TableMap } from '@tiptap/pm/tables';
15
16
  import { Mapping } from '@tiptap/pm/transform';
16
17
  export var RESIZE_MIN_WIDTH = 35;
17
18
  export var EMPTY_CELL_WIDTH = 120;
18
19
  export var EMPTY_CELL_HEIGHT = 40;
19
- // ============================================================================
20
- // HELPER CONSTANTS & UTILITIES
21
- // ============================================================================
22
-
23
20
  var EMPTY_CELLS_RESULT = {
24
21
  cells: [],
25
22
  mergedCells: []
@@ -31,11 +28,6 @@ export function safeClosest(start, selector) {
31
28
  var _ref, _start$closest;
32
29
  return (_ref = start === null || start === void 0 || (_start$closest = start.closest) === null || _start$closest === void 0 ? void 0 : _start$closest.call(start, selector)) !== null && _ref !== void 0 ? _ref : null;
33
30
  }
34
-
35
- /**
36
- * Walk up from an element until we find a TD/TH or the table wrapper.
37
- * Returns the found element plus its tbody (if present).
38
- */
39
31
  export function domCellAround(target) {
40
32
  var current = target;
41
33
  while (current && current.tagName !== 'TD' && current.tagName !== 'TH' && !current.classList.contains('tableWrapper')) {
@@ -56,24 +48,12 @@ export function domCellAround(target) {
56
48
  tbodyNode: current.querySelector('tbody')
57
49
  };
58
50
  }
59
-
60
- /**
61
- * Clamps a value between min and max bounds
62
- */
63
51
  export function clamp(value, min, max) {
64
52
  return Math.max(min, Math.min(value, max));
65
53
  }
66
-
67
- /**
68
- * Validates if row/col indices are within table bounds
69
- */
70
54
  function isWithinBounds(row, col, map) {
71
55
  return row >= 0 && row < map.height && col >= 0 && col < map.width;
72
56
  }
73
-
74
- /**
75
- * Resolves the index for a row or column based on current selection or provided value
76
- */
77
57
  function resolveOrientationIndex(state, table, orientation, providedIndex) {
78
58
  var _cellAround;
79
59
  if (typeof providedIndex === 'number') {
@@ -89,10 +69,6 @@ function resolveOrientationIndex(state, table, orientation, providedIndex) {
89
69
  var rect = table.map.findCell(rel);
90
70
  return orientation === 'row' ? rect.top : rect.left;
91
71
  }
92
-
93
- /**
94
- * Creates a CellInfo object from position data
95
- */
96
72
  function createCellInfo(row, column, cellPos, cellNode) {
97
73
  return {
98
74
  row: row,
@@ -103,10 +79,6 @@ function createCellInfo(row, column, cellPos, cellNode) {
103
79
  depth: cellNode ? cellNode.content.size : 0
104
80
  };
105
81
  }
106
-
107
- /**
108
- * Checks if a cell is merged (has colspan or rowspan > 1)
109
- */
110
82
  export function isCellMerged(node) {
111
83
  var _node$attrs$colspan, _node$attrs$rowspan;
112
84
  if (!node) return false;
@@ -114,10 +86,35 @@ export function isCellMerged(node) {
114
86
  var rowspan = (_node$attrs$rowspan = node.attrs.rowspan) !== null && _node$attrs$rowspan !== void 0 ? _node$attrs$rowspan : 1;
115
87
  return colspan > 1 || rowspan > 1;
116
88
  }
117
-
118
- /**
119
- * Generic function to collect cells along a row or column
120
- */
89
+ export function getUniqueCellsWithRect(table) {
90
+ var seen = new Set();
91
+ var cells = [];
92
+ var map = table.map,
93
+ node = table.node,
94
+ start = table.start;
95
+ var _iterator = _createForOfIteratorHelper(map.map),
96
+ _step;
97
+ try {
98
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
99
+ var offset = _step.value;
100
+ if (seen.has(offset)) continue;
101
+ seen.add(offset);
102
+ var cellNode = node.nodeAt(offset);
103
+ if (!cellNode) continue;
104
+ var rect = map.findCell(offset);
105
+ cells.push({
106
+ pos: start + offset,
107
+ node: cellNode,
108
+ rect: rect
109
+ });
110
+ }
111
+ } catch (err) {
112
+ _iterator.e(err);
113
+ } finally {
114
+ _iterator.f();
115
+ }
116
+ return cells;
117
+ }
121
118
  function collectCells(editor, orientation, index, tablePos) {
122
119
  if (!editor) return EMPTY_CELLS_RESULT;
123
120
  var state = editor.state;
@@ -129,7 +126,7 @@ function collectCells(editor, orientation, index, tablePos) {
129
126
  var resolvedIndex = resolveOrientationIndex(state, table, orientation, index);
130
127
  if (resolvedIndex === null) return EMPTY_CELLS_RESULT;
131
128
 
132
- // Bounds check
129
+ // 边界检查
133
130
  var maxIndex = orientation === 'row' ? map.height : map.width;
134
131
  if (resolvedIndex < 0 || resolvedIndex >= maxIndex) {
135
132
  return EMPTY_CELLS_RESULT;
@@ -161,10 +158,6 @@ function collectCells(editor, orientation, index, tablePos) {
161
158
  mergedCells: mergedCells
162
159
  };
163
160
  }
164
-
165
- /**
166
- * Generic function to count empty cells from the end of a row or column
167
- */
168
161
  function countEmptyCellsFromEnd(editor, tablePos, orientation) {
169
162
  var table = getTable(editor, tablePos);
170
163
  if (!table) return 0;
@@ -193,10 +186,6 @@ function countEmptyCellsFromEnd(editor, tablePos, orientation) {
193
186
  }
194
187
  return emptyCount;
195
188
  }
196
-
197
- /**
198
- * Get information about the table at the current selection or a specific position.
199
- */
200
189
  export function getTable(editor, tablePos) {
201
190
  if (!editor) return null;
202
191
  var table = null;
@@ -223,10 +212,6 @@ export function getTable(editor, tablePos) {
223
212
  map: tableMap
224
213
  });
225
214
  }
226
-
227
- /**
228
- * Checks if the current text selection is inside a table cell.
229
- */
230
215
  export function isSelectionInCell(state) {
231
216
  var selection = state.selection;
232
217
  var $from = selection.$from;
@@ -238,10 +223,6 @@ export function isSelectionInCell(state) {
238
223
  }
239
224
  return false;
240
225
  }
241
-
242
- /**
243
- * Runs a function while preserving the editor's selection.
244
- */
245
226
  export function runPreservingCursor(editor, fn) {
246
227
  var view = editor.view;
247
228
  var startSel = view.state.selection;
@@ -262,8 +243,7 @@ export function runPreservingCursor(editor, fn) {
262
243
  view.dispatch(view.state.tr.setSelection(sel));
263
244
  return true;
264
245
  } catch (_unused) {
265
- // Fallback: if the exact spot vanished (e.g., cell deleted),
266
- // go to the nearest valid position.
246
+ // 若原位置失效(如单元格被删),则跳转到最近可用位置
267
247
  var mappedPos = mapping.map(startSel.from, -1);
268
248
  var clamped = clamp(mappedPos, 0, view.state.doc.content.size);
269
249
  var near = Selection.near(view.state.doc.resolve(clamped), -1);
@@ -271,10 +251,6 @@ export function runPreservingCursor(editor, fn) {
271
251
  return false;
272
252
  }
273
253
  }
274
-
275
- /**
276
- * Determines whether a table cell is effectively empty.
277
- */
278
254
  export function isCellEmpty(cellNode) {
279
255
  if (cellNode.childCount === 0) return true;
280
256
  var isEmpty = true;
@@ -292,24 +268,12 @@ export function isCellEmpty(cellNode) {
292
268
  });
293
269
  return isEmpty;
294
270
  }
295
-
296
- /**
297
- * Counts how many consecutive empty rows exist at the bottom of a given table.
298
- */
299
271
  export function countEmptyRowsFromEnd(editor, tablePos) {
300
272
  return countEmptyCellsFromEnd(editor, tablePos, 'row');
301
273
  }
302
-
303
- /**
304
- * Counts how many consecutive empty columns exist at the right edge of a given table.
305
- */
306
274
  export function countEmptyColumnsFromEnd(editor, tablePos) {
307
275
  return countEmptyCellsFromEnd(editor, tablePos, 'column');
308
276
  }
309
-
310
- /**
311
- * Rounds a number with a symmetric "dead-zone" around integer boundaries.
312
- */
313
277
  export function marginRound(num) {
314
278
  var margin = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0.3;
315
279
  var floor = Math.floor(num);
@@ -320,10 +284,6 @@ export function marginRound(num) {
320
284
  if (num > upperBound) return ceil;
321
285
  return Math.round(num);
322
286
  }
323
-
324
- /**
325
- * Applies the transaction based on the specified mode
326
- */
327
287
  function applySelectionWithMode(state, transaction, options) {
328
288
  var _options$mode;
329
289
  var mode = (_options$mode = options.mode) !== null && _options$mode !== void 0 ? _options$mode : 'state';
@@ -343,10 +303,6 @@ function applySelectionWithMode(state, transaction, options) {
343
303
  return state.apply(transaction);
344
304
  }
345
305
  }
346
-
347
- /**
348
- * Selects table cells by their (row, col) coordinates.
349
- */
350
306
  export function selectCellsByCoords(editor, tablePos, coords) {
351
307
  var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
352
308
  mode: 'state'
@@ -368,7 +324,7 @@ export function selectCellsByCoords(editor, tablePos, coords) {
368
324
  return;
369
325
  }
370
326
 
371
- // --- Find the smallest rectangle that contains all our coordinates ---
327
+ // 找出包含所有坐标的最小矩形
372
328
  var allRows = cleanedCoords.map(function (coord) {
373
329
  return coord.row;
374
330
  });
@@ -380,22 +336,22 @@ export function selectCellsByCoords(editor, tablePos, coords) {
380
336
  var leftCol = Math.min.apply(Math, _toConsumableArray(allCols));
381
337
  var rightCol = Math.max.apply(Math, _toConsumableArray(allCols));
382
338
 
383
- // --- Convert visual coordinates to document positions ---
339
+ // 将可视坐标转换为文档位置
384
340
  var getCellPositionFromMap = function getCellPositionFromMap(row, col) {
385
341
  var cellOffset = tableMap.map[row * tableMap.width + col];
386
342
  if (cellOffset === undefined) return null;
387
343
  return tablePos + 1 + cellOffset;
388
344
  };
389
345
 
390
- // Anchor = where the selection starts (top-left of bounding box)
346
+ // Anchor:选区起点(外接矩形左上)
391
347
  var anchorPosition = getCellPositionFromMap(topRow, leftCol);
392
348
  if (anchorPosition === null) return;
393
349
 
394
- // Head = where the selection ends (usually bottom-right of bounding box)
350
+ // Head:选区终点(通常是右下)
395
351
  var headPosition = getCellPositionFromMap(bottomRow, rightCol);
396
352
  if (headPosition === null) return;
397
353
 
398
- // --- Handle edge case with merged cells ---
354
+ // 处理合并单元格的特殊情况
399
355
  if (headPosition === anchorPosition) {
400
356
  var foundDifferentCell = false;
401
357
  for (var row = bottomRow; row >= topRow && !foundDifferentCell; row--) {
@@ -419,10 +375,6 @@ export function selectCellsByCoords(editor, tablePos, coords) {
419
375
  return;
420
376
  }
421
377
  }
422
-
423
- /**
424
- * Select the cell at (row, col) using `cellAround` to respect merged cells.
425
- */
426
378
  export function selectCellAt(_ref2) {
427
379
  var editor = _ref2.editor,
428
380
  row = _ref2.row,
@@ -435,7 +387,7 @@ export function selectCellAt(_ref2) {
435
387
  var found = getTable(editor, tablePos);
436
388
  if (!found) return false;
437
389
 
438
- // Bounds check
390
+ // 边界检查
439
391
  if (!isWithinBounds(row, col, found.map)) {
440
392
  return false;
441
393
  }
@@ -450,22 +402,18 @@ export function selectCellAt(_ref2) {
450
402
  doDispatch(state.tr.setSelection(sel));
451
403
  return true;
452
404
  }
453
-
454
- /**
455
- * Selects a boundary cell of the table based on orientation.
456
- */
457
405
  export function selectLastCell(editor, tableNode, tablePos, orientation) {
458
406
  var map = TableMap.get(tableNode);
459
407
  var isRow = orientation === 'row';
460
408
 
461
- // For rows, select bottom-left cell; for columns, select top-right cell
409
+ // 行选左下,列选右上
462
410
  var row = isRow ? map.height - 1 : 0;
463
411
  var col = isRow ? 0 : map.width - 1;
464
412
 
465
- // Calculate the index in the table map
413
+ // 计算索引
466
414
  var index = row * map.width + col;
467
415
 
468
- // Get the actual cell position from the map (handles merged cells)
416
+ // 获取真实单元格位置(兼容合并单元格)
469
417
  var cellPos = map.map[index];
470
418
  if (!cellPos && cellPos !== 0) {
471
419
  console.warn('selectLastCell: cell position not found in map', {
@@ -477,7 +425,7 @@ export function selectLastCell(editor, tableNode, tablePos, orientation) {
477
425
  return false;
478
426
  }
479
427
 
480
- // Find the row and column of the actual cell
428
+ // 找到实际的行列坐标
481
429
  var cellIndex = map.map.indexOf(cellPos);
482
430
  var actualRow = cellIndex >= 0 ? Math.floor(cellIndex / map.width) : 0;
483
431
  var actualCol = cellIndex >= 0 ? cellIndex % map.width : 0;
@@ -489,10 +437,6 @@ export function selectLastCell(editor, tableNode, tablePos, orientation) {
489
437
  dispatch: editor.view.dispatch.bind(editor.view)
490
438
  });
491
439
  }
492
-
493
- /**
494
- * Get all (row, col) coordinates for a given row or column index.
495
- */
496
440
  export function getIndexCoordinates(_ref3) {
497
441
  var editor = _ref3.editor,
498
442
  index = _ref3.index,
@@ -523,10 +467,70 @@ export function getIndexCoordinates(_ref3) {
523
467
  };
524
468
  });
525
469
  }
526
-
527
- /**
528
- * Given a DOM cell element, find its (row, col) indices within the table.
529
- */
470
+ export function getRowOriginCoords(_ref4) {
471
+ var editor = _ref4.editor,
472
+ rowIndex = _ref4.rowIndex,
473
+ tablePos = _ref4.tablePos,
474
+ _ref4$includeMerged = _ref4.includeMerged,
475
+ includeMerged = _ref4$includeMerged === void 0 ? false : _ref4$includeMerged;
476
+ if (!editor) return null;
477
+ var table = getTable(editor, tablePos);
478
+ if (!table) return null;
479
+ var coords = [];
480
+ var seen = new Set();
481
+ var map = table.map,
482
+ node = table.node;
483
+ for (var col = 0; col < map.width; col++) {
484
+ var cellIndex = rowIndex * map.width + col;
485
+ var offset = map.map[cellIndex];
486
+ if (offset === undefined || seen.has(offset)) continue;
487
+ seen.add(offset);
488
+ var rect = map.findCell(offset);
489
+ if (rect.top !== rowIndex) continue;
490
+ var cellNode = node.nodeAt(offset);
491
+ if (!cellNode) continue;
492
+ if (!includeMerged && isCellMerged(cellNode)) continue;
493
+ coords.push({
494
+ row: rect.top,
495
+ col: rect.left
496
+ });
497
+ }
498
+ return coords.length ? coords : null;
499
+ }
500
+ export function getRowOriginCells(editor, rowIndex, tablePos) {
501
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
502
+ if (!editor) return EMPTY_CELLS_RESULT;
503
+ var table = getTable(editor, tablePos);
504
+ if (!table) return EMPTY_CELLS_RESULT;
505
+ var cells = [];
506
+ var mergedCells = [];
507
+ var map = table.map,
508
+ node = table.node,
509
+ start = table.start;
510
+ var _options$includeMerge = options.includeMerged,
511
+ includeMerged = _options$includeMerge === void 0 ? false : _options$includeMerge;
512
+ var seen = new Set();
513
+ for (var col = 0; col < map.width; col++) {
514
+ var cellIndex = rowIndex * map.width + col;
515
+ var offset = map.map[cellIndex];
516
+ if (offset === undefined || seen.has(offset)) continue;
517
+ seen.add(offset);
518
+ var rect = map.findCell(offset);
519
+ if (rect.top !== rowIndex) continue;
520
+ var cellNode = node.nodeAt(offset);
521
+ if (!cellNode) continue;
522
+ var cell = createCellInfo(rect.top, rect.left, start + offset, cellNode);
523
+ if (isCellMerged(cellNode)) {
524
+ mergedCells.push(cell);
525
+ if (!includeMerged) continue;
526
+ }
527
+ cells.push(cell);
528
+ }
529
+ return {
530
+ cells: cells,
531
+ mergedCells: mergedCells
532
+ };
533
+ }
530
534
  export function getCellIndicesFromDOM(cell, tableNode, editor) {
531
535
  if (!tableNode) return null;
532
536
  try {
@@ -551,10 +555,6 @@ export function getCellIndicesFromDOM(cell, tableNode, editor) {
551
555
  }
552
556
  return null;
553
557
  }
554
-
555
- /**
556
- * Given a DOM element inside a table, find the corresponding table node and its position.
557
- */
558
558
  export function getTableFromDOM(tableElement, editor) {
559
559
  try {
560
560
  var pos = editor.view.posAtDOM(tableElement, 0);
@@ -573,31 +573,64 @@ export function getTableFromDOM(tableElement, editor) {
573
573
  }
574
574
  return null;
575
575
  }
576
-
577
- /**
578
- * Checks if a node is a table node
579
- */
580
576
  export function isTableNode(node) {
581
577
  return !!node && (node.type.name === 'table' || node.type.spec.tableRole === 'table');
582
578
  }
583
-
584
- /**
585
- * Get all cells (and unique merged cells) from a specific row.
586
- */
587
579
  export function getRowCells(editor, rowIndex, tablePos) {
588
580
  return collectCells(editor, 'row', rowIndex, tablePos);
589
581
  }
590
-
591
- /**
592
- * Collect cells (and unique merged cells) from the current table.
593
- */
594
582
  export function getColumnCells(editor, columnIndex, tablePos) {
595
583
  return collectCells(editor, 'column', columnIndex, tablePos);
596
584
  }
597
-
598
- /**
599
- * Compare two DOMRects for equality (within a small tolerance)
600
- */
585
+ export function adjustRowspanForRowInsert(editor, tablePos, insertRowIndex) {
586
+ var table = getTable(editor, tablePos);
587
+ if (!table) return false;
588
+ var cells = getUniqueCellsWithRect(table);
589
+ var tr = editor.state.tr;
590
+ var changed = false;
591
+ cells.forEach(function (_ref5) {
592
+ var _node$attrs$rowspan2;
593
+ var pos = _ref5.pos,
594
+ node = _ref5.node,
595
+ rect = _ref5.rect;
596
+ var rowspan = (_node$attrs$rowspan2 = node.attrs.rowspan) !== null && _node$attrs$rowspan2 !== void 0 ? _node$attrs$rowspan2 : 1;
597
+ if (rowspan > 1 && rect.top <= insertRowIndex && insertRowIndex < rect.bottom) {
598
+ tr = tr.setNodeMarkup(pos, undefined, _objectSpread(_objectSpread({}, node.attrs), {}, {
599
+ rowspan: rowspan + 1
600
+ }), node.marks);
601
+ changed = true;
602
+ }
603
+ });
604
+ if (changed) {
605
+ editor.view.dispatch(tr);
606
+ }
607
+ return changed;
608
+ }
609
+ export function adjustRowspanForRowDelete(editor, tablePos, deleteRowIndex) {
610
+ var table = getTable(editor, tablePos);
611
+ if (!table) return false;
612
+ var cells = getUniqueCellsWithRect(table);
613
+ var tr = editor.state.tr;
614
+ var changed = false;
615
+ cells.forEach(function (_ref6) {
616
+ var _node$attrs$rowspan3;
617
+ var pos = _ref6.pos,
618
+ node = _ref6.node,
619
+ rect = _ref6.rect;
620
+ var rowspan = (_node$attrs$rowspan3 = node.attrs.rowspan) !== null && _node$attrs$rowspan3 !== void 0 ? _node$attrs$rowspan3 : 1;
621
+ if (rowspan > 1 && rect.top < deleteRowIndex && deleteRowIndex < rect.bottom) {
622
+ var nextSpan = Math.max(1, rowspan - 1);
623
+ tr = tr.setNodeMarkup(pos, undefined, _objectSpread(_objectSpread({}, node.attrs), {}, {
624
+ rowspan: nextSpan
625
+ }), node.marks);
626
+ changed = true;
627
+ }
628
+ });
629
+ if (changed) {
630
+ editor.view.dispatch(tr);
631
+ }
632
+ return changed;
633
+ }
601
634
  export function rectEq(rect1, rect2) {
602
635
  if (!rect1 || !rect2) return rect1 === rect2;
603
636
  var tolerance = 0.5;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctzhian/tiptap",
3
- "version": "2.6.1",
3
+ "version": "2.7.0",
4
4
  "description": "基于 Tiptap 二次开发的编辑器组件",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -82,37 +82,37 @@
82
82
  "dependencies": {
83
83
  "@floating-ui/dom": "^1.7.2",
84
84
  "@floating-ui/react": "^0.27.16",
85
- "@tiptap/core": "^3.11.1",
86
- "@tiptap/extension-bubble-menu": "^3.11.1",
87
- "@tiptap/extension-code": "^3.11.1",
88
- "@tiptap/extension-code-block-lowlight": "^3.11.1",
89
- "@tiptap/extension-details": "^3.11.1",
90
- "@tiptap/extension-drag-handle-react": "^3.11.1",
91
- "@tiptap/extension-emoji": "^3.11.1",
92
- "@tiptap/extension-file-handler": "^3.11.1",
93
- "@tiptap/extension-highlight": "^3.11.1",
94
- "@tiptap/extension-horizontal-rule": "^3.11.1",
95
- "@tiptap/extension-image": "^3.11.1",
96
- "@tiptap/extension-invisible-characters": "^3.11.1",
97
- "@tiptap/extension-list": "^3.11.1",
98
- "@tiptap/extension-mathematics": "^3.11.1",
99
- "@tiptap/extension-mention": "^3.11.1",
100
- "@tiptap/extension-subscript": "^3.11.1",
101
- "@tiptap/extension-superscript": "^3.11.1",
102
- "@tiptap/extension-table": "^3.11.1",
103
- "@tiptap/extension-table-of-contents": "^3.11.1",
104
- "@tiptap/extension-text-align": "^3.11.1",
105
- "@tiptap/extension-text-style": "^3.11.1",
106
- "@tiptap/extension-unique-id": "^3.11.1",
107
- "@tiptap/extension-youtube": "^3.11.1",
108
- "@tiptap/extensions": "^3.11.1",
109
- "@tiptap/html": "^3.11.1",
110
- "@tiptap/markdown": "^3.11.1",
111
- "@tiptap/pm": "^3.11.1",
112
- "@tiptap/react": "^3.11.1",
113
- "@tiptap/starter-kit": "^3.11.1",
114
- "@tiptap/static-renderer": "^3.11.1",
115
- "@tiptap/suggestion": "^3.11.1",
85
+ "@tiptap/core": "^3.13.0",
86
+ "@tiptap/extension-bubble-menu": "^3.13.0",
87
+ "@tiptap/extension-code": "^3.13.0",
88
+ "@tiptap/extension-code-block-lowlight": "^3.13.0",
89
+ "@tiptap/extension-details": "^3.13.0",
90
+ "@tiptap/extension-drag-handle-react": "^3.13.0",
91
+ "@tiptap/extension-emoji": "^3.13.0",
92
+ "@tiptap/extension-file-handler": "^3.13.0",
93
+ "@tiptap/extension-highlight": "^3.13.0",
94
+ "@tiptap/extension-horizontal-rule": "^3.13.0",
95
+ "@tiptap/extension-image": "^3.13.0",
96
+ "@tiptap/extension-invisible-characters": "^3.13.0",
97
+ "@tiptap/extension-list": "^3.13.0",
98
+ "@tiptap/extension-mathematics": "^3.13.0",
99
+ "@tiptap/extension-mention": "^3.13.0",
100
+ "@tiptap/extension-subscript": "^3.13.0",
101
+ "@tiptap/extension-superscript": "^3.13.0",
102
+ "@tiptap/extension-table": "^3.13.0",
103
+ "@tiptap/extension-table-of-contents": "^3.13.0",
104
+ "@tiptap/extension-text-align": "^3.13.0",
105
+ "@tiptap/extension-text-style": "^3.13.0",
106
+ "@tiptap/extension-unique-id": "^3.13.0",
107
+ "@tiptap/extension-youtube": "^3.13.0",
108
+ "@tiptap/extensions": "^3.13.0",
109
+ "@tiptap/html": "^3.13.0",
110
+ "@tiptap/markdown": "^3.13.0",
111
+ "@tiptap/pm": "^3.13.0",
112
+ "@tiptap/react": "^3.13.0",
113
+ "@tiptap/starter-kit": "^3.13.0",
114
+ "@tiptap/static-renderer": "^3.13.0",
115
+ "@tiptap/suggestion": "^3.13.0",
116
116
  "ace-builds": "^1.43.4",
117
117
  "core-js": "^3.46.0",
118
118
  "diff-match-patch": "^1.0.5",
@@ -1,6 +0,0 @@
1
- import type { NodeType } from '@tiptap/pm/model';
2
- import { Plugin } from '@tiptap/pm/state';
3
- export declare const TableOfContentsPlugin: ({ getId, anchorTypes, }: {
4
- getId?: ((textContent: string) => string) | undefined;
5
- anchorTypes?: (string | NodeType)[] | undefined;
6
- }) => Plugin<any>;
@@ -1,58 +0,0 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
- import { Plugin, PluginKey } from '@tiptap/pm/state';
8
- import { v4 as uuidv4 } from 'uuid';
9
- export var TableOfContentsPlugin = function TableOfContentsPlugin(_ref) {
10
- var getId = _ref.getId,
11
- _ref$anchorTypes = _ref.anchorTypes,
12
- anchorTypes = _ref$anchorTypes === void 0 ? ['heading'] : _ref$anchorTypes;
13
- return new Plugin({
14
- key: new PluginKey('tableOfContent'),
15
- appendTransaction: function appendTransaction(transactions, oldState, newState) {
16
- var _view;
17
- // 若处于组合输入,完全避免在此事务中写入 id,交由扩展层异步生成
18
- if ((_view = newState.view) !== null && _view !== void 0 && _view.composing || typeof document !== 'undefined' && document.composing || typeof window !== 'undefined' && window.compositionState) {
19
- return null;
20
- }
21
-
22
- // 检查是否有任何事务包含组合相关的变化
23
- var hasCompositionChanges = transactions.some(function (tr) {
24
- return tr.getMeta('composition') || tr.getMeta('compositionend') || tr.getMeta('compositionstart');
25
- });
26
- if (hasCompositionChanges) {
27
- return null;
28
- }
29
- var tr = newState.tr;
30
- var modified = false;
31
- if (transactions.some(function (transaction) {
32
- return transaction.docChanged;
33
- }) && !oldState.doc.eq(newState.doc)) {
34
- var existingIds = [];
35
- newState.doc.descendants(function (node, pos) {
36
- var nodeId = node.attrs.id;
37
- if (!anchorTypes.includes(node.type.name) || node.textContent.length === 0) {
38
- return;
39
- }
40
- if (nodeId === null || nodeId === undefined || existingIds.includes(nodeId)) {
41
- var id = '';
42
- if (getId) {
43
- id = getId(node.textContent);
44
- } else {
45
- id = uuidv4();
46
- }
47
- tr.setNodeMarkup(pos, undefined, _objectSpread(_objectSpread({}, node.attrs), {}, {
48
- id: id
49
- }));
50
- modified = true;
51
- }
52
- existingIds.push(nodeId);
53
- });
54
- }
55
- return modified ? tr : null;
56
- }
57
- });
58
- };