@tiptap/extension-table 2.0.0-beta.21 → 2.0.0-beta.211

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-table",
3
3
  "description": "table extension for tiptap",
4
- "version": "2.0.0-beta.21",
4
+ "version": "2.0.0-beta.211",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -12,20 +12,46 @@
12
12
  "type": "github",
13
13
  "url": "https://github.com/sponsors/ueberdosis"
14
14
  },
15
- "main": "dist/tiptap-extension-table.cjs.js",
16
- "umd": "dist/tiptap-extension-table.umd.js",
17
- "module": "dist/tiptap-extension-table.esm.js",
18
- "types": "dist/packages/extension-table/src/index.d.ts",
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "main": "dist/index.cjs",
24
+ "module": "dist/index.js",
25
+ "types": "dist/index.d.ts",
19
26
  "files": [
20
27
  "src",
21
28
  "dist"
22
29
  ],
23
30
  "peerDependencies": {
24
- "@tiptap/core": "^2.0.0-beta.1"
31
+ "@tiptap/core": "^2.0.0-beta.209",
32
+ "@tiptap/pm": "^2.0.0-beta.209"
33
+ },
34
+ "devDependencies": {
35
+ "@tiptap/core": "^2.0.0-beta.211",
36
+ "@tiptap/pm": "^2.0.0-beta.211"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/ueberdosis/tiptap",
41
+ "directory": "packages/extension-table"
25
42
  },
26
- "dependencies": {
27
- "prosemirror-tables": "^1.1.1",
28
- "prosemirror-view": "^1.18.6"
43
+ "scripts": {
44
+ "build": "tsup"
29
45
  },
30
- "gitHead": "6b154e4c8161fbba987e162ebebc155243432f24"
46
+ "tsup": {
47
+ "entry": [
48
+ "src/index.ts"
49
+ ],
50
+ "dts": true,
51
+ "splitting": true,
52
+ "format": [
53
+ "esm",
54
+ "cjs"
55
+ ]
56
+ }
31
57
  }
package/src/TableView.ts CHANGED
@@ -1,8 +1,15 @@
1
1
  // @ts-nocheck
