| 152 | } |
| 153 | |
| 154 | handlePaste (e) { |
| 155 | if (isEmpty(this.state.editing)) { |
| 156 | let { start, end } = this.getState() |
| 157 | |
| 158 | start = { i: Math.min(start.i, end.i), j: Math.min(start.j, end.j) } |
| 159 | end = { i: Math.max(start.i, end.i), j: Math.max(start.j, end.j) } |
| 160 | |
| 161 | const parse = this.props.parsePaste || defaultParsePaste |
| 162 | const changes = [] |
| 163 | let pasteData = [] |
| 164 | if (window.clipboardData && window.clipboardData.getData) { // IE |
| 165 | pasteData = parse(window.clipboardData.getData('Text')) |
| 166 | } else if (e.clipboardData && e.clipboardData.getData) { |
| 167 | pasteData = parse(e.clipboardData.getData('text/plain')) |
| 168 | } |
| 169 | |
| 170 | // in order of preference |
| 171 | const { data, onCellsChanged, onPaste, onChange } = this.props |
| 172 | if (onCellsChanged) { |
| 173 | const additions = [] |
| 174 | pasteData.forEach((row, i) => { |
| 175 | row.forEach((value, j) => { |
| 176 | end = {i: start.i + i, j: start.j + j} |
| 177 | const cell = data[end.i] && data[end.i][end.j] |
| 178 | if (!cell) { |
| 179 | additions.push({ row: end.i, col: end.j, value }) |
| 180 | } else if (!cell.readOnly) { |
| 181 | changes.push({ cell, row: end.i, col: end.j, value }) |
| 182 | } |
| 183 | }) |
| 184 | }) |
| 185 | if (additions.length) { |
| 186 | onCellsChanged(changes, additions) |
| 187 | } else { |
| 188 | onCellsChanged(changes) |
| 189 | } |
| 190 | } else if (onPaste) { |
| 191 | pasteData.forEach((row, i) => { |
| 192 | const rowData = [] |
| 193 | row.forEach((pastedData, j) => { |
| 194 | end = {i: start.i + i, j: start.j + j} |
| 195 | const cell = data[end.i] && data[end.i][end.j] |
| 196 | rowData.push({cell: cell, data: pastedData}) |
| 197 | }) |
| 198 | changes.push(rowData) |
| 199 | }) |
| 200 | onPaste(changes) |
| 201 | } else if (onChange) { |
| 202 | pasteData.forEach((row, i) => { |
| 203 | row.forEach((value, j) => { |
| 204 | end = {i: start.i + i, j: start.j + j} |
| 205 | const cell = data[end.i] && data[end.i][end.j] |
| 206 | if (cell && !cell.readOnly) { |
| 207 | onChange(cell, end.i, end.j, value) |
| 208 | } |
| 209 | }) |
| 210 | }) |
| 211 | } |