| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.DSVFileTransferHandler = function VeUiDSVFileTransferHandler() { |
| 18 |
|
| 19 |
ve.ui.DSVFileTransferHandler.super.apply( this, arguments ); |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
OO.inheritClass( ve.ui.DSVFileTransferHandler, ve.ui.FileTransferHandler ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
ve.ui.DSVFileTransferHandler.static.name = 'dsv'; |
| 29 |
|
| 30 |
ve.ui.DSVFileTransferHandler.static.types = [ 'text/csv', 'text/tab-separated-values' ]; |
| 31 |
|
| 32 |
ve.ui.DSVFileTransferHandler.static.extensions = [ 'csv', 'tsv' ]; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
ve.ui.DSVFileTransferHandler.prototype.onFileLoad = function () { |
| 40 |
const data = []; |
| 41 |
const input = Papa.parse( this.reader.result ); |
| 42 |
|
| 43 |
if ( input.meta.aborted || ( input.data.length <= 0 ) ) { |
| 44 |
this.abort(); |
| 45 |
} else { |
| 46 |
|
| 47 |
const tableNodeName = ve.dm.modelRegistry.matchElement( document.createElement( 'table' ) ); |
| 48 |
const tableNodeClass = ve.dm.modelRegistry.lookup( tableNodeName ); |
| 49 |
const tableElement = { type: tableNodeName }; |
| 50 |
|
| 51 |
tableNodeClass.static.sanitize( tableElement ); |
| 52 |
|
| 53 |
data.push( |
| 54 |
tableElement, |
| 55 |
{ type: 'tableSection', attributes: { style: 'body' } } |
| 56 |
); |
| 57 |
|
| 58 |
for ( let i = 0; i < input.data.length; i++ ) { |
| 59 |
const line = input.data[ i ]; |
| 60 |
|
| 61 |
|
| 62 |
if ( i === input.data.length - 1 && line.length === 1 && line[ 0 ] === '' ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
data.push( { type: 'tableRow' } ); |
| 67 |
for ( let j = 0; j < line.length; j++ ) { |
| 68 |
data.push( |
| 69 |
{ type: 'tableCell', attributes: { style: ( i === 0 ? 'header' : 'data' ) } }, |
| 70 |
{ type: 'paragraph', internal: { generated: 'wrapper' } } |
| 71 |
); |
| 72 |
ve.batchPush( data, line[ j ].split( '' ) ); |
| 73 |
data.push( |
| 74 |
{ type: '/paragraph' }, |
| 75 |
{ type: '/tableCell' } |
| 76 |
); |
| 77 |
} |
| 78 |
data.push( { type: '/tableRow' } ); |
| 79 |
} |
| 80 |
|
| 81 |
data.push( |
| 82 |
{ type: '/tableSection' }, |
| 83 |
{ type: '/' + tableElement.type } |
| 84 |
); |
| 85 |
|
| 86 |
this.resolve( data ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
ve.ui.DSVFileTransferHandler.super.prototype.onFileLoad.apply( this, arguments ); |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
ve.ui.dataTransferHandlerFactory.register( ve.ui.DSVFileTransferHandler ); |