notebookBrowser.ts 17.4 KB
Newer Older
R
rebornix 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
R
rebornix 已提交
7 8
import { IListEvent, IListMouseEvent } from 'vs/base/browser/ui/list/list';
import { IListOptions, IListStyles } from 'vs/base/browser/ui/list/listWidget';
9
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
10
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
11
import { CancellationTokenSource } from 'vs/base/common/cancellation';
R
rebornix 已提交
12 13 14
import { Event } from 'vs/base/common/event';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { ScrollEvent } from 'vs/base/common/scrollable';
R
rebornix 已提交
15
import { URI } from 'vs/base/common/uri';
R
Rob Lourens 已提交
16
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
17
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
K
kieferrm 已提交
18
import { IPosition } from 'vs/editor/common/core/position';
19
import { Range } from 'vs/editor/common/core/range';
20
import { FindMatch, IReadonlyTextBuffer, ITextModel } from 'vs/editor/common/model';
21
import { ContextKeyExpr, RawContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
22
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
23
import { CellLanguageStatusBarItem, TimerRenderer } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer';
24
import { CellViewModel, IModelDecorationsChangeAccessor, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
R
rebornix 已提交
25
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
R
rebornix 已提交
26
import { CellKind, IOutput, IRenderOutput, NotebookCellMetadata, NotebookDocumentMetadata, INotebookKernelInfo, IEditor } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
27
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
R
rebornix 已提交
28
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
R
rebornix 已提交
29 30

export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = new RawContextKey<boolean>('notebookFindWidgetFocused', false);
R
rebornix 已提交
31

32 33 34
// Is Notebook
export const NOTEBOOK_IS_ACTIVE_EDITOR = ContextKeyExpr.equals('activeEditor', 'workbench.editor.notebook');

35
// Editor keys
36
export const NOTEBOOK_EDITOR_FOCUSED = new RawContextKey<boolean>('notebookEditorFocused', false);
37 38 39
export const NOTEBOOK_EDITOR_EDITABLE = new RawContextKey<boolean>('notebookEditable', true);
export const NOTEBOOK_EDITOR_RUNNABLE = new RawContextKey<boolean>('notebookRunnable', true);
export const NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK = new RawContextKey<boolean>('notebookExecuting', false);
40

41 42 43 44 45 46 47
// Cell keys
export const NOTEBOOK_VIEW_TYPE = new RawContextKey<string>('notebookViewType', undefined);
export const NOTEBOOK_CELL_TYPE = new RawContextKey<string>('notebookCellType', undefined); // code, markdown
export const NOTEBOOK_CELL_EDITABLE = new RawContextKey<boolean>('notebookCellEditable', false); // bool
export const NOTEBOOK_CELL_RUNNABLE = new RawContextKey<boolean>('notebookCellRunnable', false); // bool
export const NOTEBOOK_CELL_MARKDOWN_EDIT_MODE = new RawContextKey<boolean>('notebookCellMarkdownEditMode', false); // bool
export const NOTEBOOK_CELL_RUN_STATE = new RawContextKey<string>('notebookCellRunState', undefined); // idle, running
R
rebornix 已提交
48
export const NOTEBOOK_CELL_HAS_OUTPUTS = new RawContextKey<boolean>('notebookCellHasOutputs', false); // bool
49

R
rebornix 已提交
50 51 52 53
// Kernels

export const NOTEBOOK_HAS_MULTIPLE_KERNELS = new RawContextKey<boolean>('notebookHasMultipleKernels', false);

R
rebornix 已提交
54 55 56 57 58 59
export interface NotebookLayoutInfo {
	width: number;
	height: number;
	fontInfo: BareFontInfo;
}

R
rebornix 已提交
60 61 62 63 64 65 66 67 68 69 70
export interface NotebookLayoutChangeEvent {
	width?: boolean;
	height?: boolean;
	fontInfo?: boolean;
}

export interface CodeCellLayoutInfo {
	readonly fontInfo: BareFontInfo | null;
	readonly editorHeight: number;
	readonly editorWidth: number;
	readonly totalHeight: number;
71
	readonly outputContainerOffset: number;
R
rebornix 已提交
72 73
	readonly outputTotalHeight: number;
	readonly indicatorHeight: number;
R
rebornix 已提交
74
	readonly bottomToolbarOffset: number;
R
rebornix 已提交
75 76 77 78 79 80
}

export interface CodeCellLayoutChangeEvent {
	editorHeight?: boolean;
	outputHeight?: boolean;
	totalHeight?: boolean;
R
rebornix 已提交
81 82
	outerWidth?: number;
	font?: BareFontInfo;
R
rebornix 已提交
83 84 85 86 87
}

export interface MarkdownCellLayoutInfo {
	readonly fontInfo: BareFontInfo | null;
	readonly editorWidth: number;
R
Rob Lourens 已提交
88
	readonly editorHeight: number;
R
rebornix 已提交
89
	readonly bottomToolbarOffset: number;
R
rebornix 已提交
90
	readonly totalHeight: number;
R
rebornix 已提交
91 92 93
}

export interface MarkdownCellLayoutChangeEvent {
R
rebornix 已提交
94 95
	font?: BareFontInfo;
	outerWidth?: number;
R
rebornix 已提交
96
	totalHeight?: number;
R
Rob Lourens 已提交
97
	editorHeight?: boolean;
R
rebornix 已提交
98 99
}

R
rebornix 已提交
100
export interface ICellViewModel {
R
rebornix 已提交
101
	readonly model: NotebookCellTextModel;
R
rebornix 已提交
102
	readonly id: string;
103
	readonly textBuffer: IReadonlyTextBuffer;
104
	dragging: boolean;
R
rebornix 已提交
105 106
	handle: number;
	uri: URI;
107
	language: string;
R
rebornix 已提交
108
	cellKind: CellKind;
109
	editState: CellEditState;
110 111
	readonly runState: CellRunState;
	currentTokenSource: CancellationTokenSource | undefined;
R
rebornix 已提交
112 113
	focusMode: CellFocusMode;
	getText(): string;
114
	metadata: NotebookCellMetadata | undefined;
115 116 117
	textModel: ITextModel | undefined;
	hasModel(): this is IEditableCellViewModel;
	resolveTextModel(): Promise<ITextModel>;
R
rebornix 已提交
118
	getEvaluatedMetadata(documentMetadata: NotebookDocumentMetadata | undefined): NotebookCellMetadata;
K
kieferrm 已提交
119
	getSelectionsStartPosition(): IPosition[] | undefined;
R
rebornix 已提交
120 121
}

122 123 124 125
export interface IEditableCellViewModel extends ICellViewModel {
	textModel: ITextModel;
}

R
rebornix 已提交
126 127 128 129 130
export interface INotebookEditorMouseEvent {
	readonly event: MouseEvent;
	readonly target: CellViewModel;
}

R
rebornix 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
export interface INotebookEditorContribution {
	/**
	 * Dispose this contribution.
	 */
	dispose(): void;
	/**
	 * Store view state.
	 */
	saveViewState?(): any;
	/**
	 * Restore view state.
	 */
	restoreViewState?(state: any): void;
}

R
rebornix 已提交
146
export interface INotebookEditor extends IEditor {
R
rebornix 已提交
147 148 149 150

	/**
	 * Notebook view model attached to the current editor
	 */
R
rebornix 已提交
151
	viewModel: NotebookViewModel | undefined;
R
rebornix 已提交
152

R
rebornix 已提交
153 154 155 156
	/**
	 * An event emitted when the model of this editor has changed.
	 * @event
	 */
R
rebornix 已提交
157 158
	readonly onDidChangeModel: Event<NotebookTextModel | undefined>;
	readonly onDidFocusEditorWidget: Event<void>;
159
	isNotebookEditor: boolean;
R
rebornix 已提交
160 161
	activeKernel: INotebookKernelInfo | undefined;
	readonly onDidChangeKernel: Event<void>;
162

R
rebornix 已提交
163
	getId(): string;
R
rebornix 已提交
164
	getDomNode(): HTMLElement;
R
rebornix 已提交
165 166
	getInnerWebview(): Webview | undefined;

R
rebornix 已提交
167 168 169
	/**
	 * Focus the notebook editor cell list
	 */
R
rebornix 已提交
170
	focus(): void;
R
rebornix 已提交
171

R
rebornix 已提交
172 173
	hasFocus(): boolean;

R
rebornix 已提交
174 175 176
	/**
	 * Select & focus cell
	 */
R
rebornix 已提交
177
	selectElement(cell: ICellViewModel): void;
R
rebornix 已提交
178

R
rebornix 已提交
179 180 181
	/**
	 * Layout info for the notebook editor
	 */
R
rebornix 已提交
182
	getLayoutInfo(): NotebookLayoutInfo;
R
rebornix 已提交
183 184 185
	/**
	 * Fetch the output renderers for notebook outputs.
	 */
R
rebornix 已提交
186
	getOutputRenderer(): OutputRenderer;
R
rebornix 已提交
187 188 189 190

	/**
	 * Insert a new cell around `cell`
	 */
R
rebornix 已提交
191
	insertNotebookCell(cell: ICellViewModel | undefined, type: CellKind, direction?: 'above' | 'below', initialText?: string, ui?: boolean): CellViewModel | null;
R
rebornix 已提交
192

K
kieferrm 已提交
193 194 195
	/**
	 * Split a given cell into multiple cells of the same type using the selection start positions.
	 */
196
	splitNotebookCell(cell: ICellViewModel): Promise<CellViewModel[] | null>;
K
kieferrm 已提交
197 198 199 200 201 202

	/**
	 * Joins the given cell either with the cell above or the one below depending on the given direction.
	 */
	joinNotebookCells(cell: ICellViewModel, direction: 'above' | 'below', constraint?: CellKind): Promise<ICellViewModel | null>;

R
rebornix 已提交
203 204 205
	/**
	 * Delete a cell from the notebook
	 */
206
	deleteNotebookCell(cell: ICellViewModel): Promise<boolean>;
R
rebornix 已提交
207

208 209 210
	/**
	 * Move a cell up one spot
	 */
211
	moveCellUp(cell: ICellViewModel): Promise<boolean>;
212 213 214 215

	/**
	 * Move a cell down one spot
	 */
216
	moveCellDown(cell: ICellViewModel): Promise<boolean>;
217

R
Rob Lourens 已提交
218 219 220
	/**
	 * Move a cell above or below another cell
	 */
221
	moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<boolean>;
R
Rob Lourens 已提交
222

R
rebornix 已提交
223 224 225 226 227 228
	/**
	 * Switch the cell into editing mode.
	 *
	 * For code cell, the monaco editor will be focused.
	 * For markdown cell, it will switch from preview mode to editing mode, which focuses the monaco editor.
	 */
R
rebornix 已提交
229
	editNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
230 231 232 233

	/**
	 * Quit cell editing mode.
	 */
R
rebornix 已提交
234
	saveNotebookCell(cell: ICellViewModel): void;
R
rebornix 已提交
235 236 237 238

	/**
	 * Focus the container of a cell (the monaco editor inside is not focused).
	 */
C
Christopher Maynard 已提交
239
	focusNotebookCell(cell: ICellViewModel, focus: 'editor' | 'container' | 'output'): void;
R
rebornix 已提交
240

241 242 243 244 245
	/**
	 * Execute the given notebook cell
	 */
	executeNotebookCell(cell: ICellViewModel): Promise<void>;

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	/**
	 * Cancel the cell execution
	 */
	cancelNotebookCellExecution(cell: ICellViewModel): void;

	/**
	 * Executes all notebook cells in order
	 */
	executeNotebook(): Promise<void>;

	/**
	 * Cancel the notebook execution
	 */
	cancelNotebookExecution(): void;

R
rebornix 已提交
261 262 263
	/**
	 * Get current active cell
	 */
R
rebornix 已提交
264
	getActiveCell(): ICellViewModel | undefined;
R
rebornix 已提交
265 266 267 268

	/**
	 * Layout the cell with a new height
	 */
269
	layoutNotebookCell(cell: ICellViewModel, height: number): Promise<void>;
R
rebornix 已提交
270 271 272 273

	/**
	 * Render the output in webview layer
	 */
R
rebornix 已提交
274
	createInset(cell: ICellViewModel, output: IOutput, shadowContent: string, offset: number): void;
R
rebornix 已提交
275 276 277 278

	/**
	 * Remove the output from the webview layer
	 */
R
rebornix 已提交
279
	removeInset(output: IOutput): void;
R
rebornix 已提交
280

281 282 283 284 285
	/**
	 * Send message to the webview for outputs.
	 */
	postMessage(message: any): void;

R
rebornix 已提交
286 287 288
	/**
	 * Trigger the editor to scroll from scroll event programmatically
	 */
289
	triggerScroll(event: IMouseWheelEvent): void;
R
rebornix 已提交
290 291 292 293

	/**
	 * Reveal cell into viewport.
	 */
R
rebornix 已提交
294
	revealInView(cell: ICellViewModel): void;
R
rebornix 已提交
295 296 297 298

	/**
	 * Reveal cell into viewport center.
	 */
R
rebornix 已提交
299
	revealInCenter(cell: ICellViewModel): void;
R
rebornix 已提交
300 301 302 303

	/**
	 * Reveal cell into viewport center if cell is currently out of the viewport.
	 */
R
rebornix 已提交
304
	revealInCenterIfOutsideViewport(cell: ICellViewModel): void;
R
rebornix 已提交
305 306 307 308

	/**
	 * Reveal a line in notebook cell into viewport with minimal scrolling.
	 */
R
rebornix 已提交
309
	revealLineInView(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
310

R
rebornix 已提交
311 312 313
	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
314
	revealLineInCenter(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
315 316 317 318

	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
319
	revealLineInCenterIfOutsideViewport(cell: ICellViewModel, line: number): void;
R
rebornix 已提交
320

R
rebornix 已提交
321 322 323
	/**
	 * Reveal a range in notebook cell into viewport with minimal scrolling.
	 */
R
rebornix 已提交
324
	revealRangeInView(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
325 326 327 328

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
329
	revealRangeInCenter(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
330 331 332 333

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
334
	revealRangeInCenterIfOutsideViewport(cell: ICellViewModel, range: Range): void;
R
rebornix 已提交
335

R
rebornix 已提交
336 337 338 339 340
	/**
	 * Set hidden areas on cell text models.
	 */
	setHiddenAreas(_ranges: ICellRange[]): boolean;

R
rebornix 已提交
341
	setCellSelection(cell: ICellViewModel, selection: Range): void;
R
rebornix 已提交
342

R
rebornix 已提交
343 344 345 346
	/**
	 * Change the decorations on cells.
	 * The notebook is virtualized and this method should be called to create/delete editor decorations safely.
	 */
R
rebornix 已提交
347
	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any;
R
rebornix 已提交
348

R
rebornix 已提交
349 350 351 352 353 354 355 356 357 358 359
	/**
	 * An event emitted on a "mouseup".
	 * @event
	 */
	onMouseUp(listener: (e: INotebookEditorMouseEvent) => void): IDisposable;

	/**
	 * An event emitted on a "mousedown".
	 * @event
	 */
	onMouseDown(listener: (e: INotebookEditorMouseEvent) => void): IDisposable;
R
rebornix 已提交
360 361 362 363 364 365 366

	/**
	 * Get a contribution of this editor.
	 * @id Unique identifier of the contribution.
	 * @return The contribution or null if contribution not found.
	 */
	getContribution<T extends INotebookEditorContribution>(id: string): T;
R
rebornix 已提交
367 368
}

R
rebornix 已提交
369
export interface INotebookCellList {
370
	readonly contextKeyService: IContextKeyService;
R
Rob Lourens 已提交
371
	elementAt(position: number): ICellViewModel | undefined;
372
	elementHeight(element: ICellViewModel): number;
R
rebornix 已提交
373 374 375 376 377 378 379 380 381
	onWillScroll: Event<ScrollEvent>;
	onDidChangeFocus: Event<IListEvent<ICellViewModel>>;
	onDidChangeContentHeight: Event<number>;
	scrollTop: number;
	scrollHeight: number;
	scrollLeft: number;
	length: number;
	rowsContainer: HTMLElement;
	readonly onDidRemoveOutput: Event<IOutput>;
382
	readonly onDidHideOutput: Event<IOutput>;
R
rebornix 已提交
383 384
	readonly onMouseUp: Event<IListMouseEvent<CellViewModel>>;
	readonly onMouseDown: Event<IListMouseEvent<CellViewModel>>;
R
rebornix 已提交
385 386 387
	detachViewModel(): void;
	attachViewModel(viewModel: NotebookViewModel): void;
	clear(): void;
388
	getViewIndex(cell: ICellViewModel): number | undefined;
R
rebornix 已提交
389 390 391 392 393 394 395 396 397 398 399 400
	focusElement(element: ICellViewModel): void;
	selectElement(element: ICellViewModel): void;
	getFocusedElements(): ICellViewModel[];
	revealElementInView(element: ICellViewModel): void;
	revealElementInCenterIfOutsideViewport(element: ICellViewModel): void;
	revealElementInCenter(element: ICellViewModel): void;
	revealElementLineInView(element: ICellViewModel, line: number): void;
	revealElementLineInCenter(element: ICellViewModel, line: number): void;
	revealElementLineInCenterIfOutsideViewport(element: ICellViewModel, line: number): void;
	revealElementRangeInView(element: ICellViewModel, range: Range): void;
	revealElementRangeInCenter(element: ICellViewModel, range: Range): void;
	revealElementRangeInCenterIfOutsideViewport(element: ICellViewModel, range: Range): void;
R
rebornix 已提交
401
	setHiddenAreas(_ranges: ICellRange[], triggerViewUpdate: boolean): boolean;
R
rebornix 已提交
402 403 404 405 406 407 408 409 410 411 412
	domElementOfElement(element: ICellViewModel): HTMLElement | null;
	focusView(): void;
	getAbsoluteTopOfElement(element: ICellViewModel): number;
	triggerScrollFromMouseWheelEvent(browserEvent: IMouseWheelEvent): void;
	updateElementHeight2(element: ICellViewModel, size: number): void;
	domFocus(): void;
	setCellSelection(element: ICellViewModel, range: Range): void;
	style(styles: IListStyles): void;
	updateOptions(options: IListOptions<ICellViewModel>): void;
	layout(height?: number, width?: number): void;
	dispose(): void;
413 414 415 416 417

	// TODO resolve differences between List<CellViewModel> and INotebookCellList<ICellViewModel>
	getFocus(): number[];
	setFocus(indexes: number[]): void;
	setSelection(indexes: number[]): void;
R
rebornix 已提交
418 419
}

420
export interface BaseCellRenderTemplate {
421
	contextKeyService: IContextKeyService;
R
rebornix 已提交
422 423
	container: HTMLElement;
	cellContainer: HTMLElement;
424
	toolbar: ToolBar;
425
	focusIndicator: HTMLElement;
426
	disposables: DisposableStore;
427
	elementDisposables: DisposableStore;
R
rebornix 已提交
428
	bottomCellContainer: HTMLElement;
R
Rob Lourens 已提交
429
	currentRenderedCell?: ICellViewModel;
430 431
	statusBarContainer: HTMLElement;
	languageStatusBarItem: CellLanguageStatusBarItem;
R
rebornix 已提交
432
	toJSON: () => any;
433 434 435
}

export interface MarkdownCellRenderTemplate extends BaseCellRenderTemplate {
436 437
	editorPart: HTMLElement;
	editorContainer: HTMLElement;
R
rebornix 已提交
438
	foldingIndicator: HTMLElement;
R
Rob Lourens 已提交
439
	currentEditor?: ICodeEditor;
440 441 442
}

export interface CodeCellRenderTemplate extends BaseCellRenderTemplate {
R
Rob Lourens 已提交
443 444
	cellRunStatusContainer: HTMLElement;
	cellStatusMessageContainer: HTMLElement;
445 446 447 448
	runToolbar: ToolBar;
	runButtonContainer: HTMLElement;
	executionOrderLabel: HTMLElement;
	outputContainer: HTMLElement;
R
Rob Lourens 已提交
449
	editor: ICodeEditor;
450
	progressBar: ProgressBar;
451
	timer: TimerRenderer;
R
rebornix 已提交
452
}
R
rebornix 已提交
453 454 455 456 457 458 459 460 461

export interface IOutputTransformContribution {
	/**
	 * Dispose this contribution.
	 */
	dispose(): void;

	render(output: IOutput, container: HTMLElement, preferredMimeType: string | undefined): IRenderOutput;
}
R
rebornix 已提交
462 463 464 465 466 467

export interface CellFindMatch {
	cell: CellViewModel;
	matches: FindMatch[];
}

R
rebornix 已提交
468 469 470 471 472 473 474 475 476
export enum CellRevealType {
	Line,
	Range
}

export enum CellRevealPosition {
	Top,
	Center
}
R
rebornix 已提交
477

478 479 480 481 482 483
export enum CellRunState {
	Idle,
	Running
}

export enum CellEditState {
R
rebornix 已提交
484 485 486 487 488
	/**
	 * Default state.
	 * For markdown cell, it's Markdown preview.
	 * For code cell, the browser focus should be on the container instead of the editor
	 */
489
	Preview,
R
rebornix 已提交
490 491 492 493 494 495 496


	/**
	 * Eding mode. Source for markdown or code is rendered in editors and the state will be persistent.
	 */
	Editing
}
R
rebornix 已提交
497 498 499 500 501 502 503 504 505 506 507 508

export enum CellFocusMode {
	Container,
	Editor
}

export enum CursorAtBoundary {
	None,
	Top,
	Bottom,
	Both
}
R
rebornix 已提交
509

510 511 512 513 514 515 516
export interface CellViewModelStateChangeEvent {
	metadataChanged?: boolean;
	selectionChanged?: boolean;
	focusModeChanged?: boolean;
	runStateChanged?: boolean;
	editStateChanged?: boolean;
	languageChanged?: boolean;
R
rebornix 已提交
517 518
	foldingStateChanged?: boolean;
	contentChanged?: boolean;
519
	outputIsHoveredChanged?: boolean;
520 521
}

R
rebornix 已提交
522
/**
R
rebornix 已提交
523
 * [start, end]
R
rebornix 已提交
524 525
 */
export interface ICellRange {
R
rebornix 已提交
526 527 528
	/**
	 * zero based index
	 */
R
rebornix 已提交
529
	start: number;
R
rebornix 已提交
530

R
rebornix 已提交
531
	/**
R
rebornix 已提交
532
	 * zero based index
R
rebornix 已提交
533
	 */
R
rebornix 已提交
534
	end: number;
R
rebornix 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548
}


/**
 * @param _ranges
 */
export function reduceCellRanges(_ranges: ICellRange[]): ICellRange[] {
	if (!_ranges.length) {
		return [];
	}

	let ranges = _ranges.sort((a, b) => a.start - b.start);
	let result: ICellRange[] = [];
	let currentRangeStart = ranges[0].start;
R
rebornix 已提交
549
	let currentRangeEnd = ranges[0].end + 1;
R
rebornix 已提交
550 551 552 553 554

	for (let i = 0, len = ranges.length; i < len; i++) {
		let range = ranges[i];

		if (range.start > currentRangeEnd) {
R
rebornix 已提交
555
			result.push({ start: currentRangeStart, end: currentRangeEnd - 1 });
R
rebornix 已提交
556
			currentRangeStart = range.start;
R
rebornix 已提交
557 558 559
			currentRangeEnd = range.end + 1;
		} else if (range.end + 1 > currentRangeEnd) {
			currentRangeEnd = range.end + 1;
R
rebornix 已提交
560 561 562
		}
	}

R
rebornix 已提交
563
	result.push({ start: currentRangeStart, end: currentRangeEnd - 1 });
R
rebornix 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	return result;
}

export function getVisibleCells(cells: CellViewModel[], hiddenRanges: ICellRange[]) {
	if (!hiddenRanges.length) {
		return cells;
	}

	let start = 0;
	let hiddenRangeIndex = 0;
	let result: any[] = [];

	while (start < cells.length && hiddenRangeIndex < hiddenRanges.length) {
		if (start < hiddenRanges[hiddenRangeIndex].start) {
			result.push(...cells.slice(start, hiddenRanges[hiddenRangeIndex].start));
		}

R
rebornix 已提交
581
		start = hiddenRanges[hiddenRangeIndex].end + 1;
R
rebornix 已提交
582 583 584 585 586 587 588 589 590
		hiddenRangeIndex++;
	}

	if (start < cells.length) {
		result.push(...cells.slice(start));
	}

	return result;
}