Skip to content

Commit

Permalink
group cell listeners.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Nov 12, 2021
1 parent 0c6e106 commit 98550aa
Showing 1 changed file with 54 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,72 +46,106 @@ export class CodeCell extends Disposable {
) {
super();

const width = this.viewCell.layoutInfo.editorWidth;
const editorHeight = this.calculateInitEditorHeight();
this.initializeEditor(editorHeight);
this.registerEditorOptionsListener();
this.registerViewCellStateChange();
this.registerFocusModeTracker();
this.registerEditorLayoutListeners();
this.registerDecorations();
this.registerMouseListener();

// Render Outputs
this._outputContainerRenderer = this.instantiationService.createInstance(CellOutputContainer, notebookEditor, viewCell, templateData, { limit: 500 });
this._outputContainerRenderer.render(editorHeight);
// Need to do this after the intial renderOutput
if (this.viewCell.isOutputCollapsed === undefined && this.viewCell.isInputCollapsed === undefined) {
this.initialViewUpdateExpanded();
this.viewCell.layoutChange({});
}

this._register(this.viewCell.onLayoutInfoRead(() => {
this._outputContainerRenderer.probeHeight();
}));

this.updateForCollapseState();
}

private calculateInitEditorHeight() {
const lineNum = this.viewCell.lineCount;
const lineHeight = this.viewCell.layoutInfo.fontInfo?.lineHeight || 17;
const editorPadding = this.notebookEditor.notebookOptions.computeEditorPadding(this.viewCell.internalMetadata);

const editorHeight = this.viewCell.layoutInfo.editorHeight === 0
? lineNum * lineHeight + editorPadding.top + editorPadding.bottom
: this.viewCell.layoutInfo.editorHeight;
return editorHeight;
}

private initializeEditor(initEditorHeight: number) {
const width = this.viewCell.layoutInfo.editorWidth;
this.layoutEditor(
{
width: width,
height: editorHeight
height: initEditorHeight
}
);

const cts = new CancellationTokenSource();
this._register({ dispose() { cts.dispose(true); } });
raceCancellation(viewCell.resolveTextModel(), cts.token).then(model => {
raceCancellation(this.viewCell.resolveTextModel(), cts.token).then(model => {
if (this._isDisposed) {
return;
}

if (model && templateData.editor) {
templateData.editor.setModel(model);
viewCell.attachTextEditor(templateData.editor);
if (model && this.templateData.editor) {
this.templateData.editor.setModel(model);
this.viewCell.attachTextEditor(this.templateData.editor);
const focusEditorIfNeeded = () => {
if (
notebookEditor.getActiveCell() === viewCell &&
viewCell.focusMode === CellFocusMode.Editor &&
this.notebookEditor.getActiveCell() === this.viewCell &&
this.viewCell.focusMode === CellFocusMode.Editor &&
(this.notebookEditor.hasEditorFocus() || document.activeElement === document.body)) // Don't steal focus from other workbench parts, but if body has focus, we can take it
{
templateData.editor?.focus();
this.templateData.editor?.focus();
}
};
focusEditorIfNeeded();

const realContentHeight = templateData.editor?.getContentHeight();
if (realContentHeight !== undefined && realContentHeight !== editorHeight) {
const realContentHeight = this.templateData.editor?.getContentHeight();
if (realContentHeight !== undefined && realContentHeight !== initEditorHeight) {
this.onCellEditorHeightChange(realContentHeight);
}

focusEditorIfNeeded();
}
});
}

private registerEditorOptionsListener() {
const updateEditorOptions = () => {
const editor = templateData.editor;
const editor = this.templateData.editor;
if (!editor) {
return;
}

const isReadonly = notebookEditor.isReadOnly;
const padding = notebookEditor.notebookOptions.computeEditorPadding(viewCell.internalMetadata);
const isReadonly = this.notebookEditor.isReadOnly;
const padding = this.notebookEditor.notebookOptions.computeEditorPadding(this.viewCell.internalMetadata);
const options = editor.getOptions();
if (options.get(EditorOption.readOnly) !== isReadonly || options.get(EditorOption.padding) !== padding) {
editor.updateOptions({ readOnly: notebookEditor.isReadOnly, padding: notebookEditor.notebookOptions.computeEditorPadding(viewCell.internalMetadata) });
editor.updateOptions({ readOnly: this.notebookEditor.isReadOnly, padding: this.notebookEditor.notebookOptions.computeEditorPadding(this.viewCell.internalMetadata) });
}
};

updateEditorOptions();
this._register(viewCell.onDidChangeState((e) => {
this._register(this.viewCell.onDidChangeState((e) => {
if (e.metadataChanged || e.internalMetadataChanged) {
updateEditorOptions();
}
}));
}

private registerViewCellStateChange() {
this._register(this.viewCell.onDidChangeState((e) => {
if (e.inputCollapsedChanged || e.outputCollapsedChanged) {
this.viewCell.pauseLayout();
const updated = this.updateForCollapseState();
Expand All @@ -122,10 +156,10 @@ export class CodeCell extends Disposable {
}
}));

this._register(viewCell.onDidChangeLayout((e) => {
this._register(this.viewCell.onDidChangeLayout((e) => {
if (e.outerWidth !== undefined) {
const layoutInfo = templateData.editor.getLayoutInfo();
if (layoutInfo.width !== viewCell.layoutInfo.editorWidth) {
const layoutInfo = this.templateData.editor.getLayoutInfo();
if (layoutInfo.width !== this.viewCell.layoutInfo.editorWidth) {
this.onCellWidthChange();
}
}
Expand All @@ -134,26 +168,6 @@ export class CodeCell extends Disposable {
this.relayoutCell();
}
}));

this.registerFocusModeTracker();
this.registerEditorLayoutListeners();
this.registerDecorations();
this.registerMouseListener();

// Render Outputs
this._outputContainerRenderer = this.instantiationService.createInstance(CellOutputContainer, notebookEditor, viewCell, templateData, { limit: 500 });
this._outputContainerRenderer.render(editorHeight);
// Need to do this after the intial renderOutput
if (this.viewCell.isOutputCollapsed === undefined && this.viewCell.isInputCollapsed === undefined) {
this.initialViewUpdateExpanded();
this.viewCell.layoutChange({});
}

this._register(this.viewCell.onLayoutInfoRead(() => {
this._outputContainerRenderer.probeHeight();
}));

this.updateForCollapseState();
}

private registerEditorLayoutListeners() {
Expand Down

0 comments on commit 98550aa

Please sign in to comment.