Set notebook as trusted after executing a cell. (#9108)

* Also set trusted by default when opening a notebook without any code cells.
This commit is contained in:
Cory Rivera
2020-02-10 17:03:57 -08:00
committed by GitHub
parent 45341d786b
commit 3a8b74a311
4 changed files with 18 additions and 6 deletions

View File

@@ -339,6 +339,8 @@ export class CellModel implements ICellModel {
}
let content = this.source;
if ((Array.isArray(content) && content.length > 0) || (!Array.isArray(content) && content)) {
this.notebookModel.trustedMode = true;
// requestExecute expects a string for the code parameter
content = Array.isArray(content) ? content.join('') : content;
const future = kernel.requestExecute({

View File

@@ -308,6 +308,11 @@ export class NotebookModel extends Disposable implements INotebookModel {
});
}
}
// Trust notebook by default if there are no code cells
if (this._cells.length === 0 || this._cells.every(cell => cell.cellType === CellTypes.Markdown)) {
this.trustedMode = true;
}
} catch (error) {
this._inErrorState = true;
throw error;

View File

@@ -21,8 +21,8 @@ import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import * as DOM from 'vs/base/browser/dom';
import { AngularDisposable } from 'sql/base/browser/lifecycle';
import { CellTypes, CellType } from 'sql/workbench/contrib/notebook/common/models/contracts';
import { ICellModel, IModelFactory, INotebookModel } from 'sql/workbench/contrib/notebook/browser/models/modelInterfaces';
import { CellTypes, CellType, NotebookChangeType } from 'sql/workbench/contrib/notebook/common/models/contracts';
import { ICellModel, IModelFactory, INotebookModel, NotebookContentChange } from 'sql/workbench/contrib/notebook/browser/models/modelInterfaces';
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
import { INotebookService, INotebookParams, INotebookManager, INotebookEditor, DEFAULT_NOTEBOOK_PROVIDER, SQL_NOTEBOOK_PROVIDER, INotebookSection, INavigationProvider, ICellEditorProvider } from 'sql/workbench/services/notebook/browser/notebookService';
import { NotebookModel } from 'sql/workbench/contrib/notebook/browser/models/notebookModel';
@@ -309,7 +309,7 @@ export class NotebookComponent extends AngularDisposable implements OnInit, OnDe
}, this.profile, this.logService, this.notificationService, this.telemetryService);
let trusted = await this.notebookService.isNotebookTrustCached(this._notebookParams.notebookUri, this.isDirty());
this._register(model.onError((errInfo: INotification) => this.handleModelError(errInfo)));
this._register(model.contentChanged((change) => this.handleContentChanged()));
this._register(model.contentChanged((change) => this.handleContentChanged(change)));
this._register(model.onProviderIdChange((provider) => this.handleProviderIdChanged(provider)));
this._register(model.kernelChanged((kernelArgs) => this.handleKernelChanged(kernelArgs)));
this._model = this._register(model);
@@ -360,7 +360,11 @@ export class NotebookComponent extends AngularDisposable implements OnInit, OnDe
this.notificationService.notify(notification);
}
private handleContentChanged() {
private handleContentChanged(change: NotebookContentChange) {
if (change.changeType === NotebookChangeType.TrustChanged) {
this._trustedAction.trusted = this._model.trustedMode;
}
// Note: for now we just need to set dirty state and refresh the UI.
this.detectChanges();
}

View File

@@ -155,8 +155,9 @@ suite('notebook model', function (): void {
// Then I expect to have 0 code cell as the contents
assert.equal(model.cells.length, 0);
// And Trust should be false by default
assert(!model.trustedMode);
// And Trust should be true by default if there are no cells
assert(model.trustedMode);
});
test('Should use trusted state set in model load', async function (): Promise<void> {