-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathTableClipboard.js
145 lines (131 loc) · 3.56 KB
/
TableClipboard.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
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
import {
DefaultDOMElement as DOM, platform, getRangeFromMatrix
} from 'substance'
export default class TableClipboard {
constructor (tableEditing) {
this.tableEditing = tableEditing
}
onCopy (e) {
e.preventDefault()
e.stopPropagation()
if (e.clipboardData) {
let data = this._copy()
if (data) {
// store as plain text and html
e.clipboardData.setData('text/plain', data.text)
try {
e.clipboardData.setData('text/html', data.html)
} catch (err) {
// fails under some browsers
}
}
}
}
onCut (e) {
this.onCopy(e)
this._cut()
}
onPaste (event) {
let clipboardData = event.clipboardData
let types = {}
for (let i = 0; i < clipboardData.types.length; i++) {
types[clipboardData.types[i]] = true
}
event.preventDefault()
event.stopPropagation()
let plainText
let html
if (types['text/plain']) {
plainText = clipboardData.getData('text/plain')
}
if (types['text/html']) {
html = clipboardData.getData('text/html')
}
// WORKAROUND: FF does not provide HTML coming in from other applications
// so fall back to pasting plain text
if (platform.isFF && !html) {
this._pastePlainText(plainText)
return
}
// if we have content given as HTML we let the importer assess the quality first
// and fallback to plain text import if it's bad
if (html) {
this._pasteHtml(html, plainText)
} else {
this._pastePlainText(plainText)
}
}
_pasteHtml (html, plainText) {
let vals = this._htmlToVals(html)
if (vals && vals.length > 0) {
let selData = this.tableEditing.getSelectionData()
if (selData) {
this._setValues(selData.anchorCellId, vals)
}
} else {
this._pastePlainText(plainText)
}
}
_pastePlainText (plainText) {
let selData = this.tableEditing.getSelectionData()
const { anchorCellId } = selData
// TODO: we could try to detect csv/tsv in the plain text
// and do a structured paste
this.tableEditing.setCell(anchorCellId, plainText)
}
_copy () {
const table = this.tableEditing.getTable()
const range = this._getRange()
if (!range) return null
let rows = getRangeFromMatrix(table.getCellMatrix(), range.startRow, range.startCol, range.endRow, range.endCol, true)
let vals = rows.map(row => {
return row.map(cell => {
return cell.textContent
})
})
let text = this._valsToPlainText(vals)
let html = this._valsToHTML(vals)
return { text, html }
}
_cut () {
const range = this._getRange()
if (!range) return
this._clearValues()
}
_valsToHTML (vals) {
let bodyHTML = vals.map((rowVals) => {
const rowHTML = rowVals.map((val) => {
return `<td>${val}</td>`
}).join('')
return `<tr>${rowHTML}</tr>`
}).join('\n')
return `<table>${bodyHTML}</table>`
}
_valsToPlainText (vals) {
return vals.map((rowVals) => {
return rowVals.join('\t')
}).join('\n')
}
_htmlToVals (html) {
let doc = DOM.parseHTML(html)
let table = doc.find('table')
if (table) {
let rowEls = table.findAll('tr')
let vals = rowEls.map((rowEl) => {
return rowEl.children.map((cell) => {
return cell.textContent
})
})
return vals
}
}
_setValues (anchorCellId, vals) {
this.tableEditing.setValues(anchorCellId, vals)
}
_clearValues () {
this.tableEditing.clearValues()
}
_getRange () {
return this.tableEditing.getSelectedRange()
}
}