Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax <max@nextcloud.com>2022-03-14 17:43:30 +0300
committerMax <max@nextcloud.com>2022-03-31 15:29:20 +0300
commitc41a6616a2a52090b00ac1ba8f3a2cd77ad4f716 (patch)
tree590b892ff0f1346d23560ae7372c214dc75ea2de /src
parent759f8cbc0cc4ad9c28382f384b7c84963aa9d89a (diff)
feature: add table button to create a table
Signed-off-by: Max <max@nextcloud.com>
Diffstat (limited to 'src')
-rw-r--r--src/mixins/menubar.js8
-rw-r--r--src/nodes/Table.js62
-rw-r--r--src/nodes/TableBody.js1
-rw-r--r--src/nodes/TableHead.js2
4 files changed, 73 insertions, 0 deletions
diff --git a/src/mixins/menubar.js b/src/mixins/menubar.js
index 23124958e..44a52ebf9 100644
--- a/src/mixins/menubar.js
+++ b/src/mixins/menubar.js
@@ -214,6 +214,14 @@ export default [
},
},
{
+ label: t('text', 'Table'),
+ class: 'icon-table',
+ isActive: 'table',
+ action: (command) => {
+ return command.insertTable()
+ },
+ },
+ {
label: t('text', 'Emoji picker'),
class: 'icon-emoji',
action: (command, emojiObject) => {
diff --git a/src/nodes/Table.js b/src/nodes/Table.js
index a4d93ed85..d2f55614b 100644
--- a/src/nodes/Table.js
+++ b/src/nodes/Table.js
@@ -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.
@@ -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',
@@ -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]
},
diff --git a/src/nodes/TableBody.js b/src/nodes/TableBody.js
index 5a2673424..529b6e54b 100644
--- a/src/nodes/TableBody.js
+++ b/src/nodes/TableBody.js
@@ -3,6 +3,7 @@ import { Node, mergeAttributes } from '@tiptap/core'
export default Node.create({
name: 'tableBody',
content: 'tableRow*',
+ tableRole: 'body',
addOptions() {
return {
diff --git a/src/nodes/TableHead.js b/src/nodes/TableHead.js
index 0cd7af788..e5acdc269 100644
--- a/src/nodes/TableHead.js
+++ b/src/nodes/TableHead.js
@@ -3,6 +3,7 @@ import { Node, mergeAttributes } from '@tiptap/core'
const tableHeadRow = Node.create({
name: 'tableHeadRow',
content: 'tableHeader*',
+ tableRole: 'headRow',
addOptions() {
return {
@@ -31,6 +32,7 @@ const tableHeadRow = Node.create({
export default Node.create({
name: 'tableHead',
content: 'tableHeadRow',
+ tableRole: 'head',
addOptions() {
return {