-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtableHelpers.js
111 lines (101 loc) · 3.11 KB
/
tableHelpers.js
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
import { tableHelpers, documentHelpers } from 'substance'
import Table from '../nodes/Table'
export function createTableSelection (tableId, data, surfaceId) {
if (!data.anchorCellId || !data.focusCellId) throw new Error('Invalid selection data')
return {
type: 'custom',
customType: 'table',
nodeId: tableId,
data: data,
surfaceId
}
}
export function getSelectionData (sel) {
if (sel && sel.customType === 'table') {
return sel.data
}
return {}
}
export function getSelectedRange (table, selData) {
return getCellRange(table, selData.anchorCellId, selData.focusCellId)
}
export function computeSelectionRectangle (ulRect, lrRect) {
let selRect = {}
selRect.top = ulRect.top
selRect.left = ulRect.left
selRect.width = lrRect.left + lrRect.width - selRect.left
selRect.height = lrRect.top + lrRect.height - selRect.top
return selRect
}
export function getCellRange (table, anchorCellId, focusCellId) {
let anchorCell = table.get(anchorCellId)
let focusCell = table.get(focusCellId)
let startRow = Math.min(anchorCell.rowIdx, focusCell.rowIdx)
let startCol = Math.min(anchorCell.colIdx, focusCell.colIdx)
let endRow = Math.max(anchorCell.rowIdx + anchorCell.rowspan - 1, focusCell.rowIdx + focusCell.rowspan - 1)
let endCol = Math.max(anchorCell.colIdx + anchorCell.colspan - 1, focusCell.colIdx + focusCell.colspan - 1)
return { startRow, startCol, endRow, endCol }
}
export function computeUpdatedSelection (table, selData, dr, dc, expand) {
let focusCellId = selData.focusCellId
let focusCell = table.get(focusCellId)
let rowIdx = focusCell.rowIdx
let colIdx = focusCell.colIdx
let rowspan = focusCell.rowspan
let colspan = focusCell.colspan
let newFocusCell
if (dr) {
if (dr < 0) {
newFocusCell = table.getCell(rowIdx + dr, colIdx)
} else if (dr > 0) {
newFocusCell = table.getCell(rowIdx + rowspan - 1 + dr, colIdx)
}
} else if (dc) {
if (dc < 0) {
newFocusCell = table.getCell(rowIdx, colIdx + dc)
} else if (dc > 0) {
newFocusCell = table.getCell(rowIdx, colIdx + colspan - 1 + dc)
}
}
if (newFocusCell) {
if (newFocusCell.shadowed) newFocusCell = newFocusCell.masterCell
let newFocusCellId = newFocusCell.id
let newAnchorCellId = selData.anchorCellId
if (!expand) {
newAnchorCellId = newFocusCellId
}
return {
anchorCellId: newAnchorCellId,
focusCellId: newFocusCellId
}
} else {
return selData
}
}
export function generateTable (doc, nrows, ncols, tableId) {
return documentHelpers.createNodeFromJson(doc, Table.getTemplate({
id: tableId,
headerRows: 1,
rows: nrows,
cols: ncols
}))
}
export function createTableFromTabularData (doc, data, tableId) {
return documentHelpers.createNodeFromJson(doc, {
id: tableId,
type: 'table',
rows: data.map(rowData => {
return {
type: 'table-row',
cells: rowData.map(cellValue => {
return {
type: 'table-cell',
content: String(cellValue)
}
})
}
})
})
}
const { getRangeFromMatrix } = tableHelpers
export { getRangeFromMatrix }