@jvs-milkdown/components 1.0.0 → 1.1.1

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,51 +1,51 @@
1
1
  import { $ctx, $view } from '@jvs-milkdown/utils';
2
- import { selectColCommand, moveColCommand, selectRowCommand, moveRowCommand, setAlignCommand, deleteSelectedCellsCommand, addColAfterCommand, addColBeforeCommand, addRowAfterCommand, addRowBeforeCommand, tableSchema } from '@jvs-milkdown/preset-gfm';
3
- import { findParent } from '@jvs-milkdown/prose';
4
- import { h, onMounted, onUnmounted, defineComponent, ref, shallowRef, createApp } from 'vue';
2
+ import { selectColCommand, moveColCommand, selectRowCommand, moveRowCommand, mergeCellsCommand, setAlignCommand, deleteSelectedCellsCommand, addColAfterCommand, addColBeforeCommand, addRowAfterCommand, addRowBeforeCommand, tableSchema } from '@jvs-milkdown/preset-gfm';
3
+ import { h, onMounted, onUnmounted, defineComponent, ref, onBeforeUnmount, shallowRef, createApp, triggerRef } from 'vue';
4
+ import { computePosition, offset } from '@floating-ui/dom';
5
5
  import clsx from 'clsx';
6
6
  import DOMPurify from 'dompurify';
7
7
  import { editorViewCtx, commandsCtx } from '@jvs-milkdown/core';
8
- import { computePosition, offset } from '@floating-ui/dom';
9
8
  import { throttle } from 'lodash-es';
9
+ import { findParent } from '@jvs-milkdown/prose';
10
10
 
