renameInputField.ts 4.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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 'vs/css!./rename';
import {TPromise} from 'vs/base/common/winjs.base';
10 11 12 13
import * as errors from 'vs/base/common/errors';
import * as lifecycle from 'vs/base/common/lifecycle';
import * as EditorCommon from 'vs/editor/common/editorCommon';
import * as EditorBrowser from 'vs/editor/browser/editorBrowser';
E
Erich Gamma 已提交
14 15
import {Range} from 'vs/editor/common/core/range';

16
export default class RenameInputField implements EditorBrowser.IContentWidget, lifecycle.IDisposable {
E
Erich Gamma 已提交
17 18 19 20 21 22 23 24

	private _editor: EditorBrowser.ICodeEditor;
	private _position: EditorCommon.IPosition;
	private _domNode: HTMLElement;
	private _inputField: HTMLInputElement;
	private _visible: boolean;

	// Editor.IContentWidget.allowEditorOverflow
25
	public allowEditorOverflow: boolean = true;
E
Erich Gamma 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

	constructor(editor: EditorBrowser.ICodeEditor) {
		this._editor = editor;
		this._editor.addContentWidget(this);
	}

	public dispose(): void {
		this._editor.removeContentWidget(this);
	}

	public getId(): string {
		return '__renameInputWidget';
	}

	public getDomNode(): HTMLElement {
		if (!this._domNode) {
			this._inputField = document.createElement('input');
			this._inputField.className = 'rename-input';
44
			this._inputField.type = 'text';
E
Erich Gamma 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
			this._domNode = document.createElement('div');
			this._domNode.style.height = `${this._editor.getConfiguration().lineHeight}px`;
			this._domNode.className = 'monaco-editor rename-box';
			this._domNode.appendChild(this._inputField);
		}
		return this._domNode;
	}

	public getPosition(): EditorBrowser.IContentWidgetPosition {
		return this._visible
			? { position: this._position, preference: [EditorBrowser.ContentWidgetPositionPreference.BELOW, EditorBrowser.ContentWidgetPositionPreference.ABOVE] }
			: null;
	}

	private _currentAcceptInput: () => void = null;
	private _currentCancelInput: () => void = null;

	public acceptInput(): void {
		if (this._currentAcceptInput) {
			this._currentAcceptInput();
		}
	}

	public cancelInput(): void {
		if (this._currentCancelInput) {
			this._currentCancelInput();
		}
	}

	public getInput(where: EditorCommon.IRange, value: string, selectionStart: number, selectionEnd: number): TPromise<string> {

		this._position = { lineNumber: where.startLineNumber, column: where.startColumn };
		this._inputField.value = value;
		this._inputField.setAttribute('selectionStart', selectionStart.toString());
		this._inputField.setAttribute('selectionEnd', selectionEnd.toString());
		this._inputField.size = Math.max((where.endColumn - where.startColumn) * 1.1, 20);

82
		let disposeOnDone: lifecycle.IDisposable[] = [],
E
Erich Gamma 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			always: Function;

		always = () => {
			lifecycle.disposeAll(disposeOnDone);
			this._hide();
		};

		return new TPromise<string>((c, e) => {

			this._currentCancelInput = () => {
				this._currentAcceptInput = null;
				this._currentCancelInput = null;
				e(errors.canceled());
				return true;
			};

			this._currentAcceptInput = () => {
				if (this._inputField.value.trim().length === 0 || this._inputField.value === value) {
					// empty or whitespace only or not changed
					this._currentCancelInput();
					return;
				}

				this._currentAcceptInput = null;
				this._currentCancelInput = null;
				c(this._inputField.value);
			}

111
			let onCursorChanged = () => {
E
Erich Gamma 已提交
112 113 114 115 116 117 118 119 120 121
				if (!Range.containsPosition(where, this._editor.getPosition())) {
					this._currentCancelInput();
				}
			};

			disposeOnDone.push(this._editor.addListener2(EditorCommon.EventType.CursorSelectionChanged, onCursorChanged));
			disposeOnDone.push(this._editor.addListener2(EditorCommon.EventType.EditorBlur, this._currentCancelInput));

			this._show();

122
		}, this._currentCancelInput).then(newValue => {
E
Erich Gamma 已提交
123
			always();
124
			return newValue;
E
Erich Gamma 已提交
125 126 127 128 129 130
		}, err => {
			always();
			return TPromise.wrapError(err);
		});
	}

131 132
	private _show(): void {
		this._editor.revealLineInCenterIfOutsideViewport(this._position.lineNumber);
E
Erich Gamma 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
		this._visible = true;
		this._editor.layoutContentWidget(this);

		setTimeout(() => {
			this._inputField.focus();
			this._inputField.setSelectionRange(
				parseInt(this._inputField.getAttribute('selectionStart')),
				parseInt(this._inputField.getAttribute('selectionEnd')));
		}, 25);
	}

	private _hide(): void {
		this._visible = false;
		this._editor.layoutContentWidget(this);
	}
}