@jupyterlab/csvviewer 4.7.0-alpha.2 → 4.7.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/model.d.ts +94 -0
- package/lib/model.js +318 -15
- package/lib/model.js.map +1 -1
- package/lib/parse.d.ts +5 -1
- package/lib/parse.js +16 -8
- package/lib/parse.js.map +1 -1
- package/lib/toolbar.d.ts +26 -0
- package/lib/toolbar.js +85 -22
- package/lib/toolbar.js.map +1 -1
- package/lib/widget.d.ts +49 -0
- package/lib/widget.js +269 -25
- package/lib/widget.js.map +1 -1
- package/package.json +8 -8
- package/src/model.ts +371 -14
- package/src/parse.ts +24 -10
- package/src/toolbar.ts +103 -26
- package/src/widget.ts +320 -27
- package/style/base.css +8 -4
package/src/toolbar.ts
CHANGED
|
@@ -20,6 +20,12 @@ const CSV_DELIMITER_LABEL_CLASS = 'jp-CSVDelimiter-label';
|
|
|
20
20
|
*/
|
|
21
21
|
const CSV_DELIMITER_DROPDOWN_CLASS = 'jp-CSVDelimiter-dropdown';
|
|
22
22
|
|
|
23
|
+
const CSV_COMMENT_CLASS = 'jp-CSVComment';
|
|
24
|
+
|
|
25
|
+
const CSV_COMMENT_LABEL_CLASS = 'jp-CSVComment-label';
|
|
26
|
+
|
|
27
|
+
const CSV_COMMENT_DROPDOWN_CLASS = 'jp-CSVComment-dropdown';
|
|
28
|
+
|
|
23
29
|
/**
|
|
24
30
|
* A widget for selecting a delimiter.
|
|
25
31
|
*/
|
|
@@ -28,8 +34,21 @@ export class CSVDelimiter extends Widget {
|
|
|
28
34
|
* Construct a new csv table widget.
|
|
29
35
|
*/
|
|
30
36
|
constructor(options: CSVToolbar.IOptions) {
|
|
37
|
+
const trans = (options.translator || nullTranslator).load('jupyterlab');
|
|
31
38
|
super({
|
|
32
|
-
node: Private.createNode(
|
|
39
|
+
node: Private.createNode({
|
|
40
|
+
selected: options.widget.delimiter,
|
|
41
|
+
label: trans.__('Delimiter: '),
|
|
42
|
+
labelClassName: CSV_DELIMITER_LABEL_CLASS,
|
|
43
|
+
dropdownClassName: CSV_DELIMITER_DROPDOWN_CLASS,
|
|
44
|
+
values: [
|
|
45
|
+
[',', ','],
|
|
46
|
+
[';', ';'],
|
|
47
|
+
['\t', trans.__('tab')],
|
|
48
|
+
['|', trans.__('pipe')],
|
|
49
|
+
['#', trans.__('hash')]
|
|
50
|
+
]
|
|
51
|
+
})
|
|
33
52
|
});
|
|
34
53
|
this._widget = options.widget;
|
|
35
54
|
this.addClass(CSV_DELIMITER_CLASS);
|
|
@@ -79,6 +98,68 @@ export class CSVDelimiter extends Widget {
|
|
|
79
98
|
protected _widget: CSVViewer;
|
|
80
99
|
}
|
|
81
100
|
|
|
101
|
+
/**
|
|
102
|
+
* A widget for selecting a comment character.
|
|
103
|
+
*/
|
|
104
|
+
export class CSVComment extends Widget {
|
|
105
|
+
/**
|
|
106
|
+
* Construct a new csv comment widget.
|
|
107
|
+
*/
|
|
108
|
+
constructor(options: CSVToolbar.IOptions) {
|
|
109
|
+
const trans = (options.translator || nullTranslator).load('jupyterlab');
|
|
110
|
+
super({
|
|
111
|
+
node: Private.createNode({
|
|
112
|
+
selected: options.widget.comment ?? '',
|
|
113
|
+
label: trans.__('Comment: '),
|
|
114
|
+
labelClassName: CSV_COMMENT_LABEL_CLASS,
|
|
115
|
+
dropdownClassName: CSV_COMMENT_DROPDOWN_CLASS,
|
|
116
|
+
values: [
|
|
117
|
+
['', trans.__('none')],
|
|
118
|
+
['#', trans.__('hash')]
|
|
119
|
+
]
|
|
120
|
+
})
|
|
121
|
+
});
|
|
122
|
+
this._widget = options.widget;
|
|
123
|
+
this.addClass(CSV_COMMENT_CLASS);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The comment dropdown menu.
|
|
128
|
+
*/
|
|
129
|
+
get selectNode(): HTMLSelectElement {
|
|
130
|
+
return this.node.getElementsByTagName('select')![0];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Handle DOM events for the widget.
|
|
135
|
+
*/
|
|
136
|
+
handleEvent(event: Event): void {
|
|
137
|
+
switch (event.type) {
|
|
138
|
+
case 'change':
|
|
139
|
+
this._widget.comment = this.selectNode.value || null;
|
|
140
|
+
break;
|
|
141
|
+
default:
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Handle `after-attach` messages for the widget.
|
|
148
|
+
*/
|
|
149
|
+
protected onAfterAttach(msg: Message): void {
|
|
150
|
+
this.selectNode.addEventListener('change', this);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Handle `before-detach` messages for the widget.
|
|
155
|
+
*/
|
|
156
|
+
protected onBeforeDetach(msg: Message): void {
|
|
157
|
+
this.selectNode.removeEventListener('change', this);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
protected _widget: CSVViewer;
|
|
161
|
+
}
|
|
162
|
+
|
|
82
163
|
/**
|
|
83
164
|
* A namespace for `CSVToolbar` statics.
|
|
84
165
|
*/
|
|
@@ -103,42 +184,38 @@ export namespace CSVToolbar {
|
|
|
103
184
|
* A namespace for private toolbar methods.
|
|
104
185
|
*/
|
|
105
186
|
namespace Private {
|
|
187
|
+
interface ICreateNodeOptions {
|
|
188
|
+
selected: string;
|
|
189
|
+
label: string;
|
|
190
|
+
labelClassName: string;
|
|
191
|
+
dropdownClassName: string;
|
|
192
|
+
values: Array<[string, string]>;
|
|
193
|
+
}
|
|
194
|
+
|
|
106
195
|
/**
|
|
107
|
-
* Create the node for
|
|
196
|
+
* Create the node for a CSV toolbar select.
|
|
108
197
|
*/
|
|
109
|
-
export function createNode(
|
|
110
|
-
selected
|
|
111
|
-
|
|
112
|
-
): HTMLElement {
|
|
113
|
-
translator = translator || nullTranslator;
|
|
114
|
-
const trans = translator?.load('jupyterlab');
|
|
115
|
-
|
|
116
|
-
// The supported parsing delimiters and labels.
|
|
117
|
-
const delimiters = [
|
|
118
|
-
[',', ','],
|
|
119
|
-
[';', ';'],
|
|
120
|
-
['\t', trans.__('tab')],
|
|
121
|
-
['|', trans.__('pipe')],
|
|
122
|
-
['#', trans.__('hash')]
|
|
123
|
-
];
|
|
198
|
+
export function createNode(options: ICreateNodeOptions): HTMLElement {
|
|
199
|
+
const { selected, label, labelClassName, dropdownClassName, values } =
|
|
200
|
+
options;
|
|
124
201
|
|
|
125
202
|
const div = document.createElement('div');
|
|
126
|
-
const
|
|
203
|
+
const labelNode = document.createElement('span');
|
|
127
204
|
const select = document.createElement('select');
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
for (const [
|
|
205
|
+
labelNode.textContent = label;
|
|
206
|
+
labelNode.className = labelClassName;
|
|
207
|
+
for (const [value, optionLabel] of values) {
|
|
131
208
|
const option = document.createElement('option');
|
|
132
|
-
option.value =
|
|
133
|
-
option.textContent =
|
|
134
|
-
if (
|
|
209
|
+
option.value = value;
|
|
210
|
+
option.textContent = optionLabel;
|
|
211
|
+
if (value === selected) {
|
|
135
212
|
option.selected = true;
|
|
136
213
|
}
|
|
137
214
|
select.appendChild(option);
|
|
138
215
|
}
|
|
139
|
-
div.appendChild(
|
|
216
|
+
div.appendChild(labelNode);
|
|
140
217
|
const node = Styling.wrapSelect(select);
|
|
141
|
-
node.classList.add(
|
|
218
|
+
node.classList.add(dropdownClassName);
|
|
142
219
|
div.appendChild(node);
|
|
143
220
|
return div;
|
|
144
221
|
}
|
package/src/widget.ts
CHANGED
|
@@ -14,7 +14,7 @@ import type { ISignal } from '@lumino/signaling';
|
|
|
14
14
|
import { Signal } from '@lumino/signaling';
|
|
15
15
|
import { PanelLayout, Widget } from '@lumino/widgets';
|
|
16
16
|
import type * as DSVModelModule from './model';
|
|
17
|
-
import { CSVDelimiter } from './toolbar';
|
|
17
|
+
import { CSVComment, CSVDelimiter } from './toolbar';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* The class name added to a CSV viewer.
|
|
@@ -59,6 +59,10 @@ export class TextRenderConfig {
|
|
|
59
59
|
* font family
|
|
60
60
|
*/
|
|
61
61
|
fontFamily?: string;
|
|
62
|
+
/**
|
|
63
|
+
* text color of comment rows
|
|
64
|
+
*/
|
|
65
|
+
commentColor?: string;
|
|
62
66
|
}
|
|
63
67
|
|
|
64
68
|
/**
|
|
@@ -86,20 +90,34 @@ export class GridSearchService {
|
|
|
86
90
|
* Returns a cellrenderer config function to render each cell background.
|
|
87
91
|
* If cell match, background is matchBackgroundColor, if it's the current
|
|
88
92
|
* match, background is currentMatchBackgroundColor.
|
|
93
|
+
*
|
|
94
|
+
* The search covers the column headers and the body, so the row headers
|
|
95
|
+
* and the corner header are never highlighted.
|
|
89
96
|
*/
|
|
90
97
|
cellBackgroundColorRendererFunc(
|
|
91
98
|
config: TextRenderConfig
|
|
92
99
|
): DataGridModule.CellRenderer.ConfigFunc<string> {
|
|
93
|
-
return ({ value, row, column }) => {
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
return ({ value, region, row, column }) => {
|
|
101
|
+
if (
|
|
102
|
+
!this._query ||
|
|
103
|
+
region === 'row-header' ||
|
|
104
|
+
region === 'corner-header' ||
|
|
105
|
+
(region === 'column-header' && row >= this._headerRows) ||
|
|
106
|
+
!Private.matches(value, this._query)
|
|
107
|
+
) {
|
|
108
|
+
return '';
|
|
109
|
+
}
|
|
110
|
+
// The search goes through the column header rows, then the body rows.
|
|
111
|
+
const searchRow =
|
|
112
|
+
region === 'column-header' ? row : row + this._headerRows;
|
|
113
|
+
if (
|
|
114
|
+
this._hasMatch &&
|
|
115
|
+
this._row === searchRow &&
|
|
116
|
+
this._column === column
|
|
117
|
+
) {
|
|
118
|
+
return config.currentMatchBackgroundColor;
|
|
101
119
|
}
|
|
102
|
-
return
|
|
120
|
+
return config.matchBackgroundColor;
|
|
103
121
|
};
|
|
104
122
|
}
|
|
105
123
|
|
|
@@ -110,23 +128,102 @@ export class GridSearchService {
|
|
|
110
128
|
this._query = null;
|
|
111
129
|
this._row = 0;
|
|
112
130
|
this._column = -1;
|
|
131
|
+
this._hasMatch = false;
|
|
132
|
+
this._currentMatchIndex = null;
|
|
133
|
+
this._matchesCount = null;
|
|
113
134
|
this._changed.emit(undefined);
|
|
114
135
|
}
|
|
115
136
|
|
|
137
|
+
/**
|
|
138
|
+
* The number of cells matching the current query, or `null` without a query.
|
|
139
|
+
*/
|
|
140
|
+
get matchesCount(): number | null {
|
|
141
|
+
const model = this._grid.dataModel;
|
|
142
|
+
if (!this._query || !model) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
const headerRows = this._syncWithGrid(model);
|
|
146
|
+
if (this._matchesCount === null) {
|
|
147
|
+
this._matchesCount = Private.countMatchesBefore(
|
|
148
|
+
model,
|
|
149
|
+
headerRows,
|
|
150
|
+
this._query,
|
|
151
|
+
headerRows + model.rowCount('body'),
|
|
152
|
+
0
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return this._matchesCount;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The index of the current match, or `null` if there is none.
|
|
160
|
+
*/
|
|
161
|
+
get currentMatchIndex(): number | null {
|
|
162
|
+
const model = this._grid.dataModel;
|
|
163
|
+
if (!this._query || !model) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
// The current match is lost when the grid hides the header that holds it.
|
|
167
|
+
const headerRows = this._syncWithGrid(model);
|
|
168
|
+
if (!this._hasMatch) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
if (this._currentMatchIndex === null) {
|
|
172
|
+
// The data changed after the last step, so count the matches again.
|
|
173
|
+
if (
|
|
174
|
+
this._row >= headerRows + model.rowCount('body') ||
|
|
175
|
+
this._column >= model.columnCount('body') ||
|
|
176
|
+
!Private.matches(
|
|
177
|
+
Private.data(model, headerRows, this._row, this._column),
|
|
178
|
+
this._query
|
|
179
|
+
)
|
|
180
|
+
) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
this._currentMatchIndex = Private.countMatchesBefore(
|
|
184
|
+
model,
|
|
185
|
+
headerRows,
|
|
186
|
+
this._query,
|
|
187
|
+
this._row,
|
|
188
|
+
this._column
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
return this._currentMatchIndex;
|
|
192
|
+
}
|
|
193
|
+
|
|
116
194
|
/**
|
|
117
195
|
* incrementally look for searchText.
|
|
118
196
|
*/
|
|
119
197
|
find(query: RegExp, reverse = false): boolean {
|
|
120
|
-
const model = this._grid.dataModel
|
|
121
|
-
|
|
198
|
+
const model = this._grid.dataModel;
|
|
199
|
+
if (!model) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
// The search goes through the column header rows, then the body rows.
|
|
203
|
+
const headerRows = this._syncWithGrid(model);
|
|
204
|
+
const rowCount = headerRows + model.rowCount('body');
|
|
122
205
|
const columnCount = model.columnCount('body');
|
|
123
206
|
|
|
124
|
-
if (
|
|
207
|
+
if (
|
|
208
|
+
this._query !== query ||
|
|
209
|
+
// The data got smaller than the position of the last match.
|
|
210
|
+
(this._looping && (this._row >= rowCount || this._column >= columnCount))
|
|
211
|
+
) {
|
|
125
212
|
// reset search
|
|
126
213
|
this._row = 0;
|
|
127
214
|
this._column = -1;
|
|
215
|
+
this._currentMatchIndex = null;
|
|
216
|
+
}
|
|
217
|
+
if (this._query !== query) {
|
|
218
|
+
this._matchesCount = null;
|
|
128
219
|
}
|
|
220
|
+
const previousIndex = this._hasMatch ? this._currentMatchIndex : null;
|
|
221
|
+
const startsBeforeFirstCell = this._row === 0 && this._column === -1;
|
|
222
|
+
const startsAfterLastCell =
|
|
223
|
+
this._row === rowCount - 1 && this._column === columnCount;
|
|
129
224
|
this._query = query;
|
|
225
|
+
this._hasMatch = false;
|
|
226
|
+
this._currentMatchIndex = null;
|
|
130
227
|
|
|
131
228
|
// check if the match is in current viewport
|
|
132
229
|
|
|
@@ -160,19 +257,29 @@ export class GridSearchService {
|
|
|
160
257
|
reverse ? col >= 0 : col < columnCount;
|
|
161
258
|
col += increment
|
|
162
259
|
) {
|
|
163
|
-
|
|
164
|
-
|
|
260
|
+
if (Private.matches(Private.data(model, headerRows, row, col), query)) {
|
|
261
|
+
// The column headers do not scroll.
|
|
262
|
+
const bodyRow = row - headerRows;
|
|
263
|
+
if (bodyRow >= 0 && !isInViewport(bodyRow, col)) {
|
|
264
|
+
this._grid.scrollToRow(bodyRow);
|
|
265
|
+
}
|
|
266
|
+
this._row = row;
|
|
267
|
+
this._column = col;
|
|
268
|
+
this._hasMatch = true;
|
|
269
|
+
if (!reverse && startsBeforeFirstCell) {
|
|
270
|
+
this._currentMatchIndex = 0;
|
|
271
|
+
} else if (reverse && startsAfterLastCell) {
|
|
272
|
+
this._currentMatchIndex =
|
|
273
|
+
this._matchesCount === null ? null : this._matchesCount - 1;
|
|
274
|
+
} else if (previousIndex !== null) {
|
|
275
|
+
this._currentMatchIndex = previousIndex + increment;
|
|
276
|
+
}
|
|
277
|
+
|
|
165
278
|
// to update the background of matching cells.
|
|
166
279
|
|
|
167
280
|
// TODO: we only really need to invalidate the previous and current
|
|
168
281
|
// cell rects, not the entire grid.
|
|
169
282
|
this._changed.emit(undefined);
|
|
170
|
-
|
|
171
|
-
if (!isInViewport(row, col)) {
|
|
172
|
-
this._grid.scrollToRow(row);
|
|
173
|
-
}
|
|
174
|
-
this._row = row;
|
|
175
|
-
this._column = col;
|
|
176
283
|
return true;
|
|
177
284
|
}
|
|
178
285
|
}
|
|
@@ -191,6 +298,8 @@ export class GridSearchService {
|
|
|
191
298
|
this._looping = true;
|
|
192
299
|
}
|
|
193
300
|
}
|
|
301
|
+
// to remove the highlights of the previous query.
|
|
302
|
+
this._changed.emit(undefined);
|
|
194
303
|
return false;
|
|
195
304
|
}
|
|
196
305
|
|
|
@@ -199,7 +308,7 @@ export class GridSearchService {
|
|
|
199
308
|
*/
|
|
200
309
|
private _wrapRows(reverse = false) {
|
|
201
310
|
const model = this._grid.dataModel!;
|
|
202
|
-
const rowCount = model.rowCount('body');
|
|
311
|
+
const rowCount = this._headerRows + model.rowCount('body');
|
|
203
312
|
const columnCount = model.columnCount('body');
|
|
204
313
|
|
|
205
314
|
if (reverse && this._row <= 0) {
|
|
@@ -217,10 +326,57 @@ export class GridSearchService {
|
|
|
217
326
|
return this._query;
|
|
218
327
|
}
|
|
219
328
|
|
|
329
|
+
/**
|
|
330
|
+
* Follow the data model and the header visibility of the grid, which both
|
|
331
|
+
* change the cells that the search goes through.
|
|
332
|
+
*
|
|
333
|
+
* @returns The number of column header rows that the search goes through.
|
|
334
|
+
*/
|
|
335
|
+
private _syncWithGrid(model: DataGridModule.DataModel): number {
|
|
336
|
+
// The search skips the column headers when the grid hides them.
|
|
337
|
+
const visibility = this._grid.headerVisibility;
|
|
338
|
+
const headerRows =
|
|
339
|
+
visibility === 'all' || visibility === 'column'
|
|
340
|
+
? model.rowCount('column-header')
|
|
341
|
+
: 0;
|
|
342
|
+
if (model !== this._model) {
|
|
343
|
+
this._model?.changed.disconnect(this._onModelChanged, this);
|
|
344
|
+
model.changed.connect(this._onModelChanged, this);
|
|
345
|
+
this._model = model;
|
|
346
|
+
this._onModelChanged();
|
|
347
|
+
} else if (headerRows !== this._headerRows) {
|
|
348
|
+
if (this._hasMatch && this._row >= this._headerRows) {
|
|
349
|
+
// Keep the current match on the same body cell.
|
|
350
|
+
this._row += headerRows - this._headerRows;
|
|
351
|
+
} else if (this._hasMatch) {
|
|
352
|
+
// The current match is in a column header that is now hidden.
|
|
353
|
+
this._row = 0;
|
|
354
|
+
this._column = -1;
|
|
355
|
+
this._hasMatch = false;
|
|
356
|
+
}
|
|
357
|
+
this._onModelChanged();
|
|
358
|
+
}
|
|
359
|
+
this._headerRows = headerRows;
|
|
360
|
+
return headerRows;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Forget the matches counted in the previous data.
|
|
365
|
+
*/
|
|
366
|
+
private _onModelChanged(): void {
|
|
367
|
+
this._matchesCount = null;
|
|
368
|
+
this._currentMatchIndex = null;
|
|
369
|
+
}
|
|
370
|
+
|
|
220
371
|
private _grid: DataGridModule.DataGrid;
|
|
372
|
+
private _model: DataGridModule.DataModel | null = null;
|
|
373
|
+
private _headerRows = 0;
|
|
221
374
|
private _query: RegExp | null;
|
|
222
375
|
private _row: number;
|
|
223
376
|
private _column: number;
|
|
377
|
+
private _hasMatch = false;
|
|
378
|
+
private _currentMatchIndex: number | null = null;
|
|
379
|
+
private _matchesCount: number | null = null;
|
|
224
380
|
private _looping = true;
|
|
225
381
|
private _changed = new Signal<GridSearchService, void>(this);
|
|
226
382
|
}
|
|
@@ -321,6 +477,20 @@ export class CSVViewer extends Widget {
|
|
|
321
477
|
void this._updateGrid();
|
|
322
478
|
}
|
|
323
479
|
|
|
480
|
+
/**
|
|
481
|
+
* The optional comment character for the file.
|
|
482
|
+
*/
|
|
483
|
+
get comment(): string | null {
|
|
484
|
+
return this._comment;
|
|
485
|
+
}
|
|
486
|
+
set comment(value: string | null) {
|
|
487
|
+
if (value === this._comment) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
this._comment = value;
|
|
491
|
+
void this._updateGrid();
|
|
492
|
+
}
|
|
493
|
+
|
|
324
494
|
/**
|
|
325
495
|
* The style used by the data grid.
|
|
326
496
|
*/
|
|
@@ -346,6 +516,14 @@ export class CSVViewer extends Widget {
|
|
|
346
516
|
return this._searchService;
|
|
347
517
|
}
|
|
348
518
|
|
|
519
|
+
/**
|
|
520
|
+
* A signal emitted when the data shown in the grid changes: after the file
|
|
521
|
+
* or the parsing options change, and when the rest of a large file is parsed.
|
|
522
|
+
*/
|
|
523
|
+
get dataChanged(): ISignal<CSVViewer, void> {
|
|
524
|
+
return this._dataChanged;
|
|
525
|
+
}
|
|
526
|
+
|
|
349
527
|
/**
|
|
350
528
|
* Dispose of the resources used by the widget.
|
|
351
529
|
*/
|
|
@@ -360,7 +538,10 @@ export class CSVViewer extends Widget {
|
|
|
360
538
|
* Go to line
|
|
361
539
|
*/
|
|
362
540
|
goToLine(lineNumber: number): void {
|
|
363
|
-
this._grid.
|
|
541
|
+
const model = this._grid.dataModel as DSVModelModule.DSVModel | null;
|
|
542
|
+
this._grid.scrollToRow(
|
|
543
|
+
model ? model.bodyRowForNumber(lineNumber) : lineNumber
|
|
544
|
+
);
|
|
364
545
|
}
|
|
365
546
|
|
|
366
547
|
/**
|
|
@@ -375,19 +556,58 @@ export class CSVViewer extends Widget {
|
|
|
375
556
|
* Create the model for the grid.
|
|
376
557
|
*/
|
|
377
558
|
private async _updateGrid(): Promise<void> {
|
|
559
|
+
const updateId = ++this._updateId;
|
|
378
560
|
const { BasicSelectionModel } = await Private.ensureDataGrid();
|
|
379
561
|
const { DSVModel } = await Private.ensureDSVModel();
|
|
562
|
+
if (updateId !== this._updateId) {
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
380
565
|
const data: string = this._context.model.toString();
|
|
381
566
|
const delimiter = this._delimiter;
|
|
382
567
|
const oldModel = this._grid.dataModel as DSVModelModule.DSVModel;
|
|
568
|
+
// Replacing the data model clears the grid's column sizes.
|
|
569
|
+
for (let i = 0; i < this._grid.columnCount('body'); i++) {
|
|
570
|
+
this._columnWidths[i] = this._grid.columnSize('body', i);
|
|
571
|
+
}
|
|
383
572
|
const dataModel = (this._grid.dataModel = new DSVModel({
|
|
384
573
|
data,
|
|
385
|
-
delimiter
|
|
574
|
+
delimiter,
|
|
575
|
+
comment: this._comment ?? undefined
|
|
386
576
|
}));
|
|
577
|
+
const columnCount = Math.min(
|
|
578
|
+
dataModel.columnCount('body'),
|
|
579
|
+
this._columnWidths.length
|
|
580
|
+
);
|
|
581
|
+
for (let i = 0; i < columnCount; i++) {
|
|
582
|
+
if (this._columnWidths[i] !== this._grid.defaultSizes.columnWidth) {
|
|
583
|
+
this._grid.resizeColumn('body', i, this._columnWidths[i]);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
dataModel.changed.connect(this._onDataModelChanged, this);
|
|
387
587
|
this._grid.selectionModel = new BasicSelectionModel({ dataModel });
|
|
388
588
|
if (oldModel) {
|
|
589
|
+
oldModel.changed.disconnect(this._onDataModelChanged, this);
|
|
389
590
|
oldModel.dispose();
|
|
390
591
|
}
|
|
592
|
+
this._dataChanged.emit(undefined);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Restore widths of new columns and notify listeners of data changes.
|
|
597
|
+
*/
|
|
598
|
+
private _onDataModelChanged(
|
|
599
|
+
sender: DSVModelModule.DSVModel,
|
|
600
|
+
args: DataGridModule.DataModel.ChangedArgs
|
|
601
|
+
): void {
|
|
602
|
+
if (args.type === 'columns-inserted' && args.region === 'body') {
|
|
603
|
+
const end = Math.min(args.index + args.span, this._columnWidths.length);
|
|
604
|
+
for (let i = args.index; i < end; i++) {
|
|
605
|
+
if (this._columnWidths[i] !== this._grid.defaultSizes.columnWidth) {
|
|
606
|
+
this._grid.resizeColumn('body', i, this._columnWidths[i]);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
this._dataChanged.emit(undefined);
|
|
391
611
|
}
|
|
392
612
|
|
|
393
613
|
/**
|
|
@@ -399,10 +619,19 @@ export class CSVViewer extends Widget {
|
|
|
399
619
|
}
|
|
400
620
|
const { TextRenderer } = await Private.ensureDataGrid();
|
|
401
621
|
const rendererConfig = this._baseRenderer;
|
|
622
|
+
const font = `${rendererConfig.fontSize ?? '12px'} ${rendererConfig.fontFamily ?? 'sans-serif'}`;
|
|
623
|
+
const isComment = (config: DataGridModule.CellRenderer.CellConfig) =>
|
|
624
|
+
config.region === 'body' && config.metadata.comment === true;
|
|
402
625
|
const renderer = new TextRenderer({
|
|
403
|
-
font:
|
|
404
|
-
textColor:
|
|
405
|
-
|
|
626
|
+
font: config => (isComment(config) ? `italic ${font}` : font),
|
|
627
|
+
textColor: config =>
|
|
628
|
+
isComment(config)
|
|
629
|
+
? (rendererConfig.commentColor ?? rendererConfig.textColor)
|
|
630
|
+
: rendererConfig.textColor,
|
|
631
|
+
horizontalAlignment: config =>
|
|
632
|
+
isComment(config) && config.metadata.merged === true
|
|
633
|
+
? 'left'
|
|
634
|
+
: rendererConfig.horizontalAlignment,
|
|
406
635
|
backgroundColor:
|
|
407
636
|
this._searchService.cellBackgroundColorRendererFunc(rendererConfig)
|
|
408
637
|
});
|
|
@@ -418,9 +647,13 @@ export class CSVViewer extends Widget {
|
|
|
418
647
|
private _grid: DataGridModule.DataGrid;
|
|
419
648
|
private _defaultStyle: typeof DataGridModule.DataGrid.defaultStyle;
|
|
420
649
|
private _searchService: GridSearchService;
|
|
650
|
+
private _dataChanged = new Signal<CSVViewer, void>(this);
|
|
421
651
|
private _monitor: ActivityMonitor<DocumentRegistry.IModel, void> | null =
|
|
422
652
|
null;
|
|
423
653
|
private _delimiter = ',';
|
|
654
|
+
private _comment: string | null = null;
|
|
655
|
+
private _columnWidths: number[] = [];
|
|
656
|
+
private _updateId = 0;
|
|
424
657
|
private _revealed = new PromiseDelegate<void>();
|
|
425
658
|
private _baseRenderer: TextRenderConfig | null = null;
|
|
426
659
|
private _ready: Promise<void>;
|
|
@@ -524,6 +757,13 @@ export class CSVViewerFactory extends ABCWidgetFactory<
|
|
|
524
757
|
widget: widget.content,
|
|
525
758
|
translator: this.translator
|
|
526
759
|
})
|
|
760
|
+
},
|
|
761
|
+
{
|
|
762
|
+
name: 'comment',
|
|
763
|
+
widget: new CSVComment({
|
|
764
|
+
widget: widget.content,
|
|
765
|
+
translator: this.translator
|
|
766
|
+
})
|
|
527
767
|
}
|
|
528
768
|
];
|
|
529
769
|
}
|
|
@@ -552,6 +792,59 @@ namespace Private {
|
|
|
552
792
|
let gridLoaded: PromiseDelegate<typeof DataGridModule> | null = null;
|
|
553
793
|
let modelLoaded: PromiseDelegate<typeof DSVModelModule> | null = null;
|
|
554
794
|
|
|
795
|
+
/**
|
|
796
|
+
* Get the value of a cell, where the column header rows come before the
|
|
797
|
+
* body rows.
|
|
798
|
+
*/
|
|
799
|
+
export function data(
|
|
800
|
+
model: DataGridModule.DataModel,
|
|
801
|
+
headerRows: number,
|
|
802
|
+
row: number,
|
|
803
|
+
column: number
|
|
804
|
+
): unknown {
|
|
805
|
+
return row < headerRows
|
|
806
|
+
? model.data('column-header', row, column)
|
|
807
|
+
: model.data('body', row - headerRows, column);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* Test whether the text that the grid shows for a value matches the query.
|
|
812
|
+
*/
|
|
813
|
+
export function matches(value: unknown, query: RegExp): boolean {
|
|
814
|
+
let text: string;
|
|
815
|
+
if (typeof value === 'string') {
|
|
816
|
+
// The values of a CSV file are strings, so they skip the conversion.
|
|
817
|
+
text = value;
|
|
818
|
+
} else {
|
|
819
|
+
// A missing value is shown as an empty cell.
|
|
820
|
+
text = value === null || value === undefined ? '' : String(value);
|
|
821
|
+
}
|
|
822
|
+
return text.search(query) !== -1;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* Count the matching cells before the given cell, row by row.
|
|
827
|
+
*/
|
|
828
|
+
export function countMatchesBefore(
|
|
829
|
+
model: DataGridModule.DataModel,
|
|
830
|
+
headerRows: number,
|
|
831
|
+
query: RegExp,
|
|
832
|
+
row: number,
|
|
833
|
+
column: number
|
|
834
|
+
): number {
|
|
835
|
+
const columnCount = model.columnCount('body');
|
|
836
|
+
let count = 0;
|
|
837
|
+
for (let r = 0; r <= row; r++) {
|
|
838
|
+
const end = r === row ? column : columnCount;
|
|
839
|
+
for (let c = 0; c < end; c++) {
|
|
840
|
+
if (matches(data(model, headerRows, r, c), query)) {
|
|
841
|
+
count++;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
return count;
|
|
846
|
+
}
|
|
847
|
+
|
|
555
848
|
/**
|
|
556
849
|
* Lazily load the datagrid module when the first grid is requested.
|
|
557
850
|
*/
|
package/style/base.css
CHANGED
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
font-size: var(--jp-ui-font-size1);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
.jp-CSVDelimiter
|
|
16
|
+
.jp-CSVDelimiter,
|
|
17
|
+
.jp-CSVComment {
|
|
17
18
|
display: flex;
|
|
18
19
|
flex: 0 0 auto;
|
|
19
20
|
flex-direction: row;
|
|
@@ -23,14 +24,16 @@
|
|
|
23
24
|
z-index: 1;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
.jp-CSVDelimiter .jp-CSVDelimiter-label
|
|
27
|
+
.jp-CSVDelimiter .jp-CSVDelimiter-label,
|
|
28
|
+
.jp-CSVComment .jp-CSVComment-label {
|
|
27
29
|
color: var(--jp-ui-font-color1);
|
|
28
30
|
font-size: var(--jp-ui-font-size1);
|
|
29
31
|
padding-left: 8px;
|
|
30
32
|
padding-right: 8px;
|
|
31
33
|
}
|
|
32
34
|
|
|
33
|
-
.jp-CSVDelimiter .jp-CSVDelimiter-dropdown
|
|
35
|
+
.jp-CSVDelimiter .jp-CSVDelimiter-dropdown,
|
|
36
|
+
.jp-CSVComment .jp-CSVComment-dropdown {
|
|
34
37
|
flex: 0 0 auto;
|
|
35
38
|
vertical-align: middle;
|
|
36
39
|
border-radius: 0;
|
|
@@ -40,7 +43,8 @@
|
|
|
40
43
|
margin-bottom: 2px;
|
|
41
44
|
}
|
|
42
45
|
|
|
43
|
-
.jp-CSVDelimiter .jp-CSVDelimiter-dropdown select.jp-mod-styled
|
|
46
|
+
.jp-CSVDelimiter .jp-CSVDelimiter-dropdown select.jp-mod-styled,
|
|
47
|
+
.jp-CSVComment .jp-CSVComment-dropdown select.jp-mod-styled {
|
|
44
48
|
color: var(--jp-ui-font-color1);
|
|
45
49
|
background: var(--jp-layout-color1);
|
|
46
50
|
font-size: var(--jp-ui-font-size1);
|