Skip to content

Commit

Permalink
feature: add table button to create a table
Browse files Browse the repository at this point in the history
Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Mar 31, 2022
1 parent 0128849 commit 28ac4bd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions css/icons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@include icon-black-white('ol', 'text', 1);
@include icon-black-white('ul', 'text', 1);
@include icon-black-white('tasklist', 'text', 1);
@include icon-black-white('table', 'text', 1);
@include icon-black-white('hr', 'text', 1);
@include icon-black-white('quote', 'text', 1);
@include icon-black-white('paragraph', 'text', 1);
Expand Down
8 changes: 8 additions & 0 deletions src/mixins/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ export default [
return command.toggleCodeBlock()
},
},
{
label: t('text', 'Table'),
class: 'icon-table',
isActive: 'table',
action: (command) => {
return command.insertTable()
},
},
{
label: t('text', 'Emoji picker'),
class: 'icon-emoji',
Expand Down
62 changes: 62 additions & 0 deletions src/nodes/Table.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Table } from '@tiptap/extension-table'
import { Node, mergeAttributes } from '@tiptap/core'
import { TextSelection } from 'prosemirror-state'

/*
* Markdown tables do not include captions.
Expand All @@ -26,8 +27,53 @@ const tableCaption = Node.create({
{ tag: 'table caption', priority: 90 },
]
},

})

function getTableNodeTypes(schema) {
if (schema.cached.tableNodeTypes) {
return schema.cached.tableNodeTypes
}

const roles = {}

Object.keys(schema.nodes).forEach(type => {
const nodeType = schema.nodes[type]

if (nodeType.spec.tableRole) {
roles[nodeType.spec.tableRole] = nodeType
}
})

schema.cached.tableNodeTypes = roles

return roles
}

function createTable(schema, rowsCount, colsCount, cellContent) {
const types = getTableNodeTypes(schema)
const headerCells = []
const cells = []
for (let index = 0; index < colsCount; index += 1) {
const cell = types.cell.createAndFill()
if (cell) {
cells.push(cell)
}
const headerCell = types.header_cell.createAndFill()
if (headerCell) {
headerCells.push(headerCell)
}
}
const headRow = types.headRow.createChecked(null, headerCells)
const rows = []
for (let index = 1; index < rowsCount; index += 1) {
rows.push(types.row.createChecked(null, cells))
}
const head = types.head.createChecked(null, headRow)
const body = types.body.createChecked(null, rows)
return types.table.createChecked(null, [head, body])
}

export default Table.extend({
content: 'tableCaption? tableHead tableBody',

Expand All @@ -37,6 +83,22 @@ export default Table.extend({
]
},

addCommands() {
return {
...this.parent(),
insertTable: () => ({ tr, dispatch, editor }) => {
const node = createTable(editor.schema, 3, 3, true)
if (dispatch) {
const offset = tr.selection.anchor + 1
tr.replaceSelectionWith(node)
.scrollIntoView()
.setSelection(TextSelection.near(tr.doc.resolve(offset)))
}
return true
},
}
},

renderHTML({ HTMLAttributes }) {
return ['table', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
Expand Down
1 change: 1 addition & 0 deletions src/nodes/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Node, mergeAttributes } from '@tiptap/core'
export default Node.create({
name: 'tableBody',
content: 'tableRow*',
tableRole: 'body',

addOptions() {
return {
Expand Down
2 changes: 2 additions & 0 deletions src/nodes/TableHead.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Node, mergeAttributes } from '@tiptap/core'
const tableHeadRow = Node.create({
name: 'tableHeadRow',
content: 'tableHeader*',
tableRole: 'headRow',

addOptions() {
return {
Expand Down Expand Up @@ -31,6 +32,7 @@ const tableHeadRow = Node.create({
export default Node.create({
name: 'tableHead',
content: 'tableHeadRow',
tableRole: 'head',

addOptions() {
return {
Expand Down

0 comments on commit 28ac4bd

Please sign in to comment.