Merge branch 'feature/tables' of github.com:ueberdosis/tiptap-next into feature/tables

This commit is contained in:
Hans Pagel
2021-01-23 14:29:15 +01:00
4 changed files with 24 additions and 13 deletions

View File

@@ -25,6 +25,7 @@
"@tiptap/core": "^2.0.0-alpha.6"
},
"dependencies": {
"prosemirror-tables": "^1.1.1"
"prosemirror-tables": "^1.1.1",
"prosemirror-view": "^1.16.3"
}
}

View File

@@ -1,4 +1,6 @@
// @ts-nocheck
import { NodeView } from 'prosemirror-view'
export function updateColumns(node, colgroup, table, cellMinWidth, overrideCol, overrideValue) {
let totalWidth = 0
let fixedWidth = true
@@ -44,7 +46,7 @@ export function updateColumns(node, colgroup, table, cellMinWidth, overrideCol,
}
}
export class TableView {
export class TableView implements NodeView {
constructor(node, cellMinWidth) {
this.node = node
this.cellMinWidth = cellMinWidth

View File

@@ -19,6 +19,7 @@ import {
setCellAttr,
fixTables,
} from 'prosemirror-tables'
import { NodeView } from 'prosemirror-view'
import { TextSelection } from 'prosemirror-state'
import { createTable } from './utilities/createTable'
import { TableView } from './TableView'
@@ -30,7 +31,7 @@ export interface TableOptions {
resizable: boolean,
handleWidth: number,
cellMinWidth: number,
View: TableView,
View: NodeView,
lastColumnResizable: boolean,
allowTableNodeSelection: boolean,
}
@@ -66,16 +67,17 @@ export const Table = Node.create({
addCommands() {
return {
insertTable: ({ rows, cols, withHeaderRow }): Command => ({ state, dispatch }) => {
insertTable: ({ rows = 3, cols = 3, withHeaderRow = true }): Command => ({ state, dispatch }) => {
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)
if (dispatch) {
tr.setSelection(TextSelection.near(resolvedPos))
}
return dispatch(tr)
return true
},
addColumnBefore: (): Command => ({ state, dispatch }) => {
return addColumnBefore(state, dispatch)
@@ -148,11 +150,15 @@ export const Table = Node.create({
return true
}
if (this.editor.commands.addRowAfter()) {
return this.editor.commands.goToNextCell()
if (!this.editor.can().addRowAfter()) {
return false
}
return false
return this.editor
.chain()
.addRowAfter()
.goToNextCell()
.run()
},
'Shift-Tab': () => this.editor.commands.goToPreviousCell(),
}
@@ -164,9 +170,11 @@ export const Table = Node.create({
handleWidth: this.options.handleWidth,
cellMinWidth: this.options.cellMinWidth,
View: this.options.View,
lastColumnResizable: this.options.lastColumnResizable,
// lastColumnResizable: this.options.lastColumnResizable,
})] : []),
tableEditing(this.options.allowTableNodeSelection),
tableEditing({
allowTableNodeSelection: this.options.allowTableNodeSelection,
}),
]
},
})

View File

@@ -7,7 +7,7 @@ import { createCell } from './createCell'
import { getTableNodeTypes } from './getTableNodeTypes'
export function createTable(schema: Schema, rowsCount: 3, colsCount: 3, withHeaderRow: true, cellContent?: Fragment<Schema> | ProsemirrorNode<Schema> | Array<ProsemirrorNode<Schema>>) {
export function createTable(schema: Schema, rowsCount: number, colsCount: number, withHeaderRow: boolean, cellContent?: Fragment<Schema> | ProsemirrorNode<Schema> | Array<ProsemirrorNode<Schema>>) {
const types = getTableNodeTypes(schema)
const headerCells = []