Maddy/edit mode events cleanup (#17636)

* remove multiple events

* correct preview check

* add test
This commit is contained in:
Maddy
2021-11-10 10:59:29 -08:00
committed by GitHub
parent b6047ad87d
commit 1d3debb897
6 changed files with 52 additions and 33 deletions

View File

@@ -7,7 +7,7 @@ import 'vs/css!./code';
import { OnInit, Component, Input, Inject, ElementRef, ViewChild, Output, EventEmitter, OnChanges, SimpleChange, forwardRef, ChangeDetectorRef } from '@angular/core';
import { QueryTextEditor } from 'sql/workbench/browser/modelComponents/queryTextEditor';
import { ICellModel, CellExecutionState } from 'sql/workbench/services/notebook/browser/models/modelInterfaces';
import { ICellModel, CellExecutionState, CellEditModes } from 'sql/workbench/services/notebook/browser/models/modelInterfaces';
import { Taskbar } from 'sql/base/browser/ui/taskbar/taskbar';
import { RunCellAction, CellContext } from 'sql/workbench/contrib/notebook/browser/cellViews/codeActions';
import { NotebookModel } from 'sql/workbench/services/notebook/browser/models/notebookModel';
@@ -262,8 +262,9 @@ export class CodeComponent extends CellView implements OnInit, OnChanges {
this._register(this.cellModel.onCollapseStateChanged(isCollapsed => {
this.onCellCollapse(isCollapsed);
}));
this._register(this.cellModel.onCellPreviewModeChanged((e) => {
if (!e && this._cellModel.cellSourceChanged) {
this._register(this.cellModel.onCurrentEditModeChanged((e) => {
let preview = e !== CellEditModes.MARKDOWN;
if (!preview && this._cellModel.cellSourceChanged) {
this.updateModel();
this._cellModel.cellSourceChanged = false;
}

View File

@@ -215,7 +215,15 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges {
}
this._changeRef.detectChanges();
}));
this._register(this.cellModel.onCellPreviewModeChanged(preview => {
this._register(this.cellModel.onCurrentEditModeChanged(editMode => {
let markdown: boolean = editMode !== CellEditModes.WYSIWYG;
if (!markdown) {
let editorControl = this.cellEditors.length > 0 ? this.cellEditors[0].getEditor().getControl() : undefined;
if (editorControl) {
let selection = editorControl.getSelection();
this.cellModel.markdownCursorPosition = selection?.getPosition();
}
}
// On preview mode change, get the cursor position (get the position only when the selection node is a text node)
if (window.getSelection() && window.getSelection().focusNode?.nodeName === '#text' && window.getSelection().getRangeAt(0)) {
let selection = window.getSelection().getRangeAt(0);
@@ -247,17 +255,7 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges {
this.cellModel.richTextCursorPosition = cursorPosition;
}
}
this.previewMode = preview;
this.focusIfPreviewMode();
}));
this._register(this.cellModel.onCellMarkdownModeChanged(markdown => {
if (!markdown) {
let editorControl = this.cellEditors.length > 0 ? this.cellEditors[0].getEditor().getControl() : undefined;
if (editorControl) {
let selection = editorControl.getSelection();
this.cellModel.markdownCursorPosition = selection?.getPosition();
}
}
this.previewMode = editMode !== CellEditModes.MARKDOWN;
this.markdownMode = markdown;
this.focusIfPreviewMode();
}));

View File

@@ -27,7 +27,7 @@ import { NotebookFindNextAction, NotebookFindPreviousAction } from 'sql/workbenc
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService';
import { INotebookModel } from 'sql/workbench/services/notebook/browser/models/modelInterfaces';
import { CellEditModes, INotebookModel } from 'sql/workbench/services/notebook/browser/models/modelInterfaces';
import { INotebookFindModel } from 'sql/workbench/contrib/notebook/browser/models/notebookFindModel';
import { IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { IModelDecorationsChangeAccessor, IModelDeltaDecoration } from 'vs/editor/common/model';
@@ -380,8 +380,8 @@ export class NotebookEditor extends EditorPane implements IFindNotebookControlle
this._onFindStateChange(changeEvent).catch(onUnexpectedError);
}
}));
this._register(cell.onCellMarkdownModeChanged(e => {
if (e) {
this._register(cell.onCurrentEditModeChanged(editMode => {
if (editMode !== CellEditModes.WYSIWYG) {
this._onFindStateChange(changeEvent).catch(onUnexpectedError);
}
}));