tiptap/packages/extension-table/src/table.ts

188 lines
5.0 KiB
TypeScript
Raw Normal View History

import { Command, Node, mergeAttributes } from '@tiptap/core'
import {
tableEditing,
columnResizing,
goToNextCell,
addColumnBefore,
addColumnAfter,
deleteColumn,
addRowBefore,
addRowAfter,
deleteRow,
deleteTable,
mergeCells,
splitCell,
toggleHeaderColumn,
toggleHeaderRow,
toggleHeaderCell,
setCellAttr,
fixTables,
} from 'prosemirror-tables'
2021-01-23 20:11:22 +08:00
import { NodeView } from 'prosemirror-view'
2021-01-23 05:37:43 +08:00
import { TextSelection } from 'prosemirror-state'
import { createTable } from './utilities/createTable'
import { TableView } from './TableView'
2021-01-21 06:53:53 +08:00
export interface TableOptions {
HTMLAttributes: {
[key: string]: any
},
resizable: boolean,
2021-01-23 06:45:50 +08:00
handleWidth: number,
cellMinWidth: number,
2021-01-23 20:11:22 +08:00
View: NodeView,
2021-01-23 06:45:50 +08:00
lastColumnResizable: boolean,
allowTableNodeSelection: boolean,
2021-01-21 06:53:53 +08:00
}
2021-01-21 06:42:01 +08:00
export const Table = Node.create({
name: 'table',
2021-01-21 06:53:53 +08:00
defaultOptions: <TableOptions>{
HTMLAttributes: {},
resizable: false,
2021-01-23 06:45:50 +08:00
handleWidth: 5,
cellMinWidth: 25,
View: TableView,
lastColumnResizable: true,
allowTableNodeSelection: false,
2021-01-21 06:53:53 +08:00
},
content: 'tableRow+',
2021-01-21 06:53:53 +08:00
tableRole: 'table',
2021-01-21 06:53:53 +08:00
isolating: true,
group: 'block',
parseHTML() {
return [{ tag: 'table' }]
},
renderHTML({ HTMLAttributes }) {
return ['table', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), ['tbody', 0]]
},
addCommands() {
return {
2021-01-23 20:11:22 +08:00
insertTable: ({ rows = 3, cols = 3, withHeaderRow = true }): Command => ({ state, dispatch }) => {
2021-01-23 05:37:43 +08:00
const offset = state.tr.selection.anchor + 1
const nodes = createTable(this.editor.schema, rows, cols, withHeaderRow)
const tr = state.tr.replaceSelectionWith(nodes).scrollIntoView()
const resolvedPos = tr.doc.resolve(offset)
2021-01-23 20:11:22 +08:00
if (dispatch) {
tr.setSelection(TextSelection.near(resolvedPos))
}
2021-01-23 20:11:22 +08:00
return true
2021-01-23 05:37:43 +08:00
},
addColumnBefore: (): Command => ({ state, dispatch }) => {
return addColumnBefore(state, dispatch)
},
addColumnAfter: (): Command => ({ state, dispatch }) => {
return addColumnAfter(state, dispatch)
},
deleteColumn: (): Command => ({ state, dispatch }) => {
return deleteColumn(state, dispatch)
},
addRowBefore: (): Command => ({ state, dispatch }) => {
return addRowBefore(state, dispatch)
},
addRowAfter: (): Command => ({ state, dispatch }) => {
return addRowAfter(state, dispatch)
},
deleteRow: (): Command => ({ state, dispatch }) => {
return deleteRow(state, dispatch)
},
deleteTable: (): Command => ({ state, dispatch }) => {
return deleteTable(state, dispatch)
},
mergeCells: (): Command => ({ state, dispatch }) => {
return mergeCells(state, dispatch)
},
splitCell: (): Command => ({ state, dispatch }) => {
return splitCell(state, dispatch)
},
toggleHeaderColumn: (): Command => ({ state, dispatch }) => {
return toggleHeaderColumn(state, dispatch)
},
toggleHeaderRow: (): Command => ({ state, dispatch }) => {
return toggleHeaderRow(state, dispatch)
},
toggleHeaderCell: (): Command => ({ state, dispatch }) => {
return toggleHeaderCell(state, dispatch)
},
2021-01-23 06:45:50 +08:00
mergeOrSplit: (): Command => ({ state, dispatch }) => {
if (mergeCells(state, dispatch)) {
return true
}
2021-01-23 06:45:50 +08:00
return splitCell(state, dispatch)
},
2021-01-24 03:59:19 +08:00
setCellAttributes: ({ name, value }: { name: string, value: any }): Command => ({ state, dispatch }) => {
return setCellAttr(name, value)(state, dispatch)
},
goToNextCell: (): Command => ({ state, dispatch }) => {
return goToNextCell(1)(state, dispatch)
},
goToPreviousCell: (): Command => ({ state, dispatch }) => {
return goToNextCell(-1)(state, dispatch)
},
2021-01-23 06:45:50 +08:00
fixTables: (): Command => ({ state, dispatch }) => {
const transaction = fixTables(state)
if (transaction) {
2021-01-24 03:59:19 +08:00
return dispatch?.(transaction)
2021-01-23 06:45:50 +08:00
}
return false
},
}
},
addKeyboardShortcuts() {
return {
Tab: () => {
if (this.editor.commands.goToNextCell()) {
return true
}
2021-01-23 07:32:43 +08:00
if (!this.editor.can().addRowAfter()) {
return false
}
2021-01-23 07:32:43 +08:00
return this.editor
.chain()
.addRowAfter()
.goToNextCell()
.run()
},
'Shift-Tab': () => this.editor.commands.goToPreviousCell(),
}
},
addProseMirrorPlugins() {
return [
2021-01-23 06:45:50 +08:00
...(this.options.resizable ? [columnResizing({
handleWidth: this.options.handleWidth,
cellMinWidth: this.options.cellMinWidth,
View: this.options.View,
2021-01-24 03:59:19 +08:00
// TODO: PR for @types/prosemirror-tables
// @ts-ignore (incorrect type)
lastColumnResizable: this.options.lastColumnResizable,
2021-01-23 06:45:50 +08:00
})] : []),
2021-01-23 20:11:22 +08:00
tableEditing({
allowTableNodeSelection: this.options.allowTableNodeSelection,
}),
]
},
2021-01-21 06:42:01 +08:00
})
declare module '@tiptap/core' {
interface AllExtensions {
Table: typeof Table,
}
}