@lofcz/platejs-table 52.0.11 → 52.3.6

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.
@@ -1,11 +1,28 @@
1
- import { B as getTableOverriddenColSizes, Q as isSelectedCellBorder, V as getTableGridAbove, Y as getTableCellBorders, Z as getSelectedCellsBorders, _ as setTableMarginLeft, a as BaseTableRowPlugin, dt as getColSpan, g as setTableRowSize, i as BaseTablePlugin, j as isTableRectangular, lt as getCellIndices, n as BaseTableCellHeaderPlugin, nt as getSelectedCellsBoundingBox, ot as getLeftTableCell, r as BaseTableCellPlugin, rt as getRowSpan, st as getCellTypes, t as KEY_SHIFT_EDGES, tt as getTopTableCell, ut as computeCellIndices, v as setTableColSize, x as moveSelectionFromCell, y as setBorderSize } from "../constants-B6Sm9BNa.js";
1
+ import { $ as getSelectedCellEntries, C as moveSelectionFromCell, Ct as getColSpan, K as getSelectedCellsBorders, S as setBorderSize, St as getRowSpan, V as getTableOverriddenColSizes, W as getTableCellBorders, X as getTopTableCell, Z as getSelectedCellsBoundingBox, _ as hasAdjacentBlockInCell, _t as getCellTypes, a as BaseTableRowPlugin, b as setTableMarginLeft, bt as computeCellIndices, et as getSelectedCellIds, ft as getLeftTableCell, g as getTableMoveSelectionContext, i as BaseTablePlugin, n as BaseTableCellHeaderPlugin, q as isSelectedCellBorder, r as BaseTableCellPlugin, t as KEY_SHIFT_EDGES, v as shouldMoveSelectionFromCell, x as setTableColSize, y as setTableRowSize, yt as getCellIndices } from "../constants-DGdhsla3.js";
2
2
  import { Hotkeys, KEYS, PathApi } from "platejs";
3
- import { atom, createAtomStore, toPlatePlugin, useEditorPlugin, useEditorRef, useEditorSelection, useEditorSelector, useElement, useElementSelector, usePluginOption, useReadOnly, useSelected } from "platejs/react";
3
+ import { atom, createAtomStore, toPlatePlugin, useEditorPlugin, useEditorSelector, useElement, useElementSelector, usePluginOption, useReadOnly } from "platejs/react";
4
4
  import { c } from "react-compiler-runtime";
5
5
  import React from "react";
6
6
  import { resizeLengthClampStatic } from "@platejs/resizable";
7
7
 
8
8
  //#region src/react/onKeyDownTable.ts
