Skip to content

Commit 98550aa

Browse files
committed
group cell listeners.
1 parent 0c6e106 commit 98550aa

File tree

1 file changed

+54
-40
lines changed
  • src/vs/workbench/contrib/notebook/browser/view/renderers

1 file changed

+54
-40
lines changed

src/vs/workbench/contrib/notebook/browser/view/renderers/codeCell.ts

+54-40
Original file line numberDiff line numberDiff line change
@@ -46,72 +46,106 @@ export class CodeCell extends Disposable {
4646
) {
4747
super();
4848

49-
const width = this.viewCell.layoutInfo.editorWidth;
49+
const editorHeight = this.calculateInitEditorHeight();
50+
this.initializeEditor(editorHeight);
51+
this.registerEditorOptionsListener();
52+
this.registerViewCellStateChange();
53+
this.registerFocusModeTracker();
54+
this.registerEditorLayoutListeners();
55+
this.registerDecorations();
56+
this.registerMouseListener();
57+
58+
// Render Outputs
59+
this._outputContainerRenderer = this.instantiationService.createInstance(CellOutputContainer, notebookEditor, viewCell, templateData, { limit: 500 });
60+
this._outputContainerRenderer.render(editorHeight);
61+
// Need to do this after the intial renderOutput
62+
if (this.viewCell.isOutputCollapsed === undefined && this.viewCell.isInputCollapsed === undefined) {
63+
this.initialViewUpdateExpanded();
64+
this.viewCell.layoutChange({});
65+
}
66+
67+
this._register(this.viewCell.onLayoutInfoRead(() => {
68+
this._outputContainerRenderer.probeHeight();
69+
}));
70+
71+
this.updateForCollapseState();
72+
}
73+
74+
private calculateInitEditorHeight() {
5075
const lineNum = this.viewCell.lineCount;
5176
const lineHeight = this.viewCell.layoutInfo.fontInfo?.lineHeight || 17;
5277
const editorPadding = this.notebookEditor.notebookOptions.computeEditorPadding(this.viewCell.internalMetadata);
53-
5478
const editorHeight = this.viewCell.layoutInfo.editorHeight === 0
5579
? lineNum * lineHeight + editorPadding.top + editorPadding.bottom
5680
: this.viewCell.layoutInfo.editorHeight;
81+
return editorHeight;
82+
}
5783

