Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/browser/viewModel/cellSelectionCollection.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Emitter, Event } from '../../../../../base/common/event.js';
7
import { Disposable } from '../../../../../base/common/lifecycle.js';
8
import { cellRangesEqual, ICellRange } from '../../common/notebookRange.js';
9
10
// Challenge is List View talks about `element`, which needs extra work to convert to ICellRange as we support Folding and Cell Move
11
export class NotebookCellSelectionCollection extends Disposable {
12
13
private readonly _onDidChangeSelection = this._register(new Emitter<string>());
14
get onDidChangeSelection(): Event<string> { return this._onDidChangeSelection.event; }
15
16
private _primary: ICellRange = { start: 0, end: 0 };
17
18
private _selections: ICellRange[] = [{ start: 0, end: 0 }];
19
20
get selections(): ICellRange[] {
21
return this._selections;
22
}
23
24
get focus(): ICellRange {
25
return this._primary;
26
}
27
28
setState(primary: ICellRange | null, selections: ICellRange[], forceEventEmit: boolean, source: 'view' | 'model') {
29
const validPrimary = primary ?? { start: 0, end: 0 };
30
const validSelections = selections.length > 0 ? selections : [{ start: 0, end: 0 }];
31
32
const changed = !cellRangesEqual([validPrimary], [this._primary]) || !cellRangesEqual(this._selections, validSelections);
33
34
this._primary = validPrimary;
35
this._selections = validSelections;
36
if (changed || forceEventEmit) {
37
this._onDidChangeSelection.fire(source);
38
}
39
}
40
41
setSelections(selections: ICellRange[], forceEventEmit: boolean, source: 'view' | 'model') {
42
this.setState(this._primary, selections, forceEventEmit, source);
43
}
44
}
45
46