9
+ const shouldMoveSingleCellSelection = (editor, key) => {
10
+ const context = getTableMoveSelectionContext(editor, editor.selection?.focus);
11
+ if (!context) return false;
12
+ const { blockPath, cellPath, point } = context;
13
+ if (key === "shift+left") return editor.api.isStart(point, cellPath);
14
+ if (key === "shift+right") return editor.api.isEnd(point, cellPath);
15
+ if (hasAdjacentBlockInCell(editor, {
16
+ blockPath,
17
+ cellPath,
18
+ reverse: key === "shift+up"
19
+ })) return false;
20
+ return shouldMoveSelectionFromCell(editor, {
21
+ blockPath,
22
+ point,
23
+ reverse: key === "shift+up"
24
+ });
25
+ };
9
26
  const onKeyDownTable = ({ editor, event }) => {
10
27
  if (event.defaultPrevented) return;
11
28
  if (event.which === 229 && editor.selection && editor.api.isExpanded()) {
@@ -24,9 +41,15 @@ const onKeyDownTable = ({ editor, event }) => {
24
41
  "shift+up": Hotkeys.isExtendUpward(event)
25
42
  };
26
43
  Object.keys(isKeyDown).forEach((key) => {
27
- if (isKeyDown[key] && moveSelectionFromCell(editor, {
44
+ if (!isKeyDown[key]) return;
45
+ if (moveSelectionFromCell(editor, {
28
46
  edge: KEY_SHIFT_EDGES[key],
29
47
  reverse: key === "shift+up"
48
+ }) || shouldMoveSingleCellSelection(editor, key) && moveSelectionFromCell(editor, {
49
+ at: editor.selection,
50
+ edge: KEY_SHIFT_EDGES[key],
51
+ fromOneCell: true,
52
+ reverse: key === "shift+up"
30
53
  })) {
31
54
  event.preventDefault();
32
55
  event.stopPropagation();
@@ -52,8 +75,7 @@ const TablePlugin = toPlatePlugin(BaseTablePlugin, {
52
75
  //#endregion
53
76
  //#region src/react/components/TableCellElement/getOnSelectTableBorderFactory.ts
54
77
  /** Helper: sets one cell's specific border(s) to `size`. */
55
- function setCellBorderSize(editor, cell, directions, size) {
56
- const at = editor.api.findPath(cell);
78
+ function setCellBorderSize(editor, at, directions, size) {
57
79
  if (!at) return;
58
80
  if (directions === "all") setBorderSize(editor, size, {
59
81
  at,
@@ -64,6 +86,19 @@ function setCellBorderSize(editor, cell, directions, size) {
64
86
  border: dir
65
87
  });
66
88
  }
89
+ const getSelectedCellBorderTargets = (editor, cells) => cells.map((cell) => {
90
+ const path = editor.api.findPath(cell) ?? null;
91
+ const { col, row } = getCellIndices(editor, cell);
92
+ return {
93
+ cSpan: getColSpan(cell),
94
+ col,
95
+ leftCellPath: path ? getLeftTableCell(editor, { at: path })?.[1] ?? null : null,
96
+ path,
97
+ rSpan: getRowSpan(cell),
98
+ row,
99
+ topCellPath: path ? getTopTableCell(editor, { at: path })?.[1] ?? null : null
100
+ };
101
+ });
67
102
  /**
68
103
  * Toggle logic for `'none'`, `'outer'`, `'top'|'bottom'|'left'|'right'`.
69
104
  * `'none'` toggles no borders ↔ all borders, `'outer'` toggles the bounding
@@ -72,32 +107,19 @@ function setCellBorderSize(editor, cell, directions, size) {
72
107
  */
73
108
  function setSelectedCellsBorder(editor, { border, cells }) {
74
109
  if (cells.length === 0) return;
110
+ const targets = getSelectedCellBorderTargets(editor, cells);
75
111
  if (border === "none") {
76
112
  const { none: allNone } = getSelectedCellsBorders(editor, cells);
77
113
  const newSize$1 = allNone ? 1 : 0;
78
- for (const cell of cells) {
79
- const cellPath = editor.api.findPath(cell);
80
- if (!cellPath) continue;
81
- const { col, row } = getCellIndices(editor, cell);
114
+ for (const target of targets) {
115
+ if (!target.path) continue;
82
116
  const edges = [];
83
- if (row === 0) edges.push("top");
84
- if (col === 0) edges.push("left");
117
+ if (target.row === 0) edges.push("top");
118
+ if (target.col === 0) edges.push("left");
85
119
  edges.push("bottom", "right");
86
- if (row > 0) {
87
- const cellAboveEntry = getTopTableCell(editor, { at: cellPath });
88
- if (cellAboveEntry) {
89
- const [cellAbove] = cellAboveEntry;
90
- setCellBorderSize(editor, cellAbove, ["bottom"], newSize$1);
91
- }
92
- }
93
- if (col > 0) {
94
- const prevCellEntry = getLeftTableCell(editor, { at: cellPath });
95
- if (prevCellEntry) {
96
- const [prevCell] = prevCellEntry;
97
- setCellBorderSize(editor, prevCell, ["right"], newSize$1);
98
- }
99
- }
100
- if (edges.length > 0) setCellBorderSize(editor, cell, edges, newSize$1);
120
+ if (target.row > 0) setCellBorderSize(editor, target.topCellPath, ["bottom"], newSize$1);
121
+ if (target.col > 0) setCellBorderSize(editor, target.leftCellPath, ["right"], newSize$1);
122
+ if (edges.length > 0) setCellBorderSize(editor, target.path, edges, newSize$1);
101
123
  }
102
124
  return;
103
125
  }
@@ -105,49 +127,31 @@ function setSelectedCellsBorder(editor, { border, cells }) {
105
127
  const { outer: allOut } = getSelectedCellsBorders(editor, cells);
106
128
  const newSize$1 = allOut ? 0 : 1;
107
129
  const { maxCol: maxCol$1, maxRow: maxRow$1, minCol: minCol$1, minRow: minRow$1 } = getSelectedCellsBoundingBox(editor, cells);
108
- for (const cell of cells) {
109
- const { col, row } = getCellIndices(editor, cell);
110
- const cSpan = getColSpan(cell);
111
- const rSpan = getRowSpan(cell);
112
- for (let rr = row; rr < row + rSpan; rr++) for (let cc = col; cc < col + cSpan; cc++) {
130
+ for (const target of targets) {
131
+ if (!target.path) continue;
132
+ for (let rr = target.row; rr < target.row + target.rSpan; rr++) for (let cc = target.col; cc < target.col + target.cSpan; cc++) {
113
133
  const edges = [];
114
134
  if (rr === minRow$1) edges.push("top");
115
135
  if (rr === maxRow$1) edges.push("bottom");
116
136
  if (cc === minCol$1) edges.push("left");
117
137
  if (cc === maxCol$1) edges.push("right");
118
- if (edges.length > 0) setCellBorderSize(editor, cell, edges, newSize$1);
138
+ if (edges.length > 0) setCellBorderSize(editor, target.path, edges, newSize$1);
119
139
  }
120
140
  }
121
141
  return;
122
142
  }
123
143
  const newSize = isSelectedCellBorder(editor, cells, border) ? 0 : 1;
124
144
  const { maxCol, maxRow, minCol, minRow } = getSelectedCellsBoundingBox(editor, cells);
125
- for (const cell of cells) {
126
- const { col, row } = getCellIndices(editor, cell);
127
- const cSpan = getColSpan(cell);
128
- const rSpan = getRowSpan(cell);
129
- const cellPath = editor.api.findPath(cell);
130
- if (!cellPath) continue;
145
+ for (const target of targets) {
146
+ if (!target.path) continue;
131
147
  const edges = [];
132
- if (border === "top" && row === minRow) if (row === 0) edges.push("top");
133
- else {
134
- const cellAboveEntry = getTopTableCell(editor, { at: cellPath });
135
- if (cellAboveEntry) {
136
- const [cellAbove] = cellAboveEntry;
137
- setCellBorderSize(editor, cellAbove, ["bottom"], newSize);
138
- }
139
- }
140
- if (border === "bottom" && row + rSpan - 1 === maxRow) edges.push("bottom");
141
- if (border === "left" && col === minCol) if (col === 0) edges.push("left");
142
- else {
143
- const prevCellEntry = getLeftTableCell(editor, { at: cellPath });
144
- if (prevCellEntry) {
145
- const [prevCell] = prevCellEntry;
146
- setCellBorderSize(editor, prevCell, ["right"], newSize);
147
- }
148
- }
149
- if (border === "right" && col + cSpan - 1 === maxCol) edges.push("right");
150
- if (edges.length > 0) setCellBorderSize(editor, cell, edges, newSize);
148
+ if (border === "top" && target.row === minRow) if (target.row === 0) edges.push("top");
149
+ else setCellBorderSize(editor, target.topCellPath, ["bottom"], newSize);
150
+ if (border === "bottom" && target.row + target.rSpan - 1 === maxRow) edges.push("bottom");
151
+ if (border === "left" && target.col === minCol) if (target.col === 0) edges.push("left");
152
+ else setCellBorderSize(editor, target.leftCellPath, ["right"], newSize);
153
+ if (border === "right" && target.col + target.cSpan - 1 === maxCol) edges.push("right");
154
+ if (edges.length > 0) setCellBorderSize(editor, target.path, edges, newSize);
151
155
  }
152
156
  }
153
157
  /**
@@ -155,8 +159,8 @@ function setSelectedCellsBorder(editor, { border, cells }) {
155
159
  * selection has one or many cells, it's the same approach: we read the bounding
156
160
  * rectangle, then decide which edges to flip on/off.
157
161
  */
158
- const getOnSelectTableBorderFactory = (editor, selectedCells) => (border) => () => {
159
- let cells = selectedCells;
162
+ const getOnSelectTableBorderFactory = (editor) => (border) => () => {
163
+ let cells = editor.getApi(TablePlugin).table.getSelectedCells();
160
164
  if (!cells || cells.length === 0) {
161
165
  const cell = editor.api.block({ match: { type: getCellTypes(editor) } });
162
166
  if (cell) cells = [cell[0]];
@@ -180,21 +184,25 @@ const roundCellSizeToStep = (size, step) => step ? Math.round(size / step) * ste
180
184
  //#region src/react/components/TableCellElement/useIsCellSelected.ts
181
185
  const useIsCellSelected = (element) => {
182
186
  const $ = c(3);
183
- const selectedCells = usePluginOption(TablePlugin, "selectedCells");
184
187
  let t0;
185
- if ($[0] !== element || $[1] !== selectedCells) {
186
- t0 = selectedCells?.includes(element);
187
- $[0] = element;
188
- $[1] = selectedCells;
189
- $[2] = t0;
190
- } else t0 = $[2];
191
- return !!t0;
188
+ let t1;
189
+ if ($[0] !== element.id) {
190
+ t0 = (editor) => editor.getApi(TablePlugin).table.isCellSelected(element.id);
191
+ t1 = [element.id];
192
+ $[0] = element.id;
193
+ $[1] = t0;
194
+ $[2] = t1;
195
+ } else {
196
+ t0 = $[1];
197
+ t1 = $[2];
198
+ }
199
+ return useEditorSelector(t0, t1);
192
200
  };
193
201
 
194
202
  //#endregion
195
203
  //#region src/react/components/TableCellElement/useTableBordersDropdownMenuContentState.ts
196
204
  const useTableBordersDropdownMenuContentState = (t0) => {
197
- const $ = c(18);
205
+ const $ = c(14);
198
206
  let t1;
199
207
  if ($[0] !== t0) {
200
208
  t1 = t0 === void 0 ? {} : t0;
@@ -204,32 +212,23 @@ const useTableBordersDropdownMenuContentState = (t0) => {
204
212
  const { element: el } = t1;
205
213
  const { editor } = useEditorPlugin(TablePlugin);
206
214
  const element = useElement() ?? el;
207
- const selectedCells = usePluginOption(TablePlugin, "selectedCells");
208
215
  let t2;
209
- if ($[2] !== selectedCells) {
210
- t2 = (editor_0) => getSelectedCellsBorders(editor_0, selectedCells);
211
- $[2] = selectedCells;
216
+ if ($[2] !== element) {
217
+ t2 = [element];
218
+ $[2] = element;
212
219
  $[3] = t2;
213
220
  } else t2 = $[3];
221
+ const borderStates = useEditorSelector(_temp$5, t2);
214
222
  let t3;
215
- if ($[4] !== element || $[5] !== selectedCells) {
216
- t3 = [selectedCells, element];
217
- $[4] = element;
218
- $[5] = selectedCells;
219
- $[6] = t3;
220
- } else t3 = $[6];
221
- const borderStates = useEditorSelector(t2, t3);
223
+ if ($[4] !== editor) {
224
+ t3 = getOnSelectTableBorderFactory(editor);
225
+ $[4] = editor;
226
+ $[5] = t3;
227
+ } else t3 = $[5];
222
228
  let t4;
223
- if ($[7] !== editor || $[8] !== selectedCells) {
224
- t4 = getOnSelectTableBorderFactory(editor, selectedCells);
225
- $[7] = editor;
226
- $[8] = selectedCells;
227
- $[9] = t4;
228
- } else t4 = $[9];
229
- let t5;
230
- if ($[10] !== borderStates.bottom || $[11] !== borderStates.left || $[12] !== borderStates.none || $[13] !== borderStates.outer || $[14] !== borderStates.right || $[15] !== borderStates.top || $[16] !== t4) {
231
- t5 = {
232
- getOnSelectTableBorder: t4,
229
+ if ($[6] !== borderStates.bottom || $[7] !== borderStates.left || $[8] !== borderStates.none || $[9] !== borderStates.outer || $[10] !== borderStates.right || $[11] !== borderStates.top || $[12] !== t3) {
230
+ t4 = {
231
+ getOnSelectTableBorder: t3,
233
232
  hasBottomBorder: borderStates.bottom,
234
233
  hasLeftBorder: borderStates.left,
235
234
  hasNoBorders: borderStates.none,
@@ -237,17 +236,20 @@ const useTableBordersDropdownMenuContentState = (t0) => {
237
236
  hasRightBorder: borderStates.right,
238
237
  hasTopBorder: borderStates.top
239
238
  };
240
- $[10] = borderStates.bottom;
241
- $[11] = borderStates.left;
242
- $[12] = borderStates.none;
243
- $[13] = borderStates.outer;
244
- $[14] = borderStates.right;
245
- $[15] = borderStates.top;
246
- $[16] = t4;
247
- $[17] = t5;
248
- } else t5 = $[17];
249
- return t5;
239
+ $[6] = borderStates.bottom;
240
+ $[7] = borderStates.left;
241
+ $[8] = borderStates.none;
242
+ $[9] = borderStates.outer;
243
+ $[10] = borderStates.right;
244
+ $[11] = borderStates.top;
245
+ $[12] = t3;
246
+ $[13] = t4;
247
+ } else t4 = $[13];
248
+ return t4;
250
249
  };
250
+ function _temp$5(editor_0) {
251
+ return getSelectedCellsBorders(editor_0);
252
+ }
251
253
 
252
254
  //#endregion
253
255
  //#region src/react/hooks/useCellIndices.ts
@@ -350,94 +352,68 @@ const useOverrideMarginLeft = () => {
350
352
 
351
353
  //#endregion
352
354
  //#region src/react/components/TableElement/useSelectedCells.ts
353
- /**
354
- * Many grid cells above and diff -> set No many grid cells above and diff ->
355
- * unset No selection -> unset
356
- */
355
+ const hasSameIds$1 = (nextValue, prevValue) => {
356
+ if (nextValue === prevValue) return true;
357
+ if (!nextValue || !prevValue) return !nextValue && !prevValue;
358
+ if (nextValue.length !== prevValue.length) return false;
359
+ for (const [index, nextId] of nextValue.entries()) if (nextId !== prevValue[index]) return false;
360
+ return true;
361
+ };
362
+ const hasSameSelectionState = (nextValue, prevValue) => nextValue.selectedContent === prevValue.selectedContent && hasSameIds$1(nextValue.selectedCellIds, prevValue.selectedCellIds);
357
363
  const useSelectedCells = () => {
358
- const $ = c(20);
364
+ const $ = c(8);
359
365
  const readOnly = useReadOnly();
360
- const selected = useSelected();
361
- const editor = useEditorRef();
362
- const selection = useEditorSelection();
363
- const { setOption } = useEditorPlugin(TablePlugin);
364
- const selectedCells = usePluginOption(TablePlugin, "selectedCells");
366
+ const { setOptions } = useEditorPlugin(BaseTablePlugin);
365
367
  let t0;
366
- if ($[0] !== readOnly || $[1] !== selected || $[2] !== setOption) {
367
- t0 = () => {
368
- if (!selected || readOnly) {
369
- setOption("selectedCells", null);
370
- setOption("selectedTables", null);
371
- }
368
+ let t1;
369
+ if ($[0] !== readOnly) {
370
+ t0 = (editor) => {
371
+ if (readOnly) return {
372
+ selectedCellIds: null,
373
+ selectedContent: null
374
+ };
375
+ const selectedCellIds = getSelectedCellIds(editor);
376
+ return {
377
+ selectedCellIds,
378
+ selectedContent: selectedCellIds ? editor.children : null
379
+ };
372
380
  };
381
+ t1 = [readOnly];
373
382
  $[0] = readOnly;
374
- $[1] = selected;
375
- $[2] = setOption;
376
- $[3] = t0;
377
- } else t0 = $[3];
378
- let t1;
379
- if ($[4] !== editor || $[5] !== readOnly || $[6] !== selected || $[7] !== setOption) {
380
- t1 = [
381
- selected,
382
- editor,
383
- readOnly,
384
- setOption
385
- ];
386
- $[4] = editor;
387
- $[5] = readOnly;
388
- $[6] = selected;
389
- $[7] = setOption;
390
- $[8] = t1;
391
- } else t1 = $[8];
392
- React.useEffect(t0, t1);
383
+ $[1] = t0;
384
+ $[2] = t1;
385
+ } else {
386
+ t0 = $[1];
387
+ t1 = $[2];
388
+ }
393
389
  let t2;
394
- if ($[9] !== editor || $[10] !== readOnly || $[11] !== selectedCells || $[12] !== setOption) {
395
- t2 = () => {
396
- if (readOnly) return;
397
- const tableEntries = getTableGridAbove(editor, { format: "table" });
398
- const cellEntries = getTableGridAbove(editor, { format: "cell" });
399
- if (cellEntries?.length > 1) {
400
- const cells = cellEntries.map(_temp$3);
401
- const tables = tableEntries.map(_temp2);
402
- if (JSON.stringify(cells) !== JSON.stringify(selectedCells)) {
403
- setOption("selectedCells", cells);
404
- setOption("selectedTables", tables);
405
- }
406
- } else if (selectedCells) {
407
- setOption("selectedCells", null);
408
- setOption("selectedTables", null);
409
- }
410
- };
411
- $[9] = editor;
412
- $[10] = readOnly;
413
- $[11] = selectedCells;
414
- $[12] = setOption;
415
- $[13] = t2;
416
- } else t2 = $[13];
390
+ if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
391
+ t2 = { equalityFn: hasSameSelectionState };
392
+ $[3] = t2;
393
+ } else t2 = $[3];
394
+ const selectionState = useEditorSelector(t0, t1, t2);
417
395
  let t3;
418
- if ($[14] !== editor || $[15] !== readOnly || $[16] !== selectedCells || $[17] !== selection || $[18] !== setOption) {
419
- t3 = [
420
- editor,
421
- selection,
422
- readOnly,
423
- selectedCells,
424
- setOption
425
- ];
426
- $[14] = editor;
427
- $[15] = readOnly;
428
- $[16] = selectedCells;
429
- $[17] = selection;
430
- $[18] = setOption;
431
- $[19] = t3;
432
- } else t3 = $[19];
433
- React.useEffect(t2, t3);
396
+ let t4;
397
+ if ($[4] !== selectionState || $[5] !== setOptions) {
398
+ t3 = () => {
399
+ const nextSelectedCellIds = selectionState.selectedCellIds;
400
+ setOptions((draft) => {
401
+ if (!hasSameIds$1(draft._selectedCellIds, nextSelectedCellIds)) draft._selectedCellIds = nextSelectedCellIds;
402
+ if (draft._selectedTableIds !== void 0) draft._selectedTableIds = void 0;
403
+ draft._selectionVersion = (draft._selectionVersion ?? 0) + 1;
404
+ });
405
+ };
406
+ t4 = [selectionState, setOptions];
407
+ $[4] = selectionState;
408
+ $[5] = setOptions;
409
+ $[6] = t3;
410
+ $[7] = t4;
411
+ } else {
412
+ t3 = $[6];
413
+ t4 = $[7];
414
+ }
415
+ React.useLayoutEffect(t3, t4);
434
416
  };
435
- function _temp$3(entry) {
436
- return entry[0];
437
- }
438
- function _temp2(entry_0) {
439
- return entry_0[0];
440
- }
441
417
 
442
418
  //#endregion
443
419
  //#region src/react/components/TableElement/useTableColSizes.ts
@@ -483,52 +459,153 @@ const useTableColSizes = (t0) => {
483
459
  if ($[7] === Symbol.for("react.memo_cache_sentinel")) {
484
460
  t5 = {
485
461
  key: KEYS.table,
486
- equalityFn: _temp$2
462
+ equalityFn: _temp$4
487
463
  };
488
464
  $[7] = t5;
489
465
  } else t5 = $[7];
490
466
  return useElementSelector(t3, t4, t5);
491
467
  };
492
- function _temp$2(a, b) {
468
+ function _temp$4(a, b) {
493
469
  return !!a && !!b && PathApi.equals(a, b);
494
470
  }
495
471
 
496
472
  //#endregion
497
473
  //#region src/react/components/TableElement/useTableElement.ts
498
474
  const useTableElement = () => {
499
- const $ = c(7);
475
+ const $ = c(5);
500
476
  const { editor, getOptions } = useEditorPlugin(TablePlugin);
501
477
  const { disableMarginLeft } = getOptions();
502
478
  const element = useElement();
503
- const selectedCells = usePluginOption(TablePlugin, "selectedCells");
504
479
  const marginLeftOverride = useTableValue("marginLeftOverride");
505
480
  const marginLeft = disableMarginLeft ? 0 : marginLeftOverride ?? element.marginLeft ?? 0;
506
- useSelectedCells();
507
- const t0 = !!selectedCells;
508
- let t1;
509
- if ($[0] !== editor || $[1] !== selectedCells) {
510
- t1 = { onMouseDown: () => {
511
- if (selectedCells) editor.tf.collapse();
481
+ let t0;
482
+ if ($[0] !== editor) {
483
+ t0 = { onMouseDown: () => {
484
+ if (editor.getOption(TablePlugin, "isSelectingCell")) editor.tf.collapse();
512
485
  } };
513
486
  $[0] = editor;
514
- $[1] = selectedCells;
515
- $[2] = t1;
516
- } else t1 = $[2];
517
- let t2;
518
- if ($[3] !== marginLeft || $[4] !== t0 || $[5] !== t1) {
519
- t2 = {
520
- isSelectingCell: t0,
487
+ $[1] = t0;
488
+ } else t0 = $[1];
489
+ let t1;
490
+ if ($[2] !== marginLeft || $[3] !== t0) {
491
+ t1 = {
521
492
  marginLeft,
522
- props: t1
493
+ props: t0
523
494
  };
524
- $[3] = marginLeft;
525
- $[4] = t0;
526
- $[5] = t1;
527
- $[6] = t2;
528
- } else t2 = $[6];
529
- return t2;
495
+ $[2] = marginLeft;
496
+ $[3] = t0;
497
+ $[4] = t1;
498
+ } else t1 = $[4];
499
+ return t1;
530
500
  };
531
501
 
502
+ //#endregion
503
+ //#region src/react/components/TableElement/useTableSelectionDom.ts
504
+ const hasSameIds = (nextValue, prevValue) => {
505
+ if (nextValue === prevValue) return true;
506
+ if (!nextValue || !prevValue) return !nextValue && !prevValue;
507
+ if (nextValue.length !== prevValue.length) return false;
508
+ for (const [index, nextId] of nextValue.entries()) if (nextId !== prevValue[index]) return false;
509
+ return true;
510
+ };
511
+ const TABLE_CELL_SELECTED_ATTRIBUTE = "data-table-cell-selected";
512
+ const TABLE_SELECTING_ATTRIBUTE = "data-table-selecting";
513
+ const TABLE_CELL_SELECTOR = "[data-table-cell-id]";
514
+ const setTableSelectingAttribute = (table, isSelecting) => {
515
+ if (isSelecting) {
516
+ table.setAttribute(TABLE_SELECTING_ATTRIBUTE, "true");
517
+ return;
518
+ }
519
+ table.removeAttribute(TABLE_SELECTING_ATTRIBUTE);
520
+ };
521
+ const escapeForAttributeSelector = (value) => globalThis.CSS?.escape ? globalThis.CSS.escape(value) : value.replaceAll("\"", "\\\"");
522
+ const createTableCellElementsById = (table) => {
523
+ const tableCellElementsById = /* @__PURE__ */ new Map();
524
+ table.querySelectorAll(TABLE_CELL_SELECTOR).forEach((element) => {
525
+ const cellId = element.getAttribute("data-table-cell-id");
526
+ if (cellId) tableCellElementsById.set(cellId, element);
527
+ });
528
+ return tableCellElementsById;
529
+ };
530
+ const getSelectedCellElement = (table, cellId, tableCellElementsById) => {
531
+ const cachedElement = tableCellElementsById.get(cellId);
532
+ if (cachedElement?.isConnected && table.contains(cachedElement)) return cachedElement;
533
+ const element = table.querySelector(`[data-table-cell-id="${escapeForAttributeSelector(cellId)}"]`);
534
+ if (element) tableCellElementsById.set(cellId, element);
535
+ else tableCellElementsById.delete(cellId);
536
+ return element;
537
+ };
538
+ const useTableSelectionDom = (tableRef) => {
539
+ const $ = c(5);
540
+ const previousTableRef = React.useRef(null);
541
+ const previousSelectedCellIdsRef = React.useRef(null);
542
+ const tableCellElementsByIdRef = React.useRef(null);
543
+ let t0;
544
+ let t1;
545
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
546
+ t0 = [];
547
+ t1 = { equalityFn: hasSameIds };
548
+ $[0] = t0;
549
+ $[1] = t1;
550
+ } else {
551
+ t0 = $[0];
552
+ t1 = $[1];
553
+ }
554
+ const selectedCellIds = useEditorSelector(_temp$3, t0, t1);
555
+ let t2;
556
+ if ($[2] !== selectedCellIds || $[3] !== tableRef) {
557
+ t2 = () => {
558
+ const table = tableRef.current;
559
+ if (!table) return;
560
+ const tableChanged = previousTableRef.current !== table;
561
+ const previousSelectedCellIdsRefValue = previousSelectedCellIdsRef.current;
562
+ if (!tableChanged && hasSameIds(selectedCellIds, previousSelectedCellIdsRefValue)) return;
563
+ const previousSelectedCellIds = tableChanged ? [] : previousSelectedCellIdsRefValue ?? [];
564
+ const nextSelectedCellIds = selectedCellIds ?? [];
565
+ const tableCellElementsById = tableChanged || !tableCellElementsByIdRef.current ? createTableCellElementsById(table) : tableCellElementsByIdRef.current;
566
+ tableCellElementsByIdRef.current = tableCellElementsById;
567
+ if (previousSelectedCellIds.length === 0) {
568
+ setTableSelectingAttribute(table, nextSelectedCellIds.length > 0);
569
+ nextSelectedCellIds.forEach((cellId) => {
570
+ getSelectedCellElement(table, cellId, tableCellElementsById)?.setAttribute(TABLE_CELL_SELECTED_ATTRIBUTE, "true");
571
+ });
572
+ previousTableRef.current = table;
573
+ previousSelectedCellIdsRef.current = nextSelectedCellIds;
574
+ return;
575
+ }
576
+ if (nextSelectedCellIds.length === 0) {
577
+ setTableSelectingAttribute(table, false);
578
+ previousSelectedCellIds.forEach((cellId_0) => {
579
+ getSelectedCellElement(table, cellId_0, tableCellElementsById)?.removeAttribute(TABLE_CELL_SELECTED_ATTRIBUTE);
580
+ });
581
+ previousTableRef.current = table;
582
+ previousSelectedCellIdsRef.current = nextSelectedCellIds;
583
+ return;
584
+ }
585
+ const nextSelectedCellIdsSet = new Set(nextSelectedCellIds);
586
+ const previousSelectedCellIdsSet = new Set(previousSelectedCellIds);
587
+ setTableSelectingAttribute(table, true);
588
+ previousSelectedCellIds.forEach((cellId_1) => {
589
+ if (nextSelectedCellIdsSet.has(cellId_1)) return;
590
+ getSelectedCellElement(table, cellId_1, tableCellElementsById)?.removeAttribute(TABLE_CELL_SELECTED_ATTRIBUTE);
591
+ });
592
+ nextSelectedCellIds.forEach((cellId_2) => {
593
+ if (previousSelectedCellIdsSet.has(cellId_2)) return;
594
+ getSelectedCellElement(table, cellId_2, tableCellElementsById)?.setAttribute(TABLE_CELL_SELECTED_ATTRIBUTE, "true");
595
+ });
596
+ previousTableRef.current = table;
597
+ previousSelectedCellIdsRef.current = nextSelectedCellIds;
598
+ };
599
+ $[2] = selectedCellIds;
600
+ $[3] = tableRef;
601
+ $[4] = t2;
602
+ } else t2 = $[4];
603
+ React.useLayoutEffect(t2);
604
+ };
605
+ function _temp$3(editor) {
606
+ return getSelectedCellIds(editor);
607
+ }
608
+
532
609
  //#endregion
533
610
  //#region src/react/components/TableCellElement/useTableCellSize.ts
534
611
  function useTableCellSize(t0) {
@@ -555,7 +632,7 @@ function useTableCellSize(t0) {
555
632
  t2 = $[2];
556
633
  t3 = $[3];
557
634
  }
558
- const rowSize = useElementSelector(_temp$1, t2, t3);
635
+ const rowSize = useElementSelector(_temp$2, t2, t3);
559
636
  let t4;
560
637
  if ($[4] !== api.table || $[5] !== cellIndices || $[6] !== colSizes || $[7] !== element || $[8] !== rowSize) {
561
638
  t4 = api.table.getCellSize({
@@ -573,7 +650,7 @@ function useTableCellSize(t0) {
573
650
  } else t4 = $[9];
574
651
  return t4;
575
652
  }
576
- function _temp$1(t0) {
653
+ function _temp$2(t0) {
577
654
  const [node] = t0;
578
655
  return node.size;
579
656
  }
@@ -581,34 +658,70 @@ function _temp$1(t0) {
581
658
  //#endregion
582
659
  //#region src/react/components/TableCellElement/useTableCellElement.ts
583
660
  const useTableCellElement = () => {
584
- const { api, setOption } = useEditorPlugin(TablePlugin);
661
+ const $ = c(18);
662
+ const { api } = useEditorPlugin(TablePlugin);
585
663
  const element = useElement();
586
664
  const isCellSelected = useIsCellSelected(element);
587
- const selectedCells = usePluginOption(TablePlugin, "selectedCells");
588
- React.useEffect(() => {
589
- if (selectedCells?.some((v) => v.id === element.id && element !== v)) setOption("selectedCells", selectedCells.map((v) => v.id === element.id ? element : v));
590
- }, [element]);
665
+ let t0;
666
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
667
+ t0 = [];
668
+ $[0] = t0;
669
+ } else t0 = $[0];
670
+ const isSelectingCell = useEditorSelector(_temp$1, t0);
591
671
  const rowSizeOverrides = useTableValue("rowSizeOverrides");
592
- const { minHeight, width } = useTableCellSize({ element });
593
- const borders = useTableCellBorders({ element });
594
- /**
595
- * Row size: if rowSpan > 1, we might look up the rowSize for the bottom row
596
- * or you can do something simpler if row-spanning is unusual in your app.
597
- */
672
+ let t1;
673
+ if ($[1] !== element) {
674
+ t1 = { element };
675
+ $[1] = element;
676
+ $[2] = t1;
677
+ } else t1 = $[2];
678
+ const { minHeight, width } = useTableCellSize(t1);
679
+ let t2;
680
+ if ($[3] !== element) {
681
+ t2 = { element };
682
+ $[3] = element;
683
+ $[4] = t2;
684
+ } else t2 = $[4];
685
+ const borders = useTableCellBorders(t2);
598
686
  const { col, row } = useCellIndices();
599
687
  const colSpan = api.table.getColSpan(element);
600
688
  const endingRowIndex = row + api.table.getRowSpan(element) - 1;
601
- return {
602
- borders,
603
- colIndex: col + colSpan - 1,
604
- colSpan,
605
- isSelectingCell: !!selectedCells,
606
- minHeight: rowSizeOverrides.get?.(endingRowIndex) ?? minHeight,
607
- rowIndex: endingRowIndex,
608
- selected: isCellSelected,
609
- width
610
- };
689
+ const endingColIndex = col + colSpan - 1;
690
+ let t3;
691
+ if ($[5] !== endingRowIndex || $[6] !== minHeight || $[7] !== rowSizeOverrides) {
692
+ t3 = rowSizeOverrides.get?.(endingRowIndex) ?? minHeight;
693
+ $[5] = endingRowIndex;
694
+ $[6] = minHeight;
695
+ $[7] = rowSizeOverrides;
696
+ $[8] = t3;
697
+ } else t3 = $[8];
698
+ let t4;
699
+ if ($[9] !== borders || $[10] !== colSpan || $[11] !== endingColIndex || $[12] !== endingRowIndex || $[13] !== isCellSelected || $[14] !== isSelectingCell || $[15] !== t3 || $[16] !== width) {
700
+ t4 = {
701
+ borders,
702
+ colIndex: endingColIndex,
703
+ colSpan,
704
+ isSelectingCell,
705
+ minHeight: t3,
706
+ rowIndex: endingRowIndex,
707
+ selected: isCellSelected,
708
+ width
709
+ };
710
+ $[9] = borders;
711
+ $[10] = colSpan;
712
+ $[11] = endingColIndex;
713
+ $[12] = endingRowIndex;
714
+ $[13] = isCellSelected;
715
+ $[14] = isSelectingCell;
716
+ $[15] = t3;
717
+ $[16] = width;
718
+ $[17] = t4;
719
+ } else t4 = $[17];
720
+ return t4;
611
721
  };
722
+ function _temp$1(editor) {
723
+ return editor.getApi(TablePlugin).table.isSelectingCell();
724
+ }
612
725
 
613
726
  //#endregion
614
727
  //#region src/react/components/TableCellElement/useTableCellElementResizable.ts
@@ -852,28 +965,36 @@ function _temp(t0) {
852
965
  //#endregion
853
966
  //#region src/react/hooks/useTableMergeState.ts
854
967
  const useTableMergeState = () => {
855
- const { api, getOptions } = useEditorPlugin(TablePlugin);
968
+ const { api, editor, getOptions } = useEditorPlugin(TablePlugin);
856
969
  const { disableMerge } = getOptions();
857
970
  if (disableMerge) return {
858
971
  canMerge: false,
859
972
  canSplit: false
860
973
  };
861
974
  const readOnly = useReadOnly();
862
- const someTable = useEditorSelector((editor) => editor.api.some({ match: { type: KEYS.table } }), []);
863
- const selectionExpanded = useEditorSelector((editor) => editor.api.isExpanded(), []);
975
+ const someTable = useEditorSelector((editor$1) => editor$1.api.some({ match: { type: KEYS.table } }), []);
976
+ const selectionExpanded = useEditorSelector((editor$1) => editor$1.api.isExpanded(), []);
864
977
  const collapsed = !readOnly && someTable && !selectionExpanded;
865
- const selectedTable = usePluginOption(TablePlugin, "selectedTables")?.[0];
866
- const selectedCellEntries = useEditorSelector((editor) => getTableGridAbove(editor, { format: "cell" }), []);
978
+ const selectedCellEntries = useEditorSelector((editor$1) => getSelectedCellEntries(editor$1), []);
979
+ const isRectangularSelection = React.useMemo(() => {
980
+ if (selectedCellEntries.length <= 1) return false;
981
+ const selectedCells = selectedCellEntries.map(([cell]) => cell);
982
+ const { maxCol, maxRow, minCol, minRow } = getSelectedCellsBoundingBox(editor, selectedCells);
983
+ return selectedCells.reduce((total, cell) => total + api.table.getColSpan(cell) * api.table.getRowSpan(cell), 0) === (maxCol - minCol + 1) * (maxRow - minRow + 1);
984
+ }, [
985
+ api.table,
986
+ editor,
987
+ selectedCellEntries
988
+ ]);
867
989
  if (!selectedCellEntries) return {
868
990
  canMerge: false,
869
991
  canSplit: false
870
992
  };
871
993
  return {
872
- canMerge: !readOnly && someTable && selectionExpanded && selectedCellEntries.length > 1 && isTableRectangular(selectedTable),
994
+ canMerge: !readOnly && someTable && selectionExpanded && selectedCellEntries.length > 1 && isRectangularSelection,
873
995
  canSplit: collapsed && selectedCellEntries.length === 1 && (api.table.getColSpan(selectedCellEntries[0][0]) > 1 || api.table.getRowSpan(selectedCellEntries[0][0]) > 1)
874
996
  };
875
997
  };
876
998
 
877
999
  //#endregion
878
- export { TableCellHeaderPlugin, TableCellPlugin, TablePlugin, TableProvider, TableRowPlugin, getOnSelectTableBorderFactory, onKeyDownTable, roundCellSizeToStep, setSelectedCellsBorder, tableStore, useCellIndices, useIsCellSelected, useOverrideColSize, useOverrideMarginLeft, useOverrideRowSize, useSelectedCells, useTableBordersDropdownMenuContentState, useTableCellBorders, useTableCellElement, useTableCellElementResizable, useTableCellSize, useTableColSizes, useTableElement, useTableMergeState, useTableSet, useTableState, useTableStore, useTableValue };
879
- //# sourceMappingURL=index.js.map
1000
+ export { TableCellHeaderPlugin, TableCellPlugin, TablePlugin, TableProvider, TableRowPlugin, getOnSelectTableBorderFactory, onKeyDownTable, roundCellSizeToStep, setSelectedCellsBorder, tableStore, useCellIndices, useIsCellSelected, useOverrideColSize, useOverrideMarginLeft, useOverrideRowSize, useSelectedCells, useTableBordersDropdownMenuContentState, useTableCellBorders, useTableCellElement, useTableCellElementResizable, useTableCellSize, useTableColSizes, useTableElement, useTableMergeState, useTableSelectionDom, useTableSet, useTableState, useTableStore, useTableValue };