refactoring

This commit is contained in:
Philipp Kühn
2021-01-23 22:48:34 +01:00
parent 71376f6de7
commit 56417a44a7
7 changed files with 34 additions and 25 deletions

View File

@@ -57,7 +57,9 @@ export const Table = Node.create({
group: 'block',
parseHTML() {
return [{ tag: 'table' }]
return [
{ tag: 'table' },
]
},
renderHTML({ HTMLAttributes }) {
@@ -66,14 +68,14 @@ export const Table = Node.create({
addCommands() {
return {
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)
insertTable: ({ rows = 3, cols = 3, withHeaderRow = true }): Command => ({ tr, dispatch, editor }) => {
const offset = tr.selection.anchor + 1
const node = createTable(editor.schema, rows, cols, withHeaderRow)
if (dispatch) {
tr.setSelection(TextSelection.near(resolvedPos))
tr.replaceSelectionWith(node)
.scrollIntoView()
.setSelection(TextSelection.near(tr.doc.resolve(offset)))
}
return true

View File

@@ -4,7 +4,7 @@ import {
Schema,
} from 'prosemirror-model'
export function createCell(cellType: NodeType, cellContent?: Fragment<Schema> | ProsemirrorNode<Schema> | Array<ProsemirrorNode<Schema>>) {
export function createCell(cellType: NodeType, cellContent?: Fragment<Schema> | ProsemirrorNode<Schema> | Array<ProsemirrorNode<Schema>>): ProsemirrorNode | null | undefined {
if (cellContent) {
return cellType.createChecked(null, cellContent)
}

View File

@@ -1,23 +1,25 @@
import {
Schema,
Fragment,
Node as ProsemirrorNode,
} from 'prosemirror-model'
import { Schema, Fragment, Node as ProsemirrorNode } from 'prosemirror-model'
import { createCell } from './createCell'
import { getTableNodeTypes } from './getTableNodeTypes'
export function createTable(schema: Schema, rowsCount: number, colsCount: number, withHeaderRow: boolean, 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>>): ProsemirrorNode {
const types = getTableNodeTypes(schema)
const headerCells = []
const cells = []
for (let index = 0; index < colsCount; index += 1) {
cells.push(createCell(types.cell, cellContent))
const cell = createCell(types.cell, cellContent)
if (cell) {
cells.push(cell)
}
if (withHeaderRow) {
headerCells.push(createCell(types.header_cell, cellContent))
const headerCell = createCell(types.header_cell, cellContent)
if (headerCell) {
headerCells.push(headerCell)
}
}
}

View File

@@ -1,17 +1,16 @@
import { Schema } from 'prosemirror-model'
import { Schema, NodeType } from 'prosemirror-model'
export function getTableNodeTypes(schema: Schema) {
export function getTableNodeTypes(schema: Schema): { [key: string]: NodeType } {
if (schema.cached.tableNodeTypes) {
return schema.cached.tableNodeTypes
}
const roles = {}
const roles: { [key: string]: NodeType } = {}
Object.keys(schema.nodes).forEach(type => {
const nodeType = schema.nodes[type]
if (nodeType.spec.tableRole) {
// @ts-ignore
roles[nodeType.spec.tableRole] = nodeType
}
})