Path: blob/main/src/vs/workbench/contrib/notebook/browser/viewModel/cellSelectionCollection.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Emitter, Event } from '../../../../../base/common/event.js';6import { Disposable } from '../../../../../base/common/lifecycle.js';7import { cellRangesEqual, ICellRange } from '../../common/notebookRange.js';89// Challenge is List View talks about `element`, which needs extra work to convert to ICellRange as we support Folding and Cell Move10export class NotebookCellSelectionCollection extends Disposable {1112private readonly _onDidChangeSelection = this._register(new Emitter<string>());13get onDidChangeSelection(): Event<string> { return this._onDidChangeSelection.event; }1415private _primary: ICellRange = { start: 0, end: 0 };1617private _selections: ICellRange[] = [{ start: 0, end: 0 }];1819get selections(): ICellRange[] {20return this._selections;21}2223get focus(): ICellRange {24return this._primary;25}2627setState(primary: ICellRange | null, selections: ICellRange[], forceEventEmit: boolean, source: 'view' | 'model') {28const validPrimary = primary ?? { start: 0, end: 0 };29const validSelections = selections.length > 0 ? selections : [{ start: 0, end: 0 }];3031const changed = !cellRangesEqual([validPrimary], [this._primary]) || !cellRangesEqual(this._selections, validSelections);3233this._primary = validPrimary;34this._selections = validSelections;35if (changed || forceEventEmit) {36this._onDidChangeSelection.fire(source);37}38}3940setSelections(selections: ICellRange[], forceEventEmit: boolean, source: 'view' | 'model') {41this.setState(this._primary, selections, forceEventEmit, source);42}43}444546