refactoring
This commit is contained in:
@@ -33,7 +33,9 @@ export const TableCell = Node.create({
|
||||
isolating: true,
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'td' }]
|
||||
return [
|
||||
{ tag: 'td' },
|
||||
]
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
|
||||
@@ -33,7 +33,9 @@ export const TableHeader = Node.create({
|
||||
isolating: true,
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'th' }]
|
||||
return [
|
||||
{ tag: 'th' },
|
||||
]
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
|
||||
@@ -18,7 +18,9 @@ export const TableRow = Node.create({
|
||||
tableRole: 'row',
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'tr' }]
|
||||
return [
|
||||
{ tag: 'tr' },
|
||||
]
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user