提交 68f34021 编写于 作者: B Benjamin Pasero

input cleanup - extract binary editor model

上级 9be10be9
......@@ -9,6 +9,7 @@ import 'vs/css!./resourceviewer';
import nls = require('vs/nls');
import strings = require('vs/base/common/strings');
import mimes = require('vs/base/common/mime');
import URI from 'vs/base/common/uri';
import paths = require('vs/base/common/paths');
import {Builder, $} from 'vs/base/browser/builder';
import DOM = require('vs/base/browser/dom');
......@@ -69,14 +70,14 @@ const mapExtToMediaMimes = {
*/
export class ResourceViewer {
public static show(name: string, url: string, container: Builder, scrollbar?: IScrollableElement): void {
public static show(name: string, resource: URI, container: Builder, scrollbar?: IScrollableElement): void {
// Ensure CSS class
$(container).addClass('monaco-resource-viewer');
// Lookup media mime if any
let mime: string;
const ext = paths.extname(url);
const ext = paths.extname(resource.toString());
if (ext) {
mime = mapExtToMediaMimes[ext];
}
......@@ -91,7 +92,7 @@ export class ResourceViewer {
.empty()
.style({ paddingLeft: '20px' }) // restore CSS value in case the user saw a PDF before where we remove padding
.img({
src: url + '?' + new Date().getTime() // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
src: resource.toString() + '?' + new Date().getTime() // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
}).on(DOM.EventType.LOAD, () => {
if (scrollbar) {
scrollbar.onElementInternalDimensions();
......@@ -106,7 +107,7 @@ export class ResourceViewer {
.style({ padding: 0, margin: 0 }) // We really do not want any paddings or margins when displaying PDFs
.element('object')
.attr({
data: url + '?' + new Date().getTime(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
data: resource.toString() + '?' + new Date().getTime(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
width: '100%',
height: '100%',
type: mime
......@@ -120,7 +121,7 @@ export class ResourceViewer {
.style({ paddingLeft: '20px' }) // restore CSS value in case the user saw a PDF before where we remove padding
.element('audio')
.attr({
src: url + '?' + new Date().getTime(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
src: resource.toString() + '?' + new Date().getTime(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
text: nls.localize('missingAudioSupport', "Sorry but playback of audio files is not supported."),
controls: 'controls'
}).on(DOM.EventType.LOAD, () => {
......@@ -137,7 +138,7 @@ export class ResourceViewer {
.style({ paddingLeft: '20px' }) // restore CSS value in case the user saw a PDF before where we remove padding
.element('video')
.attr({
src: url + '?' + new Date().getTime(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
src: resource.toString() + '?' + new Date().getTime(), // We really want to avoid the browser from caching this resource, so we add a fake query param that is unique
text: nls.localize('missingVideoSupport', "Sorry but playback of video files is not supported."),
controls: 'controls'
}).on(DOM.EventType.LOAD, () => {
......
......@@ -8,6 +8,7 @@
import 'vs/css!./media/binarydiffeditor';
import {TPromise} from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import URI from 'vs/base/common/uri';
import {Sash, ISashEvent, IVerticalSashLayoutProvider} from 'vs/base/browser/ui/sash/sash';
import {Dimension, Builder, $} from 'vs/base/browser/builder';
import {ResourceViewer} from 'vs/base/browser/ui/resourceviewer/resourceViewer';
......@@ -15,7 +16,7 @@ import {IScrollableElement} from 'vs/base/browser/ui/scrollbar/scrollableElement
import {ScrollableElement} from 'vs/base/browser/ui/scrollbar/impl/scrollableElement';
import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor';
import {EditorInput, EditorOptions} from 'vs/workbench/common/editor';
import {BinaryResourceEditorModel} from 'vs/workbench/browser/parts/editor/resourceEditorModel';
import {BinaryEditorModel} from 'vs/workbench/browser/parts/editor/binaryEditorModel';
import {DiffEditorModel} from 'vs/workbench/browser/parts/editor/diffEditorModel';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
......@@ -96,7 +97,7 @@ export class BinaryResourceDiffEditor extends BaseEditor implements IVerticalSas
return this.editorService.resolveEditorModel(input, true /* Reload */).then((resolvedModel: DiffEditorModel) => {
// Assert model instance
if (!(resolvedModel.originalModel instanceof BinaryResourceEditorModel) || !(resolvedModel.modifiedModel instanceof BinaryResourceEditorModel)) {
if (!(resolvedModel.originalModel instanceof BinaryEditorModel) || !(resolvedModel.modifiedModel instanceof BinaryEditorModel)) {
return TPromise.wrapError<void>(nls.localize('cannotDiffTextToBinary', "Comparing binary files to non binary files is currently not supported"));
}
......@@ -106,16 +107,16 @@ export class BinaryResourceDiffEditor extends BaseEditor implements IVerticalSas
}
// Render original
let original = <BinaryResourceEditorModel>resolvedModel.originalModel;
this.renderInput(original.getName(), original.getUrl(), true);
let original = <BinaryEditorModel>resolvedModel.originalModel;
this.renderInput(original.getName(), original.getResource(), true);
// Render modified
let modified = <BinaryResourceEditorModel>resolvedModel.modifiedModel;
this.renderInput(modified.getName(), modified.getUrl(), false);
let modified = <BinaryEditorModel>resolvedModel.modifiedModel;
this.renderInput(modified.getName(), modified.getResource(), false);
});
}
private renderInput(name: string, url: string, isOriginal: boolean): void {
private renderInput(name: string, resource: URI, isOriginal: boolean): void {
// Reset Sash to default 50/50 ratio if needed
if (this.leftContainerWidth && this.dimension && this.leftContainerWidth !== this.dimension.width / 2) {
......@@ -128,7 +129,7 @@ export class BinaryResourceDiffEditor extends BaseEditor implements IVerticalSas
let container = isOriginal ? this.leftBinaryContainer : this.rightBinaryContainer;
let scrollbar = isOriginal ? this.leftScrollbar : this.rightScrollbar;
ResourceViewer.show(name, url, container, scrollbar);
ResourceViewer.show(name, resource, container, scrollbar);
}
public clearInput(): void {
......
......@@ -13,7 +13,7 @@ import {Dimension, Builder, $} from 'vs/base/browser/builder';
import {ResourceViewer} from 'vs/base/browser/ui/resourceviewer/resourceViewer';
import {EditorModel, EditorInput, EditorOptions} from 'vs/workbench/common/editor';
import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor';
import {BinaryResourceEditorModel} from 'vs/workbench/browser/parts/editor/resourceEditorModel';
import {BinaryEditorModel} from 'vs/workbench/browser/parts/editor/binaryEditorModel';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
......@@ -61,8 +61,8 @@ export abstract class BaseBinaryResourceEditor extends BaseEditor {
return this._editorService.resolveEditorModel(input, true /* Reload */).then((resolvedModel: EditorModel) => {
// Assert Model instance
if (!(resolvedModel instanceof BinaryResourceEditorModel)) {
return TPromise.wrapError<void>('Invalid editor input. Binary resource editor requires a model instance of BinaryResourceEditorModel.');
if (!(resolvedModel instanceof BinaryEditorModel)) {
return TPromise.wrapError<void>('Invalid editor input. Binary resource editor requires a model instance of BinaryEditorModel.');
}
// Assert that the current input is still the one we expect. This prevents a race condition when loading takes long and another input was set meanwhile
......@@ -71,8 +71,8 @@ export abstract class BaseBinaryResourceEditor extends BaseEditor {
}
// Render Input
let binaryResourceModel = <BinaryResourceEditorModel>resolvedModel;
ResourceViewer.show(binaryResourceModel.getName(), binaryResourceModel.getUrl(), this.binaryContainer);
let binaryResourceModel = <BinaryEditorModel>resolvedModel;
ResourceViewer.show(binaryResourceModel.getName(), binaryResourceModel.getResource(), this.binaryContainer);
return TPromise.as<void>(null);
});
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {EditorModel} from 'vs/workbench/common/editor';
import URI from 'vs/base/common/uri';
/**
* An editor model that just represents a URL and mime for a resource that can be loaded.
*/
export class BinaryEditorModel extends EditorModel {
private name: string;
private resource: URI;
constructor(resource: URI, name: string) {
super();
this.name = name;
this.resource = resource;
}
/**
* The name of the binary resource.
*/
public getName(): string {
return this.name;
}
/**
* The resource of the binary resource.
*/
public getResource(): URI {
return this.resource;
}
}
\ No newline at end of file
......@@ -10,7 +10,8 @@ import {isBinaryMime} from 'vs/base/common/mime';
import objects = require('vs/base/common/objects');
import {IEditorRegistry, Extensions} from 'vs/workbench/browser/parts/editor/baseEditor';
import {EditorModel, EditorInput} from 'vs/workbench/common/editor';
import {TextResourceEditorModel, BinaryResourceEditorModel} from 'vs/workbench/browser/parts/editor/resourceEditorModel';
import {TextResourceEditorModel} from 'vs/workbench/browser/parts/editor/resourceEditorModel';
import {BinaryEditorModel} from 'vs/workbench/browser/parts/editor/binaryEditorModel';
import URI from 'vs/base/common/uri';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
......@@ -141,7 +142,7 @@ export class ResourceEditorInput extends EditorInput {
// Binary model if editor is binary editor
let model: EditorModel;
if (descriptor.getId() === BINARY_EDITOR_ID) {
model = new BinaryResourceEditorModel(this.name, this.url);
model = new BinaryEditorModel(URI.parse(this.url), this.name);
}
// Otherwise use text model
......
......@@ -135,33 +135,4 @@ export class TextResourceEditorModel extends BaseTextEditorModel {
return TPromise.wrapError<EditorModel>(new errors.ConnectionError(error));
});
}
}
/**
* An editor model that just represents a URL and mime for a resource that can be loaded.
*/
export class BinaryResourceEditorModel extends EditorModel {
private name: string;
private url: string;
constructor(name: string, url: string) {
super();
this.name = name;
this.url = url;
}
/**
* The name of the binary resource.
*/
public getName(): string {
return this.name;
}
/**
* The url of the binary resource.
*/
public getUrl(): string {
return this.url;
}
}
\ No newline at end of file
......@@ -16,7 +16,7 @@ import strings = require('vs/base/common/strings');
import assert = require('vs/base/common/assert');
import {EditorModel, IInputStatus, EncodingMode} from 'vs/workbench/common/editor';
import {IEditorRegistry, Extensions, EditorDescriptor} from 'vs/workbench/browser/parts/editor/baseEditor';
import {BinaryResourceEditorModel} from 'vs/workbench/browser/parts/editor/resourceEditorModel';
import {BinaryEditorModel} from 'vs/workbench/browser/parts/editor/binaryEditorModel';
import {FileEditorDescriptor} from 'vs/workbench/parts/files/browser/files';
import {BINARY_FILE_EDITOR_ID, FILE_EDITOR_INPUT_ID, FileEditorInput as CommonFileEditorInput} from 'vs/workbench/parts/files/common/files';
import {CACHE, TextFileEditorModel, State} from 'vs/workbench/parts/files/browser/editors/textFileEditorModel';
......@@ -242,7 +242,7 @@ export class FileEditorInput extends CommonFileEditorInput {
FileEditorInput.FILE_EDITOR_MODEL_LOADERS[this.resource.toString()] = modelPromise;
}
return modelPromise.then((resolvedModel: TextFileEditorModel | BinaryResourceEditorModel) => {
return modelPromise.then((resolvedModel: TextFileEditorModel | BinaryEditorModel) => {
if (resolvedModel instanceof TextFileEditorModel) {
CACHE.add(this.resource, resolvedModel); // Store into the text model cache unless this file is binary
}
......@@ -278,7 +278,7 @@ export class FileEditorInput extends CommonFileEditorInput {
// Binary model if editor is binary editor
let model: EditorModel;
if (descriptor.getId() === BINARY_FILE_EDITOR_ID) {
model = new BinaryResourceEditorModel(this.getName(), this.resource.toString());
model = new BinaryEditorModel(this.resource, this.getName());
}
// Otherwise use text model
......
......@@ -17,7 +17,7 @@ import {SaveErrorHandler} from 'vs/workbench/parts/files/browser/saveErrorHandle
import {BaseTextEditor} from 'vs/workbench/browser/parts/editor/textEditor';
import {EditorInput, EditorOptions, TextEditorOptions, EditorModel} from 'vs/workbench/common/editor';
import {TextFileEditorModel} from 'vs/workbench/parts/files/browser/editors/textFileEditorModel';
import {BinaryResourceEditorModel} from 'vs/workbench/browser/parts/editor/resourceEditorModel';
import {BinaryEditorModel} from 'vs/workbench/browser/parts/editor/binaryEditorModel';
import {FileEditorInput} from 'vs/workbench/parts/files/browser/editors/fileEditorInput';
import {ExplorerViewlet} from 'vs/workbench/parts/files/browser/explorerViewlet';
import {IQuickOpenService} from 'vs/workbench/services/quickopen/browser/quickOpenService';
......@@ -102,9 +102,9 @@ export class TextFileEditor extends BaseTextEditor {
return this.editorService.resolveEditorModel(input, true /* Reload */).then((resolvedModel: EditorModel) => {
// There is a special case where the text editor has to handle binary file editor input: if a file with application/unknown
// mime has been resolved and cached before, it maybe an actual instance of BinaryResourceEditorModel. In this case our text
// mime has been resolved and cached before, it maybe an actual instance of BinaryEditorModel. In this case our text
// editor has to open this model using the binary editor. We return early in this case.
if (resolvedModel instanceof BinaryResourceEditorModel && this.openAsBinary(input, options)) {
if (resolvedModel instanceof BinaryEditorModel && this.openAsBinary(input, options)) {
return null;
}
......
......@@ -8,7 +8,7 @@
import * as assert from 'assert';
import {create} from 'vs/platform/instantiation/common/instantiationService';
import {ResourceEditorInput} from 'vs/workbench/browser/parts/editor/resourceEditorInput';
import {BinaryResourceEditorModel} from 'vs/workbench/browser/parts/editor/resourceEditorModel';
import {BinaryEditorModel} from 'vs/workbench/browser/parts/editor/binaryEditorModel';
import {BaseTextEditorModel} from 'vs/workbench/browser/parts/editor/textEditorModel';
import {MockRequestService, TestWorkspace, TestEditorService, TestContextService, TestMessageService, TestEventService} from 'vs/workbench/test/browser/servicesTestUtils';
import Severity from 'vs/base/common/severity';
......@@ -106,7 +106,7 @@ suite('Workbench - ResourceEditorInput', () => {
return c1.resolve().then((m1) => {
assert(m1.isResolved());
assert(m1 instanceof BinaryResourceEditorModel);
assert(m1 instanceof BinaryEditorModel);
done();
});
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册