Move handler into CoreService and adopt

This also fine tunes some of the data events to not clear selection
and scroll to the bottom of the viewport anymore.

Part of #1507
Fixes #2112
This commit is contained in:
Daniel Imms
2019-06-23 10:10:17 -07:00
parent 94a2ad9416
commit bfa7a2df76
12 changed files with 153 additions and 80 deletions
+43
View File
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
* @license MIT
*/
import { ICoreService, IOptionsService, IBufferService } from 'common/services/Services';
import { EventEmitter, IEvent } from 'common/EventEmitter';
export class CoreService implements ICoreService {
private _onData = new EventEmitter<string>();
public get onData(): IEvent<string> { return this._onData.event; }
private _onUserInput = new EventEmitter<void>();
public get onUserInput(): IEvent<void> { return this._onUserInput.event; }
constructor(
// TODO: Move this into a service
private readonly _scrollToBottom: () => void,
private readonly _bufferService: IBufferService,
private readonly _optionsService: IOptionsService
) {
}
public triggerDataEvent(data: string, wasUserInput: boolean = false): void {
// Prevents all events to pty process if stdin is disabled
if (this._optionsService.options.disableStdin) {
return;
}
// Input is being sent to the terminal, the terminal should focus the prompt.
const buffer = this._bufferService.buffer;
if (buffer.ybase !== buffer.ydisp) {
this._scrollToBottom();
}
// Fire onUserInput so listeners can react as well (eg. clear selection)
if (wasUserInput) {
this._onUserInput.fire();
}
// Fire onData API
this._onData.fire(data);
}
}
+15
View File
@@ -18,6 +18,21 @@ export interface IBufferService {
reset(): void;
}
export interface ICoreService {
readonly onData: IEvent<string>;
readonly onUserInput: IEvent<void>;
/**
* Triggers the onData event in the public API.
* @param data The data that is being emitted.
* @param wasFromUser Whether the data originated from the user (as opposed to
* resulting from parsing incoming data). When true this will also:
* - Scroll to the bottom of the buffer.s
* - Fire the `onUserInput` event (so selection can be cleared).
*/
triggerDataEvent(data: string, wasUserInput?: boolean): void;
}
export interface IOptionsService {
readonly options: ITerminalOptions;