84+
private initializeEditor(initEditorHeight: number) {
85+
const width = this.viewCell.layoutInfo.editorWidth;
5886
this.layoutEditor(
5987
{
6088
width: width,
61-
height: editorHeight
89+
height: initEditorHeight
6290
}
6391
);
6492

6593
const cts = new CancellationTokenSource();
6694
this._register({ dispose() { cts.dispose(true); } });
67-
raceCancellation(viewCell.resolveTextModel(), cts.token).then(model => {
95+
raceCancellation(this.viewCell.resolveTextModel(), cts.token).then(model => {
6896
if (this._isDisposed) {
6997
return;
7098
}
7199

72-
if (model && templateData.editor) {
73-
templateData.editor.setModel(model);
74-
viewCell.attachTextEditor(templateData.editor);
100+
if (model && this.templateData.editor) {
101+
this.templateData.editor.setModel(model);
102+
this.viewCell.attachTextEditor(this.templateData.editor);
75103
const focusEditorIfNeeded = () => {
76104
if (
77-
notebookEditor.getActiveCell() === viewCell &&
78-
viewCell.focusMode === CellFocusMode.Editor &&
105+
this.notebookEditor.getActiveCell() === this.viewCell &&
106+
this.viewCell.focusMode === CellFocusMode.Editor &&
79107
(this.notebookEditor.hasEditorFocus() || document.activeElement === document.body)) // Don't steal focus from other workbench parts, but if body has focus, we can take it
80108
{
81-
templateData.editor?.focus();
109+
this.templateData.editor?.focus();
82110
}
83111
};
84112
focusEditorIfNeeded();
85113

86-
const realContentHeight = templateData.editor?.getContentHeight();
87-
if (realContentHeight !== undefined && realContentHeight !== editorHeight) {
114+
const realContentHeight = this.templateData.editor?.getContentHeight();
115+
if (realContentHeight !== undefined && realContentHeight !== initEditorHeight) {
88116
this.onCellEditorHeightChange(realContentHeight);
89117
}
90118

91119
focusEditorIfNeeded();
92120
}
93121
});
122+
}
94123

124+
private registerEditorOptionsListener() {
95125
const updateEditorOptions = () => {
96-
const editor = templateData.editor;
126+
const editor = this.templateData.editor;
97127
if (!editor) {
98128
return;
99129
}
100130

101-
const isReadonly = notebookEditor.isReadOnly;
102-
const padding = notebookEditor.notebookOptions.computeEditorPadding(viewCell.internalMetadata);
131+
const isReadonly = this.notebookEditor.isReadOnly;
132+
const padding = this.notebookEditor.notebookOptions.computeEditorPadding(this.viewCell.internalMetadata);
103133
const options = editor.getOptions();
104134
if (options.get(EditorOption.readOnly) !== isReadonly || options.get(EditorOption.padding) !== padding) {
105-
editor.updateOptions({ readOnly: notebookEditor.isReadOnly, padding: notebookEditor.notebookOptions.computeEditorPadding(viewCell.internalMetadata) });
135+
editor.updateOptions({ readOnly: this.notebookEditor.isReadOnly, padding: this.notebookEditor.notebookOptions.computeEditorPadding(this.viewCell.internalMetadata) });
106136
}
107137
};
108138

109139
updateEditorOptions();
110-
this._register(viewCell.onDidChangeState((e) => {
140+
this._register(this.viewCell.onDidChangeState((e) => {
111141
if (e.metadataChanged || e.internalMetadataChanged) {
112142
updateEditorOptions();
113143
}
144+
}));
145+
}
114146

147+
private registerViewCellStateChange() {
148+
this._register(this.viewCell.onDidChangeState((e) => {
115149
if (e.inputCollapsedChanged || e.outputCollapsedChanged) {
116150
this.viewCell.pauseLayout();
117151
const updated = this.updateForCollapseState();
@@ -122,10 +156,10 @@ export class CodeCell extends Disposable {
122156
}
123157
}));
124158

125-
this._register(viewCell.onDidChangeLayout((e) => {
159+
this._register(this.viewCell.onDidChangeLayout((e) => {
126160
if (e.outerWidth !== undefined) {
127-
const layoutInfo = templateData.editor.getLayoutInfo();
128-
if (layoutInfo.width !== viewCell.layoutInfo.editorWidth) {
161+
const layoutInfo = this.templateData.editor.getLayoutInfo();
162+
if (layoutInfo.width !== this.viewCell.layoutInfo.editorWidth) {
129163
this.onCellWidthChange();
130164
}
131165
}
@@ -134,26 +168,6 @@ export class CodeCell extends Disposable {
134168
this.relayoutCell();
135169
}
136170
}));
137-
138-
this.registerFocusModeTracker();
139-
this.registerEditorLayoutListeners();
140-
this.registerDecorations();
141-
this.registerMouseListener();
142-
143-
// Render Outputs
144-
this._outputContainerRenderer = this.instantiationService.createInstance(CellOutputContainer, notebookEditor, viewCell, templateData, { limit: 500 });
145-
this._outputContainerRenderer.render(editorHeight);
146-
// Need to do this after the intial renderOutput
147-
if (this.viewCell.isOutputCollapsed === undefined && this.viewCell.isInputCollapsed === undefined) {
148-
this.initialViewUpdateExpanded();
149-
this.viewCell.layoutChange({});
150-
}
151-
152-
this._register(this.viewCell.onLayoutInfoRead(() => {
153-
this._outputContainerRenderer.probeHeight();
154-
}));
155-
156-
this.updateForCollapseState();
157171
}
158172

159173
private registerEditorLayoutListeners() {

0 commit comments

Comments
 (0)