store folding state in cell view model.

This commit is contained in:
rebornix
2020-08-24 10:51:47 -07:00
parent 86593ed18e
commit ec2dd67ae3
4 changed files with 25 additions and 20 deletions
@@ -7,7 +7,7 @@ import * as DOM from 'vs/base/browser/dom';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { CellDiffViewModel } from 'vs/workbench/contrib/notebook/browser/diff/celllDiffViewModel';
import { CellDiffViewModel, MetadataFoldingState } from 'vs/workbench/contrib/notebook/browser/diff/celllDiffViewModel';
import { CellDiffRenderTemplate, CellDiffViewModelLayoutChangeEvent, INotebookTextDiffEditor } from 'vs/workbench/contrib/notebook/browser/diff/common';
import { EDITOR_BOTTOM_PADDING, EDITOR_TOP_PADDING } from 'vs/workbench/contrib/notebook/browser/constants';
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
@@ -71,11 +71,6 @@ const fixedEditorOptions: IEditorOptions = {
renderValidationDecorations: 'on'
};
enum MetadataFoldingState {
Expanded,
Collapsed
}
abstract class AbstractCellRenderer extends Disposable {
protected _metadataHeaderContainer!: HTMLElement;
protected _metadataInfoContainer!: HTMLElement;
@@ -89,7 +84,6 @@ abstract class AbstractCellRenderer extends Disposable {
bodyMargin: number;
};
protected _foldingIndicator!: HTMLElement;
protected _foldingState!: MetadataFoldingState;
protected _metadataEditorContainer?: HTMLElement;
protected _metadataEditorDisposeStore!: DisposableStore;
protected _metadataEditor?: CodeEditorWidget;
@@ -114,7 +108,6 @@ abstract class AbstractCellRenderer extends Disposable {
bodyMargin: 16
};
this._metadataEditorDisposeStore = new DisposableStore();
this._foldingState = MetadataFoldingState.Collapsed;
this.initData();
this.buildBody(templateData.container);
this._register(cell.onDidLayoutChange(e => this.onDidLayoutChange(e)));
@@ -175,7 +168,7 @@ abstract class AbstractCellRenderer extends Disposable {
const cellViewModel = e.target;
if (cellViewModel === this.cell) {
this._foldingState = this._foldingState === MetadataFoldingState.Expanded ? MetadataFoldingState.Collapsed : MetadataFoldingState.Expanded;
this.cell.foldingState = this.cell.foldingState === MetadataFoldingState.Expanded ? MetadataFoldingState.Collapsed : MetadataFoldingState.Expanded;
this.updateMetadataRendering();
}
}
@@ -187,7 +180,7 @@ abstract class AbstractCellRenderer extends Disposable {
}
updateMetadataRendering() {
if (this._foldingState === MetadataFoldingState.Expanded) {
if (this.cell.foldingState === MetadataFoldingState.Expanded) {
// we should expand the metadata editor
this._metadataInfoContainer.style.display = 'block';
@@ -214,7 +207,7 @@ abstract class AbstractCellRenderer extends Disposable {
this.layout({ metadataEditor: true });
this._register(this._metadataEditor.onDidContentSizeChange((e) => {
if (e.contentHeightChanged && this._foldingState === MetadataFoldingState.Expanded) {
if (e.contentHeightChanged && this.cell.foldingState === MetadataFoldingState.Expanded) {
this._layoutInfo.metadataHeight = e.contentHeight;
this.layout({ metadataEditor: true });
}
@@ -236,7 +229,7 @@ abstract class AbstractCellRenderer extends Disposable {
}
private _updateFoldingIcon() {
if (this._foldingState === MetadataFoldingState.Collapsed) {
if (this.cell.foldingState === MetadataFoldingState.Collapsed) {
this._foldingIndicator.innerHTML = renderCodicons('$(chevron-right)');
} else {
this._foldingIndicator.innerHTML = renderCodicons('$(chevron-down)');
@@ -6,11 +6,16 @@
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
import { NotebookDiffEditorEventDispatcher } from 'vs/workbench/contrib/notebook/browser/viewModel/eventDispatcher';
import { Emitter } from 'vs/base/common/event';
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
import { Disposable } from 'vs/base/common/lifecycle';
import { CellDiffViewModelLayoutChangeEvent } from 'vs/workbench/contrib/notebook/browser/diff/common';
export enum MetadataFoldingState {
Expanded,
Collapsed
}
export class CellDiffViewModel extends Disposable {
public foldingState: MetadataFoldingState;
private _layoutInfoEmitter = new Emitter<CellDiffViewModelLayoutChangeEvent>();
onDidLayoutChange = this._layoutInfoEmitter.event;
@@ -22,6 +27,7 @@ export class CellDiffViewModel extends Disposable {
readonly editorEventDispatcher: NotebookDiffEditorEventDispatcher
) {
super();
this.foldingState = MetadataFoldingState.Collapsed;
this._register(this.editorEventDispatcher.onDidChangeLayout(e => {
this._layoutInfoEmitter.fire({ outerWidth: e.value.width });
@@ -7,6 +7,7 @@ import { NotebookLayoutInfo } from 'vs/workbench/contrib/notebook/browser/notebo
import { CellDiffViewModel } from 'vs/workbench/contrib/notebook/browser/diff/celllDiffViewModel';
import { Event } from 'vs/base/common/event';
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
import { DisposableStore } from 'vs/base/common/lifecycle';
export interface INotebookTextDiffEditor {
onMouseUp: Event<{ readonly event: MouseEvent; readonly target: CellDiffViewModel; }>;
@@ -16,7 +17,7 @@ export interface INotebookTextDiffEditor {
export interface CellDiffRenderTemplate {
readonly container: HTMLElement;
readonly elementDisposables: DisposableStore;
}
export interface CellDiffViewModelLayoutChangeEvent {
@@ -7,7 +7,7 @@ import 'vs/css!./notebookDiff';
import { IListRenderer, IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
import * as DOM from 'vs/base/browser/dom';
import { IListStyles, IStyleController } from 'vs/base/browser/ui/list/listWidget';
import { IDisposable } from 'vs/base/common/lifecycle';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
@@ -55,7 +55,8 @@ export class CellDiffRenderer implements IListRenderer<CellDiffViewModel, CellDi
renderTemplate(container: HTMLElement): CellDiffRenderTemplate {
return {
container
container,
elementDisposables: new DisposableStore()
};
}
@@ -63,16 +64,16 @@ export class CellDiffRenderer implements IListRenderer<CellDiffViewModel, CellDi
templateData.container.innerText = '';
switch (element.type) {
case 'unchanged':
this.instantiationService.createInstance(UnchangedCell, this.notebookEditor, element, templateData);
templateData.elementDisposables.add(this.instantiationService.createInstance(UnchangedCell, this.notebookEditor, element, templateData));
return;
case 'delete':
this.instantiationService.createInstance(DeletedCell, this.notebookEditor, element, templateData);
templateData.elementDisposables.add(this.instantiationService.createInstance(DeletedCell, this.notebookEditor, element, templateData));
return;
case 'insert':
this.instantiationService.createInstance(InsertCell, this.notebookEditor, element, templateData);
templateData.elementDisposables.add(this.instantiationService.createInstance(InsertCell, this.notebookEditor, element, templateData));
return;
case 'modified':
this.instantiationService.createInstance(ModifiedCell, this.notebookEditor, element, templateData);
templateData.elementDisposables.add(this.instantiationService.createInstance(ModifiedCell, this.notebookEditor, element, templateData));
return;
default:
break;
@@ -82,6 +83,10 @@ export class CellDiffRenderer implements IListRenderer<CellDiffViewModel, CellDi
disposeTemplate(templateData: CellDiffRenderTemplate): void {
templateData.container.innerText = '';
}
disposeElement(element: CellDiffViewModel, index: number, templateData: CellDiffRenderTemplate): void {
templateData.elementDisposables.clear();
}
}