@kerebron/extension-tables 0.4.28 → 0.4.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/esm/ExtensionTables.js +1 -0
  2. package/esm/ExtensionTables.js.map +1 -0
  3. package/esm/NodeTable.js +1 -0
  4. package/esm/NodeTable.js.map +1 -0
  5. package/esm/NodeTableCell.js +1 -0
  6. package/esm/NodeTableCell.js.map +1 -0
  7. package/esm/NodeTableHeader.js +1 -0
  8. package/esm/NodeTableHeader.js.map +1 -0
  9. package/esm/NodeTableRow.js +1 -0
  10. package/esm/NodeTableRow.js.map +1 -0
  11. package/esm/_dnt.shims.js +1 -0
  12. package/esm/_dnt.shims.js.map +1 -0
  13. package/esm/utilities/CellSelection.js +1 -0
  14. package/esm/utilities/CellSelection.js.map +1 -0
  15. package/esm/utilities/TableMap.js +1 -0
  16. package/esm/utilities/TableMap.js.map +1 -0
  17. package/esm/utilities/TableView.js +1 -0
  18. package/esm/utilities/TableView.js.map +1 -0
  19. package/esm/utilities/columnResizing.js +1 -0
  20. package/esm/utilities/columnResizing.js.map +1 -0
  21. package/esm/utilities/commands.js +1 -0
  22. package/esm/utilities/commands.js.map +1 -0
  23. package/esm/utilities/copypaste.js +1 -0
  24. package/esm/utilities/copypaste.js.map +1 -0
  25. package/esm/utilities/createCell.js +1 -0
  26. package/esm/utilities/createCell.js.map +1 -0
  27. package/esm/utilities/createTable.js +1 -0
  28. package/esm/utilities/createTable.js.map +1 -0
  29. package/esm/utilities/fixTables.js +1 -0
  30. package/esm/utilities/fixTables.js.map +1 -0
  31. package/esm/utilities/getTableNodeTypes.js +1 -0
  32. package/esm/utilities/getTableNodeTypes.js.map +1 -0
  33. package/esm/utilities/input.js +1 -0
  34. package/esm/utilities/input.js.map +1 -0
  35. package/esm/utilities/tableEditing.js +1 -0
  36. package/esm/utilities/tableEditing.js.map +1 -0
  37. package/esm/utilities/tableNodeTypes.js +1 -0
  38. package/esm/utilities/tableNodeTypes.js.map +1 -0
  39. package/esm/utilities/util.js +1 -0
  40. package/esm/utilities/util.js.map +1 -0
  41. package/package.json +6 -2
  42. package/src/ExtensionTables.ts +16 -0
  43. package/src/NodeTable.ts +139 -0
  44. package/src/NodeTableCell.ts +70 -0
  45. package/src/NodeTableHeader.ts +49 -0
  46. package/src/NodeTableRow.ts +41 -0
  47. package/src/_dnt.shims.ts +60 -0
  48. package/src/utilities/CellSelection.ts +477 -0
  49. package/src/utilities/TableMap.ts +392 -0
  50. package/src/utilities/TableView.ts +102 -0
  51. package/src/utilities/columnResizing.ts +437 -0
  52. package/src/utilities/commands.ts +896 -0
  53. package/src/utilities/copypaste.ts +394 -0
  54. package/src/utilities/createCell.ts +12 -0
  55. package/src/utilities/createTable.ts +53 -0
  56. package/src/utilities/fixTables.ts +156 -0
  57. package/src/utilities/getTableNodeTypes.ts +21 -0
  58. package/src/utilities/input.ts +299 -0
  59. package/src/utilities/tableEditing.ts +90 -0
  60. package/src/utilities/tableNodeTypes.ts +32 -0
  61. package/src/utilities/util.ts +204 -0
  62. package/assets/tables.css +0 -85
