提交 3cc9e7f3 编写于 作者: A Alex Dima

Fixes Microsoft/monaco-editor#385

上级 0ff45fcd
......@@ -54,28 +54,29 @@ export interface IStandaloneDiffEditor extends IDiffEditor {
addCommand(keybinding: number, handler: ICommandHandler, context: string): string;
createContextKey<T>(key: string, defaultValue: T): IContextKey<T>;
addAction(descriptor: IActionDescriptor): IDisposable;
getOriginalEditor(): IStandaloneCodeEditor;
getModifiedEditor(): IStandaloneCodeEditor;
}
let LAST_GENERATED_COMMAND_ID = 0;
export class StandaloneEditor extends CodeEditor implements IStandaloneCodeEditor {
/**
* A code editor to be used both by the standalone editor and the standalone diff editor.
*/
export class StandaloneCodeEditor extends CodeEditor implements IStandaloneCodeEditor {
private _standaloneKeybindingService: StandaloneKeybindingService;
private _standaloneColorService: IStandaloneColorService;
private _contextViewService: IEditorContextViewService;
private _ownsModel: boolean;
private _toDispose2: IDisposable[];
constructor(
domElement: HTMLElement,
options: IEditorConstructionOptions,
toDispose: IDisposable,
@IInstantiationService instantiationService: IInstantiationService,
@ICodeEditorService codeEditorService: ICodeEditorService,
@ICommandService commandService: ICommandService,
@IContextKeyService contextKeyService: IContextKeyService,
@IKeybindingService keybindingService: IKeybindingService,
@IContextViewService contextViewService: IContextViewService,
@IStandaloneColorService standaloneColorService: IStandaloneColorService
) {
options = options || {};
......@@ -83,42 +84,11 @@ export class StandaloneEditor extends CodeEditor implements IStandaloneCodeEdito
options.theme = standaloneColorService.setTheme(options.theme);
}
super(domElement, options, instantiationService, codeEditorService, commandService, contextKeyService);
this._standaloneColorService = standaloneColorService;
this._standaloneColorService = standaloneColorService;
if (keybindingService instanceof StandaloneKeybindingService) {
this._standaloneKeybindingService = keybindingService;
}
this._contextViewService = <IEditorContextViewService>contextViewService;
this._toDispose2 = [toDispose];
let model: IModel = null;
if (typeof options.model === 'undefined') {
model = (<any>self).monaco.editor.createModel(options.value || '', options.language || 'text/plain');
this._ownsModel = true;
} else {
model = options.model;
delete options.model;
this._ownsModel = false;
}
this._attachModel(model);
if (model) {
let e: IModelChangedEvent = {
oldModelUrl: null,
newModelUrl: model.uri
};
this.emit(EventType.ModelChanged, e);
}
}
public dispose(): void {
super.dispose();
this._toDispose2 = dispose(this._toDispose2);
}
public destroy(): void {
this.dispose();
}
public updateOptions(newOptions: IEditorOptions): void {
......@@ -140,10 +110,6 @@ export class StandaloneEditor extends CodeEditor implements IStandaloneCodeEdito
}
public createContextKey<T>(key: string, defaultValue: T): IContextKey<T> {
if (!this._standaloneKeybindingService) {
console.warn('Cannot create context key because the editor is configured with an unrecognized KeybindingService');
return null;
}
return this._contextKeyService.createKey(key, defaultValue);
}
......@@ -170,6 +136,60 @@ export class StandaloneEditor extends CodeEditor implements IStandaloneCodeEdito
}
return combinedDisposable(toDispose);
}
}
export class StandaloneEditor extends StandaloneCodeEditor implements IStandaloneCodeEditor {
private _contextViewService: IEditorContextViewService;
private _ownsModel: boolean;
private _toDispose2: IDisposable[];
constructor(
domElement: HTMLElement,
options: IEditorConstructionOptions,
toDispose: IDisposable,
@IInstantiationService instantiationService: IInstantiationService,
@ICodeEditorService codeEditorService: ICodeEditorService,
@ICommandService commandService: ICommandService,
@IContextKeyService contextKeyService: IContextKeyService,
@IKeybindingService keybindingService: IKeybindingService,
@IContextViewService contextViewService: IContextViewService,
@IStandaloneColorService standaloneColorService: IStandaloneColorService
) {
options = options || {};
super(domElement, options, instantiationService, codeEditorService, commandService, contextKeyService, keybindingService, standaloneColorService);
this._contextViewService = <IEditorContextViewService>contextViewService;
this._toDispose2 = [toDispose];
let model: IModel = null;
if (typeof options.model === 'undefined') {
model = (<any>self).monaco.editor.createModel(options.value || '', options.language || 'text/plain');
this._ownsModel = true;
} else {
model = options.model;
delete options.model;
this._ownsModel = false;
}
this._attachModel(model);
if (model) {
let e: IModelChangedEvent = {
oldModelUrl: null,
newModelUrl: model.uri
};
this.emit(EventType.ModelChanged, e);
}
}
public dispose(): void {
super.dispose();
this._toDispose2 = dispose(this._toDispose2);
}
public destroy(): void {
this.dispose();
}
_attachModel(model: IModel): void {
super._attachModel(model);
......@@ -225,46 +245,27 @@ export class StandaloneDiffEditor extends DiffEditorWidget implements IStandalon
this.dispose();
}
protected _createInnerEditor(instantiationService: IInstantiationService, container: HTMLElement, options: IEditorOptions): CodeEditor {
return instantiationService.createInstance(StandaloneCodeEditor, container, options);
}
public getOriginalEditor(): IStandaloneCodeEditor {
return <StandaloneCodeEditor>super.getOriginalEditor();
}
public getModifiedEditor(): IStandaloneCodeEditor {
return <StandaloneCodeEditor>super.getModifiedEditor();
}
public addCommand(keybinding: number, handler: ICommandHandler, context: string): string {
if (!this._standaloneKeybindingService) {
console.warn('Cannot add command because the editor is configured with an unrecognized KeybindingService');
return null;
}
let commandId = 'DYNAMIC_' + (++LAST_GENERATED_COMMAND_ID);
let whenExpression = IOSupport.readKeybindingWhen(context);
this._standaloneKeybindingService.addDynamicKeybinding(commandId, keybinding, handler, whenExpression);
return commandId;
return this.getModifiedEditor().addCommand(keybinding, handler, context);
}
public createContextKey<T>(key: string, defaultValue: T): IContextKey<T> {
if (!this._standaloneKeybindingService) {
console.warn('Cannot create context key because the editor is configured with an unrecognized KeybindingService');
return null;
}
return this._contextKeyService.createKey(key, defaultValue);
return this.getModifiedEditor().createContextKey(key, defaultValue);
}
public addAction(descriptor: IActionDescriptor): IDisposable {
let addedAction = this._addAction(descriptor);
let toDispose = [addedAction.disposable];
if (!this._standaloneKeybindingService) {
console.warn('Cannot add keybinding because the editor is configured with an unrecognized KeybindingService');
return null;
}
if (Array.isArray(descriptor.keybindings)) {
let handler: ICommandHandler = (ctx) => {
return this.trigger('keyboard', descriptor.id, null);
};
let whenExpression = ContextKeyExpr.and(
IOSupport.readKeybindingWhen(descriptor.precondition),
IOSupport.readKeybindingWhen(descriptor.keybindingContext),
);
toDispose = toDispose.concat(
descriptor.keybindings.map((kb) => {
return this._standaloneKeybindingService.addDynamicKeybinding(addedAction.uniqueId, kb, handler, whenExpression);
})
);
}
return combinedDisposable(toDispose);
return this.getModifiedEditor().addAction(descriptor);
}
}
......@@ -373,17 +373,21 @@ export class DiffEditorWidget extends EventEmitter implements editorBrowser.IDif
}
private _createLeftHandSideEditor(options: editorCommon.IDiffEditorOptions, instantiationService: IInstantiationService): void {
this.originalEditor = instantiationService.createInstance(CodeEditor, this._originalDomNode, this._adjustOptionsForLeftHandSide(options, this._originalIsEditable));
this.originalEditor = this._createInnerEditor(instantiationService, this._originalDomNode, this._adjustOptionsForLeftHandSide(options, this._originalIsEditable));
this._toDispose.push(this.originalEditor.addBulkListener2((events) => this._onOriginalEditorEvents(events)));
this._toDispose.push(this.addEmitter2(this.originalEditor));
}
private _createRightHandSideEditor(options: editorCommon.IDiffEditorOptions, instantiationService: IInstantiationService): void {
this.modifiedEditor = instantiationService.createInstance(CodeEditor, this._modifiedDomNode, this._adjustOptionsForRightHandSide(options));
this.modifiedEditor = this._createInnerEditor(instantiationService, this._modifiedDomNode, this._adjustOptionsForRightHandSide(options));
this._toDispose.push(this.modifiedEditor.addBulkListener2((events) => this._onModifiedEditorEvents(events)));
this._toDispose.push(this.addEmitter2(this.modifiedEditor));
}
protected _createInnerEditor(instantiationService: IInstantiationService, container: HTMLElement, options: editorCommon.IEditorOptions): CodeEditor {
return instantiationService.createInstance(CodeEditor, container, options);
}
public destroy(): void {
this.dispose();
}
......@@ -894,7 +898,7 @@ export class DiffEditorWidget extends EventEmitter implements editorBrowser.IDif
return clonedOptions;
}
private _adjustOptionsForLeftHandSide(options: editorCommon.IDiffEditorOptions, isEditable: boolean): editorCommon.IDiffEditorOptions {
private _adjustOptionsForLeftHandSide(options: editorCommon.IDiffEditorOptions, isEditable: boolean): editorCommon.IEditorOptions {
let result = this._adjustOptionsForSubEditor(options);
result.readOnly = !isEditable;
result.overviewRulerLanes = 1;
......@@ -902,7 +906,7 @@ export class DiffEditorWidget extends EventEmitter implements editorBrowser.IDif
return result;
}
private _adjustOptionsForRightHandSide(options: editorCommon.IDiffEditorOptions): editorCommon.IDiffEditorOptions {
private _adjustOptionsForRightHandSide(options: editorCommon.IDiffEditorOptions): editorCommon.IEditorOptions {
let result = this._adjustOptionsForSubEditor(options);
result.revealHorizontalRightPadding = DefaultConfig.editor.revealHorizontalRightPadding + DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;
result.scrollbar.verticalHasArrows = false;
......
......@@ -970,6 +970,8 @@ declare module monaco.editor {
addCommand(keybinding: number, handler: ICommandHandler, context: string): string;
createContextKey<T>(key: string, defaultValue: T): IContextKey<T>;
addAction(descriptor: IActionDescriptor): IDisposable;
getOriginalEditor(): IStandaloneCodeEditor;
getModifiedEditor(): IStandaloneCodeEditor;
}
export interface ICommandHandler {
(...args: any[]): void;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册