Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/datatransferhandlers/ve.ui.DSVFileTransferHandler.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface delimiter-separated values file transfer handler class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Delimiter-separated values file transfer handler.
9 *
10 * @class
11 * @extends ve.ui.FileTransferHandler
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface
15 * @param {ve.ui.DataTransferItem} item
16 */
17 ve.ui.DSVFileTransferHandler = function VeUiDSVFileTransferHandler() {
18 // Parent constructor
19 ve.ui.DSVFileTransferHandler.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.DSVFileTransferHandler, ve.ui.FileTransferHandler );
25
26 /* Static properties */
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 /* Methods */
35
36 /**
37 * @inheritdoc
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 // Lookup the type for table elements
47 const tableNodeName = ve.dm.modelRegistry.matchElement( document.createElement( 'table' ) );
48 const tableNodeClass = ve.dm.modelRegistry.lookup( tableNodeName );
49 const tableElement = { type: tableNodeName };
50 // Sanitize, as this can add default attributes for the table type
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 // Skip 'empty' row if at the end of the file
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 // Parent method
90 ve.ui.DSVFileTransferHandler.super.prototype.onFileLoad.apply( this, arguments );
91 };
92
93 /* Registration */
94
95 ve.ui.dataTransferHandlerFactory.register( ve.ui.DSVFileTransferHandler );