@ctzhian/tiptap 1.13.8 → 2.0.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.
- package/dist/Editor/demo.js +3 -5
- package/dist/Editor/index.js +17 -0
- package/dist/component/CustomBubbleMenu/index.js +1 -1
- package/dist/component/CustomDragHandle/index.js +3 -59
- package/dist/component/Icons/delete-back-2-line-icon.d.ts +6 -0
- package/dist/component/Icons/delete-back-2-line-icon.js +13 -0
- package/dist/component/Menu/index.js +5 -1
- package/dist/contants/enums.d.ts +9 -0
- package/dist/contants/enums.js +61 -1
- package/dist/extension/component/Link/Insert.js +1 -1
- package/dist/extension/component/TableCellHandleMenu/index.d.ts +9 -0
- package/dist/extension/component/TableCellHandleMenu/index.js +443 -0
- package/dist/extension/component/TableExtendButton/TableExtendButton.css +30 -0
- package/dist/extension/component/TableExtendButton/index.d.ts +23 -0
- package/dist/extension/component/TableExtendButton/index.js +201 -0
- package/dist/extension/component/TableExtendButton/use-table-extend-row-column.d.ts +15 -0
- package/dist/extension/component/TableExtendButton/use-table-extend-row-column.js +87 -0
- package/dist/extension/component/TableHandle/TableHandleMenu.css +36 -0
- package/dist/extension/component/TableHandle/TableHandleMenu.d.ts +17 -0
- package/dist/extension/component/TableHandle/TableHandleMenu.js +685 -0
- package/dist/extension/component/TableHandle/index.d.ts +28 -0
- package/dist/extension/component/TableHandle/index.js +93 -0
- package/dist/extension/component/TableHandle/use-table-handle-positioning.d.ts +40 -0
- package/dist/extension/component/TableHandle/use-table-handle-positioning.js +193 -0
- package/dist/extension/component/TableHandle/use-table-handle-state.d.ts +22 -0
- package/dist/extension/component/TableHandle/use-table-handle-state.js +45 -0
- package/dist/extension/component/TableSelectionOverlay/index.d.ts +16 -0
- package/dist/extension/component/TableSelectionOverlay/index.js +460 -0
- package/dist/extension/node/FileHandler.d.ts +1 -1
- package/dist/extension/node/Link/index.js +4 -4
- package/dist/extension/node/Table.js +226 -43
- package/dist/extension/node/TableHandler/create-image.d.ts +9 -0
- package/dist/extension/node/TableHandler/create-image.js +235 -0
- package/dist/extension/node/TableHandler/index.d.ts +15 -0
- package/dist/extension/node/TableHandler/index.js +33 -0
- package/dist/extension/node/TableHandler/plugin.d.ts +49 -0
- package/dist/extension/node/TableHandler/plugin.js +1030 -0
- package/dist/index.css +29 -10
- package/dist/type/index.d.ts +2 -0
- package/dist/util/table-utils.d.ts +161 -0
- package/dist/util/table-utils.js +605 -0
- package/package.json +34 -33
- package/dist/extension/component/Table/ContextMenu.d.ts +0 -11
- package/dist/extension/component/Table/ContextMenu.js +0 -186
- package/dist/extension/component/Table/TableContextMenuPlugin.d.ts +0 -9
- package/dist/extension/component/Table/TableContextMenuPlugin.js +0 -336
- package/dist/extension/component/Table/index.d.ts +0 -2
- package/dist/extension/component/Table/index.js +0 -2
|
@@ -0,0 +1,1030 @@
|
|
|
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
5
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
6
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
7
|
+
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; }
|
|
8
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
9
|
+
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); }
|
|
10
|
+
import { Plugin, PluginKey, TextSelection } from '@tiptap/pm/state';
|
|
11
|
+
import { CellSelection, moveTableColumn, moveTableRow, TableMap } from '@tiptap/pm/tables';
|
|
12
|
+
import { Decoration, DecorationSet } from '@tiptap/pm/view';
|
|
13
|
+
import { clamp, domCellAround, getCellIndicesFromDOM, getColumnCells, getIndexCoordinates, getRowCells, getTableFromDOM, isHTMLElement, isTableNode, safeClosest, selectCellsByCoords } from "../../../util/table-utils";
|
|
14
|
+
import { createTableDragImage } from "./create-image";
|
|
15
|
+
function hideElements(selector, rootEl) {
|
|
16
|
+
rootEl.querySelectorAll(selector).forEach(function (el) {
|
|
17
|
+
el.style.visibility = 'hidden';
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
export var tableHandlePluginKey = new PluginKey('tableHandlePlugin');
|
|
21
|
+
var TableHandleView = /*#__PURE__*/function () {
|
|
22
|
+
function TableHandleView(editor, editorView, emitUpdate) {
|
|
23
|
+
var _this = this;
|
|
24
|
+
_classCallCheck(this, TableHandleView);
|
|
25
|
+
_defineProperty(this, "editor", void 0);
|
|
26
|
+
_defineProperty(this, "editorView", void 0);
|
|
27
|
+
_defineProperty(this, "state", undefined);
|
|
28
|
+
_defineProperty(this, "menuFrozen", false);
|
|
29
|
+
_defineProperty(this, "mouseState", 'up');
|
|
30
|
+
_defineProperty(this, "tableId", void 0);
|
|
31
|
+
_defineProperty(this, "tablePos", void 0);
|
|
32
|
+
_defineProperty(this, "tableElement", void 0);
|
|
33
|
+
_defineProperty(this, "emitUpdate", void 0);
|
|
34
|
+
_defineProperty(this, "viewMousedownHandler", function (event) {
|
|
35
|
+
_this.mouseState = 'down';
|
|
36
|
+
var _this$editor = _this.editor,
|
|
37
|
+
state = _this$editor.state,
|
|
38
|
+
view = _this$editor.view;
|
|
39
|
+
if (!(state.selection instanceof CellSelection) || _this.editor.isFocused) return;
|
|
40
|
+
var posInfo = view.posAtCoords({
|
|
41
|
+
left: event.clientX,
|
|
42
|
+
top: event.clientY
|
|
43
|
+
});
|
|
44
|
+
if (!posInfo) return;
|
|
45
|
+
var $pos = state.doc.resolve(posInfo.pos);
|
|
46
|
+
var nodes = state.schema.nodes;
|
|
47
|
+
var paraDepth = -1;
|
|
48
|
+
var inTableCell = false;
|
|
49
|
+
for (var d = $pos.depth; d >= 0; d--) {
|
|
50
|
+
var node = $pos.node(d);
|
|
51
|
+
if (!inTableCell && (node.type === nodes.tableCell || node.type === nodes.tableHeader)) {
|
|
52
|
+
inTableCell = true;
|
|
53
|
+
}
|
|
54
|
+
if (paraDepth === -1 && node.type === nodes.paragraph) {
|
|
55
|
+
paraDepth = d;
|
|
56
|
+
}
|
|
57
|
+
if (inTableCell && paraDepth !== -1) break;
|
|
58
|
+
}
|
|
59
|
+
if (!inTableCell || paraDepth === -1) return;
|
|
60
|
+
var from = $pos.start(paraDepth);
|
|
61
|
+
var to = $pos.end(paraDepth);
|
|
62
|
+
var nextSel = TextSelection.create(state.doc, from, to);
|
|
63
|
+
if (state.selection.eq(nextSel)) return;
|
|
64
|
+
view.dispatch(state.tr.setSelection(nextSel));
|
|
65
|
+
view.focus();
|
|
66
|
+
});
|
|
67
|
+
_defineProperty(this, "mouseUpHandler", function (event) {
|
|
68
|
+
_this.mouseState = 'up';
|
|
69
|
+
_this.mouseMoveHandler(event);
|
|
70
|
+
});
|
|
71
|
+
_defineProperty(this, "mouseMoveHandler", function (event) {
|
|
72
|
+
if (_this.menuFrozen || _this.mouseState === 'selecting') return;
|
|
73
|
+
var target = event.target;
|
|
74
|
+
// If target is not in editor, don't process - this prevents state from being cleared
|
|
75
|
+
// when dragging outside the editor, which is the key to preventing errors
|
|
76
|
+
if (!isHTMLElement(target) || !_this.editorView.dom.contains(target)) return;
|
|
77
|
+
|
|
78
|
+
// Check if mouse is over a table handle or extend button
|
|
79
|
+
var isOverHandle = target.closest('.tiptap-table-handle-menu') !== null;
|
|
80
|
+
var isOverExtendButton = target.closest('.tiptap-table-extend-row-column-button') !== null;
|
|
81
|
+
|
|
82
|
+
// If mouse is over handle/button, keep showing them (don't hide)
|
|
83
|
+
if (isOverHandle || isOverExtendButton) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Check if mouse is near handle/button elements (within reasonable distance)
|
|
88
|
+
var handleElements = Array.from(_this.editorView.root.querySelectorAll('.tiptap-table-handle-menu, .tiptap-table-extend-row-column-button'));
|
|
89
|
+
var isNearHandle = false;
|
|
90
|
+
for (var _i = 0, _handleElements = handleElements; _i < _handleElements.length; _i++) {
|
|
91
|
+
var handleEl = _handleElements[_i];
|
|
92
|
+
if (!isHTMLElement(handleEl)) continue;
|
|
93
|
+
var rect = handleEl.getBoundingClientRect();
|
|
94
|
+
// Expand the rect by 10px on all sides for easier mouse movement
|
|
95
|
+
var expandedRect = new DOMRect(rect.left - 10, rect.top - 10, rect.width + 20, rect.height + 20);
|
|
96
|
+
if (event.clientX >= expandedRect.left && event.clientX <= expandedRect.right && event.clientY >= expandedRect.top && event.clientY <= expandedRect.bottom) {
|
|
97
|
+
isNearHandle = true;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// If near handle, don't process further to avoid flickering
|
|
103
|
+
if (isNearHandle) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
_this._handleMouseMoveNow(event);
|
|
107
|
+
});
|
|
108
|
+
_defineProperty(this, "dragOverHandler", function (event) {
|
|
109
|
+
var _this$state;
|
|
110
|
+
if (((_this$state = _this.state) === null || _this$state === void 0 ? void 0 : _this$state.draggingState) === undefined) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
event.dataTransfer.dropEffect = 'move';
|
|
115
|
+
hideElements('.prosemirror-dropcursor-block, .prosemirror-dropcursor-inline', _this.editorView.root);
|
|
116
|
+
|
|
117
|
+
// The mouse cursor coordinates, bounded to the table's bounding box.
|
|
118
|
+
var _this$state$reference = _this.state.referencePosTable,
|
|
119
|
+
tableLeft = _this$state$reference.left,
|
|
120
|
+
tableRight = _this$state$reference.right,
|
|
121
|
+
tableTop = _this$state$reference.top,
|
|
122
|
+
tableBottom = _this$state$reference.bottom;
|
|
123
|
+
var boundedMouseCoords = {
|
|
124
|
+
left: clamp(event.clientX, tableLeft + 1, tableRight - 1),
|
|
125
|
+
top: clamp(event.clientY, tableTop + 1, tableBottom - 1)
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// Gets the table cell element
|
|
129
|
+
var tableCellElements = _this.editorView.root.elementsFromPoint(boundedMouseCoords.left, boundedMouseCoords.top).filter(function (element) {
|
|
130
|
+
return element.tagName === 'TD' || element.tagName === 'TH';
|
|
131
|
+
});
|
|
132
|
+
if (tableCellElements.length === 0) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
var tableCellElement = tableCellElements[0];
|
|
136
|
+
if (!isHTMLElement(tableCellElement)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
var cellPosition = getCellIndicesFromDOM(tableCellElement, _this.state.block, _this.editor);
|
|
140
|
+
if (!cellPosition) return;
|
|
141
|
+
var rowIndex = cellPosition.rowIndex,
|
|
142
|
+
colIndex = cellPosition.colIndex;
|
|
143
|
+
|
|
144
|
+
// Check what changed
|
|
145
|
+
var oldIndex = _this.state.draggingState.draggedCellOrientation === 'row' ? _this.state.rowIndex : _this.state.colIndex;
|
|
146
|
+
var newIndex = _this.state.draggingState.draggedCellOrientation === 'row' ? rowIndex : colIndex;
|
|
147
|
+
var dispatchDecorationsTransaction = newIndex !== oldIndex;
|
|
148
|
+
var mousePos = _this.state.draggingState.draggedCellOrientation === 'row' ? boundedMouseCoords.top : boundedMouseCoords.left;
|
|
149
|
+
|
|
150
|
+
// Check if anything needs updating
|
|
151
|
+
var cellChanged = _this.state.rowIndex !== rowIndex || _this.state.colIndex !== colIndex;
|
|
152
|
+
var mousePosChanged = _this.state.draggingState.mousePos !== mousePos;
|
|
153
|
+
if (cellChanged || mousePosChanged) {
|
|
154
|
+
var _this$state$draggingS;
|
|
155
|
+
var newCellRect = tableCellElement.getBoundingClientRect();
|
|
156
|
+
// Preserve original cell size during drag to prevent handle height changes
|
|
157
|
+
var preservedCellRect = (_this$state$draggingS = _this.state.draggingState) !== null && _this$state$draggingS !== void 0 && _this$state$draggingS.originalCellSize ? new DOMRect(newCellRect.x, newCellRect.y, _this.state.draggingState.originalCellSize.width, _this.state.draggingState.originalCellSize.height) : newCellRect;
|
|
158
|
+
_this.state = _objectSpread(_objectSpread({}, _this.state), {}, {
|
|
159
|
+
rowIndex: rowIndex,
|
|
160
|
+
colIndex: colIndex,
|
|
161
|
+
referencePosCell: preservedCellRect,
|
|
162
|
+
draggingState: _objectSpread(_objectSpread({}, _this.state.draggingState), {}, {
|
|
163
|
+
mousePos: mousePos
|
|
164
|
+
})
|
|
165
|
+
});
|
|
166
|
+
_this.emitUpdate();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Dispatch decorations transaction if needed
|
|
170
|
+
if (dispatchDecorationsTransaction) {
|
|
171
|
+
_this.editor.view.dispatch(_this.editor.state.tr.setMeta(tableHandlePluginKey, true));
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
_defineProperty(this, "dropHandler", function () {
|
|
175
|
+
_this.mouseState = 'up';
|
|
176
|
+
var st = _this.state;
|
|
177
|
+
if (!(st !== null && st !== void 0 && st.draggingState)) return false;
|
|
178
|
+
var draggingState = st.draggingState,
|
|
179
|
+
rowIndex = st.rowIndex,
|
|
180
|
+
colIndex = st.colIndex,
|
|
181
|
+
blockPos = st.blockPos;
|
|
182
|
+
if (typeof blockPos !== 'number' || blockPos < 0) return false;
|
|
183
|
+
if (draggingState.draggedCellOrientation === 'row' && rowIndex === undefined || draggingState.draggedCellOrientation === 'col' && colIndex === undefined) {
|
|
184
|
+
throw new Error('Attempted to drop table row or column, but no table block was hovered prior.');
|
|
185
|
+
}
|
|
186
|
+
var isRow = draggingState.draggedCellOrientation === 'row';
|
|
187
|
+
var orientation = isRow ? 'row' : 'column';
|
|
188
|
+
var destIndex = isRow ? rowIndex : colIndex;
|
|
189
|
+
var cellCoords = getIndexCoordinates({
|
|
190
|
+
editor: _this.editor,
|
|
191
|
+
index: draggingState.originalIndex,
|
|
192
|
+
orientation: orientation,
|
|
193
|
+
tablePos: blockPos
|
|
194
|
+
});
|
|
195
|
+
if (!cellCoords) return false;
|
|
196
|
+
var stateWithCellSel = selectCellsByCoords(_this.editor, blockPos, cellCoords, {
|
|
197
|
+
mode: 'state'
|
|
198
|
+
});
|
|
199
|
+
if (!stateWithCellSel) return false;
|
|
200
|
+
|
|
201
|
+
// When mode is 'state', selectCellsByCoords returns EditorState
|
|
202
|
+
// Type assertion is safe here because we explicitly requested 'state' mode
|
|
203
|
+
var editorState = stateWithCellSel;
|
|
204
|
+
var dispatch = function dispatch(tr) {
|
|
205
|
+
return _this.editor.view.dispatch(tr);
|
|
206
|
+
};
|
|
207
|
+
if (isRow) {
|
|
208
|
+
moveTableRow({
|
|
209
|
+
from: draggingState.originalIndex,
|
|
210
|
+
to: destIndex,
|
|
211
|
+
select: true,
|
|
212
|
+
pos: blockPos + 1
|
|
213
|
+
})(editorState, dispatch);
|
|
214
|
+
} else {
|
|
215
|
+
moveTableColumn({
|
|
216
|
+
from: draggingState.originalIndex,
|
|
217
|
+
to: destIndex,
|
|
218
|
+
select: true,
|
|
219
|
+
pos: blockPos + 1
|
|
220
|
+
})(editorState, dispatch);
|
|
221
|
+
}
|
|
222
|
+
_this.state = _objectSpread(_objectSpread({}, st), {}, {
|
|
223
|
+
draggingState: undefined
|
|
224
|
+
});
|
|
225
|
+
_this.emitUpdate();
|
|
226
|
+
_this.editor.view.dispatch(_this.editor.state.tr.setMeta(tableHandlePluginKey, null));
|
|
227
|
+
return true;
|
|
228
|
+
});
|
|
229
|
+
_defineProperty(this, "scrollHandler", function () {
|
|
230
|
+
var _this$state2, _this$tableElement;
|
|
231
|
+
// When scrolling, update positions if handles are visible
|
|
232
|
+
if ((_this$state2 = _this.state) !== null && _this$state2 !== void 0 && _this$state2.show && (_this$tableElement = _this.tableElement) !== null && _this$tableElement !== void 0 && _this$tableElement.isConnected) {
|
|
233
|
+
// Force update by recalculating positions
|
|
234
|
+
_this.updatePositions();
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
this.editor = editor;
|
|
238
|
+
this.editorView = editorView;
|
|
239
|
+
this.emitUpdate = function () {
|
|
240
|
+
return _this.state && emitUpdate(_this.state);
|
|
241
|
+
};
|
|
242
|
+
this.editorView.dom.addEventListener('mousemove', this.mouseMoveHandler);
|
|
243
|
+
this.editorView.dom.addEventListener('mousedown', this.viewMousedownHandler);
|
|
244
|
+
window.addEventListener('mouseup', this.mouseUpHandler);
|
|
245
|
+
this.editorView.root.addEventListener('dragover', this.dragOverHandler);
|
|
246
|
+
this.editorView.root.addEventListener('drop', this.dropHandler);
|
|
247
|
+
|
|
248
|
+
// Listen to scroll events to update handle positions when scrolling
|
|
249
|
+
window.addEventListener('scroll', this.scrollHandler, true);
|
|
250
|
+
// Also listen to resize events
|
|
251
|
+
window.addEventListener('resize', this.scrollHandler);
|
|
252
|
+
}
|
|
253
|
+
_createClass(TableHandleView, [{
|
|
254
|
+
key: "hideHandles",
|
|
255
|
+
value: function hideHandles() {
|
|
256
|
+
var _this$state3;
|
|
257
|
+
if (!((_this$state3 = this.state) !== null && _this$state3 !== void 0 && _this$state3.show)) return;
|
|
258
|
+
|
|
259
|
+
// Don't hide handles during drag operations - preserve dragging state
|
|
260
|
+
if (this.state.draggingState) return;
|
|
261
|
+
this.state = _objectSpread(_objectSpread({}, this.state), {}, {
|
|
262
|
+
show: false,
|
|
263
|
+
showAddOrRemoveRowsButton: false,
|
|
264
|
+
showAddOrRemoveColumnsButton: false,
|
|
265
|
+
colIndex: undefined,
|
|
266
|
+
rowIndex: undefined,
|
|
267
|
+
referencePosCell: undefined,
|
|
268
|
+
referencePosLastRow: undefined,
|
|
269
|
+
referencePosLastCol: undefined
|
|
270
|
+
});
|
|
271
|
+
this.emitUpdate();
|
|
272
|
+
}
|
|
273
|
+
}, {
|
|
274
|
+
key: "_handleMouseMoveNow",
|
|
275
|
+
value: function _handleMouseMoveNow(event) {
|
|
276
|
+
var _this$state4;
|
|
277
|
+
var around = domCellAround(event.target);
|
|
278
|
+
|
|
279
|
+
// Hide handles while selecting inside a cell
|
|
280
|
+
if ((around === null || around === void 0 ? void 0 : around.type) === 'cell' && this.mouseState === 'down' && !((_this$state4 = this.state) !== null && _this$state4 !== void 0 && _this$state4.draggingState)) {
|
|
281
|
+
this.mouseState = 'selecting';
|
|
282
|
+
this.hideHandles();
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (!around || !this.editor.isEditable) {
|
|
286
|
+
this.hideHandles();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
var tbody = around.tbodyNode;
|
|
290
|
+
if (!tbody) return;
|
|
291
|
+
var tableRect = tbody.getBoundingClientRect();
|
|
292
|
+
var coords = this.editor.view.posAtCoords({
|
|
293
|
+
left: event.clientX,
|
|
294
|
+
top: event.clientY
|
|
295
|
+
});
|
|
296
|
+
if (!coords) return;
|
|
297
|
+
|
|
298
|
+
// Find the table node at this position
|
|
299
|
+
var $pos = this.editor.view.state.doc.resolve(coords.pos);
|
|
300
|
+
var blockInfo;
|
|
301
|
+
for (var d = $pos.depth; d >= 0; d--) {
|
|
302
|
+
var node = $pos.node(d);
|
|
303
|
+
if (isTableNode(node)) {
|
|
304
|
+
blockInfo = {
|
|
305
|
+
node: node,
|
|
306
|
+
pos: d === 0 ? 0 : $pos.before(d)
|
|
307
|
+
};
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (!blockInfo || blockInfo.node.type.name !== 'table') return;
|
|
312
|
+
this.tableElement = this.editor.view.nodeDOM(blockInfo.pos);
|
|
313
|
+
this.tablePos = blockInfo.pos;
|
|
314
|
+
this.tableId = blockInfo.node.attrs.id;
|
|
315
|
+
var wrapper = safeClosest(around.domNode, '.tableWrapper');
|
|
316
|
+
var widgetContainer = wrapper === null || wrapper === void 0 ? void 0 : wrapper.querySelector('.table-controls');
|
|
317
|
+
|
|
318
|
+
// Hovering around the table (outside cells)
|
|
319
|
+
if (around.type === 'wrapper') {
|
|
320
|
+
var _this$state5, _this$state6, _this$state7;
|
|
321
|
+
var below = event.clientY >= tableRect.bottom - 1 && event.clientY < tableRect.bottom + 20;
|
|
322
|
+
var right = event.clientX >= tableRect.right - 1 && event.clientX < tableRect.right + 20;
|
|
323
|
+
var cursorBeyondRightOrBottom = event.clientX > tableRect.right || event.clientY > tableRect.bottom;
|
|
324
|
+
|
|
325
|
+
// Calculate positions for extend buttons when hovering at edges
|
|
326
|
+
var referencePosLastRow;
|
|
327
|
+
var referencePosLastCol;
|
|
328
|
+
if (below || right) {
|
|
329
|
+
var _this$tableElement2;
|
|
330
|
+
var _tbody = (_this$tableElement2 = this.tableElement) === null || _this$tableElement2 === void 0 ? void 0 : _this$tableElement2.querySelector('tbody');
|
|
331
|
+
if (_tbody) {
|
|
332
|
+
var _blockInfo$node$conte, _blockInfo$node$conte2;
|
|
333
|
+
var lastRowIndex = blockInfo.node.content.childCount - 1;
|
|
334
|
+
var lastColIndex = ((_blockInfo$node$conte = (_blockInfo$node$conte2 = blockInfo.node.content.firstChild) === null || _blockInfo$node$conte2 === void 0 ? void 0 : _blockInfo$node$conte2.content.childCount) !== null && _blockInfo$node$conte !== void 0 ? _blockInfo$node$conte : 0) - 1;
|
|
335
|
+
if (below) {
|
|
336
|
+
var lastRow = _tbody.children[lastRowIndex];
|
|
337
|
+
if (lastRow) {
|
|
338
|
+
referencePosLastRow = lastRow.getBoundingClientRect();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (right) {
|
|
342
|
+
var maxRight = 0;
|
|
343
|
+
var lastColRect = null;
|
|
344
|
+
for (var i = 0; i < _tbody.children.length; i++) {
|
|
345
|
+
var row = _tbody.children[i];
|
|
346
|
+
if (row && row.children[lastColIndex]) {
|
|
347
|
+
var cell = row.children[lastColIndex];
|
|
348
|
+
var cellRect = cell.getBoundingClientRect();
|
|
349
|
+
if (cellRect.right > maxRight) {
|
|
350
|
+
maxRight = cellRect.right;
|
|
351
|
+
lastColRect = cellRect;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (lastColRect) {
|
|
356
|
+
var firstRow = _tbody.children[0];
|
|
357
|
+
var _lastRow = _tbody.children[lastRowIndex];
|
|
358
|
+
if (firstRow && _lastRow) {
|
|
359
|
+
var firstCell = firstRow.children[lastColIndex];
|
|
360
|
+
if (firstCell) {
|
|
361
|
+
var firstRect = firstCell.getBoundingClientRect();
|
|
362
|
+
referencePosLastCol = new DOMRect(lastColRect.right, firstRect.top, 0, _lastRow.getBoundingClientRect().bottom - firstRect.top);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
this.state = _objectSpread(_objectSpread({}, this.state), {}, {
|
|
370
|
+
show: true,
|
|
371
|
+
showAddOrRemoveRowsButton: below,
|
|
372
|
+
showAddOrRemoveColumnsButton: right,
|
|
373
|
+
referencePosTable: tableRect,
|
|
374
|
+
referencePosLastRow: referencePosLastRow,
|
|
375
|
+
referencePosLastCol: referencePosLastCol,
|
|
376
|
+
block: blockInfo.node,
|
|
377
|
+
blockPos: blockInfo.pos,
|
|
378
|
+
widgetContainer: widgetContainer,
|
|
379
|
+
colIndex: cursorBeyondRightOrBottom ? undefined : (_this$state5 = this.state) === null || _this$state5 === void 0 ? void 0 : _this$state5.colIndex,
|
|
380
|
+
rowIndex: cursorBeyondRightOrBottom ? undefined : (_this$state6 = this.state) === null || _this$state6 === void 0 ? void 0 : _this$state6.rowIndex,
|
|
381
|
+
referencePosCell: cursorBeyondRightOrBottom ? undefined : (_this$state7 = this.state) === null || _this$state7 === void 0 ? void 0 : _this$state7.referencePosCell
|
|
382
|
+
});
|
|
383
|
+
} else {
|
|
384
|
+
var _blockInfo$node$conte3, _blockInfo$node$conte4, _this$state8;
|
|
385
|
+
// Hovering over a cell
|
|
386
|
+
var cellPosition = getCellIndicesFromDOM(around.domNode, blockInfo.node, this.editor);
|
|
387
|
+
if (!cellPosition) return;
|
|
388
|
+
var rowIndex = cellPosition.rowIndex,
|
|
389
|
+
colIndex = cellPosition.colIndex;
|
|
390
|
+
var _cellRect = around.domNode.getBoundingClientRect();
|
|
391
|
+
var _lastRowIndex = blockInfo.node.content.childCount - 1;
|
|
392
|
+
var _lastColIndex = ((_blockInfo$node$conte3 = (_blockInfo$node$conte4 = blockInfo.node.content.firstChild) === null || _blockInfo$node$conte4 === void 0 ? void 0 : _blockInfo$node$conte4.content.childCount) !== null && _blockInfo$node$conte3 !== void 0 ? _blockInfo$node$conte3 : 0) - 1;
|
|
393
|
+
|
|
394
|
+
// Skip update if same cell
|
|
395
|
+
if ((_this$state8 = this.state) !== null && _this$state8 !== void 0 && _this$state8.show && this.tableId === blockInfo.node.attrs.id && this.state.rowIndex === rowIndex && this.state.colIndex === colIndex) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Calculate positions for extend buttons
|
|
400
|
+
var _referencePosLastRow;
|
|
401
|
+
var _referencePosLastCol;
|
|
402
|
+
if (rowIndex === _lastRowIndex || colIndex === _lastColIndex) {
|
|
403
|
+
var _this$tableElement3;
|
|
404
|
+
var _tbody2 = (_this$tableElement3 = this.tableElement) === null || _this$tableElement3 === void 0 ? void 0 : _this$tableElement3.querySelector('tbody');
|
|
405
|
+
if (_tbody2) {
|
|
406
|
+
// Get last row position
|
|
407
|
+
if (rowIndex === _lastRowIndex) {
|
|
408
|
+
var _lastRow2 = _tbody2.children[_lastRowIndex];
|
|
409
|
+
if (_lastRow2) {
|
|
410
|
+
_referencePosLastRow = _lastRow2.getBoundingClientRect();
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
// Get last column position
|
|
414
|
+
if (colIndex === _lastColIndex) {
|
|
415
|
+
// Find the rightmost column by checking all rows
|
|
416
|
+
var _maxRight = 0;
|
|
417
|
+
var _lastColRect = null;
|
|
418
|
+
for (var _i2 = 0; _i2 < _tbody2.children.length; _i2++) {
|
|
419
|
+
var _row = _tbody2.children[_i2];
|
|
420
|
+
if (_row && _row.children[_lastColIndex]) {
|
|
421
|
+
var _cell = _row.children[_lastColIndex];
|
|
422
|
+
var _cellRect2 = _cell.getBoundingClientRect();
|
|
423
|
+
if (_cellRect2.right > _maxRight) {
|
|
424
|
+
_maxRight = _cellRect2.right;
|
|
425
|
+
_lastColRect = _cellRect2;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (_lastColRect) {
|
|
430
|
+
// Create a rect representing the entire last column
|
|
431
|
+
var _firstRow = _tbody2.children[0];
|
|
432
|
+
var _lastRow3 = _tbody2.children[_lastRowIndex];
|
|
433
|
+
if (_firstRow && _lastRow3) {
|
|
434
|
+
var _firstCell = _firstRow.children[_lastColIndex];
|
|
435
|
+
if (_firstCell) {
|
|
436
|
+
var _firstRect = _firstCell.getBoundingClientRect();
|
|
437
|
+
_referencePosLastCol = new DOMRect(_lastColRect.right, _firstRect.top, 0, _lastRow3.getBoundingClientRect().bottom - _firstRect.top);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
this.state = {
|
|
445
|
+
show: true,
|
|
446
|
+
showAddOrRemoveColumnsButton: colIndex === _lastColIndex,
|
|
447
|
+
showAddOrRemoveRowsButton: rowIndex === _lastRowIndex,
|
|
448
|
+
referencePosTable: tableRect,
|
|
449
|
+
referencePosLastRow: _referencePosLastRow,
|
|
450
|
+
referencePosLastCol: _referencePosLastCol,
|
|
451
|
+
block: blockInfo.node,
|
|
452
|
+
blockPos: blockInfo.pos,
|
|
453
|
+
draggingState: undefined,
|
|
454
|
+
referencePosCell: _cellRect,
|
|
455
|
+
colIndex: colIndex,
|
|
456
|
+
rowIndex: rowIndex,
|
|
457
|
+
widgetContainer: widgetContainer
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
this.emitUpdate();
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
}, {
|
|
464
|
+
key: "update",
|
|
465
|
+
value: function update(view) {
|
|
466
|
+
var _this$state9, _this$tableElement4, _this$tableElement5;
|
|
467
|
+
var pluginState = tableHandlePluginKey.getState(view.state);
|
|
468
|
+
if (pluginState !== undefined && pluginState !== this.menuFrozen) {
|
|
469
|
+
this.menuFrozen = pluginState;
|
|
470
|
+
}
|
|
471
|
+
if (!((_this$state9 = this.state) !== null && _this$state9 !== void 0 && _this$state9.show)) return;
|
|
472
|
+
if (!((_this$tableElement4 = this.tableElement) !== null && _this$tableElement4 !== void 0 && _this$tableElement4.isConnected)) {
|
|
473
|
+
this.hideHandles();
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
var tableInfo = getTableFromDOM(this.tableElement, this.editor);
|
|
477
|
+
if (!tableInfo) {
|
|
478
|
+
this.hideHandles();
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Check if table changed
|
|
483
|
+
var blockChanged = this.state.block !== tableInfo.node || this.state.blockPos !== tableInfo.pos;
|
|
484
|
+
if (!tableInfo.node || tableInfo.node.type.name !== 'table' || !((_this$tableElement5 = this.tableElement) !== null && _this$tableElement5 !== void 0 && _this$tableElement5.isConnected)) {
|
|
485
|
+
this.hideHandles();
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
var _TableMap$get = TableMap.get(tableInfo.node),
|
|
489
|
+
rowCount = _TableMap$get.height,
|
|
490
|
+
colCount = _TableMap$get.width;
|
|
491
|
+
|
|
492
|
+
// Calculate new indices
|
|
493
|
+
var newRowIndex = this.state.rowIndex;
|
|
494
|
+
var newColIndex = this.state.colIndex;
|
|
495
|
+
|
|
496
|
+
// Clamp indices if rows/columns were deleted
|
|
497
|
+
if (newRowIndex !== undefined && newRowIndex >= rowCount) {
|
|
498
|
+
newRowIndex = rowCount ? rowCount - 1 : undefined;
|
|
499
|
+
}
|
|
500
|
+
if (newColIndex !== undefined && newColIndex >= colCount) {
|
|
501
|
+
newColIndex = colCount ? colCount - 1 : undefined;
|
|
502
|
+
}
|
|
503
|
+
var tableBody = this.tableElement.querySelector('tbody');
|
|
504
|
+
if (!tableBody) {
|
|
505
|
+
throw new Error("Table block does not contain a 'tbody' HTML element. This should never happen.");
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// Calculate new reference positions
|
|
509
|
+
var newReferencePosCell = this.state.referencePosCell;
|
|
510
|
+
if (newRowIndex !== undefined && newColIndex !== undefined) {
|
|
511
|
+
var rowEl = tableBody.children[newRowIndex];
|
|
512
|
+
var cellEl = rowEl === null || rowEl === void 0 ? void 0 : rowEl.children[newColIndex];
|
|
513
|
+
if (cellEl) {
|
|
514
|
+
var _this$state$draggingS2;
|
|
515
|
+
var newCellRect = cellEl.getBoundingClientRect();
|
|
516
|
+
// Preserve original cell size during drag to prevent handle height changes
|
|
517
|
+
if ((_this$state$draggingS2 = this.state.draggingState) !== null && _this$state$draggingS2 !== void 0 && _this$state$draggingS2.originalCellSize) {
|
|
518
|
+
newReferencePosCell = new DOMRect(newCellRect.x, newCellRect.y, this.state.draggingState.originalCellSize.width, this.state.draggingState.originalCellSize.height);
|
|
519
|
+
} else {
|
|
520
|
+
newReferencePosCell = newCellRect;
|
|
521
|
+
}
|
|
522
|
+
} else {
|
|
523
|
+
newRowIndex = undefined;
|
|
524
|
+
newColIndex = undefined;
|
|
525
|
+
newReferencePosCell = undefined;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
var newReferencePosTable = tableBody.getBoundingClientRect();
|
|
529
|
+
|
|
530
|
+
// Update last row/col positions if needed
|
|
531
|
+
var newReferencePosLastRow = this.state.referencePosLastRow;
|
|
532
|
+
var newReferencePosLastCol = this.state.referencePosLastCol;
|
|
533
|
+
if (this.state.showAddOrRemoveRowsButton || this.state.showAddOrRemoveColumnsButton) {
|
|
534
|
+
var _tableInfo$node$conte, _tableInfo$node$conte2;
|
|
535
|
+
var lastRowIndex = tableInfo.node.content.childCount - 1;
|
|
536
|
+
var lastColIndex = ((_tableInfo$node$conte = (_tableInfo$node$conte2 = tableInfo.node.content.firstChild) === null || _tableInfo$node$conte2 === void 0 ? void 0 : _tableInfo$node$conte2.content.childCount) !== null && _tableInfo$node$conte !== void 0 ? _tableInfo$node$conte : 0) - 1;
|
|
537
|
+
if (this.state.showAddOrRemoveRowsButton) {
|
|
538
|
+
var lastRow = tableBody.children[lastRowIndex];
|
|
539
|
+
if (lastRow) {
|
|
540
|
+
newReferencePosLastRow = lastRow.getBoundingClientRect();
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (this.state.showAddOrRemoveColumnsButton) {
|
|
544
|
+
var maxRight = 0;
|
|
545
|
+
var lastColRect = null;
|
|
546
|
+
for (var i = 0; i < tableBody.children.length; i++) {
|
|
547
|
+
var row = tableBody.children[i];
|
|
548
|
+
if (row && row.children[lastColIndex]) {
|
|
549
|
+
var cell = row.children[lastColIndex];
|
|
550
|
+
var cellRect = cell.getBoundingClientRect();
|
|
551
|
+
if (cellRect.right > maxRight) {
|
|
552
|
+
maxRight = cellRect.right;
|
|
553
|
+
lastColRect = cellRect;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (lastColRect) {
|
|
558
|
+
var firstRow = tableBody.children[0];
|
|
559
|
+
var _lastRow4 = tableBody.children[lastRowIndex];
|
|
560
|
+
if (firstRow && _lastRow4) {
|
|
561
|
+
var firstCell = firstRow.children[lastColIndex];
|
|
562
|
+
if (firstCell) {
|
|
563
|
+
var firstRect = firstCell.getBoundingClientRect();
|
|
564
|
+
newReferencePosLastCol = new DOMRect(lastColRect.right, firstRect.top, 0, _lastRow4.getBoundingClientRect().bottom - firstRect.top);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// Check if anything changed
|
|
572
|
+
var indicesChanged = newRowIndex !== this.state.rowIndex || newColIndex !== this.state.colIndex;
|
|
573
|
+
var refPosChanged = newReferencePosCell !== this.state.referencePosCell || newReferencePosTable !== this.state.referencePosTable || newReferencePosLastRow !== this.state.referencePosLastRow || newReferencePosLastCol !== this.state.referencePosLastCol;
|
|
574
|
+
if (blockChanged || indicesChanged || refPosChanged) {
|
|
575
|
+
this.state = _objectSpread(_objectSpread({}, this.state), {}, {
|
|
576
|
+
block: tableInfo.node,
|
|
577
|
+
blockPos: tableInfo.pos,
|
|
578
|
+
rowIndex: newRowIndex,
|
|
579
|
+
colIndex: newColIndex,
|
|
580
|
+
referencePosCell: newReferencePosCell,
|
|
581
|
+
referencePosTable: newReferencePosTable,
|
|
582
|
+
referencePosLastRow: newReferencePosLastRow,
|
|
583
|
+
referencePosLastCol: newReferencePosLastCol
|
|
584
|
+
});
|
|
585
|
+
this.emitUpdate();
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}, {
|
|
589
|
+
key: "updatePositions",
|
|
590
|
+
value: function updatePositions() {
|
|
591
|
+
var _this$state10, _this$tableElement6;
|
|
592
|
+
if (!((_this$state10 = this.state) !== null && _this$state10 !== void 0 && _this$state10.show) || !((_this$tableElement6 = this.tableElement) !== null && _this$tableElement6 !== void 0 && _this$tableElement6.isConnected)) return;
|
|
593
|
+
var tableInfo = getTableFromDOM(this.tableElement, this.editor);
|
|
594
|
+
if (!tableInfo || tableInfo.node.type.name !== 'table') return;
|
|
595
|
+
var tableBody = this.tableElement.querySelector('tbody');
|
|
596
|
+
if (!tableBody) return;
|
|
597
|
+
var newReferencePosTable = tableBody.getBoundingClientRect();
|
|
598
|
+
|
|
599
|
+
// Update cell position if we have row/col indices
|
|
600
|
+
var newReferencePosCell = this.state.referencePosCell;
|
|
601
|
+
if (this.state.rowIndex !== undefined && this.state.colIndex !== undefined) {
|
|
602
|
+
var rowEl = tableBody.children[this.state.rowIndex];
|
|
603
|
+
var cellEl = rowEl === null || rowEl === void 0 ? void 0 : rowEl.children[this.state.colIndex];
|
|
604
|
+
if (cellEl) {
|
|
605
|
+
var _this$state$draggingS3;
|
|
606
|
+
var newCellRect = cellEl.getBoundingClientRect();
|
|
607
|
+
// Preserve original cell size during drag
|
|
608
|
+
if ((_this$state$draggingS3 = this.state.draggingState) !== null && _this$state$draggingS3 !== void 0 && _this$state$draggingS3.originalCellSize) {
|
|
609
|
+
newReferencePosCell = new DOMRect(newCellRect.x, newCellRect.y, this.state.draggingState.originalCellSize.width, this.state.draggingState.originalCellSize.height);
|
|
610
|
+
} else {
|
|
611
|
+
newReferencePosCell = newCellRect;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// Update last row/col positions if needed
|
|
617
|
+
var newReferencePosLastRow = this.state.referencePosLastRow;
|
|
618
|
+
var newReferencePosLastCol = this.state.referencePosLastCol;
|
|
619
|
+
if (this.state.showAddOrRemoveRowsButton || this.state.showAddOrRemoveColumnsButton) {
|
|
620
|
+
var _tableInfo$node$conte3, _tableInfo$node$conte4;
|
|
621
|
+
var lastRowIndex = tableInfo.node.content.childCount - 1;
|
|
622
|
+
var lastColIndex = ((_tableInfo$node$conte3 = (_tableInfo$node$conte4 = tableInfo.node.content.firstChild) === null || _tableInfo$node$conte4 === void 0 ? void 0 : _tableInfo$node$conte4.content.childCount) !== null && _tableInfo$node$conte3 !== void 0 ? _tableInfo$node$conte3 : 0) - 1;
|
|
623
|
+
if (this.state.showAddOrRemoveRowsButton) {
|
|
624
|
+
var lastRow = tableBody.children[lastRowIndex];
|
|
625
|
+
if (lastRow) {
|
|
626
|
+
newReferencePosLastRow = lastRow.getBoundingClientRect();
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (this.state.showAddOrRemoveColumnsButton) {
|
|
630
|
+
var maxRight = 0;
|
|
631
|
+
var lastColRect = null;
|
|
632
|
+
for (var i = 0; i < tableBody.children.length; i++) {
|
|
633
|
+
var row = tableBody.children[i];
|
|
634
|
+
if (row && row.children[lastColIndex]) {
|
|
635
|
+
var cell = row.children[lastColIndex];
|
|
636
|
+
var cellRect = cell.getBoundingClientRect();
|
|
637
|
+
if (cellRect.right > maxRight) {
|
|
638
|
+
maxRight = cellRect.right;
|
|
639
|
+
lastColRect = cellRect;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
if (lastColRect) {
|
|
644
|
+
var firstRow = tableBody.children[0];
|
|
645
|
+
var _lastRow5 = tableBody.children[lastRowIndex];
|
|
646
|
+
if (firstRow && _lastRow5) {
|
|
647
|
+
var firstCell = firstRow.children[lastColIndex];
|
|
648
|
+
if (firstCell) {
|
|
649
|
+
var firstRect = firstCell.getBoundingClientRect();
|
|
650
|
+
newReferencePosLastCol = new DOMRect(lastColRect.right, firstRect.top, 0, _lastRow5.getBoundingClientRect().bottom - firstRect.top);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// Check if positions actually changed (compare individual properties)
|
|
658
|
+
var refPosChanged = !newReferencePosCell || !this.state.referencePosCell || newReferencePosCell.x !== this.state.referencePosCell.x || newReferencePosCell.y !== this.state.referencePosCell.y || newReferencePosTable.x !== this.state.referencePosTable.x || newReferencePosTable.y !== this.state.referencePosTable.y || newReferencePosLastRow && this.state.referencePosLastRow && (newReferencePosLastRow.x !== this.state.referencePosLastRow.x || newReferencePosLastRow.y !== this.state.referencePosLastRow.y) || newReferencePosLastCol && this.state.referencePosLastCol && (newReferencePosLastCol.x !== this.state.referencePosLastCol.x || newReferencePosLastCol.y !== this.state.referencePosLastCol.y);
|
|
659
|
+
if (refPosChanged) {
|
|
660
|
+
this.state = _objectSpread(_objectSpread({}, this.state), {}, {
|
|
661
|
+
referencePosCell: newReferencePosCell,
|
|
662
|
+
referencePosTable: newReferencePosTable,
|
|
663
|
+
referencePosLastRow: newReferencePosLastRow,
|
|
664
|
+
referencePosLastCol: newReferencePosLastCol
|
|
665
|
+
});
|
|
666
|
+
this.emitUpdate();
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}, {
|
|
670
|
+
key: "destroy",
|
|
671
|
+
value: function destroy() {
|
|
672
|
+
this.editorView.dom.removeEventListener('mousemove', this.mouseMoveHandler);
|
|
673
|
+
window.removeEventListener('mouseup', this.mouseUpHandler);
|
|
674
|
+
this.editorView.dom.removeEventListener('mousedown', this.viewMousedownHandler);
|
|
675
|
+
this.editorView.root.removeEventListener('dragover', this.dragOverHandler);
|
|
676
|
+
this.editorView.root.removeEventListener('drop', this.dropHandler);
|
|
677
|
+
window.removeEventListener('scroll', this.scrollHandler, true);
|
|
678
|
+
window.removeEventListener('resize', this.scrollHandler);
|
|
679
|
+
}
|
|
680
|
+
}]);
|
|
681
|
+
return TableHandleView;
|
|
682
|
+
}();
|
|
683
|
+
var tableHandleView = null;
|
|
684
|
+
export function TableHandlePlugin(editor, emitUpdate) {
|
|
685
|
+
return new Plugin({
|
|
686
|
+
key: tableHandlePluginKey,
|
|
687
|
+
state: {
|
|
688
|
+
init: function init() {
|
|
689
|
+
return false;
|
|
690
|
+
},
|
|
691
|
+
apply: function apply(tr, frozen) {
|
|
692
|
+
var meta = tr.getMeta(tableHandlePluginKey);
|
|
693
|
+
return meta !== undefined ? meta : frozen;
|
|
694
|
+
}
|
|
695
|
+
},
|
|
696
|
+
view: function view(editorView) {
|
|
697
|
+
tableHandleView = new TableHandleView(editor, editorView, emitUpdate);
|
|
698
|
+
return tableHandleView;
|
|
699
|
+
},
|
|
700
|
+
props: {
|
|
701
|
+
decorations: function decorations(state) {
|
|
702
|
+
if (!tableHandleView) return null;
|
|
703
|
+
if (tableHandleView === undefined || tableHandleView.state === undefined || tableHandleView.state.draggingState === undefined || tableHandleView.tablePos === undefined) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
var newIndex = tableHandleView.state.draggingState.draggedCellOrientation === 'row' ? tableHandleView.state.rowIndex : tableHandleView.state.colIndex;
|
|
707
|
+
if (newIndex === undefined) {
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
var decorations = [];
|
|
711
|
+
var draggingState = tableHandleView.state.draggingState;
|
|
712
|
+
var originalIndex = draggingState.originalIndex;
|
|
713
|
+
if (tableHandleView.state.draggingState.draggedCellOrientation === 'row') {
|
|
714
|
+
var originalCells = getRowCells(editor, originalIndex, tableHandleView.state.blockPos);
|
|
715
|
+
originalCells.cells.forEach(function (cell) {
|
|
716
|
+
if (cell.node) {
|
|
717
|
+
decorations.push(Decoration.node(cell.pos, cell.pos + cell.node.nodeSize, {
|
|
718
|
+
class: 'table-cell-dragging-source'
|
|
719
|
+
}));
|
|
720
|
+
}
|
|
721
|
+
});
|
|
722
|
+
} else {
|
|
723
|
+
var _originalCells = getColumnCells(editor, originalIndex, tableHandleView.state.blockPos);
|
|
724
|
+
_originalCells.cells.forEach(function (cell) {
|
|
725
|
+
if (cell.node) {
|
|
726
|
+
decorations.push(Decoration.node(cell.pos, cell.pos + cell.node.nodeSize, {
|
|
727
|
+
class: 'table-cell-dragging-source'
|
|
728
|
+
}));
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// Return empty decorations if:
|
|
734
|
+
// - original index is same as new index (no change)
|
|
735
|
+
// - editor is not defined for some reason
|
|
736
|
+
if (newIndex === originalIndex || !editor) {
|
|
737
|
+
return DecorationSet.create(state.doc, decorations);
|
|
738
|
+
}
|
|
739
|
+
if (tableHandleView.state.draggingState.draggedCellOrientation === 'row') {
|
|
740
|
+
var cellsInRow = getRowCells(editor, newIndex, tableHandleView.state.blockPos);
|
|
741
|
+
cellsInRow.cells.forEach(function (cell) {
|
|
742
|
+
var cellNode = cell.node;
|
|
743
|
+
if (!cellNode) {
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
// Creates a decoration at the start or end of each cell,
|
|
748
|
+
// depending on whether the new index is before or after the
|
|
749
|
+
// original index.
|
|
750
|
+
var decorationPos = cell.pos + (newIndex > originalIndex ? cellNode.nodeSize - 2 : 2);
|
|
751
|
+
decorations.push(Decoration.widget(decorationPos, function () {
|
|
752
|
+
var widget = document.createElement('div');
|
|
753
|
+
widget.className = 'tiptap-table-dropcursor';
|
|
754
|
+
widget.style.position = 'absolute';
|
|
755
|
+
widget.style.left = '0';
|
|
756
|
+
widget.style.right = '0';
|
|
757
|
+
widget.style.zIndex = '20';
|
|
758
|
+
widget.style.pointerEvents = 'none';
|
|
759
|
+
// This is only necessary because the drop indicator's height
|
|
760
|
+
// is an even number of pixels, whereas the border between
|
|
761
|
+
// table cells is an odd number of pixels. So this makes the
|
|
762
|
+
// positioning slightly more consistent regardless of where
|
|
763
|
+
// the row is being dropped.
|
|
764
|
+
if (newIndex > originalIndex) {
|
|
765
|
+
widget.style.bottom = '-1px';
|
|
766
|
+
} else {
|
|
767
|
+
widget.style.top = '-1px';
|
|
768
|
+
}
|
|
769
|
+
widget.style.height = '3px';
|
|
770
|
+
widget.style.backgroundColor = 'var(--mui-palette-primary-main, #1976d2)';
|
|
771
|
+
return widget;
|
|
772
|
+
}));
|
|
773
|
+
});
|
|
774
|
+
} else {
|
|
775
|
+
var cellsInColumn = getColumnCells(editor, newIndex, tableHandleView.state.blockPos);
|
|
776
|
+
cellsInColumn.cells.forEach(function (cell) {
|
|
777
|
+
var cellNode = cell.node;
|
|
778
|
+
if (!cellNode) {
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
// Creates a decoration at the start or end of each cell,
|
|
782
|
+
// depending on whether the new index is before or after the
|
|
783
|
+
// original index.
|
|
784
|
+
var decorationPos = cell.pos + (newIndex > originalIndex ? cellNode.nodeSize - 2 : 2);
|
|
785
|
+
decorations.push(Decoration.widget(decorationPos, function () {
|
|
786
|
+
var widget = document.createElement('div');
|
|
787
|
+
widget.className = 'tiptap-table-dropcursor';
|
|
788
|
+
widget.style.position = 'absolute';
|
|
789
|
+
widget.style.top = '0';
|
|
790
|
+
widget.style.bottom = '0';
|
|
791
|
+
widget.style.zIndex = '20';
|
|
792
|
+
widget.style.pointerEvents = 'none';
|
|
793
|
+
// This is only necessary because the drop indicator's width
|
|
794
|
+
// is an even number of pixels, whereas the border between
|
|
795
|
+
// table cells is an odd number of pixels. So this makes the
|
|
796
|
+
// positioning slightly more consistent regardless of where
|
|
797
|
+
// the column is being dropped.
|
|
798
|
+
if (newIndex > originalIndex) {
|
|
799
|
+
widget.style.right = '-1px';
|
|
800
|
+
} else {
|
|
801
|
+
widget.style.left = '-1px';
|
|
802
|
+
}
|
|
803
|
+
widget.style.width = '3px';
|
|
804
|
+
widget.style.backgroundColor = 'var(--mui-palette-primary-main, #1976d2)';
|
|
805
|
+
return widget;
|
|
806
|
+
}));
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
return DecorationSet.create(state.doc, decorations);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/**
|
|
816
|
+
* Shared drag start handler for table rows and columns
|
|
817
|
+
*/
|
|
818
|
+
var tableDragStart = function tableDragStart(orientation, event) {
|
|
819
|
+
var _tableHandleView;
|
|
820
|
+
// If state doesn't exist, try to recover from DOM or data attributes
|
|
821
|
+
if (!((_tableHandleView = tableHandleView) !== null && _tableHandleView !== void 0 && _tableHandleView.state)) {
|
|
822
|
+
var _tableHandleView2, _tableHandleView3;
|
|
823
|
+
var handleElement = event.currentTarget;
|
|
824
|
+
|
|
825
|
+
// Try to recover from data attributes first (set by React component)
|
|
826
|
+
var dataIndex = handleElement.dataset.tableIndex;
|
|
827
|
+
var dataTablePos = handleElement.dataset.tablePos;
|
|
828
|
+
var dataTableId = handleElement.dataset.tableId;
|
|
829
|
+
if (dataIndex && dataTablePos && tableHandleView) {
|
|
830
|
+
var _index = parseInt(dataIndex, 10);
|
|
831
|
+
var _blockPos = parseInt(dataTablePos, 10);
|
|
832
|
+
if (!isNaN(_index) && !isNaN(_blockPos)) {
|
|
833
|
+
// Try to find the table node
|
|
834
|
+
var tableNode = tableHandleView.editor.state.doc.nodeAt(_blockPos);
|
|
835
|
+
if (tableNode && isTableNode(tableNode)) {
|
|
836
|
+
var tableWrapper = safeClosest(handleElement, '.tableWrapper');
|
|
837
|
+
var tbody = tableWrapper === null || tableWrapper === void 0 ? void 0 : tableWrapper.querySelector('tbody');
|
|
838
|
+
if (tbody) {
|
|
839
|
+
var tableRect = tbody.getBoundingClientRect();
|
|
840
|
+
|
|
841
|
+
// Create minimal state for drag operation
|
|
842
|
+
var recoveredState = {
|
|
843
|
+
show: true,
|
|
844
|
+
showAddOrRemoveRowsButton: false,
|
|
845
|
+
showAddOrRemoveColumnsButton: false,
|
|
846
|
+
referencePosTable: tableRect,
|
|
847
|
+
block: tableNode,
|
|
848
|
+
blockPos: _blockPos,
|
|
849
|
+
colIndex: orientation === 'col' ? _index : undefined,
|
|
850
|
+
rowIndex: orientation === 'row' ? _index : undefined,
|
|
851
|
+
referencePosCell: undefined,
|
|
852
|
+
widgetContainer: undefined
|
|
853
|
+
};
|
|
854
|
+
tableHandleView.state = recoveredState;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// If still no state, try to recover from DOM
|
|
861
|
+
if (!((_tableHandleView2 = tableHandleView) !== null && _tableHandleView2 !== void 0 && _tableHandleView2.state)) {
|
|
862
|
+
var _tableWrapper = safeClosest(handleElement, '.tableWrapper');
|
|
863
|
+
if (!_tableWrapper || !tableHandleView) {
|
|
864
|
+
// Can't recover - cancel drag silently
|
|
865
|
+
if (event.dataTransfer) {
|
|
866
|
+
event.dataTransfer.effectAllowed = 'none';
|
|
867
|
+
}
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// Try to recover table info from DOM
|
|
872
|
+
var tableInfo = getTableFromDOM(_tableWrapper, tableHandleView.editor);
|
|
873
|
+
if (!tableInfo) {
|
|
874
|
+
if (event.dataTransfer) {
|
|
875
|
+
event.dataTransfer.effectAllowed = 'none';
|
|
876
|
+
}
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
// Recover basic state from DOM
|
|
881
|
+
var _tbody3 = _tableWrapper.querySelector('tbody');
|
|
882
|
+
if (!_tbody3) {
|
|
883
|
+
if (event.dataTransfer) {
|
|
884
|
+
event.dataTransfer.effectAllowed = 'none';
|
|
885
|
+
}
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// Try to determine index from handle position
|
|
890
|
+
// This is a fallback - ideally state should exist
|
|
891
|
+
var _tableRect = _tbody3.getBoundingClientRect();
|
|
892
|
+
var handleRect = handleElement.getBoundingClientRect();
|
|
893
|
+
|
|
894
|
+
// Approximate index based on handle position
|
|
895
|
+
// This is not perfect but better than throwing an error
|
|
896
|
+
var approximateIndex = 0;
|
|
897
|
+
if (orientation === 'row') {
|
|
898
|
+
var rowHeight = _tableRect.height / tableInfo.node.content.childCount;
|
|
899
|
+
approximateIndex = Math.floor((handleRect.top - _tableRect.top) / rowHeight);
|
|
900
|
+
} else {
|
|
901
|
+
var _tableInfo$node$conte5, _tableInfo$node$conte6;
|
|
902
|
+
var colWidth = _tableRect.width / ((_tableInfo$node$conte5 = (_tableInfo$node$conte6 = tableInfo.node.content.firstChild) === null || _tableInfo$node$conte6 === void 0 ? void 0 : _tableInfo$node$conte6.content.childCount) !== null && _tableInfo$node$conte5 !== void 0 ? _tableInfo$node$conte5 : 1);
|
|
903
|
+
approximateIndex = Math.floor((handleRect.left - _tableRect.left) / colWidth);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
// Create minimal state for drag operation
|
|
907
|
+
var _recoveredState = {
|
|
908
|
+
show: true,
|
|
909
|
+
showAddOrRemoveRowsButton: false,
|
|
910
|
+
showAddOrRemoveColumnsButton: false,
|
|
911
|
+
referencePosTable: _tableRect,
|
|
912
|
+
block: tableInfo.node,
|
|
913
|
+
blockPos: tableInfo.pos,
|
|
914
|
+
colIndex: orientation === 'col' ? approximateIndex : undefined,
|
|
915
|
+
rowIndex: orientation === 'row' ? approximateIndex : undefined,
|
|
916
|
+
referencePosCell: undefined,
|
|
917
|
+
widgetContainer: undefined
|
|
918
|
+
};
|
|
919
|
+
tableHandleView.state = _recoveredState;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
// Final check - if still no state, cancel drag
|
|
923
|
+
if (!((_tableHandleView3 = tableHandleView) !== null && _tableHandleView3 !== void 0 && _tableHandleView3.state)) {
|
|
924
|
+
if (event.dataTransfer) {
|
|
925
|
+
event.dataTransfer.effectAllowed = 'none';
|
|
926
|
+
}
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
var _tableHandleView4 = tableHandleView,
|
|
931
|
+
state = _tableHandleView4.state,
|
|
932
|
+
editor = _tableHandleView4.editor;
|
|
933
|
+
var index = orientation === 'col' ? state.colIndex : state.rowIndex;
|
|
934
|
+
if (index === undefined) {
|
|
935
|
+
// Can't determine index - cancel drag silently
|
|
936
|
+
if (event.dataTransfer) {
|
|
937
|
+
event.dataTransfer.effectAllowed = 'none';
|
|
938
|
+
}
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
var blockPos = state.blockPos,
|
|
942
|
+
referencePosCell = state.referencePosCell;
|
|
943
|
+
var mousePos = orientation === 'col' ? event.clientX : event.clientY;
|
|
944
|
+
|
|
945
|
+
// Clear cell selection to prevent table reference collapse
|
|
946
|
+
if (editor.state.selection instanceof CellSelection) {
|
|
947
|
+
var safeSel = TextSelection.near(editor.state.doc.resolve(blockPos), 1);
|
|
948
|
+
editor.view.dispatch(editor.state.tr.setSelection(safeSel));
|
|
949
|
+
}
|
|
950
|
+
var dragImage = createTableDragImage(editor, orientation, index, blockPos);
|
|
951
|
+
|
|
952
|
+
// Configure drag image
|
|
953
|
+
if (event.dataTransfer) {
|
|
954
|
+
var _handleRect = event.currentTarget.getBoundingClientRect();
|
|
955
|
+
var offset = orientation === 'col' ? {
|
|
956
|
+
x: _handleRect.width / 2,
|
|
957
|
+
y: 0
|
|
958
|
+
} : {
|
|
959
|
+
x: 0,
|
|
960
|
+
y: _handleRect.height / 2
|
|
961
|
+
};
|
|
962
|
+
event.dataTransfer.effectAllowed = orientation === 'col' ? 'move' : 'copyMove';
|
|
963
|
+
event.dataTransfer.setDragImage(dragImage, offset.x, offset.y);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// Cleanup drag image
|
|
967
|
+
var cleanup = function cleanup() {
|
|
968
|
+
var _dragImage$parentNode;
|
|
969
|
+
return (_dragImage$parentNode = dragImage.parentNode) === null || _dragImage$parentNode === void 0 ? void 0 : _dragImage$parentNode.removeChild(dragImage);
|
|
970
|
+
};
|
|
971
|
+
document.addEventListener('drop', cleanup, {
|
|
972
|
+
once: true
|
|
973
|
+
});
|
|
974
|
+
document.addEventListener('dragend', cleanup, {
|
|
975
|
+
once: true
|
|
976
|
+
});
|
|
977
|
+
var initialOffset = referencePosCell ? (orientation === 'col' ? referencePosCell.left : referencePosCell.top) - mousePos : 0;
|
|
978
|
+
|
|
979
|
+
// Save original cell size to preserve it during drag
|
|
980
|
+
var originalCellSize = referencePosCell ? {
|
|
981
|
+
width: referencePosCell.width,
|
|
982
|
+
height: referencePosCell.height
|
|
983
|
+
} : undefined;
|
|
984
|
+
|
|
985
|
+
// Update dragging state
|
|
986
|
+
tableHandleView.state = _objectSpread(_objectSpread({}, state), {}, {
|
|
987
|
+
draggingState: {
|
|
988
|
+
draggedCellOrientation: orientation,
|
|
989
|
+
originalIndex: index,
|
|
990
|
+
mousePos: mousePos,
|
|
991
|
+
initialOffset: initialOffset,
|
|
992
|
+
originalCellSize: originalCellSize
|
|
993
|
+
}
|
|
994
|
+
});
|
|
995
|
+
tableHandleView.emitUpdate();
|
|
996
|
+
editor.view.dispatch(editor.state.tr.setMeta(tableHandlePluginKey, true));
|
|
997
|
+
};
|
|
998
|
+
|
|
999
|
+
/**
|
|
1000
|
+
* Callback for column drag handle
|
|
1001
|
+
*/
|
|
1002
|
+
export var colDragStart = function colDragStart(event) {
|
|
1003
|
+
return tableDragStart('col', _objectSpread(_objectSpread({}, event), {}, {
|
|
1004
|
+
clientY: 0
|
|
1005
|
+
}));
|
|
1006
|
+
};
|
|
1007
|
+
|
|
1008
|
+
/**
|
|
1009
|
+
* Callback for row drag handle
|
|
1010
|
+
*/
|
|
1011
|
+
export var rowDragStart = function rowDragStart(event) {
|
|
1012
|
+
return tableDragStart('row', _objectSpread(_objectSpread({}, event), {}, {
|
|
1013
|
+
clientX: 0
|
|
1014
|
+
}));
|
|
1015
|
+
};
|
|
1016
|
+
|
|
1017
|
+
/**
|
|
1018
|
+
* Drag end cleanup
|
|
1019
|
+
*/
|
|
1020
|
+
export var dragEnd = function dragEnd() {
|
|
1021
|
+
if (!tableHandleView || tableHandleView.state === undefined) {
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
tableHandleView.state = _objectSpread(_objectSpread({}, tableHandleView.state), {}, {
|
|
1025
|
+
draggingState: undefined
|
|
1026
|
+
});
|
|
1027
|
+
tableHandleView.emitUpdate();
|
|
1028
|
+
var editor = tableHandleView.editor;
|
|
1029
|
+
editor.view.dispatch(editor.state.tr.setMeta(tableHandlePluginKey, null));
|
|
1030
|
+
};
|