improve types

This commit is contained in:
Philipp Kühn
2021-01-23 13:11:22 +01:00
parent 1e6f19667e
commit 1fc50705c6
4 changed files with 17 additions and 10 deletions

View File

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

View File

@@ -1,4 +1,6 @@
// @ts-nocheck // @ts-nocheck
import { NodeView } from 'prosemirror-view'
export function updateColumns(node, colgroup, table, cellMinWidth, overrideCol, overrideValue) { export function updateColumns(node, colgroup, table, cellMinWidth, overrideCol, overrideValue) {
let totalWidth = 0 let totalWidth = 0
let fixedWidth = true 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) { constructor(node, cellMinWidth) {
this.node = node this.node = node
this.cellMinWidth = cellMinWidth this.cellMinWidth = cellMinWidth

View File

@@ -19,6 +19,7 @@ import {
setCellAttr, setCellAttr,
fixTables, fixTables,
} from 'prosemirror-tables' } from 'prosemirror-tables'
import { NodeView } from 'prosemirror-view'
import { TextSelection } from 'prosemirror-state' import { TextSelection } from 'prosemirror-state'
import { createTable } from './utilities/createTable' import { createTable } from './utilities/createTable'
import { TableView } from './TableView' import { TableView } from './TableView'
@@ -30,7 +31,7 @@ export interface TableOptions {
resizable: boolean, resizable: boolean,
handleWidth: number, handleWidth: number,
cellMinWidth: number, cellMinWidth: number,
View: TableView, View: NodeView,
lastColumnResizable: boolean, lastColumnResizable: boolean,
allowTableNodeSelection: boolean, allowTableNodeSelection: boolean,
} }
@@ -66,16 +67,17 @@ export const Table = Node.create({
addCommands() { addCommands() {
return { 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 offset = state.tr.selection.anchor + 1
const nodes = createTable(this.editor.schema, rows, cols, withHeaderRow) const nodes = createTable(this.editor.schema, rows, cols, withHeaderRow)
const tr = state.tr.replaceSelectionWith(nodes).scrollIntoView() const tr = state.tr.replaceSelectionWith(nodes).scrollIntoView()
const resolvedPos = tr.doc.resolve(offset) const resolvedPos = tr.doc.resolve(offset)
tr.setSelection(TextSelection.near(resolvedPos)) if (dispatch) {
tr.setSelection(TextSelection.near(resolvedPos))
}
return dispatch(tr) return true
}, },
addColumnBefore: (): Command => ({ state, dispatch }) => { addColumnBefore: (): Command => ({ state, dispatch }) => {
return addColumnBefore(state, dispatch) return addColumnBefore(state, dispatch)
@@ -168,9 +170,11 @@ export const Table = Node.create({
handleWidth: this.options.handleWidth, handleWidth: this.options.handleWidth,
cellMinWidth: this.options.cellMinWidth, cellMinWidth: this.options.cellMinWidth,
View: this.options.View, 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' 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 types = getTableNodeTypes(schema)
const headerCells = [] const headerCells = []