@tiptap/extension-table 2.11.7 → 3.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +5 -1
  3. package/dist/cell/index.cjs +68 -0
  4. package/dist/cell/index.cjs.map +1 -0
  5. package/dist/cell/index.d.cts +33 -0
  6. package/dist/cell/index.d.ts +33 -0
  7. package/dist/cell/index.js +41 -0
  8. package/dist/cell/index.js.map +1 -0
  9. package/dist/header/index.cjs +68 -0
  10. package/dist/header/index.cjs.map +1 -0
  11. package/dist/header/index.d.cts +33 -0
  12. package/dist/header/index.d.ts +33 -0
  13. package/dist/header/index.js +41 -0
  14. package/dist/header/index.js.map +1 -0
  15. package/dist/index.cjs +475 -331
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +291 -0
  18. package/dist/index.d.ts +291 -7
  19. package/dist/index.js +462 -324
  20. package/dist/index.js.map +1 -1
  21. package/dist/kit/index.cjs +497 -0
  22. package/dist/kit/index.cjs.map +1 -0
  23. package/dist/kit/index.d.cts +252 -0
  24. package/dist/kit/index.d.ts +252 -0
  25. package/dist/kit/index.js +490 -0
  26. package/dist/kit/index.js.map +1 -0
  27. package/dist/row/index.cjs +49 -0
  28. package/dist/row/index.cjs.map +1 -0
  29. package/dist/row/index.d.cts +33 -0
  30. package/dist/row/index.d.ts +33 -0
  31. package/dist/row/index.js +22 -0
  32. package/dist/row/index.js.map +1 -0
  33. package/dist/table/index.cjs +384 -0
  34. package/dist/table/index.cjs.map +1 -0
  35. package/dist/{table.d.ts → table/index.d.cts} +43 -19
  36. package/dist/table/index.d.ts +220 -0
  37. package/dist/table/index.js +373 -0
  38. package/dist/table/index.js.map +1 -0
  39. package/package.json +52 -10
  40. package/src/cell/index.ts +1 -0
  41. package/src/cell/table-cell.ts +60 -0
  42. package/src/header/index.ts +1 -0
  43. package/src/header/table-header.ts +60 -0
  44. package/src/index.ts +5 -8
  45. package/src/kit/index.ts +64 -0
  46. package/src/row/index.ts +1 -0
  47. package/src/row/table-row.ts +38 -0
  48. package/src/{TableView.ts → table/TableView.ts} +6 -9
  49. package/src/table/index.ts +3 -0
  50. package/src/{table.ts → table/table.ts} +54 -60
  51. package/src/{utilities → table/utilities}/colStyle.ts +0 -1
  52. package/src/{utilities → table/utilities}/createCell.ts +1 -1
  53. package/src/{utilities → table/utilities}/createColGroup.ts +11 -15
  54. package/src/{utilities → table/utilities}/createTable.ts +1 -1
  55. package/src/{utilities → table/utilities}/deleteTableWhenAllCellsSelected.ts +2 -1
  56. package/src/{utilities → table/utilities}/getTableNodeTypes.ts +1 -1
  57. package/src/types.ts +19 -0
  58. package/dist/TableView.d.ts +0 -16
  59. package/dist/TableView.d.ts.map +0 -1
  60. package/dist/index.d.ts.map +0 -1
  61. package/dist/index.umd.js +0 -369
  62. package/dist/index.umd.js.map +0 -1
  63. package/dist/table.d.ts.map +0 -1
  64. package/dist/utilities/colStyle.d.ts +0 -2
  65. package/dist/utilities/colStyle.d.ts.map +0 -1
  66. package/dist/utilities/createCell.d.ts +0 -3
  67. package/dist/utilities/createCell.d.ts.map +0 -1
  68. package/dist/utilities/createColGroup.d.ts +0 -18
  69. package/dist/utilities/createColGroup.d.ts.map +0 -1
  70. package/dist/utilities/createTable.d.ts +0 -3
  71. package/dist/utilities/createTable.d.ts.map +0 -1
  72. package/dist/utilities/deleteTableWhenAllCellsSelected.d.ts +0 -3
  73. package/dist/utilities/deleteTableWhenAllCellsSelected.d.ts.map +0 -1
  74. package/dist/utilities/getTableNodeTypes.d.ts +0 -5
  75. package/dist/utilities/getTableNodeTypes.d.ts.map +0 -1
  76. package/dist/utilities/isCellSelection.d.ts +0 -3
  77. package/dist/utilities/isCellSelection.d.ts.map +0 -1
  78. /package/src/{utilities → table/utilities}/isCellSelection.ts +0 -0