2
- import { NodeView } from 'prosemirror-view'
3
- import { Node as ProseMirrorNode } from 'prosemirror-model'
4
-
5
- export function updateColumns(node: ProseMirrorNode, colgroup: Element, table: Element, cellMinWidth: number, overrideCol?: number, overrideValue?: any) {
2
+ import { Node as ProseMirrorNode } from '@tiptap/pm/model'
3
+ import { NodeView } from '@tiptap/pm/view'
4
+
5
+ export function updateColumns(
6
+ node: ProseMirrorNode,
7
+ colgroup: Element,
8
+ table: Element,
9
+ cellMinWidth: number,
10
+ overrideCol?: number,
11
+ overrideValue?: any,
12
+ ) {
6
13
  let totalWidth = 0
7
14
  let fixedWidth = true
8
15
  let nextDOM = colgroup.firstChild
@@ -14,6 +21,7 @@ export function updateColumns(node: ProseMirrorNode, colgroup: Element, table: E
14
21
  for (let j = 0; j < colspan; j += 1, col += 1) {
15
22
  const hasWidth = overrideCol === col ? overrideValue : colwidth && colwidth[j]
16
23
  const cssWidth = hasWidth ? `${hasWidth}px` : ''
24
+
17
25
  totalWidth += hasWidth || cellMinWidth
18
26
 
19
27
  if (!hasWidth) {
@@ -34,6 +42,7 @@ export function updateColumns(node: ProseMirrorNode, colgroup: Element, table: E
34
42
 
35
43
  while (nextDOM) {
36
44
  const after = nextDOM.nextSibling
45
+
37
46
  nextDOM.parentNode.removeChild(nextDOM)
38
47
  nextDOM = after
39
48
  }
@@ -48,7 +57,6 @@ export function updateColumns(node: ProseMirrorNode, colgroup: Element, table: E
48
57
  }
49
58
 
50
59
  export class TableView implements NodeView {
51
-
52
60
  node: ProseMirrorNode
53
61
 
54
62
  cellMinWidth: number
@@ -84,6 +92,9 @@ export class TableView implements NodeView {
84
92
  }
85
93
 
86
94
  ignoreMutation(mutation: MutationRecord | { type: 'selection'; target: Element }) {
87
- return mutation.type === 'attributes' && (mutation.target === this.table || this.colgroup.contains(mutation.target))
95
+ return (
96
+ mutation.type === 'attributes'
97
+ && (mutation.target === this.table || this.colgroup.contains(mutation.target))
98
+ )
88
99
  }
89
100
  }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Table } from './table'
2
2
 
3
3
  export * from './table'
4
+ export * from './utilities/createTable'
4
5
 
5
6
  export default Table
package/src/table.ts CHANGED
@@ -1,97 +1,101 @@
1
1
  import {
2
- Node,
3
- Command,
4
- ParentConfig,
5
- mergeAttributes,
6
- getExtensionField,
7
- callOrReturn,
2
+ callOrReturn, getExtensionField, mergeAttributes, Node, ParentConfig,
8
3
  } from '@tiptap/core'
4
+ import { TextSelection } from '@tiptap/pm/state'
9
5
  import {
10
- tableEditing,
11
- columnResizing,
12
- goToNextCell,
13
- addColumnBefore,
14
6
  addColumnAfter,
15
- deleteColumn,
16
- addRowBefore,
7
+ addColumnBefore,
17
8
  addRowAfter,
9
+ addRowBefore,
10
+ CellSelection,
11
+ columnResizing,
12
+ deleteColumn,
18
13
  deleteRow,
19
14
  deleteTable,
15
+ fixTables,
16
+ goToNextCell,
20
17
  mergeCells,
18
+ setCellAttr,
21
19
  splitCell,
22
- toggleHeaderColumn,
23
- toggleHeaderRow,
20
+ tableEditing,
21
+ toggleHeader,
24
22
  toggleHeaderCell,
25
- setCellAttr,
26
- fixTables,
27
- CellSelection,
28
- } from 'prosemirror-tables'
29
- import { NodeView } from 'prosemirror-view'
30
- import { TextSelection } from 'prosemirror-state'
23
+ } from '@tiptap/pm/tables'
24
+ import { NodeView } from '@tiptap/pm/view'
25
+
26
+ import { TableView } from './TableView'
31
27
  import { createTable } from './utilities/createTable'
32
28
  import { deleteTableWhenAllCellsSelected } from './utilities/deleteTableWhenAllCellsSelected'
33
- import { TableView } from './TableView'
34
29
 
35
30
  export interface TableOptions {
36
- HTMLAttributes: Record<string, any>,
37
- resizable: boolean,
38
- handleWidth: number,
39
- cellMinWidth: number,
40
- View: NodeView,
41
- lastColumnResizable: boolean,
42
- allowTableNodeSelection: boolean,
31
+ HTMLAttributes: Record<string, any>
32
+ resizable: boolean
33
+ handleWidth: number
34
+ cellMinWidth: number
35
+ View: NodeView
36
+ lastColumnResizable: boolean
37
+ allowTableNodeSelection: boolean
43
38
  }
44
39
 
45
40
  declare module '@tiptap/core' {
46
- interface Commands {
41
+ interface Commands<ReturnType> {
47
42
  table: {
48
- insertTable: (options?: { rows?: number, cols?: number, withHeaderRow?: boolean }) => Command,
49
- addColumnBefore: () => Command,
50
- addColumnAfter: () => Command,
51
- deleteColumn: () => Command,
52
- addRowBefore: () => Command,
53
- addRowAfter: () => Command,
54
- deleteRow: () => Command,
55
- deleteTable: () => Command,
56
- mergeCells: () => Command,
57
- splitCell: () => Command,
58
- toggleHeaderColumn: () => Command,
59
- toggleHeaderRow: () => Command,
60
- toggleHeaderCell: () => Command,
61
- mergeOrSplit: () => Command,
62
- setCellAttribute: (name: string, value: any) => Command,
63
- goToNextCell: () => Command,
64
- goToPreviousCell: () => Command,
65
- fixTables: () => Command,
66
- setCellSelection: (position: { anchorCell: number, headCell?: number }) => Command,
43
+ insertTable: (options?: {
44
+ rows?: number
45
+ cols?: number
46
+ withHeaderRow?: boolean
47
+ }) => ReturnType
48
+ addColumnBefore: () => ReturnType
49
+ addColumnAfter: () => ReturnType
50
+ deleteColumn: () => ReturnType
51
+ addRowBefore: () => ReturnType
52
+ addRowAfter: () => ReturnType
53
+ deleteRow: () => ReturnType
54
+ deleteTable: () => ReturnType
55
+ mergeCells: () => ReturnType
56
+ splitCell: () => ReturnType
57
+ toggleHeaderColumn: () => ReturnType
58
+ toggleHeaderRow: () => ReturnType
59
+ toggleHeaderCell: () => ReturnType
60
+ mergeOrSplit: () => ReturnType
61
+ setCellAttribute: (name: string, value: any) => ReturnType
62
+ goToNextCell: () => ReturnType
63
+ goToPreviousCell: () => ReturnType
64
+ fixTables: () => ReturnType
65
+ setCellSelection: (position: { anchorCell: number; headCell?: number }) => ReturnType
67
66
  }
68
67
  }
69
68
 
70
- interface NodeConfig<Options> {
69
+ interface NodeConfig<Options, Storage> {
71
70
  /**
72
71
  * Table Role
73
72
  */
74
- tableRole?: string | ((this: {
75
- name: string,
76
- options: Options,
77
- parent: ParentConfig<NodeConfig<Options>>['tableRole'],
78
- }) => string),
73
+ tableRole?:
74
+ | string
75
+ | ((this: {
76
+ name: string
77
+ options: Options
78
+ storage: Storage
79
+ parent: ParentConfig<NodeConfig<Options>>['tableRole']
80
+ }) => string)
79
81
  }
80
82
  }
81
83
 
82
84
  export const Table = Node.create<TableOptions>({
83
85
  name: 'table',
84
86
 
85
- defaultOptions: {
86
- HTMLAttributes: {},
87
- resizable: false,
88
- handleWidth: 5,
89
- cellMinWidth: 25,
90
- // TODO: fix
91
- // @ts-ignore
92
- View: TableView,
93
- lastColumnResizable: true,
94
- allowTableNodeSelection: false,
87
+ // @ts-ignore
88
+ addOptions() {
89
+ return {
90
+ HTMLAttributes: {},
91
+ resizable: false,
92
+ handleWidth: 5,
93
+ cellMinWidth: 25,
94
+ // TODO: fix
95
+ View: TableView,
96
+ lastColumnResizable: true,
97
+ allowTableNodeSelection: false,
98
+ }
95
99
  },
96
100
 
97
101
  content: 'tableRow+',
@@ -103,9 +107,7 @@ export const Table = Node.create<TableOptions>({
103
107
  group: 'block',
104
108
 
105
109
  parseHTML() {
106
- return [
107
- { tag: 'table' },
108
- ]
110
+ return [{ tag: 'table' }]
109
111
  },
110
112
 
111
113
  renderHTML({ HTMLAttributes }) {
@@ -114,88 +116,107 @@ export const Table = Node.create<TableOptions>({
114
116
 
115
117
  addCommands() {
116
118
  return {
117
- insertTable: ({ rows = 3, cols = 3, withHeaderRow = true } = {}) => ({ tr, dispatch, editor }) => {
118
- const node = createTable(editor.schema, rows, cols, withHeaderRow)
119
+ insertTable:
120
+ ({ rows = 3, cols = 3, withHeaderRow = true } = {}) => ({ tr, dispatch, editor }) => {
121
+ const node = createTable(editor.schema, rows, cols, withHeaderRow)
119
122
 
120
- if (dispatch) {
121
- const offset = tr.selection.anchor + 1
123
+ if (dispatch) {
124
+ const offset = tr.selection.anchor + 1
122
125
 
123
- tr.replaceSelectionWith(node)
124
- .scrollIntoView()
125
- .setSelection(TextSelection.near(tr.doc.resolve(offset)))
126
- }
126
+ tr.replaceSelectionWith(node)
127
+ .scrollIntoView()
128
+ .setSelection(TextSelection.near(tr.doc.resolve(offset)))
129
+ }
127
130
 
128
- return true
129
- },
130
- addColumnBefore: () => ({ state, dispatch }) => {
131
- return addColumnBefore(state, dispatch)
132
- },
133
- addColumnAfter: () => ({ state, dispatch }) => {
134
- return addColumnAfter(state, dispatch)
135
- },
136
- deleteColumn: () => ({ state, dispatch }) => {
137
- return deleteColumn(state, dispatch)
138
- },
139
- addRowBefore: () => ({ state, dispatch }) => {
140
- return addRowBefore(state, dispatch)
141
- },
142
- addRowAfter: () => ({ state, dispatch }) => {
143
- return addRowAfter(state, dispatch)
144
- },
145
- deleteRow: () => ({ state, dispatch }) => {
146
- return deleteRow(state, dispatch)
147
- },
148
- deleteTable: () => ({ state, dispatch }) => {
149
- return deleteTable(state, dispatch)
150
- },
151
- mergeCells: () => ({ state, dispatch }) => {
152
- return mergeCells(state, dispatch)
153
- },
154
- splitCell: () => ({ state, dispatch }) => {
155
- return splitCell(state, dispatch)
156
- },
157
- toggleHeaderColumn: () => ({ state, dispatch }) => {
158
- return toggleHeaderColumn(state, dispatch)
159
- },
160
- toggleHeaderRow: () => ({ state, dispatch }) => {
161
- return toggleHeaderRow(state, dispatch)
162
- },
163
- toggleHeaderCell: () => ({ state, dispatch }) => {
164
- return toggleHeaderCell(state, dispatch)
165
- },
166
- mergeOrSplit: () => ({ state, dispatch }) => {
167
- if (mergeCells(state, dispatch)) {
168
131
  return true
169
- }
132
+ },
133
+ addColumnBefore:
134
+ () => ({ state, dispatch }) => {
135
+ return addColumnBefore(state, dispatch)
136
+ },
137
+ addColumnAfter:
138
+ () => ({ state, dispatch }) => {
139
+ return addColumnAfter(state, dispatch)
140
+ },
141
+ deleteColumn:
142
+ () => ({ state, dispatch }) => {
143
+ return deleteColumn(state, dispatch)
144
+ },
145
+ addRowBefore:
146
+ () => ({ state, dispatch }) => {
147
+ return addRowBefore(state, dispatch)
148
+ },
149
+ addRowAfter:
150
+ () => ({ state, dispatch }) => {
151
+ return addRowAfter(state, dispatch)
152
+ },
153
+ deleteRow:
154
+ () => ({ state, dispatch }) => {
155
+ return deleteRow(state, dispatch)
156
+ },
157
+ deleteTable:
158
+ () => ({ state, dispatch }) => {
159
+ return deleteTable(state, dispatch)
160
+ },
161
+ mergeCells:
162
+ () => ({ state, dispatch }) => {
163
+ return mergeCells(state, dispatch)
164
+ },
165
+ splitCell:
166
+ () => ({ state, dispatch }) => {
167
+ return splitCell(state, dispatch)
168
+ },
169
+ toggleHeaderColumn:
170
+ () => ({ state, dispatch }) => {
171
+ return toggleHeader('column')(state, dispatch)
172
+ },
173
+ toggleHeaderRow:
174
+ () => ({ state, dispatch }) => {
175
+ return toggleHeader('row')(state, dispatch)
176
+ },
177
+ toggleHeaderCell:
178
+ () => ({ state, dispatch }) => {
179
+ return toggleHeaderCell(state, dispatch)
180
+ },
181
+ mergeOrSplit:
182
+ () => ({ state, dispatch }) => {
183
+ if (mergeCells(state, dispatch)) {
184
+ return true
185
+ }
170
186
 
171
- return splitCell(state, dispatch)
172
- },
173
- setCellAttribute: (name, value) => ({ state, dispatch }) => {
174
- return setCellAttr(name, value)(state, dispatch)
175
- },
176
- goToNextCell: () => ({ state, dispatch }) => {
177
- return goToNextCell(1)(state, dispatch)
178
- },
179
- goToPreviousCell: () => ({ state, dispatch }) => {
180
- return goToNextCell(-1)(state, dispatch)
181
- },
182
- fixTables: () => ({ state, dispatch }) => {
183
- if (dispatch) {
184
- fixTables(state)
185
- }
187
+ return splitCell(state, dispatch)
188
+ },
189
+ setCellAttribute:
190
+ (name, value) => ({ state, dispatch }) => {
191
+ return setCellAttr(name, value)(state, dispatch)
192
+ },
193
+ goToNextCell:
194
+ () => ({ state, dispatch }) => {
195
+ return goToNextCell(1)(state, dispatch)
196
+ },
197
+ goToPreviousCell:
198
+ () => ({ state, dispatch }) => {
199
+ return goToNextCell(-1)(state, dispatch)
200
+ },
201
+ fixTables:
202
+ () => ({ state, dispatch }) => {
203
+ if (dispatch) {
204
+ fixTables(state)
205
+ }
186
206
 
187
- return true
188
- },
189
- setCellSelection: position => ({ tr, dispatch }) => {
190
- if (dispatch) {
191
- const selection = CellSelection.create(tr.doc, position.anchorCell, position.headCell)
207
+ return true
208
+ },
209
+ setCellSelection:
210
+ position => ({ tr, dispatch }) => {
211
+ if (dispatch) {
212
+ const selection = CellSelection.create(tr.doc, position.anchorCell, position.headCell)
192
213
 
193
- // @ts-ignore
194
- tr.setSelection(selection)
195
- }
214
+ // @ts-ignore
215
+ tr.setSelection(selection)
216
+ }
196
217
 
197
- return true
198
- },
218
+ return true
219
+ },
199
220
  }
200
221
  },
201
222
 
@@ -210,11 +231,7 @@ export const Table = Node.create<TableOptions>({
210
231
  return false
211
232
  }
212
233
 
213
- return this.editor
214
- .chain()
215
- .addRowAfter()
216
- .goToNextCell()
217
- .run()
234
+ return this.editor.chain().addRowAfter().goToNextCell().run()
218
235
  },
219
236
  'Shift-Tab': () => this.editor.commands.goToPreviousCell(),
220
237
  Backspace: deleteTableWhenAllCellsSelected,
@@ -225,15 +242,22 @@ export const Table = Node.create<TableOptions>({
225
242
  },
226
243
 
227
244
  addProseMirrorPlugins() {
245
+ const isResizable = this.options.resizable && this.editor.isEditable
246
+
228
247
  return [
229
- ...(this.options.resizable ? [columnResizing({
230
- handleWidth: this.options.handleWidth,
231
- cellMinWidth: this.options.cellMinWidth,
232
- View: this.options.View,
233
- // TODO: PR for @types/prosemirror-tables
234
- // @ts-ignore (incorrect type)
235
- lastColumnResizable: this.options.lastColumnResizable,
236
- })] : []),
248
+ ...(isResizable
249
+ ? [
250
+ columnResizing({
251
+ handleWidth: this.options.handleWidth,
252
+ cellMinWidth: this.options.cellMinWidth,
253
+ // @ts-ignore (incorrect type)
254
+ View: this.options.View,
255
+ // TODO: PR for @types/prosemirror-tables
256
+ // @ts-ignore (incorrect type)
257
+ lastColumnResizable: this.options.lastColumnResizable,
258
+ }),
259
+ ]
260
+ : []),
237
261
  tableEditing({
238
262
  allowTableNodeSelection: this.options.allowTableNodeSelection,
239
263
  }),
@@ -244,6 +268,7 @@ export const Table = Node.create<TableOptions>({
244
268
  const context = {
245
269
  name: extension.name,
246
270
  options: extension.options,
271
+ storage: extension.storage,
247
272
  }
248
273
 
249
274
  return {
@@ -1,10 +1,9 @@
1
- import {
2
- NodeType, Fragment,
3
- Node as ProsemirrorNode,
4
- Schema,
5
- } from 'prosemirror-model'
1
+ import { Fragment, Node as ProsemirrorNode, NodeType } from '@tiptap/pm/model'
6
2
 
7
- export function createCell(cellType: NodeType, cellContent?: Fragment<Schema> | ProsemirrorNode<Schema> | Array<ProsemirrorNode<Schema>>): ProsemirrorNode | null | undefined {
3
+ export function createCell(
4
+ cellType: NodeType,
5
+ cellContent?: Fragment | ProsemirrorNode | Array<ProsemirrorNode>,
6
+ ): ProsemirrorNode | null | undefined {
8
7
  if (cellContent) {
9
8
  return cellType.createChecked(null, cellContent)
10
9
  }
@@ -1,11 +1,18 @@
1
- import { Schema, Fragment, Node as ProsemirrorNode } from 'prosemirror-model'
1
+ import { Fragment, Node as ProsemirrorNode, Schema } from '@tiptap/pm/model'
2
+
2
3
  import { createCell } from './createCell'
3
4
  import { getTableNodeTypes } from './getTableNodeTypes'
4
5
 
5
- export function createTable(schema: Schema, rowsCount: number, colsCount: number, withHeaderRow: boolean, cellContent?: Fragment<Schema> | ProsemirrorNode<Schema> | Array<ProsemirrorNode<Schema>>): ProsemirrorNode {
6
+ export function createTable(
7
+ schema: Schema,
8
+ rowsCount: number,
9
+ colsCount: number,
10
+ withHeaderRow: boolean,
11
+ cellContent?: Fragment | ProsemirrorNode | Array<ProsemirrorNode>,
12
+ ): ProsemirrorNode {
6
13
  const types = getTableNodeTypes(schema)
7
- const headerCells = []
8
- const cells = []
14
+ const headerCells: ProsemirrorNode[] = []
15
+ const cells: ProsemirrorNode[] = []
9
16
 
10
17
  for (let index = 0; index < colsCount; index += 1) {
11
18
  const cell = createCell(types.cell, cellContent)
@@ -23,7 +30,7 @@ export function createTable(schema: Schema, rowsCount: number, colsCount: number
23
30
  }
24
31
  }
25
32
 
26
- const rows = []
33
+ const rows: ProsemirrorNode[] = []
27
34
 
28
35
  for (let index = 0; index < rowsCount; index += 1) {
29
36
  rows.push(types.row.createChecked(null, withHeaderRow && index === 0 ? headerCells : cells))
@@ -1,4 +1,5 @@
1
- import { KeyboardShortcutCommand, findParentNodeClosestToPos } from '@tiptap/core'
1
+ import { findParentNodeClosestToPos, KeyboardShortcutCommand } from '@tiptap/core'
2
+
2
3
  import { isCellSelection } from './isCellSelection'
3
4
 
4
5
  export const deleteTableWhenAllCellsSelected: KeyboardShortcutCommand = ({ editor }) => {
@@ -1,4 +1,4 @@
1
- import { Schema, NodeType } from 'prosemirror-model'
1
+ import { NodeType, Schema } from '@tiptap/pm/model'
2
2
 
3
3
  export function getTableNodeTypes(schema: Schema): { [key: string]: NodeType } {
4
4
  if (schema.cached.tableNodeTypes) {
@@ -1,4 +1,4 @@
1
- import { CellSelection } from 'prosemirror-tables'
1
+ import { CellSelection } from '@tiptap/pm/tables'
2
2
 
3
3
  export function isCellSelection(value: unknown): value is CellSelection {
4
4
  return value instanceof CellSelection