提交 ffe09d36 编写于 作者: M Matt Bierner

Strict null work for webview

上级 7842634a
......@@ -23,7 +23,7 @@ function getActivePreviewsForResource(accessor: ServicesAccessor, resource: URI
return accessor.get(IEditorService).visibleControls
.filter(c => c instanceof HtmlPreviewPart && c.model)
.map(e => e as HtmlPreviewPart)
.filter(e => e.model.uri.scheme === uri.scheme && e.model.uri.toString() === uri.toString());
.filter(e => e.model!.uri.scheme === uri.scheme && e.model!.uri.toString() === uri.toString());
}
// --- Register Editor
......@@ -45,7 +45,7 @@ CommandsRegistry.registerCommand('_workbench.previewHtml', function (
const uri = resource instanceof URI ? resource : URI.parse(resource);
label = label || uri.fsPath;
let input: HtmlInput;
let input: HtmlInput | undefined;
const editorGroupService = accessor.get(IEditorGroupsService);
......
......@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { ITextModel } from 'vs/editor/common/model';
import { Disposable, IDisposable, dispose, IReference } from 'vs/base/common/lifecycle';
import { EditorOptions, EditorInput, IEditorMemento } from 'vs/workbench/common/editor';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
......@@ -38,8 +37,8 @@ export class HtmlPreviewPart extends BaseWebviewEditor {
private _webviewDisposables: IDisposable[];
private _modelRef: IReference<ITextEditorModel>;
public get model(): ITextModel { return this._modelRef && this._modelRef.object.textEditorModel; }
private _modelRef?: IReference<ITextEditorModel>;
public get model() { return this._modelRef ? this._modelRef.object.textEditorModel : undefined; }
private _modelChangeSubscription = Disposable.None;
private _themeChangeSubscription = Disposable.None;
......@@ -138,14 +137,14 @@ export class HtmlPreviewPart extends BaseWebviewEditor {
this._themeChangeSubscription = this.themeService.onThemeChange(this.onThemeChange.bind(this));
if (this._hasValidModel()) {
this._modelChangeSubscription = this.model.onDidChangeContent(() => this.webview.contents = this.model.getLinesContent().join('\n'));
this.webview.contents = this.model.getLinesContent().join('\n');
this._modelChangeSubscription = this.model!.onDidChangeContent(() => this.webview.contents = this.model!.getLinesContent().join('\n'));
this.webview.contents = this.model!.getLinesContent().join('\n');
}
}
}
private _hasValidModel(): boolean {
return this._modelRef && this.model && !this.model.isDisposed();
return !!(this._modelRef && this.model && !this.model.isDisposed());
}
public layout(dimension: Dimension): void {
......@@ -248,10 +247,10 @@ export class HtmlPreviewPart extends BaseWebviewEditor {
}
private saveHTMLPreviewViewState(input: HtmlInput, editorViewState: HtmlPreviewEditorViewState): void {
this.editorMemento.saveEditorState(this.group, input, editorViewState);
this.editorMemento.saveEditorState(this.group!, input, editorViewState);
}
private loadHTMLPreviewViewState(input: HtmlInput): HtmlPreviewEditorViewState {
return this.editorMemento.loadEditorState(this.group, input);
private loadHTMLPreviewViewState(input: HtmlInput): HtmlPreviewEditorViewState | undefined {
return this.editorMemento.loadEditorState(this.group!, input);
}
}
......@@ -28,7 +28,7 @@ export class WebviewEditor extends BaseWebviewEditor {
public static readonly ID = 'WebviewEditor';
private _editorFrame: HTMLElement;
private _content: HTMLElement;
private _content?: HTMLElement;
private _webviewContent: HTMLElement | undefined;
private _webviewFocusTrackerDisposables: IDisposable[] = [];
......@@ -171,8 +171,9 @@ export class WebviewEditor extends BaseWebviewEditor {
if (token.isCancellationRequested) {
return;
}
input.updateGroup(this.group.id);
if (this.group) {
input.updateGroup(this.group.id);
}
this.updateWebview(input);
});
}
......@@ -187,7 +188,7 @@ export class WebviewEditor extends BaseWebviewEditor {
useSameOriginForRoot: false,
localResourceRoots: input.options.localResourceRoots || this.getDefaultLocalResourceRoots(),
extensionLocation: input.extensionLocation
}, input.options.retainContextWhenHidden);
}, !!input.options.retainContextWhenHidden);
if (this._webviewContent) {
this._webviewContent.style.visibility = 'visible';
......@@ -198,8 +199,9 @@ export class WebviewEditor extends BaseWebviewEditor {
private getDefaultLocalResourceRoots(): URI[] {
const rootPaths = this._contextService.getWorkspace().folders.map(x => x.uri);
if ((this.input as WebviewEditorInput).extensionLocation) {
rootPaths.push((this.input as WebviewEditorInput).extensionLocation);
const extensionLocation = (this.input as WebviewEditorInput).extensionLocation;
if (extensionLocation) {
rootPaths.push(extensionLocation);
}
return rootPaths;
}
......@@ -235,7 +237,7 @@ export class WebviewEditor extends BaseWebviewEditor {
this._webview.state = input.webviewState;
this._content.setAttribute('aria-flowto', this._webviewContent.id);
this._content!.setAttribute('aria-flowto', this._webviewContent.id);
this.doUpdateContainer();
}
......@@ -254,11 +256,11 @@ export class WebviewEditor extends BaseWebviewEditor {
this._webviewFocusTrackerDisposables = dispose(this._webviewFocusTrackerDisposables);
// Track focus in webview content
const webviewContentFocusTracker = DOM.trackFocus(this._webviewContent);
const webviewContentFocusTracker = DOM.trackFocus(this._webviewContent!);
this._webviewFocusTrackerDisposables.push(webviewContentFocusTracker);
this._webviewFocusTrackerDisposables.push(webviewContentFocusTracker.onDidFocus(() => this._onDidFocusWebview.fire()));
// Track focus in webview element
this._webviewFocusTrackerDisposables.push(this._webview.onDidFocus(() => this._onDidFocusWebview.fire()));
this._webviewFocusTrackerDisposables.push(this._webview!.onDidFocus(() => this._onDidFocusWebview.fire()));
}
}
......@@ -56,7 +56,7 @@ export class WebviewEditorInput extends EditorInput {
private _html: string = '';
private _currentWebviewHtml: string = '';
public _events: WebviewEvents | undefined;
private _container: HTMLElement;
private _container?: HTMLElement;
private _webview: WebviewElement | undefined;
private _webviewOwner: any;
private _webviewDisposables: IDisposable[] = [];
......@@ -139,7 +139,7 @@ export class WebviewEditorInput extends EditorInput {
return this.getName();
}
public getDescription(): string {
public getDescription() {
return null;
}
......@@ -240,10 +240,13 @@ export class WebviewEditorInput extends EditorInput {
return this._webview;
}
public set webview(value: WebviewElement) {
public set webview(value: WebviewElement | undefined) {
this._webviewDisposables = dispose(this._webviewDisposables);
this._webview = value;
if (!this._webview) {
return;
}
this._webview.onDidClickLink(link => {
if (this._events && this._events.onDidClickLink) {
......
......@@ -44,7 +44,7 @@ interface IKeydownEvent {
class WebviewProtocolProvider extends Disposable {
constructor(
webview: Electron.WebviewTag,
private readonly _extensionLocation: URI,
private readonly _extensionLocation: URI | undefined,
private readonly _getLocalResourceRoots: () => ReadonlyArray<URI>,
private readonly _environmentService: IEnvironmentService,
private readonly _fileService: IFileService,
......@@ -356,7 +356,7 @@ export class WebviewElement extends Disposable {
}
public mountTo(parent: HTMLElement) {
parent.appendChild(this._webviewFindWidget.getDomNode());
parent.appendChild(this._webviewFindWidget.getDomNode()!);
parent.appendChild(this._webview);
}
......
......@@ -11,7 +11,7 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
export class WebviewFindWidget extends SimpleFindWidget {
constructor(
private _webview: WebviewElement,
private _webview: WebviewElement | undefined,
@IContextViewService contextViewService: IContextViewService,
@IContextKeyService contextKeyService: IContextKeyService
) {
......@@ -24,6 +24,9 @@ export class WebviewFindWidget extends SimpleFindWidget {
}
public find(previous: boolean) {
if (!this._webview) {
return;
}
const val = this.inputValue;
if (val) {
this._webview.find(val, { findNext: true, forward: !previous });
......@@ -32,11 +35,16 @@ export class WebviewFindWidget extends SimpleFindWidget {
public hide() {
super.hide();
this._webview.stopFind(true);
this._webview.focus();
if (this._webview) {
this._webview.stopFind(true);
this._webview.focus();
}
}
public onInputChanged() {
if (!this._webview) {
return;
}
const val = this.inputValue;
if (val) {
this._webview.startFind(val);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册