@tiptap/extension-table 3.0.0-next.3 → 3.0.0-next.5

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 (58) 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 +148 -31
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +95 -21
  18. package/dist/index.d.ts +95 -21
  19. package/dist/index.js +139 -31
  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/index.d.cts +220 -0
  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 +50 -6
  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 -7
  45. package/src/kit/index.ts +60 -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} +4 -7
  49. package/src/table/index.ts +3 -0
  50. package/src/{table.ts → table/table.ts} +52 -58
  51. package/src/{utilities → table/utilities}/colStyle.ts +0 -1
  52. package/src/{utilities → table/utilities}/createColGroup.ts +10 -14
  53. package/src/types.ts +19 -0
  54. /package/src/{utilities → table/utilities}/createCell.ts +0 -0
  55. /package/src/{utilities → table/utilities}/createTable.ts +0 -0
  56. /package/src/{utilities → table/utilities}/deleteTableWhenAllCellsSelected.ts +0 -0
  57. /package/src/{utilities → table/utilities}/getTableNodeTypes.ts +0 -0
  58. /package/src/{utilities → table/utilities}/isCellSelection.ts +0 -0
@@ -0,0 +1,60 @@
1
+ import { Extension } from '@tiptap/core'
2
+
3
+ import { TableCell, TableCellOptions } from '../cell/index.js'
4
+ import { TableHeader, TableHeaderOptions } from '../header/index.js'
5
+ import { TableRow, TableRowOptions } from '../row/index.js'
6
+ import { Table, TableOptions } from '../table/index.js'
7
+
8
+ export interface TableKitOptions {
9
+ /**
10
+ * If set to false, the table extension will not be registered
11
+ * @example table: false
12
+ */
13
+ table: Partial<TableOptions> | false
14
+ /**
15
+ * If set to false, the table extension will not be registered
16
+ * @example tableCell: false
17
+ */
18
+ tableCell: Partial<TableCellOptions> | false
19
+ /**
20
+ * If set to false, the table extension will not be registered
21
+ * @example tableHeader: false
22
+ */
23
+ tableHeader: Partial<TableHeaderOptions> | false
24
+ /**
25
+ * If set to false, the table extension will not be registered
26
+ * @example tableRow: false
27
+ */
28
+ tableRow: Partial<TableRowOptions> | false
29
+ }
30
+
31
+ /**
32
+ * The table kit is a collection of table editor extensions.
33
+ *
34
+ * It’s a good starting point for building your own table in Tiptap.
35
+ */
36
+ export const TableKit = Extension.create<TableKitOptions>({
37
+ name: 'tableKit',
38
+
39
+ addExtensions() {
40
+ const extensions = []
41
+
42
+ if (this.options.table !== false) {
43
+ extensions.push(Table.configure(this.options.table))
44
+ }
45
+
46
+ if (this.options.tableCell !== false) {
47
+ extensions.push(TableCell.configure(this.options.tableCell))
48
+ }
49
+
50
+ if (this.options.tableHeader !== false) {
51
+ extensions.push(TableHeader.configure(this.options.tableHeader))
52
+ }
53
+
54
+ if (this.options.tableRow !== false) {
55
+ extensions.push(TableRow.configure(this.options.tableRow))
56
+ }
57
+
58
+ return extensions
59
+ },
60
+ })
@@ -0,0 +1 @@
1
+ export * from './table-row.js'
@@ -0,0 +1,38 @@
1
+ import '../types.js'
2
+
3
+ import { mergeAttributes, Node } from '@tiptap/core'
4
+
5
+ export interface TableRowOptions {
6
+ /**
7
+ * The HTML attributes for a table row node.
8
+ * @default {}
9
+ * @example { class: 'foo' }
10
+ */
11
+ HTMLAttributes: Record<string, any>
12
+ }
13
+
14
+ /**
15
+ * This extension allows you to create table rows.
16
+ * @see https://www.tiptap.dev/api/nodes/table-row
17
+ */
18
+ export const TableRow = Node.create<TableRowOptions>({
19
+ name: 'tableRow',
20
+
21
+ addOptions() {
22
+ return {
23
+ HTMLAttributes: {},
24
+ }
25
+ },
26
+
27
+ content: '(tableCell | tableHeader)*',
28
+
29
+ tableRole: 'row',
30
+
31
+ parseHTML() {
32
+ return [{ tag: 'tr' }]
33
+ },
34
+
35
+ renderHTML({ HTMLAttributes }) {
36
+ return ['tr', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
37
+ },
38
+ })
@@ -21,7 +21,7 @@ export function updateColumns(
21
21
  const { colspan, colwidth } = row.child(i).attrs
22
22
 
23
23
  for (let j = 0; j < colspan; j += 1, col += 1) {
24
- const hasWidth = overrideCol === col ? overrideValue : (colwidth && colwidth[j]) as number | undefined
24
+ const hasWidth = overrideCol === col ? overrideValue : ((colwidth && colwidth[j]) as number | undefined)
25
25
  const cssWidth = hasWidth ? `${hasWidth}px` : ''
26
26
 
27
27
  totalWidth += hasWidth || cellMinWidth
@@ -40,9 +40,9 @@ export function updateColumns(
40
40
  colgroup.appendChild(colElement)
41
41
  } else {
42
42
  if ((nextDOM as HTMLTableColElement).style.width !== cssWidth) {
43
- const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth);
43
+ const [propertyKey, propertyValue] = getColStyleDeclaration(cellMinWidth, hasWidth)
44
44
 
45
- (nextDOM as HTMLTableColElement).style.setProperty(propertyKey, propertyValue)
45
+ ;(nextDOM as HTMLTableColElement).style.setProperty(propertyKey, propertyValue)
46
46
  }
47
47
 
48
48
  nextDOM = nextDOM.nextSibling
@@ -103,9 +103,6 @@ export class TableView implements NodeView {
103
103
  }
104
104
 
105
105
  ignoreMutation(mutation: ViewMutationRecord) {
106
- return (
107
- mutation.type === 'attributes'
108
- && (mutation.target === this.table || this.colgroup.contains(mutation.target))
109
- )
106
+ return mutation.type === 'attributes' && (mutation.target === this.table || this.colgroup.contains(mutation.target))
110
107
  }
111
108
  }
@@ -0,0 +1,3 @@
1
+ export * from './table.js'
2
+ export * from './utilities/createColGroup.js'
3
+ export * from './utilities/createTable.js'
@@ -1,6 +1,6 @@
1
- import {
2
- callOrReturn, getExtensionField, mergeAttributes, Node, ParentConfig,
3
- } from '@tiptap/core'
1
+ import '../types.js'
2
+
3
+ import { callOrReturn, getExtensionField, mergeAttributes, Node } from '@tiptap/core'
4
4
  import { DOMOutputSpec, Node as ProseMirrorNode } from '@tiptap/pm/model'
5
5
  import { TextSelection } from '@tiptap/pm/state'
6
6
  import {
@@ -88,11 +88,7 @@ declare module '@tiptap/core' {
88
88
  * @returns True if the command was successful, otherwise false
89
89
  * @example editor.commands.insertTable({ rows: 3, cols: 3, withHeaderRow: true })
90
90
  */
91
- insertTable: (options?: {
92
- rows?: number
93
- cols?: number
94
- withHeaderRow?: boolean
95
- }) => ReturnType
91
+ insertTable: (options?: { rows?: number; cols?: number; withHeaderRow?: boolean }) => ReturnType
96
92
 
97
93
  /**
98
94
  * Add a column before the current column
@@ -224,22 +220,6 @@ declare module '@tiptap/core' {
224
220
  setCellSelection: (position: { anchorCell: number; headCell?: number }) => ReturnType
225
221
  }
226
222
  }
227
-
228
- interface NodeConfig<Options, Storage> {
229
- /**
230
- * A string or function to determine the role of the table.
231
- * @default 'table'
232
- * @example () => 'table'
233
- */
234
- tableRole?:
235
- | string
236
- | ((this: {
237
- name: string
238
- options: Options
239
- storage: Storage
240
- parent: ParentConfig<NodeConfig<Options>>['tableRole']
241
- }) => string)
242
- }
243
223
  }
244
224
 
245
225
  /**
@@ -276,17 +256,12 @@ export const Table = Node.create<TableOptions>({
276
256
  },
277
257
 
278
258
  renderHTML({ node, HTMLAttributes }) {
279
- const { colgroup, tableWidth, tableMinWidth } = createColGroup(
280
- node,
281
- this.options.cellMinWidth,
282
- )
259
+ const { colgroup, tableWidth, tableMinWidth } = createColGroup(node, this.options.cellMinWidth)
283
260
 
284
261
  const table: DOMOutputSpec = [
285
262
  'table',
286
263
  mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
287
- style: tableWidth
288
- ? `width: ${tableWidth}`
289
- : `min-width: ${tableMinWidth}`,
264
+ style: tableWidth ? `width: ${tableWidth}` : `min-width: ${tableMinWidth}`,
290
265
  }),
291
266
  colgroup,
292
267
  ['tbody', 0],
@@ -298,7 +273,8 @@ export const Table = Node.create<TableOptions>({
298
273
  addCommands() {
299
274
  return {
300
275
  insertTable:
301
- ({ rows = 3, cols = 3, withHeaderRow = true } = {}) => ({ tr, dispatch, editor }) => {
276
+ ({ rows = 3, cols = 3, withHeaderRow = true } = {}) =>
277
+ ({ tr, dispatch, editor }) => {
302
278
  const node = createTable(editor.schema, rows, cols, withHeaderRow)
303
279
 
304
280
  if (dispatch) {
@@ -312,55 +288,68 @@ export const Table = Node.create<TableOptions>({
312
288
  return true
313
289
  },
314
290
  addColumnBefore:
315
- () => ({ state, dispatch }) => {
291
+ () =>
292
+ ({ state, dispatch }) => {
316
293
  return addColumnBefore(state, dispatch)
317
294
  },
318
295
  addColumnAfter:
319
- () => ({ state, dispatch }) => {
296
+ () =>
297
+ ({ state, dispatch }) => {
320
298
  return addColumnAfter(state, dispatch)
321
299
  },
322
300
  deleteColumn:
323
- () => ({ state, dispatch }) => {
301
+ () =>
302
+ ({ state, dispatch }) => {
324
303
  return deleteColumn(state, dispatch)
325
304
  },
326
305
  addRowBefore:
327
- () => ({ state, dispatch }) => {
306
+ () =>
307
+ ({ state, dispatch }) => {
328
308
  return addRowBefore(state, dispatch)
329
309
  },
330
310
  addRowAfter:
331
- () => ({ state, dispatch }) => {
311
+ () =>
312
+ ({ state, dispatch }) => {
332
313
  return addRowAfter(state, dispatch)
333
314
  },
334
315
  deleteRow:
335
- () => ({ state, dispatch }) => {
316
+ () =>
317
+ ({ state, dispatch }) => {
336
318
  return deleteRow(state, dispatch)
337
319
  },
338
320
  deleteTable:
339
- () => ({ state, dispatch }) => {
321
+ () =>
322
+ ({ state, dispatch }) => {
340
323
  return deleteTable(state, dispatch)
341
324
  },
342
325
  mergeCells:
343
- () => ({ state, dispatch }) => {
326
+ () =>
327
+ ({ state, dispatch }) => {
344
328
  return mergeCells(state, dispatch)
345
329
  },
346
330
  splitCell:
347
- () => ({ state, dispatch }) => {
331
+ () =>
332
+ ({ state, dispatch }) => {
348
333
  return splitCell(state, dispatch)
349
334
  },
350
335
  toggleHeaderColumn:
351
- () => ({ state, dispatch }) => {
336
+ () =>
337
+ ({ state, dispatch }) => {
352
338
  return toggleHeader('column')(state, dispatch)
353
339
  },
354
340
  toggleHeaderRow:
355
- () => ({ state, dispatch }) => {
341
+ () =>
342
+ ({ state, dispatch }) => {
356
343
  return toggleHeader('row')(state, dispatch)
357
344
  },
358
345
  toggleHeaderCell:
359
- () => ({ state, dispatch }) => {
346
+ () =>
347
+ ({ state, dispatch }) => {
360
348
  return toggleHeaderCell(state, dispatch)
361
349
  },
362
350
  mergeOrSplit:
363
- () => ({ state, dispatch }) => {
351
+ () =>
352
+ ({ state, dispatch }) => {
364
353
  if (mergeCells(state, dispatch)) {
365
354
  return true
366
355
  }
@@ -368,19 +357,23 @@ export const Table = Node.create<TableOptions>({
368
357
  return splitCell(state, dispatch)
369
358
  },
370
359
  setCellAttribute:
371
- (name, value) => ({ state, dispatch }) => {
360
+ (name, value) =>
361
+ ({ state, dispatch }) => {
372
362
  return setCellAttr(name, value)(state, dispatch)
373
363
  },
374
364
  goToNextCell:
375
- () => ({ state, dispatch }) => {
365
+ () =>
366
+ ({ state, dispatch }) => {
376
367
  return goToNextCell(1)(state, dispatch)
377
368
  },
378
369
  goToPreviousCell:
379
- () => ({ state, dispatch }) => {
370
+ () =>
371
+ ({ state, dispatch }) => {
380
372
  return goToNextCell(-1)(state, dispatch)
381
373
  },
382
374
  fixTables:
383
- () => ({ state, dispatch }) => {
375
+ () =>
376
+ ({ state, dispatch }) => {
384
377
  if (dispatch) {
385
378
  fixTables(state)
386
379
  }
@@ -388,7 +381,8 @@ export const Table = Node.create<TableOptions>({
388
381
  return true
389
382
  },
390
383
  setCellSelection:
391
- position => ({ tr, dispatch }) => {
384
+ position =>
385
+ ({ tr, dispatch }) => {
392
386
  if (dispatch) {
393
387
  const selection = CellSelection.create(tr.doc, position.anchorCell, position.headCell)
394
388
 
@@ -428,14 +422,14 @@ export const Table = Node.create<TableOptions>({
428
422
  return [
429
423
  ...(isResizable
430
424
  ? [
431
- columnResizing({
432
- handleWidth: this.options.handleWidth,
433
- cellMinWidth: this.options.cellMinWidth,
434
- defaultCellMinWidth: this.options.cellMinWidth,
435
- View: this.options.View,
436
- lastColumnResizable: this.options.lastColumnResizable,
437
- }),
438
- ]
425
+ columnResizing({
426
+ handleWidth: this.options.handleWidth,
427
+ cellMinWidth: this.options.cellMinWidth,
428
+ defaultCellMinWidth: this.options.cellMinWidth,
429
+ View: this.options.View,
430
+ lastColumnResizable: this.options.lastColumnResizable,
431
+ }),
432
+ ]
439
433
  : []),
440
434
  tableEditing({
441
435
  allowTableNodeSelection: this.options.allowTableNodeSelection,
@@ -6,5 +6,4 @@ export function getColStyleDeclaration(minWidth: number, width: number | undefin
6
6
 
7
7
  // set the minimum with on the column if it has no stored width
8
8
  return ['min-width', `${minWidth}px`]
9
-
10
9
  }
@@ -2,11 +2,13 @@ import { DOMOutputSpec, Node as ProseMirrorNode } from '@tiptap/pm/model'
2
2
 
3
3
  import { getColStyleDeclaration } from './colStyle.js'
4
4
 
5
- export type ColGroup = {
6
- colgroup: DOMOutputSpec
7
- tableWidth: string
8
- tableMinWidth: string
9
- } | Record<string, never>;
5
+ export type ColGroup =
6
+ | {
7
+ colgroup: DOMOutputSpec
8
+ tableWidth: string
9
+ tableMinWidth: string
10
+ }
11
+ | Record<string, never>
10
12
 
11
13
  /**
12
14
  * Creates a colgroup element for a table node in ProseMirror.
@@ -17,10 +19,7 @@ export type ColGroup = {
17
19
  * @param overrideValue - (Optional) The width value to use for the overridden column.
18
20
  * @returns An object containing the colgroup element, the total width of the table, and the minimum width of the table.
19
21
  */
20
- export function createColGroup(
21
- node: ProseMirrorNode,
22
- cellMinWidth: number,
23
- ): ColGroup
22
+ export function createColGroup(node: ProseMirrorNode, cellMinWidth: number): ColGroup
24
23
  export function createColGroup(
25
24
  node: ProseMirrorNode,
26
25
  cellMinWidth: number,
@@ -46,7 +45,7 @@ export function createColGroup(
46
45
  const { colspan, colwidth } = row.child(i).attrs
47
46
 
48
47
  for (let j = 0; j < colspan; j += 1, col += 1) {
49
- const hasWidth = overrideCol === col ? overrideValue : colwidth && colwidth[j] as number | undefined
48
+ const hasWidth = overrideCol === col ? overrideValue : colwidth && (colwidth[j] as number | undefined)
50
49
 
51
50
  totalWidth += hasWidth || cellMinWidth
52
51
 
@@ -56,10 +55,7 @@ export function createColGroup(
56
55
 
57
56
  const [property, value] = getColStyleDeclaration(cellMinWidth, hasWidth)
58
57
 
59
- cols.push([
60
- 'col',
61
- { style: `${property}: ${value}` },
62
- ])
58
+ cols.push(['col', { style: `${property}: ${value}` }])
63
59
  }
64
60
  }
65
61
 
package/src/types.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { ParentConfig } from '@tiptap/core'
2
+
3
+ declare module '@tiptap/core' {
4
+ interface NodeConfig<Options, Storage> {
5
+ /**
6
+ * A string or function to determine the role of the table.
7
+ * @default 'table'
8
+ * @example () => 'table'
9
+ */
10
+ tableRole?:
11
+ | string
12
+ | ((this: {
13
+ name: string
14
+ options: Options
15
+ storage: Storage
16
+ parent: ParentConfig<NodeConfig<Options>>['tableRole']
17
+ }) => string)
18
+ }
19
+ }