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

Table.js « Table « nodes « src - github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e095898eac82d702bb17dd70abaacb71b0a1a7c (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { mergeAttributes } from '@tiptap/core'
import { Table } from '@tiptap/extension-table'
import TableCaption from './TableCaption.js'
import TableCell from './TableCell.js'
import TableHeader from './TableHeader.js'
import TableHeadRow from './TableHeadRow.js'
import TableRow from './TableRow.js'
import { TextSelection } from 'prosemirror-state'
import { isInTable, moveCellForward, selectionCell } from 'prosemirror-tables-contently'

/**
 *
 * @param {object} schema - schema of the editor
 * @param {number} rowsCount - number of rows in the table
 * @param {number} colsCount - number of cols in the table
 * @param {object} cellContent - currently unused
 */
function createTable(schema, rowsCount, colsCount, cellContent) {
	const headerCells = []
	const cells = []
	for (let index = 0; index < colsCount; index += 1) {
		const cell = schema.nodes.tableCell.createAndFill()
		if (cell) {
			cells.push(cell)
		}
		const headerCell = schema.nodes.tableHeader.createAndFill()
		if (headerCell) {
			headerCells.push(headerCell)
		}
	}
	const headRow = schema.nodes.tableHeadRow.createChecked(null, headerCells)
	const rows = []
	for (let index = 1; index < rowsCount; index += 1) {
		rows.push(schema.nodes.tableRow.createChecked(null, cells))
	}
	return schema.nodes.table.createChecked(null, [headRow, ...rows])
}

/**
 *
 * @param {object} $cell - resolved position of the current cell
 */
function findSameCellInNextRow($cell) {
	if ($cell.index(-1) === $cell.node(-1).childCount - 1) {
		return null
	}
	let cellStart = $cell.after()
	const table = $cell.node(-1)
	for (let row = $cell.indexAfter(-1); row < table.childCount; row++) {
		const rowNode = table.child(row)
		if (rowNode.childCount >= $cell.index()) {
			for (let cell = 0; cell < $cell.index(); cell++) {
				const cellNode = rowNode.child(cell)
				cellStart += cellNode.nodeSize
			}
			return cellStart + 1
		}
		cellStart += rowNode.nodeSize
	}
}

export default Table.extend({
	content: 'tableCaption? tableHeadRow tableRow*',

	addExtensions() {
		return [
			TableCaption,
			TableCell,
			TableHeader,
			TableHeadRow,
			TableRow,
		]
	},

	addCommands() {
		return {
			...this.parent(),
			insertTable: () => ({ tr, dispatch, editor }) => {
				if (isInTable(tr)) return false
				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
			},
			// move to the next node after the table from the last cell
			leaveTable: () => ({ tr, dispatch, editor }) => {
				if (!isInTable(tr)) return false
				const { $head, empty } = tr.selection
				if (!empty) return false
				// the selection can temporarily be inside the table but outside of cells.
				const tableDepth = $head.depth < 3 ? 1 : $head.depth - 2
				if (dispatch) {
					const $next = tr.doc.resolve($head.after(tableDepth) + 1)
					const selection = TextSelection.near($next)
					dispatch(tr.setSelection(selection).scrollIntoView())
				}
				return true
			},
			goToNextRow: () => ({ tr, dispatch, editor }) => {
				if (!isInTable(tr)) return false
				const cell = findSameCellInNextRow(selectionCell(tr))
				if (cell == null) return
				if (dispatch) {
					const $cell = tr.doc.resolve(cell)
					const selection = TextSelection.between($cell, moveCellForward($cell))
					dispatch(tr.setSelection(selection).scrollIntoView())
				}
				return true
			},
		}
	},

	renderHTML({ HTMLAttributes }) {
		return ['table', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
	},

	toMarkdown(state, node) {
		state.renderContent(node)
		state.closeBlock(node)
	},

	addKeyboardShortcuts() {
		return {
			...this.parent(),
			Tab: () => this.editor.commands.goToNextCell() || this.editor.commands.leaveTable(),
			Enter: () => {
				if (this.editor.commands.goToNextRow()) {
					return true
				}

				if (!this.editor.can().addRowAfter()) {
					return false
				}

				return this.editor
					.chain()
					.addRowAfter()
					.goToNextRow()
					.run()
			},
		}
	},

})