package/dist/index.js CHANGED
@@ -1,358 +1,496 @@
1
- import { findParentNodeClosestToPos, Node, mergeAttributes, callOrReturn, getExtensionField } from '@tiptap/core';
2
- import { TextSelection } from '@tiptap/pm/state';
3
- import { CellSelection, addColumnBefore, addColumnAfter, deleteColumn, addRowBefore, addRowAfter, deleteRow, deleteTable, mergeCells, splitCell, toggleHeader, toggleHeaderCell, setCellAttr, goToNextCell, fixTables, columnResizing, tableEditing } from '@tiptap/pm/tables';
1
+ // src/cell/table-cell.ts
2
+ import { mergeAttributes, Node } from "@tiptap/core";
3
+ var TableCell = Node.create({
4
+ name: "tableCell",
5
+ addOptions() {
6
+ return {
7
+ HTMLAttributes: {}
8
+ };
9
+ },
10
+ content: "block+",
11
+ addAttributes() {
12
+ return {
13
+ colspan: {
14
+ default: 1
15
+ },
16
+ rowspan: {
17
+ default: 1
18
+ },
19
+ colwidth: {
20
+ default: null,
21
+ parseHTML: (element) => {
22
+ const colwidth = element.getAttribute("colwidth");
23
+ const value = colwidth ? colwidth.split(",").map((width) => parseInt(width, 10)) : null;
24
+ return value;
25
+ }
26
+ }
27
+ };
28
+ },
29
+ tableRole: "cell",
30
+ isolating: true,
31
+ parseHTML() {
32
+ return [{ tag: "td" }];
33
+ },
34
+ renderHTML({ HTMLAttributes }) {
35
+ return ["td", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
36
+ }
37
+ });
38
+
39
+ // src/header/table-header.ts
40
+ import { mergeAttributes as mergeAttributes2, Node as Node2 } from "@tiptap/core";
41
+ var TableHeader = Node2.create({
42
+ name: "tableHeader",
43
+ addOptions() {
44
+ return {
45
+ HTMLAttributes: {}
46
+ };
47
+ },
48
+ content: "block+",
49
+ addAttributes() {
50
+ return {
51
+ colspan: {
52
+ default: 1
53
+ },
54
+ rowspan: {
55
+ default: 1
56
+ },
57
+ colwidth: {
58
+ default: null,
59
+ parseHTML: (element) => {
60
+ const colwidth = element.getAttribute("colwidth");
61
+ const value = colwidth ? colwidth.split(",").map((width) => parseInt(width, 10)) : null;
62
+ return value;
63
+ }
64
+ }
65
+ };
66
+ },
67
+ tableRole: "header_cell",
68
+ isolating: true,
69
+ parseHTML() {
70
+ return [{ tag: "th" }];
71
+ },
72
+ renderHTML({ HTMLAttributes }) {
73
+ return ["th", mergeAttributes2(this.options.HTMLAttributes, HTMLAttributes), 0];
74
+ }
75
+ });
76
+
77
+ // src/kit/index.ts
78
+ import { Extension } from "@tiptap/core";
4
79
 
80
+ // src/row/table-row.ts
81
+ import { mergeAttributes as mergeAttributes3, Node as Node3 } from "@tiptap/core";
82
+ var TableRow = Node3.create({
83
+ name: "tableRow",
84
+ addOptions() {
85
+ return {
86
+ HTMLAttributes: {}
87
+ };
88
+ },
89
+ content: "(tableCell | tableHeader)*",
90
+ tableRole: "row",
91
+ parseHTML() {
92
+ return [{ tag: "tr" }];
93
+ },
94
+ renderHTML({ HTMLAttributes }) {
95
+ return ["tr", mergeAttributes3(this.options.HTMLAttributes, HTMLAttributes), 0];
96
+ }
97
+ });
98
+
99
+ // src/table/table.ts
100
+ import { callOrReturn, getExtensionField, mergeAttributes as mergeAttributes4, Node as Node4 } from "@tiptap/core";
101
+ import { TextSelection } from "@tiptap/pm/state";
102
+ import {
103
+ addColumnAfter,
104
+ addColumnBefore,
105
+ addRowAfter,
106
+ addRowBefore,
107
+ CellSelection as CellSelection2,
108
+ columnResizing,
109
+ deleteColumn,
110
+ deleteRow,
111
+ deleteTable,
112
+ fixTables,
113
+ goToNextCell,
114
+ mergeCells,
115
+ setCellAttr,
116
+ splitCell,
117
+ tableEditing,
118
+ toggleHeader,
119
+ toggleHeaderCell
120
+ } from "@tiptap/pm/tables";
121
+
122
+ // src/table/utilities/colStyle.ts
5
123
  function getColStyleDeclaration(minWidth, width) {
6
- if (width) {
7
- // apply the stored width unless it is below the configured minimum cell width
8
- return ['width', `${Math.max(width, minWidth)}px`];
9
- }
10
- // set the minimum with on the column if it has no stored width
11
- return ['min-width', `${minWidth}px`];
124
+ if (width) {
125
+ return ["width", `${Math.max(width, minWidth)}px`];
126
+ }
127
+ return ["min-width", `${minWidth}px`];
12
128
  }
13
129
 
14
- function updateColumns(node, colgroup, // <colgroup> has the same prototype as <col>
15
- table, cellMinWidth, overrideCol, overrideValue) {
16
- var _a;
17
- let totalWidth = 0;
18
- let fixedWidth = true;
19
- let nextDOM = colgroup.firstChild;
20
- const row = node.firstChild;
21
- if (row !== null) {
22
- for (let i = 0, col = 0; i < row.childCount; i += 1) {
23
- const { colspan, colwidth } = row.child(i).attrs;
24
- for (let j = 0; j < colspan; j += 1, col += 1) {
25
- const hasWidth = overrideCol === col ? overrideValue : (colwidth && colwidth[j]);
26
- const cssWidth = hasWidth ? `${hasWidth}px` : '';
27
- totalWidth += hasWidth || cellMinWidth;
28
- if (!hasWidth) {
29
- fixedWidth = false;
30
- }
31
- if (!nextDOM) {
32
- const colElement = document.createElement('col');
33
- const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth);
34
- colElement.style.setProperty(propertyKey, propertyValue);
35
- colgroup.appendChild(colElement);
36
- }
37
- else {
38
- if (nextDOM.style.width !== cssWidth) {
39
- const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth);
40
- nextDOM.style.setProperty(propertyKey, propertyValue);
41
- }
42
- nextDOM = nextDOM.nextSibling;
43
- }
44
- }
130
+ // src/table/TableView.ts
131
+ function updateColumns(node, colgroup, table, cellMinWidth, overrideCol, overrideValue) {
132
+ var _a;
133
+ let totalWidth = 0;
134
+ let fixedWidth = true;
135
+ let nextDOM = colgroup.firstChild;
136
+ const row = node.firstChild;
137
+ if (row !== null) {
138
+ for (let i = 0, col = 0; i < row.childCount; i += 1) {
139
+ const { colspan, colwidth } = row.child(i).attrs;
140
+ for (let j = 0; j < colspan; j += 1, col += 1) {
141
+ const hasWidth = overrideCol === col ? overrideValue : colwidth && colwidth[j];
142
+ const cssWidth = hasWidth ? `${hasWidth}px` : "";
143
+ totalWidth += hasWidth || cellMinWidth;
144
+ if (!hasWidth) {
145
+ fixedWidth = false;
45
146
  }
46
- }
47
- while (nextDOM) {
48
- const after = nextDOM.nextSibling;
49
- (_a = nextDOM.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(nextDOM);
50
- nextDOM = after;
51
- }
52
- if (fixedWidth) {
53
- table.style.width = `${totalWidth}px`;
54
- table.style.minWidth = '';
55
- }
56
- else {
57
- table.style.width = '';
58
- table.style.minWidth = `${totalWidth}px`;
59
- }
60
- }
61
- class TableView {
62
- constructor(node, cellMinWidth) {
63
- this.node = node;
64
- this.cellMinWidth = cellMinWidth;
65
- this.dom = document.createElement('div');
66
- this.dom.className = 'tableWrapper';
67
- this.table = this.dom.appendChild(document.createElement('table'));
68
- this.colgroup = this.table.appendChild(document.createElement('colgroup'));
69
- updateColumns(node, this.colgroup, this.table, cellMinWidth);
70
- this.contentDOM = this.table.appendChild(document.createElement('tbody'));
71
- }
72
- update(node) {
73
- if (node.type !== this.node.type) {
74
- return false;
147
+ if (!nextDOM) {
148
+ const colElement = document.createElement("col");
149
+ const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth);
150
+ colElement.style.setProperty(propertyKey, propertyValue);
151
+ colgroup.appendChild(colElement);
152
+ } else {
153
+ if (nextDOM.style.width !== cssWidth) {
154
+ const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth);
155
+ nextDOM.style.setProperty(propertyKey, propertyValue);
156
+ }
157
+ nextDOM = nextDOM.nextSibling;
75
158
  }
76
- this.node = node;
77
- updateColumns(node, this.colgroup, this.table, this.cellMinWidth);
78
- return true;
79
- }
80
- ignoreMutation(mutation) {
81
- return (mutation.type === 'attributes'
82
- && (mutation.target === this.table || this.colgroup.contains(mutation.target)));
159
+ }
83
160
  }
161
+ }
162
+ while (nextDOM) {
163
+ const after = nextDOM.nextSibling;
164
+ (_a = nextDOM.parentNode) == null ? void 0 : _a.removeChild(nextDOM);
165
+ nextDOM = after;
166
+ }
167
+ if (fixedWidth) {
168
+ table.style.width = `${totalWidth}px`;
169
+ table.style.minWidth = "";
170
+ } else {
171
+ table.style.width = "";
172
+ table.style.minWidth = `${totalWidth}px`;
173
+ }
84
174
  }
175
+ var TableView = class {
176
+ constructor(node, cellMinWidth) {
177
+ this.node = node;
178
+ this.cellMinWidth = cellMinWidth;
179
+ this.dom = document.createElement("div");
180
+ this.dom.className = "tableWrapper";
181
+ this.table = this.dom.appendChild(document.createElement("table"));
182
+ this.colgroup = this.table.appendChild(document.createElement("colgroup"));
183
+ updateColumns(node, this.colgroup, this.table, cellMinWidth);
184
+ this.contentDOM = this.table.appendChild(document.createElement("tbody"));
185
+ }
186
+ update(node) {
187
+ if (node.type !== this.node.type) {
188
+ return false;
189
+ }
190
+ this.node = node;
191
+ updateColumns(node, this.colgroup, this.table, this.cellMinWidth);
192
+ return true;
193
+ }
194
+ ignoreMutation(mutation) {
195
+ return mutation.type === "attributes" && (mutation.target === this.table || this.colgroup.contains(mutation.target));
196
+ }
197
+ };
85
198
 
199
+ // src/table/utilities/createColGroup.ts
86
200
  function createColGroup(node, cellMinWidth, overrideCol, overrideValue) {
87
- let totalWidth = 0;
88
- let fixedWidth = true;
89
- const cols = [];
90
- const row = node.firstChild;
91
- if (!row) {
92
- return {};
93
- }
94
- for (let i = 0, col = 0; i < row.childCount; i += 1) {
95
- const { colspan, colwidth } = row.child(i).attrs;
96
- for (let j = 0; j < colspan; j += 1, col += 1) {
97
- const hasWidth = overrideCol === col ? overrideValue : colwidth && colwidth[j];
98
- totalWidth += hasWidth || cellMinWidth;
99
- if (!hasWidth) {
100
- fixedWidth = false;
101
- }
102
- const [property, value] = getColStyleDeclaration(cellMinWidth, hasWidth);
103
- cols.push([
104
- 'col',
105
- { style: `${property}: ${value}` },
106
- ]);
107
- }
201
+ let totalWidth = 0;
202
+ let fixedWidth = true;
203
+ const cols = [];
204
+ const row = node.firstChild;
205
+ if (!row) {
206
+ return {};
207
+ }
208
+ for (let i = 0, col = 0; i < row.childCount; i += 1) {
209
+ const { colspan, colwidth } = row.child(i).attrs;
210
+ for (let j = 0; j < colspan; j += 1, col += 1) {
211
+ const hasWidth = overrideCol === col ? overrideValue : colwidth && colwidth[j];
212
+ totalWidth += hasWidth || cellMinWidth;
213
+ if (!hasWidth) {
214
+ fixedWidth = false;
215
+ }
216
+ const [property, value] = getColStyleDeclaration(cellMinWidth, hasWidth);
217
+ cols.push(["col", { style: `${property}: ${value}` }]);
108
218
  }
109
- const tableWidth = fixedWidth ? `${totalWidth}px` : '';
110
- const tableMinWidth = fixedWidth ? '' : `${totalWidth}px`;
111
- const colgroup = ['colgroup', {}, ...cols];
112
- return { colgroup, tableWidth, tableMinWidth };
219
+ }
220
+ const tableWidth = fixedWidth ? `${totalWidth}px` : "";
221
+ const tableMinWidth = fixedWidth ? "" : `${totalWidth}px`;
222
+ const colgroup = ["colgroup", {}, ...cols];
223
+ return { colgroup, tableWidth, tableMinWidth };
113
224
  }
114
225
 
226
+ // src/table/utilities/createCell.ts
115
227
  function createCell(cellType, cellContent) {
116
- if (cellContent) {
117
- return cellType.createChecked(null, cellContent);
118
- }
119
- return cellType.createAndFill();
228
+ if (cellContent) {
229
+ return cellType.createChecked(null, cellContent);
230
+ }
231
+ return cellType.createAndFill();
120
232
  }
121
233
 
234
+ // src/table/utilities/getTableNodeTypes.ts
122
235
  function getTableNodeTypes(schema) {
123
- if (schema.cached.tableNodeTypes) {
124
- return schema.cached.tableNodeTypes;
236
+ if (schema.cached.tableNodeTypes) {
237
+ return schema.cached.tableNodeTypes;
238
+ }
239
+ const roles = {};
240
+ Object.keys(schema.nodes).forEach((type) => {
241
+ const nodeType = schema.nodes[type];
242
+ if (nodeType.spec.tableRole) {
243
+ roles[nodeType.spec.tableRole] = nodeType;
125
244
  }
126
- const roles = {};
127
- Object.keys(schema.nodes).forEach(type => {
128
- const nodeType = schema.nodes[type];
129
- if (nodeType.spec.tableRole) {
130
- roles[nodeType.spec.tableRole] = nodeType;
131
- }
132
- });
133
- schema.cached.tableNodeTypes = roles;
134
- return roles;
245
+ });
246
+ schema.cached.tableNodeTypes = roles;
247
+ return roles;
135
248
  }
136
249
 
250
+ // src/table/utilities/createTable.ts
137
251
  function createTable(schema, rowsCount, colsCount, withHeaderRow, cellContent) {
138
- const types = getTableNodeTypes(schema);
139
- const headerCells = [];
140
- const cells = [];
141
- for (let index = 0; index < colsCount; index += 1) {
142
- const cell = createCell(types.cell, cellContent);
143
- if (cell) {
144
- cells.push(cell);
145
- }
146
- if (withHeaderRow) {
147
- const headerCell = createCell(types.header_cell, cellContent);
148
- if (headerCell) {
149
- headerCells.push(headerCell);
150
- }
151
- }
252
+ const types = getTableNodeTypes(schema);
253
+ const headerCells = [];
254
+ const cells = [];
255
+ for (let index = 0; index < colsCount; index += 1) {
256
+ const cell = createCell(types.cell, cellContent);
257
+ if (cell) {
258
+ cells.push(cell);
152
259
  }
153
- const rows = [];
154
- for (let index = 0; index < rowsCount; index += 1) {
155
- rows.push(types.row.createChecked(null, withHeaderRow && index === 0 ? headerCells : cells));
260
+ if (withHeaderRow) {
261
+ const headerCell = createCell(types.header_cell, cellContent);
262
+ if (headerCell) {
263
+ headerCells.push(headerCell);
264
+ }
156
265
  }
157
- return types.table.createChecked(null, rows);
266
+ }
267
+ const rows = [];
268
+ for (let index = 0; index < rowsCount; index += 1) {
269
+ rows.push(types.row.createChecked(null, withHeaderRow && index === 0 ? headerCells : cells));
270
+ }
271
+ return types.table.createChecked(null, rows);
158
272
  }
159
273
 
274
+ // src/table/utilities/deleteTableWhenAllCellsSelected.ts
275
+ import { findParentNodeClosestToPos } from "@tiptap/core";
276
+
277
+ // src/table/utilities/isCellSelection.ts
278
+ import { CellSelection } from "@tiptap/pm/tables";
160
279
  function isCellSelection(value) {
161
- return value instanceof CellSelection;
280
+ return value instanceof CellSelection;
162
281
  }
163
282
 
164
- const deleteTableWhenAllCellsSelected = ({ editor }) => {
165
- const { selection } = editor.state;
166
- if (!isCellSelection(selection)) {
167
- return false;
283
+ // src/table/utilities/deleteTableWhenAllCellsSelected.ts
284
+ var deleteTableWhenAllCellsSelected = ({ editor }) => {
285
+ const { selection } = editor.state;
286
+ if (!isCellSelection(selection)) {
287
+ return false;
288
+ }
289
+ let cellCount = 0;
290
+ const table = findParentNodeClosestToPos(selection.ranges[0].$from, (node) => {
291
+ return node.type.name === "table";
292
+ });
293
+ table == null ? void 0 : table.node.descendants((node) => {
294
+ if (node.type.name === "table") {
295
+ return false;
168
296
  }
169
- let cellCount = 0;
170
- const table = findParentNodeClosestToPos(selection.ranges[0].$from, node => {
171
- return node.type.name === 'table';
172
- });
173
- table === null || table === void 0 ? void 0 : table.node.descendants(node => {
174
- if (node.type.name === 'table') {
175
- return false;
176
- }
177
- if (['tableCell', 'tableHeader'].includes(node.type.name)) {
178
- cellCount += 1;
179
- }
180
- });
181
- const allCellsSelected = cellCount === selection.ranges.length;
182
- if (!allCellsSelected) {
183
- return false;
297
+ if (["tableCell", "tableHeader"].includes(node.type.name)) {
298
+ cellCount += 1;
184
299
  }
185
- editor.commands.deleteTable();
186
- return true;
300
+ });
301
+ const allCellsSelected = cellCount === selection.ranges.length;
302
+ if (!allCellsSelected) {
303
+ return false;
304
+ }
305
+ editor.commands.deleteTable();
306
+ return true;
187
307
  };
188
308
 
189
- /**
190
- * This extension allows you to create tables.
191
- * @see https://www.tiptap.dev/api/nodes/table
192
- */
193
- const Table = Node.create({
194
- name: 'table',
195
- // @ts-ignore
196
- addOptions() {
197
- return {
198
- HTMLAttributes: {},
199
- resizable: false,
200
- handleWidth: 5,
201
- cellMinWidth: 25,
202
- // TODO: fix
203
- View: TableView,
204
- lastColumnResizable: true,
205
- allowTableNodeSelection: false,
206
- };
207
- },
208
- content: 'tableRow+',
209
- tableRole: 'table',
210
- isolating: true,
211
- group: 'block',
212
- parseHTML() {
213
- return [{ tag: 'table' }];
214
- },
215
- renderHTML({ node, HTMLAttributes }) {
216
- const { colgroup, tableWidth, tableMinWidth } = createColGroup(node, this.options.cellMinWidth);
217
- const table = [
218
- 'table',
219
- mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
220
- style: tableWidth
221
- ? `width: ${tableWidth}`
222
- : `min-width: ${tableMinWidth}`,
223
- }),
224
- colgroup,
225
- ['tbody', 0],
226
- ];
227
- return table;
228
- },
229
- addCommands() {
230
- return {
231
- insertTable: ({ rows = 3, cols = 3, withHeaderRow = true } = {}) => ({ tr, dispatch, editor }) => {
232
- const node = createTable(editor.schema, rows, cols, withHeaderRow);
233
- if (dispatch) {
234
- const offset = tr.selection.from + 1;
235
- tr.replaceSelectionWith(node)
236
- .scrollIntoView()
237
- .setSelection(TextSelection.near(tr.doc.resolve(offset)));
238
- }
239
- return true;
240
- },
241
- addColumnBefore: () => ({ state, dispatch }) => {
242
- return addColumnBefore(state, dispatch);
243
- },
244
- addColumnAfter: () => ({ state, dispatch }) => {
245
- return addColumnAfter(state, dispatch);
246
- },
247
- deleteColumn: () => ({ state, dispatch }) => {
248
- return deleteColumn(state, dispatch);
249
- },
250
- addRowBefore: () => ({ state, dispatch }) => {
251
- return addRowBefore(state, dispatch);
252
- },
253
- addRowAfter: () => ({ state, dispatch }) => {
254
- return addRowAfter(state, dispatch);
255
- },
256
- deleteRow: () => ({ state, dispatch }) => {
257
- return deleteRow(state, dispatch);
258
- },
259
- deleteTable: () => ({ state, dispatch }) => {
260
- return deleteTable(state, dispatch);
261
- },
262
- mergeCells: () => ({ state, dispatch }) => {
263
- return mergeCells(state, dispatch);
264
- },
265
- splitCell: () => ({ state, dispatch }) => {
266
- return splitCell(state, dispatch);
267
- },
268
- toggleHeaderColumn: () => ({ state, dispatch }) => {
269
- return toggleHeader('column')(state, dispatch);
270
- },
271
- toggleHeaderRow: () => ({ state, dispatch }) => {
272
- return toggleHeader('row')(state, dispatch);
273
- },
274
- toggleHeaderCell: () => ({ state, dispatch }) => {
275
- return toggleHeaderCell(state, dispatch);
276
- },
277
- mergeOrSplit: () => ({ state, dispatch }) => {
278
- if (mergeCells(state, dispatch)) {
279
- return true;
280
- }
281
- return splitCell(state, dispatch);
282
- },
283
- setCellAttribute: (name, value) => ({ state, dispatch }) => {
284
- return setCellAttr(name, value)(state, dispatch);
285
- },
286
- goToNextCell: () => ({ state, dispatch }) => {
287
- return goToNextCell(1)(state, dispatch);
288
- },
289
- goToPreviousCell: () => ({ state, dispatch }) => {
290
- return goToNextCell(-1)(state, dispatch);
291
- },
292
- fixTables: () => ({ state, dispatch }) => {
293
- if (dispatch) {
294
- fixTables(state);
295
- }
296
- return true;
297
- },
298
- setCellSelection: position => ({ tr, dispatch }) => {
299
- if (dispatch) {
300
- const selection = CellSelection.create(tr.doc, position.anchorCell, position.headCell);
301
- // @ts-ignore
302
- tr.setSelection(selection);
303
- }
304
- return true;
305
- },
306
- };
307
- },
308
- addKeyboardShortcuts() {
309
- return {
310
- Tab: () => {
311
- if (this.editor.commands.goToNextCell()) {
312
- return true;
313
- }
314
- if (!this.editor.can().addRowAfter()) {
315
- return false;
316
- }
317
- return this.editor.chain().addRowAfter().goToNextCell().run();
318
- },
319
- 'Shift-Tab': () => this.editor.commands.goToPreviousCell(),
320
- Backspace: deleteTableWhenAllCellsSelected,
321
- 'Mod-Backspace': deleteTableWhenAllCellsSelected,
322
- Delete: deleteTableWhenAllCellsSelected,
323
- 'Mod-Delete': deleteTableWhenAllCellsSelected,
324
- };
325
- },
326
- addProseMirrorPlugins() {
327
- const isResizable = this.options.resizable && this.editor.isEditable;
328
- return [
329
- ...(isResizable
330
- ? [
331
- columnResizing({
332
- handleWidth: this.options.handleWidth,
333
- cellMinWidth: this.options.cellMinWidth,
334
- defaultCellMinWidth: this.options.cellMinWidth,
335
- View: this.options.View,
336
- lastColumnResizable: this.options.lastColumnResizable,
337
- }),
338
- ]
339
- : []),
340
- tableEditing({
341
- allowTableNodeSelection: this.options.allowTableNodeSelection,
342
- }),
343
- ];
344
- },
345
- extendNodeSchema(extension) {
346
- const context = {
347
- name: extension.name,
348
- options: extension.options,
349
- storage: extension.storage,
350
- };
351
- return {
352
- tableRole: callOrReturn(getExtensionField(extension, 'tableRole', context)),
353
- };
354
- },
309
+ // src/table/table.ts
310
+ var Table = Node4.create({
311
+ name: "table",
312
+ // @ts-ignore
313
+ addOptions() {
314
+ return {
315
+ HTMLAttributes: {},
316
+ resizable: false,
317
+ handleWidth: 5,
318
+ cellMinWidth: 25,
319
+ // TODO: fix
320
+ View: TableView,
321
+ lastColumnResizable: true,
322
+ allowTableNodeSelection: false
323
+ };
324
+ },
325
+ content: "tableRow+",
326
+ tableRole: "table",
327
+ isolating: true,
328
+ group: "block",
329
+ parseHTML() {
330
+ return [{ tag: "table" }];
331
+ },
332
+ renderHTML({ node, HTMLAttributes }) {
333
+ const { colgroup, tableWidth, tableMinWidth } = createColGroup(node, this.options.cellMinWidth);
334
+ const table = [
335
+ "table",
336
+ mergeAttributes4(this.options.HTMLAttributes, HTMLAttributes, {
337
+ style: tableWidth ? `width: ${tableWidth}` : `min-width: ${tableMinWidth}`
338
+ }),
339
+ colgroup,
340
+ ["tbody", 0]
341
+ ];
342
+ return table;
343
+ },
344
+ addCommands() {
345
+ return {
346
+ insertTable: ({ rows = 3, cols = 3, withHeaderRow = true } = {}) => ({ tr, dispatch, editor }) => {
347
+ const node = createTable(editor.schema, rows, cols, withHeaderRow);
348
+ if (dispatch) {
349
+ const offset = tr.selection.from + 1;
350
+ tr.replaceSelectionWith(node).scrollIntoView().setSelection(TextSelection.near(tr.doc.resolve(offset)));
351
+ }
352
+ return true;
353
+ },
354
+ addColumnBefore: () => ({ state, dispatch }) => {
355
+ return addColumnBefore(state, dispatch);
356
+ },
357
+ addColumnAfter: () => ({ state, dispatch }) => {
358
+ return addColumnAfter(state, dispatch);
359
+ },
360
+ deleteColumn: () => ({ state, dispatch }) => {
361
+ return deleteColumn(state, dispatch);
362
+ },
363
+ addRowBefore: () => ({ state, dispatch }) => {
364
+ return addRowBefore(state, dispatch);
365
+ },
366
+ addRowAfter: () => ({ state, dispatch }) => {
367
+ return addRowAfter(state, dispatch);
368
+ },
369
+ deleteRow: () => ({ state, dispatch }) => {
370
+ return deleteRow(state, dispatch);
371
+ },
372
+ deleteTable: () => ({ state, dispatch }) => {
373
+ return deleteTable(state, dispatch);
374
+ },
375
+ mergeCells: () => ({ state, dispatch }) => {
376
+ return mergeCells(state, dispatch);
377
+ },
378
+ splitCell: () => ({ state, dispatch }) => {
379
+ return splitCell(state, dispatch);
380
+ },
381
+ toggleHeaderColumn: () => ({ state, dispatch }) => {
382
+ return toggleHeader("column")(state, dispatch);
383
+ },
384
+ toggleHeaderRow: () => ({ state, dispatch }) => {
385
+ return toggleHeader("row")(state, dispatch);
386
+ },
387
+ toggleHeaderCell: () => ({ state, dispatch }) => {
388
+ return toggleHeaderCell(state, dispatch);
389
+ },
390
+ mergeOrSplit: () => ({ state, dispatch }) => {
391
+ if (mergeCells(state, dispatch)) {
392
+ return true;
393
+ }
394
+ return splitCell(state, dispatch);
395
+ },
396
+ setCellAttribute: (name, value) => ({ state, dispatch }) => {
397
+ return setCellAttr(name, value)(state, dispatch);
398
+ },
399
+ goToNextCell: () => ({ state, dispatch }) => {
400
+ return goToNextCell(1)(state, dispatch);
401
+ },
402
+ goToPreviousCell: () => ({ state, dispatch }) => {
403
+ return goToNextCell(-1)(state, dispatch);
404
+ },
405
+ fixTables: () => ({ state, dispatch }) => {
406
+ if (dispatch) {
407
+ fixTables(state);
408
+ }
409
+ return true;
410
+ },
411
+ setCellSelection: (position) => ({ tr, dispatch }) => {
412
+ if (dispatch) {
413
+ const selection = CellSelection2.create(tr.doc, position.anchorCell, position.headCell);
414
+ tr.setSelection(selection);
415
+ }
416
+ return true;
417
+ }
418
+ };
419
+ },
420
+ addKeyboardShortcuts() {
421
+ return {
422
+ Tab: () => {
423
+ if (this.editor.commands.goToNextCell()) {
424
+ return true;
425
+ }
426
+ if (!this.editor.can().addRowAfter()) {
427
+ return false;
428
+ }
429
+ return this.editor.chain().addRowAfter().goToNextCell().run();
430
+ },
431
+ "Shift-Tab": () => this.editor.commands.goToPreviousCell(),
432
+ Backspace: deleteTableWhenAllCellsSelected,
433
+ "Mod-Backspace": deleteTableWhenAllCellsSelected,
434
+ Delete: deleteTableWhenAllCellsSelected,
435
+ "Mod-Delete": deleteTableWhenAllCellsSelected
436
+ };
437
+ },
438
+ addProseMirrorPlugins() {
439
+ const isResizable = this.options.resizable && this.editor.isEditable;
440
+ return [
441
+ ...isResizable ? [
442
+ columnResizing({
443
+ handleWidth: this.options.handleWidth,
444
+ cellMinWidth: this.options.cellMinWidth,
445
+ defaultCellMinWidth: this.options.cellMinWidth,
446
+ View: this.options.View,
447
+ lastColumnResizable: this.options.lastColumnResizable
448
+ })
449
+ ] : [],
450
+ tableEditing({
451
+ allowTableNodeSelection: this.options.allowTableNodeSelection
452
+ })
453
+ ];
454
+ },
455
+ extendNodeSchema(extension) {
456
+ const context = {
457
+ name: extension.name,
458
+ options: extension.options,
459
+ storage: extension.storage
460
+ };
461
+ return {
462
+ tableRole: callOrReturn(getExtensionField(extension, "tableRole", context))
463
+ };
464
+ }
355
465
  });
356
466
 
357
- export { Table, TableView, createColGroup, createTable, Table as default, updateColumns };
358
- //# sourceMappingURL=index.js.map
467
+ // src/kit/index.ts
468
+ var TableKit = Extension.create({
469
+ name: "tableKit",
470
+ addExtensions() {
471
+ const extensions = [];
472
+ if (this.options.table !== false) {
473
+ extensions.push(Table.configure(this.options.table));
474
+ }
475
+ if (this.options.tableCell !== false) {
476
+ extensions.push(TableCell.configure(this.options.tableCell));
477
+ }
478
+ if (this.options.tableHeader !== false) {
479
+ extensions.push(TableHeader.configure(this.options.tableHeader));
480
+ }
481
+ if (this.options.tableRow !== false) {
482
+ extensions.push(TableRow.configure(this.options.tableRow));
483
+ }
484
+ return extensions;
485
+ }
486
+ });
487
+ export {
488
+ Table,
489
+ TableCell,
490
+ TableHeader,
491
+ TableKit,
492
+ TableRow,
493
+ createColGroup,
494
+ createTable
495
+ };
496
+ //# sourceMappingURL=index.js.map