@tiptap/extension-table 3.0.0-next.2 → 3.0.0-next.4
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/LICENSE.md +21 -0
- package/README.md +5 -1
- package/dist/cell/index.cjs +68 -0
- package/dist/cell/index.cjs.map +1 -0
- package/dist/cell/index.d.cts +17 -0
- package/dist/cell/index.d.ts +17 -0
- package/dist/cell/index.js +41 -0
- package/dist/cell/index.js.map +1 -0
- package/dist/header/index.cjs +68 -0
- package/dist/header/index.cjs.map +1 -0
- package/dist/header/index.d.cts +17 -0
- package/dist/header/index.d.ts +17 -0
- package/dist/header/index.js +41 -0
- package/dist/header/index.js.map +1 -0
- package/dist/index.cjs +148 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +79 -8
- package/dist/index.d.ts +79 -8
- package/dist/index.js +139 -31
- package/dist/index.js.map +1 -1
- package/dist/kit/index.cjs +497 -0
- package/dist/kit/index.cjs.map +1 -0
- package/dist/kit/index.d.cts +249 -0
- package/dist/kit/index.d.ts +249 -0
- package/dist/kit/index.js +490 -0
- package/dist/kit/index.js.map +1 -0
- package/dist/row/index.cjs +49 -0
- package/dist/row/index.cjs.map +1 -0
- package/dist/row/index.d.cts +17 -0
- package/dist/row/index.d.ts +17 -0
- package/dist/row/index.js +22 -0
- package/dist/row/index.js.map +1 -0
- package/dist/table/index.cjs +384 -0
- package/dist/table/index.cjs.map +1 -0
- package/dist/table/index.d.cts +217 -0
- package/dist/table/index.d.ts +217 -0
- package/dist/table/index.js +373 -0
- package/dist/table/index.js.map +1 -0
- package/package.json +50 -6
- package/src/cell/index.ts +1 -0
- package/src/cell/table-cell.ts +58 -0
- package/src/header/index.ts +1 -0
- package/src/header/table-header.ts +58 -0
- package/src/index.ts +5 -7
- package/src/kit/index.ts +60 -0
- package/src/row/index.ts +1 -0
- package/src/row/table-row.ts +36 -0
- package/src/{TableView.ts → table/TableView.ts} +4 -7
- package/src/table/index.ts +3 -0
- package/src/{table.ts → table/table.ts} +55 -47
- package/src/{utilities → table/utilities}/colStyle.ts +0 -1
- package/src/{utilities → table/utilities}/createColGroup.ts +10 -14
- /package/src/{utilities → table/utilities}/createCell.ts +0 -0
- /package/src/{utilities → table/utilities}/createTable.ts +0 -0
- /package/src/{utilities → table/utilities}/deleteTableWhenAllCellsSelected.ts +0 -0
- /package/src/{utilities → table/utilities}/getTableNodeTypes.ts +0 -0
- /package/src/{utilities → table/utilities}/isCellSelection.ts +0 -0
package/src/kit/index.ts
ADDED
|
@@ -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
|
+
})
|
package/src/row/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './table-row.js'
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { mergeAttributes, Node } from '@tiptap/core'
|
|
2
|
+
|
|
3
|
+
export interface TableRowOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The HTML attributes for a table row node.
|
|
6
|
+
* @default {}
|
|
7
|
+
* @example { class: 'foo' }
|
|
8
|
+
*/
|
|
9
|
+
HTMLAttributes: Record<string, any>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This extension allows you to create table rows.
|
|
14
|
+
* @see https://www.tiptap.dev/api/nodes/table-row
|
|
15
|
+
*/
|
|
16
|
+
export const TableRow = Node.create<TableRowOptions>({
|
|
17
|
+
name: 'tableRow',
|
|
18
|
+
|
|
19
|
+
addOptions() {
|
|
20
|
+
return {
|
|
21
|
+
HTMLAttributes: {},
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
content: '(tableCell | tableHeader)*',
|
|
26
|
+
|
|
27
|
+
tableRole: 'row',
|
|
28
|
+
|
|
29
|
+
parseHTML() {
|
|
30
|
+
return [{ tag: 'tr' }]
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
renderHTML({ HTMLAttributes }) {
|
|
34
|
+
return ['tr', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
|
|
35
|
+
},
|
|
36
|
+
})
|
|
@@ -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
|
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
callOrReturn, getExtensionField, mergeAttributes, Node, ParentConfig,
|
|
3
|
-
} from '@tiptap/core'
|
|
1
|
+
import { callOrReturn, getExtensionField, mergeAttributes, Node, ParentConfig } from '@tiptap/core'
|
|
4
2
|
import { DOMOutputSpec, Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
5
3
|
import { TextSelection } from '@tiptap/pm/state'
|
|
6
4
|
import {
|
|
@@ -88,11 +86,7 @@ declare module '@tiptap/core' {
|
|
|
88
86
|
* @returns True if the command was successful, otherwise false
|
|
89
87
|
* @example editor.commands.insertTable({ rows: 3, cols: 3, withHeaderRow: true })
|
|
90
88
|
*/
|
|
91
|
-
insertTable: (options?: {
|
|
92
|
-
rows?: number
|
|
93
|
-
cols?: number
|
|
94
|
-
withHeaderRow?: boolean
|
|
95
|
-
}) => ReturnType
|
|
89
|
+
insertTable: (options?: { rows?: number; cols?: number; withHeaderRow?: boolean }) => ReturnType
|
|
96
90
|
|
|
97
91
|
/**
|
|
98
92
|
* Add a column before the current column
|
|
@@ -234,11 +228,11 @@ declare module '@tiptap/core' {
|
|
|
234
228
|
tableRole?:
|
|
235
229
|
| string
|
|
236
230
|
| ((this: {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
231
|
+
name: string
|
|
232
|
+
options: Options
|
|
233
|
+
storage: Storage
|
|
234
|
+
parent: ParentConfig<NodeConfig<Options>>['tableRole']
|
|
235
|
+
}) => string)
|
|
242
236
|
}
|
|
243
237
|
}
|
|
244
238
|
|
|
@@ -276,17 +270,12 @@ export const Table = Node.create<TableOptions>({
|
|
|
276
270
|
},
|
|
277
271
|
|
|
278
272
|
renderHTML({ node, HTMLAttributes }) {
|
|
279
|
-
const { colgroup, tableWidth, tableMinWidth } = createColGroup(
|
|
280
|
-
node,
|
|
281
|
-
this.options.cellMinWidth,
|
|
282
|
-
)
|
|
273
|
+
const { colgroup, tableWidth, tableMinWidth } = createColGroup(node, this.options.cellMinWidth)
|
|
283
274
|
|
|
284
275
|
const table: DOMOutputSpec = [
|
|
285
276
|
'table',
|
|
286
277
|
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
|
287
|
-
style: tableWidth
|
|
288
|
-
? `width: ${tableWidth}`
|
|
289
|
-
: `min-width: ${tableMinWidth}`,
|
|
278
|
+
style: tableWidth ? `width: ${tableWidth}` : `min-width: ${tableMinWidth}`,
|
|
290
279
|
}),
|
|
291
280
|
colgroup,
|
|
292
281
|
['tbody', 0],
|
|
@@ -298,7 +287,8 @@ export const Table = Node.create<TableOptions>({
|
|
|
298
287
|
addCommands() {
|
|
299
288
|
return {
|
|
300
289
|
insertTable:
|
|
301
|
-
({ rows = 3, cols = 3, withHeaderRow = true } = {}) =>
|
|
290
|
+
({ rows = 3, cols = 3, withHeaderRow = true } = {}) =>
|
|
291
|
+
({ tr, dispatch, editor }) => {
|
|
302
292
|
const node = createTable(editor.schema, rows, cols, withHeaderRow)
|
|
303
293
|
|
|
304
294
|
if (dispatch) {
|
|
@@ -312,55 +302,68 @@ export const Table = Node.create<TableOptions>({
|
|
|
312
302
|
return true
|
|
313
303
|
},
|
|
314
304
|
addColumnBefore:
|
|
315
|
-
() =>
|
|
305
|
+
() =>
|
|
306
|
+
({ state, dispatch }) => {
|
|
316
307
|
return addColumnBefore(state, dispatch)
|
|
317
308
|
},
|
|
318
309
|
addColumnAfter:
|
|
319
|
-
() =>
|
|
310
|
+
() =>
|
|
311
|
+
({ state, dispatch }) => {
|
|
320
312
|
return addColumnAfter(state, dispatch)
|
|
321
313
|
},
|
|
322
314
|
deleteColumn:
|
|
323
|
-
() =>
|
|
315
|
+
() =>
|
|
316
|
+
({ state, dispatch }) => {
|
|
324
317
|
return deleteColumn(state, dispatch)
|
|
325
318
|
},
|
|
326
319
|
addRowBefore:
|
|
327
|
-
() =>
|
|
320
|
+
() =>
|
|
321
|
+
({ state, dispatch }) => {
|
|
328
322
|
return addRowBefore(state, dispatch)
|
|
329
323
|
},
|
|
330
324
|
addRowAfter:
|
|
331
|
-
() =>
|
|
325
|
+
() =>
|
|
326
|
+
({ state, dispatch }) => {
|
|
332
327
|
return addRowAfter(state, dispatch)
|
|
333
328
|
},
|
|
334
329
|
deleteRow:
|
|
335
|
-
() =>
|
|
330
|
+
() =>
|
|
331
|
+
({ state, dispatch }) => {
|
|
336
332
|
return deleteRow(state, dispatch)
|
|
337
333
|
},
|
|
338
334
|
deleteTable:
|
|
339
|
-
() =>
|
|
335
|
+
() =>
|
|
336
|
+
({ state, dispatch }) => {
|
|
340
337
|
return deleteTable(state, dispatch)
|
|
341
338
|
},
|
|
342
339
|
mergeCells:
|
|
343
|
-
() =>
|
|
340
|
+
() =>
|
|
341
|
+
({ state, dispatch }) => {
|
|
344
342
|
return mergeCells(state, dispatch)
|
|
345
343
|
},
|
|
346
344
|
splitCell:
|
|
347
|
-
() =>
|
|
345
|
+
() =>
|
|
346
|
+
({ state, dispatch }) => {
|
|
348
347
|
return splitCell(state, dispatch)
|
|
349
348
|
},
|
|
350
349
|
toggleHeaderColumn:
|
|
351
|
-
() =>
|
|
350
|
+
() =>
|
|
351
|
+
({ state, dispatch }) => {
|
|
352
352
|
return toggleHeader('column')(state, dispatch)
|
|
353
353
|
},
|
|
354
354
|
toggleHeaderRow:
|
|
355
|
-
() =>
|
|
355
|
+
() =>
|
|
356
|
+
({ state, dispatch }) => {
|
|
356
357
|
return toggleHeader('row')(state, dispatch)
|
|
357
358
|
},
|
|
358
359
|
toggleHeaderCell:
|
|
359
|
-
() =>
|
|
360
|
+
() =>
|
|
361
|
+
({ state, dispatch }) => {
|
|
360
362
|
return toggleHeaderCell(state, dispatch)
|
|
361
363
|
},
|
|
362
364
|
mergeOrSplit:
|
|
363
|
-
() =>
|
|
365
|
+
() =>
|
|
366
|
+
({ state, dispatch }) => {
|
|
364
367
|
if (mergeCells(state, dispatch)) {
|
|
365
368
|
return true
|
|
366
369
|
}
|
|
@@ -368,19 +371,23 @@ export const Table = Node.create<TableOptions>({
|
|
|
368
371
|
return splitCell(state, dispatch)
|
|
369
372
|
},
|
|
370
373
|
setCellAttribute:
|
|
371
|
-
(name, value) =>
|
|
374
|
+
(name, value) =>
|
|
375
|
+
({ state, dispatch }) => {
|
|
372
376
|
return setCellAttr(name, value)(state, dispatch)
|
|
373
377
|
},
|
|
374
378
|
goToNextCell:
|
|
375
|
-
() =>
|
|
379
|
+
() =>
|
|
380
|
+
({ state, dispatch }) => {
|
|
376
381
|
return goToNextCell(1)(state, dispatch)
|
|
377
382
|
},
|
|
378
383
|
goToPreviousCell:
|
|
379
|
-
() =>
|
|
384
|
+
() =>
|
|
385
|
+
({ state, dispatch }) => {
|
|
380
386
|
return goToNextCell(-1)(state, dispatch)
|
|
381
387
|
},
|
|
382
388
|
fixTables:
|
|
383
|
-
() =>
|
|
389
|
+
() =>
|
|
390
|
+
({ state, dispatch }) => {
|
|
384
391
|
if (dispatch) {
|
|
385
392
|
fixTables(state)
|
|
386
393
|
}
|
|
@@ -388,7 +395,8 @@ export const Table = Node.create<TableOptions>({
|
|
|
388
395
|
return true
|
|
389
396
|
},
|
|
390
397
|
setCellSelection:
|
|
391
|
-
position =>
|
|
398
|
+
position =>
|
|
399
|
+
({ tr, dispatch }) => {
|
|
392
400
|
if (dispatch) {
|
|
393
401
|
const selection = CellSelection.create(tr.doc, position.anchorCell, position.headCell)
|
|
394
402
|
|
|
@@ -428,14 +436,14 @@ export const Table = Node.create<TableOptions>({
|
|
|
428
436
|
return [
|
|
429
437
|
...(isResizable
|
|
430
438
|
? [
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
+
columnResizing({
|
|
440
|
+
handleWidth: this.options.handleWidth,
|
|
441
|
+
cellMinWidth: this.options.cellMinWidth,
|
|
442
|
+
defaultCellMinWidth: this.options.cellMinWidth,
|
|
443
|
+
View: this.options.View,
|
|
444
|
+
lastColumnResizable: this.options.lastColumnResizable,
|
|
445
|
+
}),
|
|
446
|
+
]
|
|
439
447
|
: []),
|
|
440
448
|
tableEditing({
|
|
441
449
|
allowTableNodeSelection: this.options.allowTableNodeSelection,
|
|
@@ -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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|