2021-01-23 04:51:36 +08:00
|
|
|
// @ts-nocheck
|
2021-01-21 07:16:45 +08:00
|
|
|
import { Command, Node, mergeAttributes } from '@tiptap/core'
|
|
|
|
import {
|
2021-01-22 07:31:00 +08:00
|
|
|
tableEditing,
|
|
|
|
columnResizing,
|
2021-01-21 07:16:45 +08:00
|
|
|
// goToNextCell,
|
|
|
|
addColumnBefore,
|
|
|
|
addColumnAfter,
|
|
|
|
deleteColumn,
|
|
|
|
addRowBefore,
|
|
|
|
addRowAfter,
|
|
|
|
deleteRow,
|
|
|
|
deleteTable,
|
|
|
|
mergeCells,
|
|
|
|
splitCell,
|
|
|
|
toggleHeaderColumn,
|
|
|
|
toggleHeaderRow,
|
|
|
|
toggleHeaderCell,
|
2021-01-23 05:37:43 +08:00
|
|
|
// setCellAttr,
|
2021-01-23 04:51:36 +08:00
|
|
|
fixTables,
|
2021-01-23 05:37:43 +08:00
|
|
|
CellSelection,
|
2021-01-21 07:16:45 +08:00
|
|
|
} from 'prosemirror-tables'
|
2021-01-23 05:37:43 +08:00
|
|
|
import { TextSelection } from 'prosemirror-state'
|
|
|
|
import { createTable } from './utilities/createTable'
|
2021-01-23 04:51:36 +08:00
|
|
|
import { TableView } from './TableView'
|
2021-01-21 06:53:53 +08:00
|
|
|
|
|
|
|
export interface TableOptions {
|
|
|
|
HTMLAttributes: {
|
|
|
|
[key: string]: any
|
|
|
|
},
|
2021-01-22 07:31:00 +08:00
|
|
|
resizable: 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: {},
|
2021-01-22 07:31:00 +08:00
|
|
|
resizable: false,
|
2021-01-21 06:53:53 +08:00
|
|
|
},
|
|
|
|
|
2021-01-21 07:16:45 +08:00
|
|
|
content: 'table_row+',
|
2021-01-21 06:53:53 +08:00
|
|
|
|
2021-01-21 07:16:45 +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]]
|
|
|
|
},
|
2021-01-21 07:16:45 +08:00
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-01-23 05:37:43 +08:00
|
|
|
createTable: ({ rows, cols, withHeaderRow }): Command => ({ state, dispatch }) => {
|
|
|
|
const offset = state.tr.selection.anchor + 1
|
2021-01-21 07:16:45 +08:00
|
|
|
|
2021-01-23 05:37:43 +08:00
|
|
|
const nodes = createTable(this.editor.schema, rows, cols, withHeaderRow)
|
|
|
|
const tr = state.tr.replaceSelectionWith(nodes).scrollIntoView()
|
|
|
|
const resolvedPos = tr.doc.resolve(offset)
|
2021-01-21 07:16:45 +08:00
|
|
|
|
2021-01-23 05:37:43 +08:00
|
|
|
tr.setSelection(TextSelection.near(resolvedPos))
|
2021-01-21 07:16:45 +08:00
|
|
|
|
2021-01-23 05:37:43 +08:00
|
|
|
return dispatch(tr)
|
|
|
|
},
|
2021-01-21 07:16:45 +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 }) => {
|
2021-01-23 05:37:43 +08:00
|
|
|
console.log('mergeCells', { state }, state.selection instanceof CellSelection)
|
2021-01-21 07:16:45 +08:00
|
|
|
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 04:51:36 +08:00
|
|
|
fixTables: (): Command => ({ state, dispatch }) => {
|
|
|
|
console.log('fixTables')
|
|
|
|
const transaction = fixTables(state)
|
|
|
|
|
|
|
|
console.log(transaction)
|
|
|
|
if (transaction) {
|
|
|
|
// @ts-ignore
|
|
|
|
return dispatch(transaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
2021-01-21 07:16:45 +08:00
|
|
|
// toggleCellMerge: () => (
|
|
|
|
// (state, dispatch) => {
|
|
|
|
// if (mergeCells(state, dispatch)) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// splitCell(state, dispatch)
|
|
|
|
// }
|
|
|
|
// ),
|
2021-01-23 04:51:36 +08:00
|
|
|
// setCellAttr: ({ name, value }): Command => () => {
|
|
|
|
// console.log('setCellAttr')
|
|
|
|
// return setCellAttr(name, value)
|
|
|
|
// },
|
2021-01-21 07:16:45 +08:00
|
|
|
}
|
|
|
|
},
|
2021-01-22 07:31:00 +08:00
|
|
|
|
|
|
|
addProseMirrorPlugins() {
|
2021-01-23 04:51:36 +08:00
|
|
|
const columnResizingOptions = {
|
|
|
|
handleWidth: 5,
|
|
|
|
cellMinWidth: 25,
|
|
|
|
View: TableView,
|
|
|
|
lastColumnResizable: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
const tableEditingOptions = {
|
|
|
|
allowTableNodeSelection: false,
|
|
|
|
}
|
|
|
|
|
2021-01-22 07:31:00 +08:00
|
|
|
return [
|
2021-01-23 04:51:36 +08:00
|
|
|
...(this.options.resizable
|
|
|
|
// @ts-ignore
|
|
|
|
? [columnResizing(columnResizingOptions)]
|
|
|
|
: []
|
|
|
|
),
|
|
|
|
tableEditing(tableEditingOptions),
|
2021-01-22 07:31:00 +08:00
|
|
|
]
|
|
|
|
},
|
2021-01-21 06:42:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface AllExtensions {
|
|
|
|
Table: typeof Table,
|
|
|
|
}
|
|
|
|
}
|