-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathTableCell.js
More file actions
89 lines (78 loc) 路 2.64 KB
/
Copy pathTableCell.js
File metadata and controls
89 lines (78 loc) 路 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { TableCell } from '@tiptap/extension-table-cell'
import { Plugin } from '@tiptap/pm/state'
import { Fragment } from '@tiptap/pm/model'
export default TableCell.extend({
content: 'inline*',
toMarkdown(state, node) {
state.write(' ')
const backup = state.options?.escapeExtraCharacters
const columnIndex = state.options.currentColumnIndex
state.options.escapeExtraCharacters = /\|/
let cellRenderedContentLength = 0
node.content.forEach((childNode, offset, index) => {
cellRenderedContentLength += (childNode.text?.length || 6)
if (childNode.text?.includes('|')) cellRenderedContentLength += 1
if (childNode.attrs.syntax === ' ') node.child(index).attrs.syntax = 'html'
})
const columnWidth = state.options.columnWidths[columnIndex]
const align = node.attrs?.textAlign || 'left'
const space = columnWidth - cellRenderedContentLength
const leftPadding = Math.floor(space / 2)
const rightPadding = Math.ceil(space / 2)
if (align === 'center') state.write(' '.repeat(leftPadding))
if (align === 'right') state.write(' '.repeat(space))
state.renderInline(node)
if (align === 'center') state.write(' '.repeat(rightPadding))
if (align === 'left') state.write(' '.repeat(space))
state.options.escapeExtraCharacters = backup
state.write(' |')
state.options.currentColumnIndex++
},
parseHTML() {
return [
{ tag: 'td', preserveWhitespace: true },
{ tag: 'th', preserveWhitespace: true },
{ tag: 'table thead ~ tbody th', priority: 70, preserveWhitespace: true },
{ tag: 'table thead ~ tbody td', priority: 70, preserveWhitespace: true },
]
},
addAttributes() {
return {
...this.parent?.(),
textAlign: {
rendered: false,
parseHTML: (element) => element.style.textAlign || null,
},
}
},
addProseMirrorPlugins() {
return [
new Plugin({
props: {
// Only paste (marked) text into table cells to prevent jumping out of cell
handlePaste: (view, event, slice) => {
if (!this.editor.isActive(this.type.name)) {
return false
}
const { state } = view
const { schema } = state
const childNodes = []
slice.content.descendants((node, pos) => {
if (node.isText) {
childNodes.push(schema.text(node.textContent, node.marks))
} else if (childNodes.length !== 0 && node.type === schema.nodes.hardBreak) {
childNodes.push(node)
}
})
const newNode = schema.node('paragraph', [], childNodes)
slice.content = Fragment.empty.addToStart(newNode)
},
},
}),
]
},
})