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

This commit is contained in:
Philipp Kühn
2021-01-22 23:56:38 +01:00
4 changed files with 46 additions and 48 deletions

View File

@@ -1,7 +1,7 @@
<template> <template>
<div v-if="editor"> <div v-if="editor">
<button @click="editor.chain().focus().createTable({ rows: 3, cols: 3, withHeaderRow: true }).run()"> <button @click="editor.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run()">
createTable insertTable
</button> </button>
<button @click="editor.chain().focus().addColumnBefore().run()"> <button @click="editor.chain().focus().addColumnBefore().run()">
addColumnBefore addColumnBefore
@@ -39,14 +39,20 @@
<button @click="editor.chain().focus().toggleHeaderCell().run()"> <button @click="editor.chain().focus().toggleHeaderCell().run()">
toggleHeaderCell toggleHeaderCell
</button> </button>
<button @click="editor.chain().focus().mergeOrSplit().run()">
mergeOrSplit
</button>
<button @click="editor.chain().focus().setCellAttributes({name: 'color', value: 'pink'}).run()">
setCellAttributes
</button>
<button @click="editor.chain().focus().fixTables().run()"> <button @click="editor.chain().focus().fixTables().run()">
fixTables fixTables
</button> </button>
<button disabled> <button @click="editor.chain().focus().goToNextCell().run()">
toggleCellMerge goToNextCell
</button> </button>
<button @click="editor.chain().focus().setCellAttributes({name: 'color', value: 'pink'}).run()"> <button @click="editor.chain().focus().goToPreviousCell().run()">
setCellAttributes (currently single cells only) goToPreviousCell
</button> </button>
<editor-content :editor="editor" /> <editor-content :editor="editor" />
</div> </div>
@@ -81,6 +87,7 @@ export default {
Document, Document,
Paragraph, Paragraph,
Text, Text,
Gapcursor,
Table.configure({ Table.configure({
resizable: true, resizable: true,
}), }),
@@ -89,6 +96,7 @@ export default {
TableCell.extend({ TableCell.extend({
addAttributes() { addAttributes() {
return { return {
// original attributes
colspan: { colspan: {
default: 1, default: 1,
}, },
@@ -98,11 +106,10 @@ export default {
colwidth: { colwidth: {
default: null, default: null,
}, },
// add a color attribute to the table cell
color: { color: {
default: null, default: null,
// Take the attribute values
renderHTML: attributes => { renderHTML: attributes => {
// … and return an object with HTML attributes.
return { return {
style: `color: ${attributes.color}`, style: `color: ${attributes.color}`,
} }
@@ -111,7 +118,6 @@ export default {
} }
}, },
}), }),
Gapcursor,
], ],
content: ` content: `
<p> <p>

View File

@@ -33,12 +33,10 @@ export const TableCell = Node.create({
isolating: true, isolating: true,
parseHTML() { parseHTML() {
// return [{ tag: 'td', getAttrs: dom => getCellAttrs(dom, extraAttrs) }]
return [{ tag: 'td' }] return [{ tag: 'td' }]
}, },
renderHTML({ HTMLAttributes }) { renderHTML({ HTMLAttributes }) {
// toDOM(node) { return ["td", setCellAttrs(node, extraAttrs), 0] }
return ['td', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] return ['td', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
}, },

View File

@@ -33,12 +33,10 @@ export const TableHeader = Node.create({
isolating: true, isolating: true,
parseHTML() { parseHTML() {
// return [{ tag: 'th', getAttrs: dom => getCellAttrs(dom, extraAttrs) }]
return [{ tag: 'th' }] return [{ tag: 'th' }]
}, },
renderHTML({ HTMLAttributes }) { renderHTML({ HTMLAttributes }) {
// toDOM(node) { return ["th", setCellAttrs(node, extraAttrs), 0] }
return ['th', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] return ['th', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
}, },

View File

@@ -18,7 +18,6 @@ import {
toggleHeaderCell, toggleHeaderCell,
setCellAttr, setCellAttr,
fixTables, fixTables,
CellSelection,
} from 'prosemirror-tables' } from 'prosemirror-tables'
import { TextSelection } from 'prosemirror-state' import { TextSelection } from 'prosemirror-state'
import { createTable } from './utilities/createTable' import { createTable } from './utilities/createTable'
@@ -29,6 +28,11 @@ export interface TableOptions {
[key: string]: any [key: string]: any
}, },
resizable: boolean, resizable: boolean,
handleWidth: number,
cellMinWidth: number,
View: TableView,
lastColumnResizable: boolean,
allowTableNodeSelection: boolean,
} }
export const Table = Node.create({ export const Table = Node.create({
@@ -37,6 +41,11 @@ export const Table = Node.create({
defaultOptions: <TableOptions>{ defaultOptions: <TableOptions>{
HTMLAttributes: {}, HTMLAttributes: {},
resizable: false, resizable: false,
handleWidth: 5,
cellMinWidth: 25,
View: TableView,
lastColumnResizable: true,
allowTableNodeSelection: false,
}, },
content: 'tableRow+', content: 'tableRow+',
@@ -57,7 +66,7 @@ export const Table = Node.create({
addCommands() { addCommands() {
return { return {
createTable: ({ rows, cols, withHeaderRow }): Command => ({ state, dispatch }) => { insertTable: ({ rows, cols, withHeaderRow }): 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)
@@ -90,7 +99,6 @@ export const Table = Node.create({
return deleteTable(state, dispatch) return deleteTable(state, dispatch)
}, },
mergeCells: (): Command => ({ state, dispatch }) => { mergeCells: (): Command => ({ state, dispatch }) => {
console.log('mergeCells', { state }, state.selection instanceof CellSelection)
return mergeCells(state, dispatch) return mergeCells(state, dispatch)
}, },
splitCell: (): Command => ({ state, dispatch }) => { splitCell: (): Command => ({ state, dispatch }) => {
@@ -105,24 +113,13 @@ export const Table = Node.create({
toggleHeaderCell: (): Command => ({ state, dispatch }) => { toggleHeaderCell: (): Command => ({ state, dispatch }) => {
return toggleHeaderCell(state, dispatch) return toggleHeaderCell(state, dispatch)
}, },
fixTables: (): Command => ({ state, dispatch }) => { mergeOrSplit: (): Command => ({ state, dispatch }) => {
const transaction = fixTables(state) if (mergeCells(state, dispatch)) {
return true
if (transaction) {
// @ts-ignore
return dispatch(transaction)
} }
return false return splitCell(state, dispatch)
}, },
// toggleCellMerge: () => (
// (state, dispatch) => {
// if (mergeCells(state, dispatch)) {
// return
// }
// splitCell(state, dispatch)
// }
// ),
setCellAttributes: ({ name, value }): Command => ({ state, dispatch }) => { setCellAttributes: ({ name, value }): Command => ({ state, dispatch }) => {
return setCellAttr(name, value)(state, dispatch) return setCellAttr(name, value)(state, dispatch)
}, },
@@ -132,6 +129,15 @@ export const Table = Node.create({
goToPreviousCell: (): Command => ({ state, dispatch }) => { goToPreviousCell: (): Command => ({ state, dispatch }) => {
return goToNextCell(-1)(state, dispatch) return goToNextCell(-1)(state, dispatch)
}, },
fixTables: (): Command => ({ state, dispatch }) => {
const transaction = fixTables(state)
if (transaction) {
return dispatch(transaction)
}
return false
},
} }
}, },
@@ -153,24 +159,14 @@ export const Table = Node.create({
}, },
addProseMirrorPlugins() { addProseMirrorPlugins() {
const columnResizingOptions = {
handleWidth: 5,
cellMinWidth: 25,
View: TableView,
lastColumnResizable: true,
}
const tableEditingOptions = {
allowTableNodeSelection: false,
}
return [ return [
...(this.options.resizable ...(this.options.resizable ? [columnResizing({
// @ts-ignore handleWidth: this.options.handleWidth,
? [columnResizing(columnResizingOptions)] cellMinWidth: this.options.cellMinWidth,
: [] View: this.options.View,
), lastColumnResizable: this.options.lastColumnResizable,
tableEditing(tableEditingOptions), })] : []),
tableEditing(this.options.allowTableNodeSelection),
] ]
}, },
}) })