@@ -0,0 +1,437 @@
1
+ import * as dntShim from "../_dnt.shims.js";
2
+ import { Attrs, Node as ProsemirrorNode } from 'prosemirror-model';
3
+ import { EditorState, Plugin, PluginKey, Transaction } from 'prosemirror-state';
4
+ import {
5
+ Decoration,
6
+ DecorationSet,
7
+ EditorView,
8
+ NodeView,
9
+ } from 'prosemirror-view';
10
+ import { tableNodeTypes } from './tableNodeTypes.js';
11
+ import { TableMap } from './TableMap.js';
12
+ import { TableView, updateColumnsOnResize } from './TableView.js';
13
+ import { cellAround, CellAttrs, pointsAtCell } from './util.js';
14
+
15
+ /**
16
+ * @public
17
+ */
18
+ export const columnResizingPluginKey = new PluginKey<ResizeState>(
19
+ 'tableColumnResizing',
20
+ );
21
+
22
+ /**
23
+ * @public
24
+ */
25
+ export type ColumnResizingOptions = {
26
+ handleWidth?: number;
27
+ /**
28
+ * Minimum width of a cell /column. The column cannot be resized smaller than this.
29
+ */
30
+ cellMinWidth?: number;
31
+ /**
32
+ * The default minWidth of a cell / column when it doesn't have an explicit width (i.e.: it has not been resized manually)
33
+ */
34
+ defaultCellMinWidth?: number;
35
+ lastColumnResizable?: boolean;
36
+ /**
37
+ * A custom node view for the rendering table nodes. By default, the plugin
38
+ * uses the {@link TableView} class. You can explicitly set this to `null` to
39
+ * not use a custom node view.
40
+ */
41
+ View?:
42
+ | (new (
43
+ node: ProsemirrorNode,
44
+ cellMinWidth: number,
45
+ view: EditorView,
46
+ ) => NodeView)
47
+ | null;
48
+ };
49
+
50
+ /**
51
+ * @public
52
+ */
53
+ export type Dragging = { startX: number; startWidth: number };
54
+
55
+ /**
56
+ * @public
57
+ */
58
+ export function columnResizing({
59
+ handleWidth = 5,
60
+ cellMinWidth = 25,
61
+ defaultCellMinWidth = 100,
62
+ View = TableView,
63
+ lastColumnResizable = true,
64
+ }: ColumnResizingOptions = {}): Plugin {
65
+ const plugin = new Plugin<ResizeState>({
66
+ key: columnResizingPluginKey,
67
+ state: {
68
+ init(_, state) {
69
+ const nodeViews = plugin.spec?.props?.nodeViews;
70
+ const tableName = tableNodeTypes(state.schema).table.name;
71
+ if (View && nodeViews) {
72
+ nodeViews[tableName] = (node, view) => {
73
+ return new View(node, defaultCellMinWidth, view);
74
+ };
75
+ }
76
+ return new ResizeState(-1, false);
77
+ },
78
+ apply(tr, prev) {
79
+ return prev.apply(tr);
80
+ },
81
+ },
82
+ props: {
83
+ attributes: (state): Record<string, string> => {
84
+ const pluginState = columnResizingPluginKey.getState(state);
85
+ return pluginState && pluginState.activeHandle > -1
86
+ ? { class: 'resize-cursor' }
87
+ : {};
88
+ },
89
+
90
+ handleDOMEvents: {
91
+ mousemove: (view, event) => {
92
+ handleMouseMove(view, event, handleWidth, lastColumnResizable);
93
+ },
94
+ mouseleave: (view) => {
95
+ handleMouseLeave(view);
96
+ },
97
+ mousedown: (view, event) => {
98
+ handleMouseDown(view, event, cellMinWidth, defaultCellMinWidth);
99
+ },
100
+ },
101
+
102
+ decorations: (state) => {
103
+ const pluginState = columnResizingPluginKey.getState(state);
104
+ if (pluginState && pluginState.activeHandle > -1) {
105
+ return handleDecorations(state, pluginState.activeHandle);
106
+ }
107
+ },
108
+
109
+ nodeViews: {},
110
+ },
111
+ });
112
+ return plugin;
113
+ }
114
+
115
+ /**
116
+ * @public
117
+ */
118
+ export class ResizeState {
119
+ constructor(
120
+ public activeHandle: number,
121
+ public dragging: Dragging | false,
122
+ ) {}
123
+
124
+ apply(tr: Transaction): ResizeState {
125
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
126
+ const action = tr.getMeta(columnResizingPluginKey);
127
+ if (action && action.setHandle != null) {
128
+ return new ResizeState(action.setHandle, false);
129
+ }
130
+ if (action && action.setDragging !== undefined) {
131
+ return new ResizeState(this.activeHandle, action.setDragging);
132
+ }
133
+ if (this.activeHandle > -1 && tr.docChanged) {
134
+ let handle = tr.mapping.map(this.activeHandle, -1);
135
+ if (!pointsAtCell(tr.doc.resolve(handle))) {
136
+ handle = -1;
137
+ }
138
+ return new ResizeState(handle, this.dragging);
139
+ }
140
+ return this;
141
+ }
142
+ }
143
+
144
+ function handleMouseMove(
145
+ view: EditorView,
146
+ event: MouseEvent,
147
+ handleWidth: number,
148
+ lastColumnResizable: boolean,
149
+ ): void {
150
+ if (!view.editable) return;
151
+
152
+ const pluginState = columnResizingPluginKey.getState(view.state);
153
+ if (!pluginState) return;
154
+
155
+ if (!pluginState.dragging) {
156
+ const target = domCellAround(event.target as HTMLElement);
157
+ let cell = -1;
158
+ if (target) {
159
+ const { left, right } = target.getBoundingClientRect();
160
+ if (event.clientX - left <= handleWidth) {
161
+ cell = edgeCell(view, event, 'left', handleWidth);
162
+ } else if (right - event.clientX <= handleWidth) {
163
+ cell = edgeCell(view, event, 'right', handleWidth);
164
+ }
165
+ }
166
+
167
+ if (cell != pluginState.activeHandle) {
168
+ if (!lastColumnResizable && cell !== -1) {
169
+ const $cell = view.state.doc.resolve(cell);
170
+ const table = $cell.node(-1);
171
+ const map = TableMap.get(table);
172
+ const tableStart = $cell.start(-1);
173
+ const col = map.colCount($cell.pos - tableStart) +
174
+ $cell.nodeAfter!.attrs.colspan -
175
+ 1;
176
+
177
+ if (col == map.width - 1) {
178
+ return;
179
+ }
180
+ }
181
+
182
+ updateHandle(view, cell);
183
+ }
184
+ }
185
+ }
186
+
187
+ function handleMouseLeave(view: EditorView): void {
188
+ if (!view.editable) return;
189
+
190
+ const pluginState = columnResizingPluginKey.getState(view.state);
191
+ if (pluginState && pluginState.activeHandle > -1 && !pluginState.dragging) {
192
+ updateHandle(view, -1);
193
+ }
194
+ }
195
+
196
+ function handleMouseDown(
197
+ view: EditorView,
198
+ event: MouseEvent,
199
+ cellMinWidth: number,
200
+ defaultCellMinWidth: number,
201
+ ): boolean {
202
+ if (!view.editable) return false;
203
+
204
+ const win = view.dom.ownerDocument.defaultView || dntShim.dntGlobalThis;
205
+
206
+ const pluginState = columnResizingPluginKey.getState(view.state);
207
+ if (!pluginState || pluginState.activeHandle == -1 || pluginState.dragging) {
208
+ return false;
209
+ }
210
+
211
+ const cell = view.state.doc.nodeAt(pluginState.activeHandle)!;
212
+ const width = currentColWidth(view, pluginState.activeHandle, cell.attrs);
213
+ view.dispatch(
214
+ view.state.tr.setMeta(columnResizingPluginKey, {
215
+ setDragging: { startX: event.clientX, startWidth: width },
216
+ }),
217
+ );
218
+
219
+ function finish(event: MouseEvent) {
220
+ win.removeEventListener('mouseup', finish);
221
+ win.removeEventListener('mousemove', move);
222
+ const pluginState = columnResizingPluginKey.getState(view.state);
223
+ if (pluginState?.dragging) {
224
+ updateColumnWidth(
225
+ view,
226
+ pluginState.activeHandle,
227
+ draggedWidth(pluginState.dragging, event, cellMinWidth),
228
+ );
229
+ view.dispatch(
230
+ view.state.tr.setMeta(columnResizingPluginKey, { setDragging: null }),
231
+ );
232
+ }
233
+ }
234
+
235
+ function move(event: MouseEvent): void {
236
+ if (!event.which) return finish(event);
237
+ const pluginState = columnResizingPluginKey.getState(view.state);
238
+ if (!pluginState) return;
239
+ if (pluginState.dragging) {
240
+ const dragged = draggedWidth(pluginState.dragging, event, cellMinWidth);
241
+ displayColumnWidth(
242
+ view,
243
+ pluginState.activeHandle,
244
+ dragged,
245
+ defaultCellMinWidth,
246
+ );
247
+ }
248
+ }
249
+
250
+ displayColumnWidth(
251
+ view,
252
+ pluginState.activeHandle,
253
+ width,
254
+ defaultCellMinWidth,
255
+ );
256
+
257
+ win.addEventListener('mouseup', finish);
258
+ win.addEventListener('mousemove', move);
259
+ event.preventDefault();
260
+ return true;
261
+ }
262
+
263
+ function currentColWidth(
264
+ view: EditorView,
265
+ cellPos: number,
266
+ { colspan, colwidth }: Attrs,
267
+ ): number {
268
+ const width = colwidth && colwidth[colwidth.length - 1];
269
+ if (width) return width;
270
+ const dom = view.domAtPos(cellPos);
271
+ const node = dom.node.childNodes[dom.offset] as HTMLElement;
272
+ let domWidth = node.offsetWidth,
273
+ parts = colspan;
274
+ if (colwidth) {
275
+ for (let i = 0; i < colspan; i++) {
276
+ if (colwidth[i]) {
277
+ domWidth -= colwidth[i];
278
+ parts--;
279
+ }
280
+ }
281
+ }
282
+ return domWidth / parts;
283
+ }
284
+
285
+ function domCellAround(target: HTMLElement | null): HTMLElement | null {
286
+ while (target && target.nodeName != 'TD' && target.nodeName != 'TH') {
287
+ target = target.classList && target.classList.contains('ProseMirror')
288
+ ? null
289
+ : (target.parentNode as HTMLElement);
290
+ }
291
+ return target;
292
+ }
293
+
294
+ function edgeCell(
295
+ view: EditorView,
296
+ event: MouseEvent,
297
+ side: 'left' | 'right',
298
+ handleWidth: number,
299
+ ): number {
300
+ // posAtCoords returns inconsistent positions when cursor is moving
301
+ // across a collapsed table border. Use an offset to adjust the
302
+ // target viewport coordinates away from the table border.
303
+ const offset = side == 'right' ? -handleWidth : handleWidth;
304
+ const found = view.posAtCoords({
305
+ left: event.clientX + offset,
306
+ top: event.clientY,
307
+ });
308
+ if (!found) return -1;
309
+ const { pos } = found;
310
+ const $cell = cellAround(view.state.doc.resolve(pos));
311
+ if (!$cell) return -1;
312
+ if (side == 'right') return $cell.pos;
313
+ const map = TableMap.get($cell.node(-1)),
314
+ start = $cell.start(-1);
315
+ const index = map.map.indexOf($cell.pos - start);
316
+ return index % map.width == 0 ? -1 : start + map.map[index - 1];
317
+ }
318
+
319
+ function draggedWidth(
320
+ dragging: Dragging,
321
+ event: MouseEvent,
322
+ resizeMinWidth: number,
323
+ ): number {
324
+ const offset = event.clientX - dragging.startX;
325
+ return Math.max(resizeMinWidth, dragging.startWidth + offset);
326
+ }
327
+
328
+ function updateHandle(view: EditorView, value: number): void {
329
+ view.dispatch(
330
+ view.state.tr.setMeta(columnResizingPluginKey, { setHandle: value }),
331
+ );
332
+ }
333
+
334
+ function updateColumnWidth(
335
+ view: EditorView,
336
+ cell: number,
337
+ width: number,
338
+ ): void {
339
+ const $cell = view.state.doc.resolve(cell);
340
+ const table = $cell.node(-1),
341
+ map = TableMap.get(table),
342
+ start = $cell.start(-1);
343
+ const col = map.colCount($cell.pos - start) + $cell.nodeAfter!.attrs.colspan -
344
+ 1;
345
+ const tr = view.state.tr;
346
+ for (let row = 0; row < map.height; row++) {
347
+ const mapIndex = row * map.width + col;
348
+ // Rowspanning cell that has already been handled
349
+ if (row && map.map[mapIndex] == map.map[mapIndex - map.width]) continue;
350
+ const pos = map.map[mapIndex];
351
+ const attrs = table.nodeAt(pos)!.attrs as CellAttrs;
352
+ const index = attrs.colspan == 1 ? 0 : col - map.colCount(pos);
353
+ if (attrs.colwidth && attrs.colwidth[index] == width) continue;
354
+ const colwidth = attrs.colwidth
355
+ ? attrs.colwidth.slice()
356
+ : zeroes(attrs.colspan);
357
+ colwidth[index] = width;
358
+ tr.setNodeMarkup(start + pos, null, { ...attrs, colwidth: colwidth });
359
+ }
360
+ if (tr.docChanged) view.dispatch(tr);
361
+ }
362
+
363
+ function displayColumnWidth(
364
+ view: EditorView,
365
+ cell: number,
366
+ width: number,
367
+ defaultCellMinWidth: number,
368
+ ): void {
369
+ const $cell = view.state.doc.resolve(cell);
370
+ const table = $cell.node(-1),
371
+ start = $cell.start(-1);
372
+ const col = TableMap.get(table).colCount($cell.pos - start) +
373
+ $cell.nodeAfter!.attrs.colspan -
374
+ 1;
375
+ let dom: Node | null = view.domAtPos($cell.start(-1)).node;
376
+ while (dom && dom.nodeName != 'TABLE') {
377
+ dom = dom.parentNode;
378
+ }
379
+ if (!dom) return;
380
+ updateColumnsOnResize(
381
+ table,
382
+ dom.firstChild as HTMLTableColElement,
383
+ dom as HTMLTableElement,
384
+ defaultCellMinWidth,
385
+ col,
386
+ width,
387
+ );
388
+ }
389
+
390
+ function zeroes(n: number): 0[] {
391
+ return Array(n).fill(0);
392
+ }
393
+
394
+ export function handleDecorations(
395
+ state: EditorState,
396
+ cell: number,
397
+ ): DecorationSet {
398
+ const decorations = [];
399
+ const $cell = state.doc.resolve(cell);
400
+ const table = $cell.node(-1);
401
+ if (!table) {
402
+ return DecorationSet.empty;
403
+ }
404
+ const map = TableMap.get(table);
405
+ const start = $cell.start(-1);
406
+ const col = map.colCount($cell.pos - start) + $cell.nodeAfter!.attrs.colspan -
407
+ 1;
408
+ for (let row = 0; row < map.height; row++) {
409
+ const index = col + row * map.width;
410
+ // For positions that have either a different cell or the end
411
+ // of the table to their right, and either the top of the table or
412
+ // a different cell above them, add a decoration
413
+ if (
414
+ (col == map.width - 1 || map.map[index] != map.map[index + 1]) &&
415
+ (row == 0 || map.map[index] != map.map[index - map.width])
416
+ ) {
417
+ const cellPos = map.map[index];
418
+ const pos = start + cellPos + table.nodeAt(cellPos)!.nodeSize - 1;
419
+ const dom = document.createElement('div');
420
+ dom.className = 'column-resize-handle';
421
+ if (columnResizingPluginKey.getState(state)?.dragging) {
422
+ decorations.push(
423
+ Decoration.node(
424
+ start + cellPos,
425
+ start + cellPos + table.nodeAt(cellPos)!.nodeSize,
426
+ {
427
+ class: 'column-resize-dragging',
428
+ },
429
+ ),
430
+ );
431
+ }
432
+
433
+ decorations.push(Decoration.widget(pos, dom));
434
+ }
435
+ }
436
+ return DecorationSet.create(state.doc, decorations);
437
+ }