11
- var __defProp$1 = Object.defineProperty;
12
- var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
13
- var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
14
- var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
15
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
16
- var __spreadValues$1 = (a, b) => {
11
+ var __defProp$2 = Object.defineProperty;
12
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
13
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
14
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
15
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
16
+ var __spreadValues$2 = (a, b) => {
17
17
  for (var prop in b || (b = {}))
18
- if (__hasOwnProp$1.call(b, prop))
19
- __defNormalProp$1(a, prop, b[prop]);
20
- if (__getOwnPropSymbols$1)
21
- for (var prop of __getOwnPropSymbols$1(b)) {
22
- if (__propIsEnum$1.call(b, prop))
23
- __defNormalProp$1(a, prop, b[prop]);
18
+ if (__hasOwnProp$2.call(b, prop))
19
+ __defNormalProp$2(a, prop, b[prop]);
20
+ if (__getOwnPropSymbols$2)
21
+ for (var prop of __getOwnPropSymbols$2(b)) {
22
+ if (__propIsEnum$2.call(b, prop))
23
+ __defNormalProp$2(a, prop, b[prop]);
24
24
  }
25
25
  return a;
26
26
  };
27
27
  function withMeta(plugin, meta) {
28
28
  Object.assign(plugin, {
29
- meta: __spreadValues$1({
29
+ meta: __spreadValues$2({
30
30
  package: "@jvs-milkdown/components"
31
31
  }, meta)
32
32
  });
33
33
  return plugin;
34
34
  }
35
35
 
36
- var __defProp = Object.defineProperty;
37
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
38
- var __hasOwnProp = Object.prototype.hasOwnProperty;
39
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
40
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
41
- var __spreadValues = (a, b) => {
36
+ var __defProp$1 = Object.defineProperty;
37
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
38
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
39
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
40
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
41
+ var __spreadValues$1 = (a, b) => {
42
42
  for (var prop in b || (b = {}))
43
- if (__hasOwnProp.call(b, prop))
44
- __defNormalProp(a, prop, b[prop]);
45
- if (__getOwnPropSymbols)
46
- for (var prop of __getOwnPropSymbols(b)) {
47
- if (__propIsEnum.call(b, prop))
48
- __defNormalProp(a, prop, b[prop]);
43
+ if (__hasOwnProp$1.call(b, prop))
44
+ __defNormalProp$1(a, prop, b[prop]);
45
+ if (__getOwnPropSymbols$1)
46
+ for (var prop of __getOwnPropSymbols$1(b)) {
47
+ if (__propIsEnum$1.call(b, prop))
48
+ __defNormalProp$1(a, prop, b[prop]);
49
49
  }
50
50
  return a;
51
51
  };
@@ -70,11 +70,15 @@ const defaultTableBlockConfig = {
70
70
  return "=";
71
71
  case "row_drag_handle":
72
72
  return "=";
73
+ case "merge_cells":
74
+ return "\u229E";
75
+ case "split_cell":
76
+ return "\u229F";
73
77
  }
74
78
  }
75
79
  };
76
80
  const tableBlockConfig = $ctx(
77
- __spreadValues({}, defaultTableBlockConfig),
81
+ __spreadValues$1({}, defaultTableBlockConfig),
78
82
  "tableBlockConfigCtx"
79
83
  );
80
84
  withMeta(tableBlockConfig, {
@@ -2430,6 +2434,13 @@ function cellAround($pos) {
2430
2434
  for (let d = $pos.depth - 1; d > 0; d--) if ($pos.node(d).type.spec.tableRole == "row") return $pos.node(0).resolve($pos.before(d + 1));
2431
2435
  return null;
2432
2436
  }
2437
+ function cellWrapping($pos) {
2438
+ for (let d = $pos.depth; d > 0; d--) {
2439
+ const role = $pos.node(d).type.spec.tableRole;
2440
+ if (role === "cell" || role === "header_cell") return $pos.node(d);
2441
+ }
2442
+ return null;
2443
+ }
2433
2444
  /**
2434
2445
  * @public
2435
2446
  */
@@ -2747,6 +2758,70 @@ function selectedRect(state) {
2747
2758
  table
2748
2759
  };
2749
2760
  }
2761
+ /**
2762
+ * Split a selected cell, whose rowpan or colspan is greater than one,
2763
+ * into smaller cells with the cell type (th, td) returned by getType function.
2764
+ *
2765
+ * @public
2766
+ */
2767
+ function splitCellWithType(getCellType) {
2768
+ return (state, dispatch) => {
2769
+ const sel = state.selection;
2770
+ let cellNode;
2771
+ let cellPos;
2772
+ if (!(sel instanceof CellSelection)) {
2773
+ var _cellAround;
2774
+ cellNode = cellWrapping(sel.$from);
2775
+ if (!cellNode) return false;
2776
+ cellPos = (_cellAround = cellAround(sel.$from)) === null || _cellAround === void 0 ? void 0 : _cellAround.pos;
2777
+ } else {
2778
+ if (sel.$anchorCell.pos != sel.$headCell.pos) return false;
2779
+ cellNode = sel.$anchorCell.nodeAfter;
2780
+ cellPos = sel.$anchorCell.pos;
2781
+ }
2782
+ if (cellNode == null || cellPos == null) return false;
2783
+ if (cellNode.attrs.colspan == 1 && cellNode.attrs.rowspan == 1) return false;
2784
+ if (dispatch) {
2785
+ let baseAttrs = cellNode.attrs;
2786
+ const attrs = [];
2787
+ const colwidth = baseAttrs.colwidth;
2788
+ if (baseAttrs.rowspan > 1) baseAttrs = {
2789
+ ...baseAttrs,
2790
+ rowspan: 1
2791
+ };
2792
+ if (baseAttrs.colspan > 1) baseAttrs = {
2793
+ ...baseAttrs,
2794
+ colspan: 1
2795
+ };
2796
+ const rect = selectedRect(state), tr = state.tr;
2797
+ for (let i = 0; i < rect.right - rect.left; i++) attrs.push(colwidth ? {
2798
+ ...baseAttrs,
2799
+ colwidth: colwidth && colwidth[i] ? [colwidth[i]] : null
2800
+ } : baseAttrs);
2801
+ let lastCell;
2802
+ for (let row = rect.top; row < rect.bottom; row++) {
2803
+ let pos = rect.map.positionAt(row, rect.left, rect.table);
2804
+ if (row == rect.top) pos += cellNode.nodeSize;
2805
+ for (let col = rect.left, i = 0; col < rect.right; col++, i++) {
2806
+ if (col == rect.left && row == rect.top) continue;
2807
+ tr.insert(lastCell = tr.mapping.map(pos + rect.tableStart, 1), getCellType({
2808
+ node: cellNode,
2809
+ row,
2810
+ col
2811
+ }).createAndFill(attrs[i]));
2812
+ }
2813
+ }
2814
+ tr.setNodeMarkup(cellPos, getCellType({
2815
+ node: cellNode,
2816
+ row: rect.top,
2817
+ col: rect.left
2818
+ }), attrs[0]);
2819
+ if (sel instanceof CellSelection) tr.setSelection(new CellSelection(tr.doc.resolve(sel.$anchorCell.pos), lastCell ? tr.doc.resolve(lastCell) : void 0));
2820
+ dispatch(tr);
2821
+ }
2822
+ return true;
2823
+ };
2824
+ }
2750
2825
  function deprecated_toggleHeader(type) {
2751
2826
  return function(state, dispatch) {
2752
2827
  if (!isInTable(state)) return false;
@@ -2978,9 +3053,7 @@ function prepareDndContext(refs) {
2978
3053
  tableWrapperRef,
2979
3054
  contentWrapperRef,
2980
3055
  yLineHandleRef,
2981
- xLineHandleRef,
2982
- colHandleRef,
2983
- rowHandleRef
3056
+ xLineHandleRef
2984
3057
  } = refs;
2985
3058
  const preview = dragPreviewRef.value;
2986
3059
  if (!preview) return;
@@ -2996,10 +3069,6 @@ function prepareDndContext(refs) {
2996
3069
  if (!yHandle) return;
2997
3070
  const xHandle = xLineHandleRef.value;
2998
3071
  if (!xHandle) return;
2999
- const colHandle = colHandleRef.value;
3000
- if (!colHandle) return;
3001
- const rowHandle = rowHandleRef.value;
3002
- if (!rowHandle) return;
3003
3072
  const context = {
3004
3073
  preview,
3005
3074
  wrapper,
@@ -3007,9 +3076,7 @@ function prepareDndContext(refs) {
3007
3076
  contentRoot,
3008
3077
  previewRoot,
3009
3078
  yHandle,
3010
- xHandle,
3011
- colHandle,
3012
- rowHandle
3079
+ xHandle
3013
3080
  };
3014
3081
  return context;
3015
3082
  }
@@ -3078,16 +3145,9 @@ function createDragColHandler(refs, ctx) {
3078
3145
  };
3079
3146
  }
3080
3147
  function updateDragInfo(axis, event, context, refs) {
3081
- const { xHandle, yHandle, colHandle, rowHandle, preview } = context;
3148
+ const { xHandle, yHandle, preview } = context;
3082
3149
  xHandle.dataset.displayType = axis === "y" ? "indicator" : "none";
3083
3150
  yHandle.dataset.displayType = axis === "x" ? "indicator" : "none";
3084
- if (axis === "y") {
3085
- colHandle.dataset.show = "false";
3086
- hideButtonGroup(rowHandle);
3087
- } else {
3088
- rowHandle.dataset.show = "false";
3089
- hideButtonGroup(colHandle);
3090
- }
3091
3151
  const { hoverIndex, dragInfo } = refs;
3092
3152
  const [rowIndex, colIndex] = hoverIndex.value;
3093
3153
  dragInfo.value = {
@@ -3109,10 +3169,6 @@ function handleDrag(refs, event, ctx, fn) {
3109
3169
  fn(context);
3110
3170
  });
3111
3171
  }
3112
- function hideButtonGroup(handle) {
3113
- var _a;
3114
- (_a = handle.querySelector(".button-group")) == null ? void 0 : _a.setAttribute("data-show", "false");
3115
- }
3116
3172
 
3117
3173
  function findNodeIndex(parent, child) {
3118
3174
  for (let i = 0; i < parent.childCount; i++) {
@@ -3120,35 +3176,6 @@ function findNodeIndex(parent, child) {
3120
3176
  }
3121
3177
  return -1;
3122
3178
  }
3123
- function findPointerIndex(event, view) {
3124
- var _a, _b, _c;
3125
- if (!view) return;
3126
- try {
3127
- const posAtCoords = view.posAtCoords({
3128
- left: event.clientX,
3129
- top: event.clientY
3130
- });
3131
- if (!posAtCoords) return;
3132
- const pos = posAtCoords == null ? void 0 : posAtCoords.inside;
3133
- if (pos == null || pos < 0) return;
3134
- const $pos = view.state.doc.resolve(pos);
3135
- const node = view.state.doc.nodeAt(pos);
3136
- if (!node) return;
3137
- const cellType = ["table_cell", "table_header"];
3138
- const rowType = ["table_row", "table_header_row"];
3139
- const cell = cellType.includes(node.type.name) ? node : (_a = findParent((node2) => cellType.includes(node2.type.name))($pos)) == null ? void 0 : _a.node;
3140
- const row = (_b = findParent((node2) => rowType.includes(node2.type.name))(
3141
- $pos
3142
- )) == null ? void 0 : _b.node;
3143
- const table = (_c = findParent((node2) => node2.type.name === "table")($pos)) == null ? void 0 : _c.node;
3144
- if (!cell || !row || !table) return;
3145
- const columnIndex = findNodeIndex(row, cell);
3146
- const rowIndex = findNodeIndex(table, row);
3147
- return [rowIndex, columnIndex];
3148
- } catch (e) {
3149
- return void 0;
3150
- }
3151
- }
3152
3179
  function getRelatedDOM(contentWrapperRef, [rowIndex, columnIndex]) {
3153
3180
  const content = contentWrapperRef.value;
3154
3181
  if (!content) return;
@@ -3178,14 +3205,7 @@ function recoveryStateBetweenUpdate(refs, view, node) {
3178
3205
  if (selection.isColSelection()) {
3179
3206
  const { $head } = selection;
3180
3207
  const colIndex = $head.index($head.depth - 1);
3181
- computeColHandlePositionByIndex({
3182
- refs,
3183
- index: [0, colIndex],
3184
- before: (handleDOM) => {
3185
- var _a;
3186
- (_a = handleDOM.querySelector(".button-group")) == null ? void 0 : _a.setAttribute("data-show", "true");
3187
- }
3188
- });
3208
+ refs.hoverIndex.value = [0, colIndex];
3189
3209
  return;
3190
3210
  }
3191
3211
  if (selection.isRowSelection()) {
@@ -3195,63 +3215,9 @@ function recoveryStateBetweenUpdate(refs, view, node) {
3195
3215
  )($head);
3196
3216
  if (!rowNode) return;
3197
3217
  const rowIndex = findNodeIndex(table.node, rowNode.node);
3198
- computeRowHandlePositionByIndex({
3199
- refs,
3200
- index: [rowIndex, 0],
3201
- before: (handleDOM) => {
3202
- var _a;
3203
- if (rowIndex > 0)
3204
- (_a = handleDOM.querySelector(".button-group")) == null ? void 0 : _a.setAttribute("data-show", "true");
3205
- }
3206
- });
3218
+ refs.hoverIndex.value = [rowIndex, 0];
3207
3219
  }
3208
3220
  }
3209
- function computeColHandlePositionByIndex({
3210
- refs,
3211
- index,
3212
- before,
3213
- after
3214
- }) {
3215
- const { contentWrapperRef, colHandleRef, hoverIndex } = refs;
3216
- const colHandle = colHandleRef.value;
3217
- if (!colHandle) return;
3218
- hoverIndex.value = index;
3219
- const dom = getRelatedDOM(contentWrapperRef, index);
3220
- if (!dom) return;
3221
- const { headerCol: col } = dom;
3222
- colHandle.dataset.show = "true";
3223
- if (before) before(colHandle);
3224
- computePosition(col, colHandle, { placement: "top" }).then(({ x, y }) => {
3225
- Object.assign(colHandle.style, {
3226
- left: `${x}px`,
3227
- top: `${y}px`
3228
- });
3229
- if (after) after(colHandle);
3230
- }).catch(console.error);
3231
- }
3232
- function computeRowHandlePositionByIndex({
3233
- refs,
3234
- index,
3235
- before,
3236
- after
3237
- }) {
3238
- const { contentWrapperRef, rowHandleRef, hoverIndex } = refs;
3239
- const rowHandle = rowHandleRef.value;
3240
- if (!rowHandle) return;
3241
- hoverIndex.value = index;
3242
- const dom = getRelatedDOM(contentWrapperRef, index);
3243
- if (!dom) return;
3244
- const { row } = dom;
3245
- rowHandle.dataset.show = "true";
3246
- if (before) before(rowHandle);
3247
- computePosition(row, rowHandle, { placement: "left" }).then(({ x, y }) => {
3248
- Object.assign(rowHandle.style, {
3249
- left: `${x}px`,
3250
- top: `${y}px`
3251
- });
3252
- if (after) after(rowHandle);
3253
- }).catch(console.error);
3254
- }
3255
3221
 
3256
3222
  function findDragOverElement(elements, pointer, axis) {
3257
3223
  const startProp = axis === "x" ? "left" : "top";
@@ -3380,10 +3346,6 @@ function useDragHandlers(refs, ctx, getPos) {
3380
3346
  if (!info) return;
3381
3347
  if (!ctx) return;
3382
3348
  if (preview.dataset.show === "false") return;
3383
- const colHandle = refs.colHandleRef.value;
3384
- if (!colHandle) return;
3385
- const rowHandle = refs.rowHandleRef.value;
3386
- if (!rowHandle) return;
3387
3349
  yHandle.dataset.show = "false";
3388
3350
  xHandle.dataset.show = "false";
3389
3351
  if (info.startIndex === info.endIndex) return;
@@ -3400,10 +3362,7 @@ function useDragHandlers(refs, ctx, getPos) {
3400
3362
  });
3401
3363
  commands.call(moveColCommand.key, payload);
3402
3364
  const index = [0, info.endIndex];
3403
- computeColHandlePositionByIndex({
3404
- refs,
3405
- index
3406
- });
3365
+ refs.hoverIndex.value = index;
3407
3366
  } else {
3408
3367
  commands.call(selectRowCommand.key, {
3409
3368
  pos: payload.pos,
@@ -3411,10 +3370,7 @@ function useDragHandlers(refs, ctx, getPos) {
3411
3370
  });
3412
3371
  commands.call(moveRowCommand.key, payload);
3413
3372
  const index = [info.endIndex, 0];
3414
- computeRowHandlePositionByIndex({
3415
- refs,
3416
- index
3417
- });
3373
+ refs.hoverIndex.value = index;
3418
3374
  }
3419
3375
  requestAnimationFrame(() => {
3420
3376
  ctx.get(editorViewCtx).focus();
@@ -3438,80 +3394,56 @@ function useDragHandlers(refs, ctx, getPos) {
3438
3394
  }
3439
3395
 
3440
3396
  function useOperation(refs, ctx, getPos) {
3441
- const {
3442
- xLineHandleRef,
3443
- contentWrapperRef,
3444
- colHandleRef,
3445
- rowHandleRef,
3446
- hoverIndex,
3447
- lineHoverIndex
3448
- } = refs;
3449
- const onAddRow = () => {
3397
+ const { contentWrapperRef, hoverIndex } = refs;
3398
+ const addRowByIndex = (index) => {
3450
3399
  var _a, _b, _c;
3451
- if (!ctx) return;
3452
- const xHandle = xLineHandleRef.value;
3453
- if (!xHandle) return;
3454
- const [rowIndex] = lineHoverIndex.value;
3455
- if (rowIndex < 0) return;
3456
- if (!ctx.get(editorViewCtx).editable) return;
3400
+ if (!ctx || !ctx.get(editorViewCtx).editable) return;
3457
3401
  const rows = Array.from(
3458
3402
  (_b = (_a = contentWrapperRef.value) == null ? void 0 : _a.querySelectorAll("tr")) != null ? _b : []
3459
3403
  );
3460
3404
  const commands = ctx.get(commandsCtx);
3461
3405
  const pos = ((_c = getPos == null ? void 0 : getPos()) != null ? _c : 0) + 1;
3462
- if (rows.length === rowIndex) {
3463
- commands.call(selectRowCommand.key, { pos, index: rowIndex - 1 });
3406
+ if (rows.length === index) {
3407
+ commands.call(selectRowCommand.key, { pos, index: index - 1 });
3464
3408
  commands.call(addRowAfterCommand.key);
3465
3409
  } else {
3466
- commands.call(selectRowCommand.key, { pos, index: rowIndex });
3410
+ commands.call(selectRowCommand.key, { pos, index });
3467
3411
  commands.call(addRowBeforeCommand.key);
3468
3412
  }
3469
- commands.call(selectRowCommand.key, { pos, index: rowIndex });
3470
- xHandle.dataset.show = "false";
3413
+ commands.call(selectRowCommand.key, { pos, index });
3471
3414
  };
3472
- const onAddCol = () => {
3415
+ const addColByIndex = (index) => {
3473
3416
  var _a, _b, _c, _d;
3474
- if (!ctx) return;
3475
- const xHandle = xLineHandleRef.value;
3476
- if (!xHandle) return;
3477
- const [_, colIndex] = lineHoverIndex.value;
3478
- if (colIndex < 0) return;
3479
- if (!ctx.get(editorViewCtx).editable) return;
3417
+ if (!ctx || !ctx.get(editorViewCtx).editable) return;
3480
3418
  const cols = Array.from(
3481
3419
  (_c = (_b = (_a = contentWrapperRef.value) == null ? void 0 : _a.querySelector("tr")) == null ? void 0 : _b.children) != null ? _c : []
3482
3420
  );
3483
3421
  const commands = ctx.get(commandsCtx);
3484
3422
  const pos = ((_d = getPos == null ? void 0 : getPos()) != null ? _d : 0) + 1;
3485
- if (cols.length === colIndex) {
3486
- commands.call(selectColCommand.key, { pos, index: colIndex - 1 });
3423
+ if (cols.length === index) {
3424
+ commands.call(selectColCommand.key, { pos, index: index - 1 });
3487
3425
  commands.call(addColAfterCommand.key);
3488
3426
  } else {
3489
- commands.call(selectColCommand.key, { pos, index: colIndex });
3427
+ commands.call(selectColCommand.key, { pos, index });
3490
3428
  commands.call(addColBeforeCommand.key);
3491
3429
  }
3492
- commands.call(selectColCommand.key, { pos, index: colIndex });
3430
+ commands.call(selectColCommand.key, { pos, index });
3493
3431
  };
3494
3432
  const selectCol = () => {
3495
- var _a, _b;
3433
+ var _a;
3496
3434
  if (!ctx) return;
3497
3435
  const [_, colIndex] = hoverIndex.value;
3498
3436
  const commands = ctx.get(commandsCtx);
3499
3437
  const pos = ((_a = getPos == null ? void 0 : getPos()) != null ? _a : 0) + 1;
3500
3438
  commands.call(selectColCommand.key, { pos, index: colIndex });
3501
- const buttonGroup = (_b = colHandleRef.value) == null ? void 0 : _b.querySelector(".button-group");
3502
- if (buttonGroup)
3503
- buttonGroup.dataset.show = buttonGroup.dataset.show === "true" ? "false" : "true";
3504
3439
  };
3505
3440
  const selectRow = () => {
3506
- var _a, _b;
3441
+ var _a;
3507
3442
  if (!ctx) return;
3508
3443
  const [rowIndex, _] = hoverIndex.value;
3509
3444
  const commands = ctx.get(commandsCtx);
3510
3445
  const pos = ((_a = getPos == null ? void 0 : getPos()) != null ? _a : 0) + 1;
3511
3446
  commands.call(selectRowCommand.key, { pos, index: rowIndex });
3512
- const buttonGroup = (_b = rowHandleRef.value) == null ? void 0 : _b.querySelector(".button-group");
3513
- if (buttonGroup && rowIndex > 0)
3514
- buttonGroup.dataset.show = buttonGroup.dataset.show === "true" ? "false" : "true";
3515
3447
  };
3516
3448
  const deleteSelected = (e) => {
3517
3449
  if (!ctx) return;
@@ -3535,137 +3467,107 @@ function useOperation(refs, ctx, getPos) {
3535
3467
  ctx.get(editorViewCtx).focus();
3536
3468
  });
3537
3469
  };
3470
+ const onMergeCells = (e) => {
3471
+ if (!ctx) return;
3472
+ if (!ctx.get(editorViewCtx).editable) return;
3473
+ e.preventDefault();
3474
+ e.stopPropagation();
3475
+ const commands = ctx.get(commandsCtx);
3476
+ commands.call(mergeCellsCommand.key);
3477
+ requestAnimationFrame(() => {
3478
+ ctx.get(editorViewCtx).focus();
3479
+ });
3480
+ };
3481
+ const onSplitCell = (e) => {
3482
+ var _a, _b;
3483
+ if (!ctx) return;
3484
+ if (!ctx.get(editorViewCtx).editable) return;
3485
+ e.preventDefault();
3486
+ e.stopPropagation();
3487
+ const view = ctx.get(editorViewCtx);
3488
+ const { state } = view;
3489
+ const { selection } = state;
3490
+ let cellNode = null;
3491
+ let cellPos = null;
3492
+ if (selection instanceof CellSelection) {
3493
+ selection.forEachCell((cell, pos) => {
3494
+ var _a2, _b2;
3495
+ if (!cellNode && (((_a2 = cell.attrs.rowspan) != null ? _a2 : 1) > 1 || ((_b2 = cell.attrs.colspan) != null ? _b2 : 1) > 1)) {
3496
+ cellNode = cell;
3497
+ cellPos = pos;
3498
+ }
3499
+ });
3500
+ } else {
3501
+ const { $from } = selection;
3502
+ for (let d = $from.depth; d > 0; d--) {
3503
+ const n = $from.node(d);
3504
+ if (n.type.name === "table_cell" || n.type.name === "table_header") {
3505
+ cellNode = n;
3506
+ cellPos = $from.before(d);
3507
+ break;
3508
+ }
3509
+ }
3510
+ }
3511
+ if (!cellNode || cellPos == null) return;
3512
+ if (((_a = cellNode.attrs.colspan) != null ? _a : 1) == 1 && ((_b = cellNode.attrs.rowspan) != null ? _b : 1) == 1)
3513
+ return;
3514
+ const $cellPos = state.doc.resolve(cellPos + 2);
3515
+ view.dispatch(state.tr.setSelection(TextSelection.near($cellPos)));
3516
+ const newState = view.state;
3517
+ const { table } = selectedRect(newState);
3518
+ const { schema } = newState;
3519
+ splitCellWithType(({ row }) => {
3520
+ const rowNode = table.child(row);
3521
+ return rowNode.type.name === "table_header_row" ? schema.nodes.table_header : schema.nodes.table_cell;
3522
+ })(newState, (tr) => {
3523
+ view.dispatch(tr);
3524
+ });
3525
+ requestAnimationFrame(() => {
3526
+ view.focus();
3527
+ });
3528
+ };
3538
3529
  return {
3539
- onAddRow,
3540
- onAddCol,
3530
+ addRowByIndex,
3531
+ addColByIndex,
3541
3532
  selectCol,
3542
3533
  selectRow,
3543
3534
  deleteSelected,
3544
- onAlign
3535
+ onAlign,
3536
+ onMergeCells,
3537
+ onSplitCell
3545
3538
  };
3546
3539
  }
3547
3540
 
3548
- function createPointerMoveHandler(refs, view) {
3549
- return throttle((e) => {
3550
- if (!(view == null ? void 0 : view.editable)) return;
3551
- const {
3552
- contentWrapperRef,
3553
- yLineHandleRef,
3554
- xLineHandleRef,
3555
- colHandleRef,
3556
- rowHandleRef,
3557
- hoverIndex,
3558
- lineHoverIndex
3559
- } = refs;
3560
- const yHandle = yLineHandleRef.value;
3561
- if (!yHandle) return;
3562
- const xHandle = xLineHandleRef.value;
3563
- if (!xHandle) return;
3564
- const content = contentWrapperRef.value;
3565
- if (!content) return;
3566
- const rowHandle = rowHandleRef.value;
3567
- if (!rowHandle) return;
3568
- const colHandle = colHandleRef.value;
3569
- if (!colHandle) return;
3570
- const index = findPointerIndex(e, view);
3571
- if (!index) return;
3572
- const dom = getRelatedDOM(contentWrapperRef, index);
3573
- if (!dom) return;
3574
- const [rowIndex, colIndex] = index;
3575
- const boundary = dom.col.getBoundingClientRect();
3576
- const closeToBoundaryLeft = Math.abs(e.clientX - boundary.left) < 8;
3577
- const closeToBoundaryRight = Math.abs(boundary.right - e.clientX) < 8;
3578
- const closeToBoundaryTop = Math.abs(e.clientY - boundary.top) < 8;
3579
- const closeToBoundaryBottom = Math.abs(boundary.bottom - e.clientY) < 8;
3580
- const closeToBoundary = closeToBoundaryLeft || closeToBoundaryRight || closeToBoundaryTop || closeToBoundaryBottom;
3581
- const rowButtonGroup = rowHandle.querySelector(".button-group");
3582
- const colButtonGroup = colHandle.querySelector(".button-group");
3583
- if (rowButtonGroup) rowButtonGroup.dataset.show = "false";
3584
- if (colButtonGroup) colButtonGroup.dataset.show = "false";
3585
- if (closeToBoundary) {
3586
- const contentBoundary = content.getBoundingClientRect();
3587
- rowHandle.dataset.show = "false";
3588
- colHandle.dataset.show = "false";
3589
- xHandle.dataset.displayType = "tool";
3590
- yHandle.dataset.displayType = "tool";
3591
- const yHandleWidth = yHandle.getBoundingClientRect().width;
3592
- const xHandleHeight = xHandle.getBoundingClientRect().height;
3593
- if (closeToBoundaryLeft || closeToBoundaryRight) {
3594
- lineHoverIndex.value[1] = closeToBoundaryLeft ? colIndex : colIndex + 1;
3595
- computePosition(dom.col, yHandle, {
3596
- placement: closeToBoundaryLeft ? "left" : "right",
3597
- middleware: [offset(closeToBoundaryLeft ? -1 * yHandleWidth : 0)]
3598
- }).then(({ x }) => {
3599
- yHandle.dataset.show = "true";
3600
- Object.assign(yHandle.style, {
3601
- height: `${contentBoundary.height}px`,
3602
- left: `${x}px`
3603
- });
3604
- }).catch(console.error);
3605
- } else {
3606
- yHandle.dataset.show = "false";
3607
- }
3608
- if (index[0] !== 0 && (closeToBoundaryTop || closeToBoundaryBottom)) {
3609
- lineHoverIndex.value[0] = closeToBoundaryTop ? rowIndex : rowIndex + 1;
3610
- computePosition(dom.row, xHandle, {
3611
- placement: closeToBoundaryTop ? "top" : "bottom",
3612
- middleware: [offset(closeToBoundaryTop ? -1 * xHandleHeight : 0)]
3613
- }).then(({ y }) => {
3614
- xHandle.dataset.show = "true";
3615
- Object.assign(xHandle.style, {
3616
- width: `${contentBoundary.width}px`,
3617
- top: `${y}px`
3618
- });
3619
- }).catch(console.error);
3620
- } else {
3621
- xHandle.dataset.show = "false";
3622
- }
3623
- return;
3624
- }
3625
- lineHoverIndex.value = [-1, -1];
3626
- yHandle.dataset.show = "false";
3627
- xHandle.dataset.show = "false";
3628
- rowHandle.dataset.show = "true";
3629
- colHandle.dataset.show = "true";
3630
- computeRowHandlePositionByIndex({
3631
- refs,
3632
- index
3633
- });
3634
- computeColHandlePositionByIndex({
3635
- refs,
3636
- index
3637
- });
3638
- hoverIndex.value = index;
3639
- }, 20);
3640
- }
3641
- function createPointerLeaveHandler(refs) {
3642
- return () => {
3643
- const { rowHandleRef, colHandleRef, yLineHandleRef, xLineHandleRef } = refs;
3644
- setTimeout(() => {
3645
- const rowHandle = rowHandleRef.value;
3646
- if (!rowHandle) return;
3647
- const colHandle = colHandleRef.value;
3648
- if (!colHandle) return;
3649
- const yHandle = yLineHandleRef.value;
3650
- if (!yHandle) return;
3651
- const xHandle = xLineHandleRef.value;
3652
- if (!xHandle) return;
3653
- rowHandle.dataset.show = "false";
3654
- colHandle.dataset.show = "false";
3655
- yHandle.dataset.show = "false";
3656
- xHandle.dataset.show = "false";
3657
- }, 200);
3541
+ function usePointerHandlers(_refs, _view) {
3542
+ const pointerMove = (_e) => {
3543
+ };
3544
+ const pointerLeave = () => {
3658
3545
  };
3659
- }
3660
- function usePointerHandlers(refs, view) {
3661
- const pointerMove = createPointerMoveHandler(refs, view);
3662
- const pointerLeave = createPointerLeaveHandler(refs);
3663
3546
  return {
3664
3547
  pointerMove,
3665
3548
  pointerLeave
3666
3549
  };
3667
3550
  }
3668
3551
 
3552
+ var __defProp = Object.defineProperty;
3553
+ var __defProps = Object.defineProperties;
3554
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
3555
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3556
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
3557
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
3558
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3559
+ var __spreadValues = (a, b) => {
3560
+ for (var prop in b || (b = {}))
3561
+ if (__hasOwnProp.call(b, prop))
3562
+ __defNormalProp(a, prop, b[prop]);
3563
+ if (__getOwnPropSymbols)
3564
+ for (var prop of __getOwnPropSymbols(b)) {
3565
+ if (__propIsEnum.call(b, prop))
3566
+ __defNormalProp(a, prop, b[prop]);
3567
+ }
3568
+ return a;
3569
+ };
3570
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
3669
3571
  keepAlive(h);
3670
3572
  const TableBlock = defineComponent({
3671
3573
  props: {
@@ -3696,52 +3598,267 @@ const TableBlock = defineComponent({
3696
3598
  },
3697
3599
  setup({ view, node, ctx, getPos, config, onMount }) {
3698
3600
  const contentWrapperRef = ref();
3601
+ let contentMounted = false;
3699
3602
  const contentWrapperFunctionRef = (div) => {
3700
3603
  if (div == null) return;
3701
3604
  if (div instanceof HTMLElement) {
3702
3605
  contentWrapperRef.value = div;
3703
- onMount(div);
3606
+ if (!contentMounted) {
3607
+ onMount(div);
3608
+ contentMounted = true;
3609
+ }
3704
3610
  } else {
3705
3611
  contentWrapperRef.value = void 0;
3706
3612
  }
3707
3613
  };
3708
- const colHandleRef = ref();
3709
- const rowHandleRef = ref();
3710
- const xLineHandleRef = ref();
3711
- const yLineHandleRef = ref();
3712
3614
  const tableWrapperRef = ref();
3713
3615
  const dragPreviewRef = ref();
3714
3616
  const hoverIndex = ref([0, 0]);
3715
- const lineHoverIndex = ref([-1, -1]);
3617
+ ref([-1, -1]);
3716
3618
  const dragInfo = ref();
3619
+ const resizingCol = ref(-1);
3620
+ const colWidths = ref([]);
3621
+ const syncColWidths = () => {
3622
+ if (resizingCol.value >= 0) return;
3623
+ const firstRow = node.value.firstChild;
3624
+ if (!firstRow) return;
3625
+ const colCount = firstRow.content.childCount;
3626
+ const widths = [];
3627
+ const hasBounds = colBounds.value.length === colCount;
3628
+ for (let i = 0; i < colCount; i++) {
3629
+ const cell = firstRow.content.child(i);
3630
+ const w = cell.attrs.colwidth;
3631
+ if (Array.isArray(w) && w[0]) {
3632
+ widths.push(w[0]);
3633
+ } else if (hasBounds) {
3634
+ widths.push(Math.round(colBounds.value[i].width));
3635
+ } else {
3636
+ widths.push(0);
3637
+ }
3638
+ }
3639
+ colWidths.value = widths;
3640
+ };
3717
3641
  const refs = {
3718
3642
  dragPreviewRef,
3719
3643
  tableWrapperRef,
3720
3644
  contentWrapperRef,
3721
- yLineHandleRef,
3722
- xLineHandleRef,
3723
- colHandleRef,
3724
- rowHandleRef,
3725
3645
  hoverIndex,
3726
- lineHoverIndex,
3727
3646
  dragInfo
3728
3647
  };
3729
- const { pointerLeave, pointerMove } = usePointerHandlers(refs, view);
3648
+ const { pointerLeave, pointerMove } = usePointerHandlers();
3730
3649
  const { dragRow, dragCol } = useDragHandlers(refs, ctx, getPos);
3731
3650
  const {
3732
- onAddRow,
3733
- onAddCol,
3651
+ addRowByIndex,
3652
+ addColByIndex,
3734
3653
  selectCol,
3735
3654
  selectRow,
3736
3655
  deleteSelected,
3737
- onAlign
3656
+ onAlign,
3657
+ onMergeCells,
3658
+ onSplitCell
3738
3659
  } = useOperation(refs, ctx, getPos);
3660
+ const cellToolbarRef = ref();
3661
+ const showCellToolbar = ref(false);
3662
+ const canMerge = ref(false);
3663
+ const canSplit = ref(false);
3664
+ const updateCellToolbar = () => {
3665
+ const { selection } = view.state;
3666
+ const tablePos = getPos();
3667
+ if (tablePos == null) {
3668
+ showCellToolbar.value = false;
3669
+ return;
3670
+ }
3671
+ const tableEnd = tablePos + node.value.nodeSize;
3672
+ if (selection.from < tablePos || selection.from > tableEnd) {
3673
+ showCellToolbar.value = false;
3674
+ return;
3675
+ }
3676
+ if (selection instanceof CellSelection) {
3677
+ let _cellCount = 0;
3678
+ let hasMerged = false;
3679
+ selection.forEachCell((cell) => {
3680
+ var _a, _b;
3681
+ _cellCount++;
3682
+ if (((_a = cell.attrs.rowspan) != null ? _a : 1) > 1 || ((_b = cell.attrs.colspan) != null ? _b : 1) > 1) {
3683
+ hasMerged = true;
3684
+ }
3685
+ });
3686
+ canMerge.value = _cellCount > 1;
3687
+ canSplit.value = hasMerged;
3688
+ } else {
3689
+ showCellToolbar.value = false;
3690
+ return;
3691
+ }
3692
+ if (!canMerge.value && !canSplit.value) {
3693
+ showCellToolbar.value = false;
3694
+ return;
3695
+ }
3696
+ showCellToolbar.value = true;
3697
+ requestAnimationFrame(() => {
3698
+ const toolbar = cellToolbarRef.value;
3699
+ const content = contentWrapperRef.value;
3700
+ if (!toolbar || !content) return;
3701
+ const selectedCells = content.querySelectorAll(".selectedCell");
3702
+ let refEl = selectedCells.length > 0 ? selectedCells[0] : null;
3703
+ if (!refEl && !(view.state.selection instanceof CellSelection)) {
3704
+ const { $from } = view.state.selection;
3705
+ for (let d = $from.depth; d > 0; d--) {
3706
+ const node2 = $from.node(d);
3707
+ if (node2.type.name === "table_cell" || node2.type.name === "table_header") {
3708
+ const cellPos = $from.before(d);
3709
+ const dom = view.nodeDOM(cellPos);
3710
+ if (dom instanceof HTMLElement) refEl = dom;
3711
+ break;
3712
+ }
3713
+ }
3714
+ }
3715
+ if (!refEl) return;
3716
+ computePosition(refEl, toolbar, { placement: "top" }).then(({ x, y }) => {
3717
+ toolbar.style.left = `${x}px`;
3718
+ toolbar.style.top = `${y}px`;
3719
+ toolbar.dataset.show = "true";
3720
+ }).catch(console.error);
3721
+ });
3722
+ };
3723
+ const colBounds = ref([]);
3724
+ const rowBounds = ref([]);
3725
+ let ro = null;
3726
+ let mo = null;
3727
+ const updateBounds = () => {
3728
+ const content = contentWrapperRef.value;
3729
+ if (!content) return;
3730
+ const firstRow = content.querySelector("tr");
3731
+ const tableRect = content.getBoundingClientRect();
3732
+ if (firstRow) {
3733
+ colBounds.value = Array.from(firstRow.children).map((c) => {
3734
+ const rect = c.getBoundingClientRect();
3735
+ return { left: rect.left - tableRect.left, width: rect.width };
3736
+ });
3737
+ }
3738
+ const rows = content.querySelectorAll("tr");
3739
+ rowBounds.value = Array.from(rows).map((c) => {
3740
+ const rect = c.getBoundingClientRect();
3741
+ return { top: rect.top - tableRect.top, height: rect.height };
3742
+ });
3743
+ syncColWidths();
3744
+ };
3745
+ const startResize = (e, colIndex) => {
3746
+ var _a, _b;
3747
+ if (!view.editable) return;
3748
+ e.preventDefault();
3749
+ e.stopPropagation();
3750
+ const startX = e.clientX;
3751
+ const startWidth = (_b = (_a = colBounds.value[colIndex]) == null ? void 0 : _a.width) != null ? _b : 100;
3752
+ resizingCol.value = colIndex;
3753
+ const onMove = (ev) => {
3754
+ const delta = ev.clientX - startX;
3755
+ const newWidth = Math.max(50, startWidth + delta);
3756
+ colWidths.value = colWidths.value.map(
3757
+ (w, i) => i === colIndex ? newWidth : w
3758
+ );
3759
+ };
3760
+ const onUp = (ev) => {
3761
+ document.removeEventListener("pointermove", onMove);
3762
+ document.removeEventListener("pointerup", onUp);
3763
+ const delta = ev.clientX - startX;
3764
+ const newWidth = Math.max(50, startWidth + delta);
3765
+ const tablePos = getPos();
3766
+ if (tablePos == null) {
3767
+ resizingCol.value = -1;
3768
+ return;
3769
+ }
3770
+ const tableNode = node.value;
3771
+ let tr = view.state.tr;
3772
+ let pos = tablePos + 1;
3773
+ tableNode.forEach((row) => {
3774
+ let cellIdx = 0;
3775
+ row.forEach((cell, _offset, cellNodePos) => {
3776
+ if (cellIdx === colIndex) {
3777
+ const absPos = tablePos + 1 + cellNodePos;
3778
+ const colwidth = [Math.round(newWidth)];
3779
+ tr = tr.setNodeMarkup(absPos, void 0, __spreadProps(__spreadValues({}, cell.attrs), {
3780
+ colwidth
3781
+ }));
3782
+ }
3783
+ cellIdx++;
3784
+ });
3785
+ pos += row.nodeSize;
3786
+ });
3787
+ resizingCol.value = -1;
3788
+ if (tr.docChanged) view.dispatch(tr);
3789
+ };
3790
+ document.addEventListener("pointermove", onMove);
3791
+ document.addEventListener("pointerup", onUp);
3792
+ };
3793
+ const activeColIndex = ref(-1);
3794
+ const activeRowIndex = ref(-1);
3795
+ const dispatchListener = () => {
3796
+ updateCellToolbar();
3797
+ const { selection } = view.state;
3798
+ if (selection instanceof CellSelection) {
3799
+ if (selection.isColSelection()) {
3800
+ const { $head } = selection;
3801
+ activeColIndex.value = $head.index($head.depth - 1);
3802
+ activeRowIndex.value = -1;
3803
+ } else if (selection.isRowSelection()) {
3804
+ activeColIndex.value = -1;
3805
+ } else {
3806
+ activeColIndex.value = -1;
3807
+ activeRowIndex.value = -1;
3808
+ }
3809
+ } else {
3810
+ activeColIndex.value = -1;
3811
+ activeRowIndex.value = -1;
3812
+ }
3813
+ };
3739
3814
  onMounted(() => {
3740
3815
  requestAnimationFrame(() => {
3741
3816
  if (view.editable) recoveryStateBetweenUpdate(refs, view, node.value);
3817
+ syncColWidths();
3818
+ });
3819
+ view.dom.addEventListener("keyup", dispatchListener);
3820
+ view.dom.addEventListener("pointerup", dispatchListener);
3821
+ updateCellToolbar();
3822
+ ro = new ResizeObserver(() => {
3823
+ requestAnimationFrame(updateBounds);
3742
3824
  });
3825
+ mo = new MutationObserver((mutations) => {
3826
+ let shouldUpdate = false;
3827
+ for (const mut of mutations) {
3828
+ if (mut.type === "childList") {
3829
+ for (const node2 of mut.addedNodes) {
3830
+ if (node2 instanceof HTMLElement && ["TR", "TD", "TH", "TBODY"].includes(node2.nodeName))
3831
+ shouldUpdate = true;
3832
+ }
3833
+ for (const node2 of mut.removedNodes) {
3834
+ if (node2 instanceof HTMLElement && ["TR", "TD", "TH", "TBODY"].includes(node2.nodeName))
3835
+ shouldUpdate = true;
3836
+ }
3837
+ } else if (mut.type === "attributes") {
3838
+ shouldUpdate = true;
3839
+ }
3840
+ }
3841
+ if (shouldUpdate) {
3842
+ requestAnimationFrame(updateBounds);
3843
+ }
3844
+ });
3845
+ if (contentWrapperRef.value) {
3846
+ ro.observe(contentWrapperRef.value);
3847
+ mo.observe(contentWrapperRef.value, {
3848
+ childList: true,
3849
+ subtree: true,
3850
+ attributes: true
3851
+ });
3852
+ }
3853
+ });
3854
+ onBeforeUnmount(() => {
3855
+ view.dom.removeEventListener("keyup", dispatchListener);
3856
+ view.dom.removeEventListener("pointerup", dispatchListener);
3857
+ if (ro) ro.disconnect();
3858
+ if (mo) mo.disconnect();
3743
3859
  });
3744
3860
  return () => {
3861
+ updateCellToolbar();
3745
3862
  return /* @__PURE__ */ h(
3746
3863
  "div",
3747
3864
  {
@@ -3749,62 +3866,189 @@ const TableBlock = defineComponent({
3749
3866
  onDragover: (e) => e.preventDefault(),
3750
3867
  onDragleave: (e) => e.preventDefault(),
3751
3868
  onPointermove: pointerMove,
3752
- onPointerleave: pointerLeave
3869
+ onPointerleave: pointerLeave,
3870
+ class: "milkdown-table-wrapper-outer"
3753
3871
  },
3754
3872
  /* @__PURE__ */ h(
3755
3873
  "div",
3756
3874
  {
3757
- "data-show": "false",
3875
+ ref: cellToolbarRef,
3876
+ "data-show": showCellToolbar.value ? "true" : "false",
3877
+ class: "cell-toolbar",
3758
3878
  contenteditable: "false",
3759
- draggable: "true",
3879
+ onPointerdown: (e) => e.stopPropagation()
3880
+ },
3881
+ canMerge.value && /* @__PURE__ */ h(
3882
+ "button",
3883
+ {
3884
+ type: "button",
3885
+ class: "cell-toolbar-btn",
3886
+ onPointerdown: onMergeCells
3887
+ },
3888
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("merge_cells") })
3889
+ ),
3890
+ canSplit.value && /* @__PURE__ */ h(
3891
+ "button",
3892
+ {
3893
+ type: "button",
3894
+ class: "cell-toolbar-btn",
3895
+ onPointerdown: onSplitCell
3896
+ },
3897
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("split_cell") })
3898
+ )
3899
+ ),
3900
+ /* @__PURE__ */ h("div", { class: "table-wrapper", ref: tableWrapperRef }, /* @__PURE__ */ h("div", { contenteditable: "false", class: "fixed-handles-col" }, colBounds.value.map((bound, i) => /* @__PURE__ */ h(
3901
+ "div",
3902
+ {
3903
+ key: `col-${i}`,
3904
+ class: "handle cell-handle fixed",
3760
3905
  "data-role": "col-drag-handle",
3761
- class: "handle cell-handle",
3762
- onDragstart: dragCol,
3763
- onClick: selectCol,
3906
+ draggable: "true",
3907
+ style: { left: `${bound.left}px`, width: `${bound.width}px` },
3908
+ onDragstart: (e) => {
3909
+ hoverIndex.value = [0, i];
3910
+ dragCol(e);
3911
+ },
3912
+ onClick: (e) => {
3913
+ hoverIndex.value = [0, i];
3914
+ selectCol(e);
3915
+ activeColIndex.value = i;
3916
+ activeRowIndex.value = -1;
3917
+ },
3764
3918
  onPointerdown: (e) => e.stopPropagation(),
3765
- onPointermove: (e) => e.stopPropagation(),
3766
- ref: colHandleRef
3919
+ onPointermove: (e) => e.stopPropagation()
3767
3920
  },
3768
- /* @__PURE__ */ h(Icon, { icon: config.renderButton("col_drag_handle") }),
3769
3921
  /* @__PURE__ */ h(
3770
3922
  "div",
3771
3923
  {
3772
- "data-show": "false",
3773
3924
  class: "button-group",
3925
+ "data-show": activeColIndex.value === i ? "true" : "false",
3774
3926
  onPointermove: (e) => e.stopPropagation()
3775
3927
  },
3776
- /* @__PURE__ */ h("button", { type: "button", onPointerdown: onAlign("left") }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_left") })),
3777
- /* @__PURE__ */ h("button", { type: "button", onPointerdown: onAlign("center") }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_center") })),
3778
- /* @__PURE__ */ h("button", { type: "button", onPointerdown: onAlign("right") }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_right") })),
3779
- /* @__PURE__ */ h("button", { type: "button", onPointerdown: deleteSelected }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("delete_col") }))
3928
+ /* @__PURE__ */ h(
3929
+ "button",
3930
+ {
3931
+ type: "button",
3932
+ onPointerdown: (e) => {
3933
+ hoverIndex.value = [0, i];
3934
+ onAlign("left")(e);
3935
+ }
3936
+ },
3937
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_left") })
3938
+ ),
3939
+ /* @__PURE__ */ h(
3940
+ "button",
3941
+ {
3942
+ type: "button",
3943
+ onPointerdown: (e) => {
3944
+ hoverIndex.value = [0, i];
3945
+ onAlign("center")(e);
3946
+ }
3947
+ },
3948
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_center") })
3949
+ ),
3950
+ /* @__PURE__ */ h(
3951
+ "button",
3952
+ {
3953
+ type: "button",
3954
+ onPointerdown: (e) => {
3955
+ hoverIndex.value = [0, i];
3956
+ onAlign("right")(e);
3957
+ }
3958
+ },
3959
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("align_col_right") })
3960
+ ),
3961
+ /* @__PURE__ */ h(
3962
+ "button",
3963
+ {
3964
+ type: "button",
3965
+ onPointerdown: (e) => {
3966
+ hoverIndex.value = [0, i];
3967
+ deleteSelected(e);
3968
+ }
3969
+ },
3970
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("delete_col") })
3971
+ )
3780
3972
  )
3781
- ),
3782
- /* @__PURE__ */ h(
3973
+ ))), /* @__PURE__ */ h("div", { contenteditable: "false", class: "add-dots-col" }, [
3974
+ ...colBounds.value.map((b) => b.left),
3975
+ colBounds.value.length ? colBounds.value[colBounds.value.length - 1].left + colBounds.value[colBounds.value.length - 1].width : 0
3976
+ ].map((left, i) => /* @__PURE__ */ h(
3783
3977
  "div",
3784
3978
  {
3785
- "data-show": "false",
3786
- contenteditable: "false",
3787
- draggable: "true",
3979
+ key: `add-col-${i}`,
3980
+ class: "add-dot",
3981
+ style: { left: `${left}px` },
3982
+ onClick: () => addColByIndex(i),
3983
+ onPointerdown: (e) => e.stopPropagation()
3984
+ },
3985
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("add_col") })
3986
+ ))), /* @__PURE__ */ h("div", { contenteditable: "false", class: "resize-handles-col" }, colBounds.value.map((bound, i) => /* @__PURE__ */ h(
3987
+ "div",
3988
+ {
3989
+ key: `resize-${i}`,
3990
+ class: [
3991
+ "col-resize-handle",
3992
+ resizingCol.value === i ? "active" : ""
3993
+ ].join(" "),
3994
+ style: {
3995
+ left: `${bound.left + bound.width - 3}px`
3996
+ },
3997
+ onPointerdown: (e) => startResize(e, i)
3998
+ }
3999
+ ))), /* @__PURE__ */ h("div", { contenteditable: "false", class: "fixed-handles-row" }, rowBounds.value.map((bound, i) => /* @__PURE__ */ h(
4000
+ "div",
4001
+ {
4002
+ key: `row-${i}`,
4003
+ class: "handle cell-handle fixed",
3788
4004
  "data-role": "row-drag-handle",
3789
- class: "handle cell-handle",
3790
- onDragstart: dragRow,
3791
- onClick: selectRow,
3792
- onPointerdown: (e) => e.stopPropagation(),
3793
- onPointermove: (e) => e.stopPropagation(),
3794
- ref: rowHandleRef
4005
+ draggable: "true",
4006
+ style: { top: `${bound.top}px`, height: `${bound.height}px` },
4007
+ onDragstart: (e) => {
4008
+ hoverIndex.value = [i, 0];
4009
+ dragRow(e);
4010
+ },
4011
+ onClick: (e) => {
4012
+ hoverIndex.value = [i, 0];
4013
+ selectRow(e);
4014
+ activeRowIndex.value = i;
4015
+ activeColIndex.value = -1;
4016
+ },
4017
+ onPointerdown: (e) => e.stopPropagation()
3795
4018
  },
3796
- /* @__PURE__ */ h(Icon, { icon: config.renderButton("row_drag_handle") }),
3797
4019
  /* @__PURE__ */ h(
3798
4020
  "div",
3799
4021
  {
3800
- "data-show": "false",
3801
4022
  class: "button-group",
4023
+ "data-show": activeRowIndex.value === i ? "true" : "false",
3802
4024
  onPointermove: (e) => e.stopPropagation()
3803
4025
  },
3804
- /* @__PURE__ */ h("button", { type: "button", onPointerdown: deleteSelected }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("delete_row") }))
4026
+ /* @__PURE__ */ h(
4027
+ "button",
4028
+ {
4029
+ type: "button",
4030
+ onPointerdown: (e) => {
4031
+ hoverIndex.value = [i, 0];
4032
+ deleteSelected(e);
4033
+ }
4034
+ },
4035
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("delete_row") })
4036
+ )
3805
4037
  )
3806
- ),
3807
- /* @__PURE__ */ h("div", { class: "table-wrapper", ref: tableWrapperRef }, /* @__PURE__ */ h(
4038
+ ))), /* @__PURE__ */ h("div", { contenteditable: "false", class: "add-dots-row" }, [
4039
+ ...rowBounds.value.map((b) => b.top),
4040
+ rowBounds.value.length ? rowBounds.value[rowBounds.value.length - 1].top + rowBounds.value[rowBounds.value.length - 1].height : 0
4041
+ ].map((top, i) => /* @__PURE__ */ h(
4042
+ "div",
4043
+ {
4044
+ key: `add-row-${i}`,
4045
+ class: "add-dot",
4046
+ style: { top: `${top}px` },
4047
+ onClick: () => addRowByIndex(i),
4048
+ onPointerdown: (e) => e.stopPropagation()
4049
+ },
4050
+ /* @__PURE__ */ h(Icon, { icon: config.renderButton("add_row") })
4051
+ ))), /* @__PURE__ */ h(
3808
4052
  "div",
3809
4053
  {
3810
4054
  "data-show": "false",
@@ -3813,29 +4057,13 @@ const TableBlock = defineComponent({
3813
4057
  ref: dragPreviewRef
3814
4058
  },
3815
4059
  /* @__PURE__ */ h("table", null, /* @__PURE__ */ h("tbody", null))
3816
- ), /* @__PURE__ */ h(
3817
- "div",
4060
+ ), /* @__PURE__ */ h("table", { ref: contentWrapperFunctionRef, class: "children" }, /* @__PURE__ */ h("colgroup", null, colWidths.value.map((w, i) => /* @__PURE__ */ h(
4061
+ "col",
3818
4062
  {
3819
- "data-show": "false",
3820
- contenteditable: "false",
3821
- "data-display-type": "tool",
3822
- "data-role": "x-line-drag-handle",
3823
- class: "handle line-handle",
3824
- ref: xLineHandleRef
3825
- },
3826
- /* @__PURE__ */ h("button", { type: "button", onClick: onAddRow, class: "add-button" }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("add_row") }))
3827
- ), /* @__PURE__ */ h(
3828
- "div",
3829
- {
3830
- "data-show": "false",
3831
- contenteditable: "false",
3832
- "data-display-type": "tool",
3833
- "data-role": "y-line-drag-handle",
3834
- class: "handle line-handle",
3835
- ref: yLineHandleRef
3836
- },
3837
- /* @__PURE__ */ h("button", { type: "button", onClick: onAddCol, class: "add-button" }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("add_col") }))
3838
- ), /* @__PURE__ */ h("table", { ref: contentWrapperFunctionRef, class: "children" }))
4063
+ key: i,
4064
+ style: w > 0 ? { width: `${w}px` } : void 0
4065
+ }
4066
+ )))))
3839
4067
  );
3840
4068
  };
3841
4069
  }
@@ -3845,16 +4073,17 @@ var __typeError = (msg) => {
3845
4073
  throw TypeError(msg);
3846
4074
  };
3847
4075
  var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
4076
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), member.get(obj));
3848
4077
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
3849
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
3850
- var _TableNodeView_instances, handleClick_fn;
4078
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
4079
+ var _selectionUpdateScheduled;
3851
4080
  class TableNodeView {
3852
4081
  constructor(ctx, node, view, getPos) {
3853
4082
  this.ctx = ctx;
3854
4083
  this.node = node;
3855
4084
  this.view = view;
3856
4085
  this.getPos = getPos;
3857
- __privateAdd(this, _TableNodeView_instances);
4086
+ __privateAdd(this, _selectionUpdateScheduled, false);
3858
4087
  const dom = document.createElement("div");
3859
4088
  dom.className = "milkdown-table-block";
3860
4089
  const contentDOM = document.createElement("tbody");
@@ -3878,8 +4107,16 @@ class TableNodeView {
3878
4107
  }
3879
4108
  update(node) {
3880
4109
  if (node.type !== this.node.type) return false;
3881
- if (node.sameMarkup(this.node) && node.content.eq(this.node.content))
3882
- return false;
4110
+ if (node.sameMarkup(this.node) && node.content.eq(this.node.content)) {
4111
+ if (!__privateGet(this, _selectionUpdateScheduled)) {
4112
+ __privateSet(this, _selectionUpdateScheduled, true);
4113
+ queueMicrotask(() => {
4114
+ triggerRef(this.nodeRef);
4115
+ __privateSet(this, _selectionUpdateScheduled, false);
4116
+ });
4117
+ }
4118
+ return true;
4119
+ }
3883
4120
  this.node = node;
3884
4121
  this.nodeRef.value = node;
3885
4122
  return true;
@@ -3888,11 +4125,6 @@ class TableNodeView {
3888
4125
  if (e.type === "drop" || e.type.startsWith("drag")) return true;
3889
4126
  if (e.type === "mousedown" || e.type === "pointerdown") {
3890
4127
  if (e.target instanceof Element && e.target.closest("button")) return true;
3891
- const target = e.target;
3892
- if (target instanceof HTMLElement && (target.closest("th") || target.closest("td"))) {
3893
- const event = e;
3894
- return __privateMethod(this, _TableNodeView_instances, handleClick_fn).call(this, event);
3895
- }
3896
4128
  }
3897
4129
  return false;
3898
4130
  }
@@ -3910,38 +4142,7 @@ class TableNodeView {
3910
4142
  this.contentDOM.remove();
3911
4143
  }
3912
4144
  }
3913
- _TableNodeView_instances = new WeakSet();
3914
- handleClick_fn = function(event) {
3915
- const view = this.view;
3916
- if (!view.editable) return false;
3917
- const { state, dispatch } = view;
3918
- const pos = view.posAtCoords({ left: event.clientX, top: event.clientY });
3919
- if (!pos) return false;
3920
- const $pos = state.doc.resolve(pos.inside);
3921
- const node = findParent(
3922
- (node2) => node2.type.name === "table_cell" || node2.type.name === "table_header"
3923
- )($pos);
3924
- if (!node) return false;
3925
- if (state.selection instanceof TextSelection) {
3926
- const currentNode = findParent(
3927
- (node2) => node2.type.name === "table_cell" || node2.type.name === "table_header"
3928
- )(state.selection.$from);
3929
- if ((currentNode == null ? void 0 : currentNode.node) === node.node) return false;
3930
- }
3931
- const { from } = node;
3932
- const selection = NodeSelection.create(state.doc, from + 1);
3933
- if (state.selection.eq(selection)) return false;
3934
- if (state.selection instanceof CellSelection) {
3935
- setTimeout(() => {
3936
- dispatch(state.tr.setSelection(selection).scrollIntoView());
3937
- }, 20);
3938
- } else {
3939
- requestAnimationFrame(() => {
3940
- dispatch(state.tr.setSelection(selection).scrollIntoView());
3941
- });
3942
- }
3943
- return true;
3944
- };
4145
+ _selectionUpdateScheduled = new WeakMap();
3945
4146
  const tableBlockView = $view(
3946
4147
  tableSchema.node,
3947
4148
  (ctx) => {