@@ -85,11 +85,11 @@ export class AccessibilityManager extends Disposable {
|
||||
this.register(this._terminal.onRender(e => this._refreshRows(e.start, e.end)));
|
||||
this.register(this._terminal.onScroll(() => this._refreshRows()));
|
||||
// Line feed is an issue as the prompt won't be read out after a command is run
|
||||
this.register(this._terminal.addDisposableListener('a11y.char', (char) => this._onChar(char)));
|
||||
this.register(this._terminal.onA11yChar(char => this._onChar(char)));
|
||||
this.register(this._terminal.onLineFeed(() => this._onChar('\n')));
|
||||
this.register(this._terminal.addDisposableListener('a11y.tab', spaceCount => this._onTab(spaceCount)));
|
||||
this.register(this._terminal.onA11yTab(spaceCount => this._onTab(spaceCount)));
|
||||
this.register(this._terminal.onKey(e => this._onKey(e.key)));
|
||||
this.register(this._terminal.addDisposableListener('blur', () => this._clearLiveRegion()));
|
||||
this.register(this._terminal.onBlur(() => this._clearLiveRegion()));
|
||||
|
||||
this._screenDprMonitor = new ScreenDprMonitor();
|
||||
this.register(this._screenDprMonitor);
|
||||
|
||||
@@ -63,7 +63,6 @@ export function pasteHandler(ev: ClipboardEvent, term: ITerminal): void {
|
||||
text = bracketTextForPaste(text, term.bracketedPasteMode);
|
||||
term.handler(text);
|
||||
term.textarea.value = '';
|
||||
term.emit('paste', text);
|
||||
term.cancel(ev);
|
||||
};
|
||||
|
||||
|
||||
+3
-3
@@ -342,7 +342,7 @@ export class InputHandler extends Disposable implements IInputHandler {
|
||||
|
||||
buffer = this._terminal.buffer;
|
||||
if (buffer.x !== cursorStartX || buffer.y !== cursorStartY) {
|
||||
this._terminal.emit('cursormove');
|
||||
this._onCursorMove.fire();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -377,7 +377,7 @@ export class InputHandler extends Disposable implements IInputHandler {
|
||||
}
|
||||
|
||||
if (screenReaderMode) {
|
||||
this._terminal.emit('a11y.char', stringFromCodePoint(code));
|
||||
this._terminal.onA11yCharEmitter.fire(stringFromCodePoint(code));
|
||||
}
|
||||
|
||||
// insert combining char at last cursor position
|
||||
@@ -542,7 +542,7 @@ export class InputHandler extends Disposable implements IInputHandler {
|
||||
const originalX = this._terminal.buffer.x;
|
||||
this._terminal.buffer.x = this._terminal.buffer.nextStop();
|
||||
if (this._terminal.options.screenReaderMode) {
|
||||
this._terminal.emit('a11y.tab', this._terminal.buffer.x - originalX);
|
||||
this._terminal.onA11yTabEmitter.fire(this._terminal.buffer.x - originalX);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-164
@@ -57,20 +57,20 @@ describe('Terminal', () => {
|
||||
term.handler('fake');
|
||||
});
|
||||
it('should fire the onCursorMove event', (done) => {
|
||||
term.on('cursormove', () => done());
|
||||
term.onCursorMove(() => done());
|
||||
term.write('foo');
|
||||
});
|
||||
it('should fire the onLineFeed event', (done) => {
|
||||
term.on('linefeed', () => done());
|
||||
term.onLineFeed(() => done());
|
||||
term.write('\n');
|
||||
});
|
||||
it('should fire a scroll event when scrollback is created', (done) => {
|
||||
term.on('scroll', () => done());
|
||||
term.onScroll(() => done());
|
||||
term.write('\n'.repeat(INIT_ROWS));
|
||||
});
|
||||
it('should fire a scroll event when scrollback is cleared', (done) => {
|
||||
term.write('\n'.repeat(INIT_ROWS));
|
||||
term.on('scroll', () => done());
|
||||
term.onScroll(() => done());
|
||||
term.clear();
|
||||
});
|
||||
it('should fire a key event after a keypress DOM event', (done) => {
|
||||
@@ -126,158 +126,6 @@ describe('Terminal', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('on', () => {
|
||||
beforeEach(() => {
|
||||
term.on('key', () => { });
|
||||
term.on('keypress', () => { });
|
||||
term.on('keydown', () => { });
|
||||
});
|
||||
|
||||
describe('data', () => {
|
||||
it('should emit a data event', (done) => {
|
||||
term.on('data', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
term.handler('fake');
|
||||
});
|
||||
});
|
||||
|
||||
describe('cursormove', () => {
|
||||
it('should emit a cursormove event', (done) => {
|
||||
term.on('cursormove', () => {
|
||||
done();
|
||||
});
|
||||
term.write('foo');
|
||||
});
|
||||
});
|
||||
|
||||
describe('linefeed', () => {
|
||||
it('should emit a linefeed event', (done) => {
|
||||
term.on('linefeed', () => {
|
||||
done();
|
||||
});
|
||||
term.write('\n');
|
||||
});
|
||||
});
|
||||
|
||||
describe('scroll', () => {
|
||||
it('should emit a scroll event when scrollback is created', (done) => {
|
||||
term.on('scroll', () => {
|
||||
done();
|
||||
});
|
||||
term.write('\n'.repeat(INIT_ROWS));
|
||||
});
|
||||
it('should emit a scroll event when scrollback is cleared', (done) => {
|
||||
term.write('\n'.repeat(INIT_ROWS));
|
||||
term.on('scroll', () => {
|
||||
done();
|
||||
});
|
||||
term.clear();
|
||||
});
|
||||
});
|
||||
|
||||
describe(`keypress (including 'key' event)`, () => {
|
||||
it('should receive a string and event object', (done) => {
|
||||
let steps = 0;
|
||||
|
||||
const finish = () => {
|
||||
if ((++steps) === 2) {
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
const evKeyPress = <KeyboardEvent>{
|
||||
preventDefault: () => { },
|
||||
stopPropagation: () => { },
|
||||
type: 'keypress',
|
||||
keyCode: 13
|
||||
};
|
||||
|
||||
term.on('keypress', (key, event) => {
|
||||
assert.equal(typeof key, 'string');
|
||||
expect(event).to.be.an.instanceof(Object);
|
||||
finish();
|
||||
});
|
||||
|
||||
term.on('key', (key, event) => {
|
||||
assert.equal(typeof key, 'string');
|
||||
expect(event).to.be.an.instanceof(Object);
|
||||
finish();
|
||||
});
|
||||
|
||||
term.keyPress(evKeyPress);
|
||||
});
|
||||
});
|
||||
|
||||
describe(`keydown (including 'key' event)`, () => {
|
||||
it(`should receive an event object for 'keydown' and a string and event object for 'key'`, (done) => {
|
||||
let steps = 0;
|
||||
|
||||
const finish = () => {
|
||||
if ((++steps) === 2) {
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
const evKeyDown = <KeyboardEvent>{
|
||||
preventDefault: () => { },
|
||||
stopPropagation: () => { },
|
||||
type: 'keydown',
|
||||
keyCode: 13
|
||||
};
|
||||
|
||||
term.on('keydown', (event) => {
|
||||
expect(event).to.be.an.instanceof(Object);
|
||||
finish();
|
||||
});
|
||||
|
||||
term.on('key', (key, event) => {
|
||||
assert.equal(typeof key, 'string');
|
||||
expect(event).to.be.an.instanceof(Object);
|
||||
finish();
|
||||
});
|
||||
|
||||
term.keyDown(evKeyDown);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resize', () => {
|
||||
it('should receive an object: {cols: number, rows: number}', (done) => {
|
||||
term.on('resize', (data) => {
|
||||
expect(data).to.have.keys(['cols', 'rows']);
|
||||
assert.equal(typeof data.cols, 'number');
|
||||
assert.equal(typeof data.rows, 'number');
|
||||
done();
|
||||
});
|
||||
|
||||
term.resize(1, 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('scroll', () => {
|
||||
it('should receive a number', (done) => {
|
||||
term.on('scroll', (ydisp) => {
|
||||
assert.equal(typeof ydisp, 'number');
|
||||
done();
|
||||
});
|
||||
|
||||
term.scroll();
|
||||
});
|
||||
});
|
||||
|
||||
describe('title', () => {
|
||||
it('should receive a string', (done) => {
|
||||
term.on('title', (title) => {
|
||||
assert.equal(typeof title, 'string');
|
||||
done();
|
||||
});
|
||||
|
||||
term.handleTitle('title');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('attachCustomKeyEventHandler', () => {
|
||||
const evKeyDown = <KeyboardEvent>{
|
||||
preventDefault: () => { },
|
||||
@@ -741,10 +589,10 @@ describe('Terminal', () => {
|
||||
it('should emit key with alt + key on keyPress', (done) => {
|
||||
const keys = ['@', '@', '\\', '\\', '|', '|'];
|
||||
|
||||
term.on('keypress', (key) => {
|
||||
if (key) {
|
||||
const index = keys.indexOf(key);
|
||||
assert(index !== -1, 'Emitted wrong key: ' + key);
|
||||
term.onKey(e => {
|
||||
if (e.key) {
|
||||
const index = keys.indexOf(e.key);
|
||||
assert(index !== -1, 'Emitted wrong key: ' + e.key);
|
||||
keys.splice(index, 1);
|
||||
}
|
||||
if (keys.length === 0) done();
|
||||
@@ -807,10 +655,10 @@ describe('Terminal', () => {
|
||||
it('should emit key with alt + ctrl + key on keyPress', (done) => {
|
||||
const keys = ['@', '@', '\\', '\\', '|', '|'];
|
||||
|
||||
term.on('keypress', (key) => {
|
||||
if (key) {
|
||||
const index = keys.indexOf(key);
|
||||
assert(index !== -1, 'Emitted wrong key: ' + key);
|
||||
term.onKey(e => {
|
||||
if (e.key) {
|
||||
const index = keys.indexOf(e.key);
|
||||
assert(index !== -1, 'Emitted wrong key: ' + e.key);
|
||||
keys.splice(index, 1);
|
||||
}
|
||||
if (keys.length === 0) done();
|
||||
|
||||
+15
-27
@@ -26,7 +26,6 @@ import { IRenderer, CharacterJoinerHandler } from 'browser/renderer/Types';
|
||||
import { BufferSet } from 'common/buffer/BufferSet';
|
||||
import { Buffer } from 'common/buffer/Buffer';
|
||||
import { CompositionHelper } from './CompositionHelper';
|
||||
import { EventEmitter } from 'common/EventEmitter';
|
||||
import { Viewport } from './Viewport';
|
||||
import { rightClickHandler, moveTextAreaUnderMouseCursor, pasteHandler, copyHandler } from './Clipboard';
|
||||
import { C0 } from 'common/data/EscapeSequences';
|
||||
@@ -56,6 +55,7 @@ import { OptionsService } from 'common/services/OptionsService';
|
||||
import { ICharSizeService } from 'browser/services/Services';
|
||||
import { CharSizeService } from 'browser/services/CharSizeService';
|
||||
import { BufferService, MINIMUM_COLS, MINIMUM_ROWS } from 'common/services/BufferService';
|
||||
import { Disposable } from 'common/Lifecycle';
|
||||
|
||||
// Let it work inside Node.js for automated testing purposes.
|
||||
const document = (typeof window !== 'undefined') ? window.document : null;
|
||||
@@ -76,7 +76,7 @@ const WRITE_BUFFER_PAUSE_THRESHOLD = 5;
|
||||
const WRITE_TIMEOUT_MS = 12;
|
||||
const WRITE_BUFFER_LENGTH_THRESHOLD = 50;
|
||||
|
||||
export class Terminal extends EventEmitter implements ITerminal, IDisposable, IInputHandlingTerminal {
|
||||
export class Terminal extends Disposable implements ITerminal, IDisposable, IInputHandlingTerminal {
|
||||
public textarea: HTMLTextAreaElement;
|
||||
public element: HTMLElement;
|
||||
public screenElement: HTMLElement;
|
||||
@@ -209,6 +209,15 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
private _onTitleChange = new EventEmitter2<string>();
|
||||
public get onTitleChange(): IEvent<string> { return this._onTitleChange.event; }
|
||||
|
||||
private _onFocus = new EventEmitter2<void>();
|
||||
public get onFocus(): IEvent<void> { return this._onFocus.event; }
|
||||
private _onBlur = new EventEmitter2<void>();
|
||||
public get onBlur(): IEvent<void> { return this._onBlur.event; }
|
||||
public onA11yCharEmitter = new EventEmitter2<string>();
|
||||
public get onA11yChar(): IEvent<string> { return this.onA11yCharEmitter.event; }
|
||||
public onA11yTabEmitter = new EventEmitter2<number>();
|
||||
public get onA11yTab(): IEvent<number> { return this.onA11yTabEmitter.event; }
|
||||
|
||||
/**
|
||||
* Creates a new `Terminal` object.
|
||||
*
|
||||
@@ -234,18 +243,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
|
||||
// this.options = clone(options);
|
||||
this._setup();
|
||||
|
||||
// TODO: Remove these in v4
|
||||
// Fire old style events from new emitters
|
||||
this.onCursorMove(() => this.emit('cursormove'));
|
||||
this.onData(e => this.emit('data', e));
|
||||
this.onKey(e => this.emit('key', e.key, e.domEvent));
|
||||
this.onLineFeed(() => this.emit('linefeed'));
|
||||
this.onRender(e => this.emit('refresh', e));
|
||||
this.onResize(e => this.emit('resize', e));
|
||||
this.onSelectionChange(() => this.emit('selection'));
|
||||
this.onScroll(e => this.emit('scroll', e));
|
||||
this.onTitleChange(e => this.emit('title', e));
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
@@ -444,7 +441,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
this.updateCursorStyle(ev);
|
||||
this.element.classList.add('focus');
|
||||
this.showCursor();
|
||||
this.emit('focus');
|
||||
this._onFocus.fire();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -467,7 +464,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
this.handler(C0.ESC + '[O');
|
||||
}
|
||||
this.element.classList.remove('focus');
|
||||
this.emit('blur');
|
||||
this._onBlur.fire();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -639,8 +636,8 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
|
||||
this.register(this.onCursorMove(() => this._renderService.onCursorMove()));
|
||||
this.register(this.onResize(() => this._renderService.onResize(this.cols, this.rows)));
|
||||
this.register(this.addDisposableListener('blur', () => this._renderService.onBlur()));
|
||||
this.register(this.addDisposableListener('focus', () => this._renderService.onFocus()));
|
||||
this.register(this.onBlur(() => this._renderService.onBlur()));
|
||||
this.register(this.onFocus(() => this._renderService.onFocus()));
|
||||
this.register(this._renderService.onDimensionsChange(() => this.viewport.syncScrollArea()));
|
||||
|
||||
this.selectionManager = new SelectionManager(this, this._charSizeService, this._bufferService);
|
||||
@@ -803,12 +800,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
// vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
|
||||
// locator: CSI P e ; P b ; P r ; P c ; P p & w
|
||||
function sendEvent(button: number, pos: {x: number, y: number}): void {
|
||||
// self.emit('mouse', {
|
||||
// x: pos.x - 32,
|
||||
// y: pos.x - 32,
|
||||
// button: button
|
||||
// });
|
||||
|
||||
if (self._vt300Mouse) {
|
||||
// NOTE: Unstable.
|
||||
// http://www.vt100.net/docs/vt3xx-gp/chapter15.html
|
||||
@@ -1597,7 +1588,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
return true;
|
||||
}
|
||||
|
||||
this.emit('keydown', event);
|
||||
this._onKey.fire({ key: result.key, domEvent: event });
|
||||
this.showCursor();
|
||||
this.handler(result.key);
|
||||
@@ -1676,7 +1666,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
|
||||
key = String.fromCharCode(key);
|
||||
|
||||
this.emit('keypress', key, ev);
|
||||
this._onKey.fire({ key, domEvent: ev });
|
||||
this.showCursor();
|
||||
this.handler(key);
|
||||
@@ -1689,7 +1678,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
||||
* Note: We could do sweet things with webaudio here
|
||||
*/
|
||||
public bell(): void {
|
||||
this.emit('bell');
|
||||
if (this._soundBell()) {
|
||||
this.soundManager.playBellSound();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import { Terminal } from './Terminal';
|
||||
import { AttributeData } from 'common/buffer/BufferLine';
|
||||
import { IColorManager, IColorSet, IMouseHelper } from 'browser/Types';
|
||||
import { IOptionsService } from 'common/services/Services';
|
||||
import { EventEmitter2 } from 'common/EventEmitter2';
|
||||
|
||||
export class TestTerminal extends Terminal {
|
||||
writeSync(data: string): void {
|
||||
@@ -23,6 +24,10 @@ export class TestTerminal extends Terminal {
|
||||
}
|
||||
|
||||
export class MockTerminal implements ITerminal {
|
||||
onBlur: IEvent<void>;
|
||||
onFocus: IEvent<void>;
|
||||
onA11yChar: IEvent<string>;
|
||||
onA11yTab: IEvent<number>;
|
||||
onCursorMove: IEvent<void>;
|
||||
onLineFeed: IEvent<void>;
|
||||
onSelectionChange: IEvent<void>;
|
||||
@@ -181,6 +186,8 @@ export class MockTerminal implements ITerminal {
|
||||
}
|
||||
|
||||
export class MockInputHandlingTerminal implements IInputHandlingTerminal {
|
||||
onA11yCharEmitter: EventEmitter2<string>;
|
||||
onA11yTabEmitter: EventEmitter2<number>;
|
||||
element: HTMLElement;
|
||||
options: ITerminalOptions = {};
|
||||
cols: number;
|
||||
|
||||
+12
-4
@@ -3,9 +3,9 @@
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { ITerminalOptions as IPublicTerminalOptions, IEventEmitter, IDisposable, IMarker, ISelectionPosition } from 'xterm';
|
||||
import { ITerminalOptions as IPublicTerminalOptions, IDisposable, IMarker, ISelectionPosition } from 'xterm';
|
||||
import { ICharset, IAttributeData, CharData } from 'common/Types';
|
||||
import { IEvent } from 'common/EventEmitter2';
|
||||
import { IEvent, EventEmitter2 } from 'common/EventEmitter2';
|
||||
import { IColorSet, IMouseHelper } from 'browser/Types';
|
||||
import { IOptionsService } from 'common/services/Services';
|
||||
import { IBuffer, IBufferSet } from 'common/buffer/Types';
|
||||
@@ -22,7 +22,7 @@ export type LinkMatcherValidationCallback = (uri: string, callback: (isValid: bo
|
||||
* InputHandler. This cleanly separates the large amount of methods needed by
|
||||
* InputHandler cleanly from the ITerminal interface.
|
||||
*/
|
||||
export interface IInputHandlingTerminal extends IEventEmitter {
|
||||
export interface IInputHandlingTerminal {
|
||||
element: HTMLElement;
|
||||
options: ITerminalOptions;
|
||||
cols: number;
|
||||
@@ -54,6 +54,9 @@ export interface IInputHandlingTerminal extends IEventEmitter {
|
||||
viewport: IViewport;
|
||||
selectionManager: ISelectionManager;
|
||||
|
||||
onA11yCharEmitter: EventEmitter2<string>;
|
||||
onA11yTabEmitter: EventEmitter2<number>;
|
||||
|
||||
bell(): void;
|
||||
focus(): void;
|
||||
updateRange(y: number): void;
|
||||
@@ -209,6 +212,11 @@ export interface ITerminal extends IPublicTerminal, IElementAccessor, IBufferAcc
|
||||
// TODO: We should remove options once components adopt optionsService
|
||||
options: ITerminalOptions;
|
||||
|
||||
onBlur: IEvent<void>;
|
||||
onFocus: IEvent<void>;
|
||||
onA11yChar: IEvent<string>;
|
||||
onA11yTab: IEvent<number>;
|
||||
|
||||
handler(data: string): void;
|
||||
scrollLines(disp: number, suppressScrollEvent?: boolean): void;
|
||||
cancel(ev: Event, force?: boolean): boolean | void;
|
||||
@@ -217,7 +225,7 @@ export interface ITerminal extends IPublicTerminal, IElementAccessor, IBufferAcc
|
||||
}
|
||||
|
||||
// Portions of the public API that are required by the internal Terminal
|
||||
export interface IPublicTerminal extends IDisposable, IEventEmitter {
|
||||
export interface IPublicTerminal extends IDisposable {
|
||||
textarea: HTMLTextAreaElement;
|
||||
rows: number;
|
||||
cols: number;
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2017 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { assert } from 'chai';
|
||||
import { EventEmitter } from 'common/EventEmitter';
|
||||
|
||||
describe('EventEmitter', () => {
|
||||
let eventEmitter: EventEmitter;
|
||||
|
||||
beforeEach(() => {
|
||||
eventEmitter = new EventEmitter();
|
||||
});
|
||||
|
||||
describe('emit', () => {
|
||||
it('should emit events to listeners', () => {
|
||||
let count1 = 0;
|
||||
let count2 = 0;
|
||||
const listener1 = () => count1++;
|
||||
const listener2 = () => count2++;
|
||||
eventEmitter.on('test', listener1);
|
||||
eventEmitter.on('test', listener2);
|
||||
eventEmitter.emit('test');
|
||||
assert.equal(count1, 1);
|
||||
assert.equal(count2, 1);
|
||||
eventEmitter.emit('test');
|
||||
assert.equal(count1, 2);
|
||||
assert.equal(count2, 2);
|
||||
});
|
||||
|
||||
it('should manage multiple listener types', () => {
|
||||
let count1 = 0;
|
||||
let count2 = 0;
|
||||
const listener1 = () => count1++;
|
||||
const listener2 = () => count2++;
|
||||
eventEmitter.on('test', listener1);
|
||||
eventEmitter.on('foo', listener2);
|
||||
eventEmitter.emit('test');
|
||||
assert.equal(count1, 1);
|
||||
assert.equal(count2, 0);
|
||||
eventEmitter.emit('foo');
|
||||
assert.equal(count1, 1);
|
||||
assert.equal(count2, 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('listeners', () => {
|
||||
it('should return listeners for the type requested', () => {
|
||||
assert.equal(eventEmitter.listeners('test').length, 0);
|
||||
const listener = () => {};
|
||||
eventEmitter.on('test', listener);
|
||||
assert.deepEqual(eventEmitter.listeners('test'), [listener]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('off', () => {
|
||||
it('should remove the specific listener', () => {
|
||||
const listener1 = () => {};
|
||||
const listener2 = () => {};
|
||||
eventEmitter.on('foo', listener1);
|
||||
eventEmitter.on('foo', listener2);
|
||||
assert.equal(eventEmitter.listeners('foo').length, 2);
|
||||
eventEmitter.off('foo', listener1);
|
||||
assert.deepEqual(eventEmitter.listeners('foo'), [listener2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeAllListeners', () => {
|
||||
it('should clear all listeners', () => {
|
||||
eventEmitter.on('foo', () => {});
|
||||
assert.equal(eventEmitter.listeners('foo').length, 1);
|
||||
eventEmitter.removeAllListeners('foo');
|
||||
assert.equal(eventEmitter.listeners('foo').length, 0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,98 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2017 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { IDisposable, IEventEmitter, XtermListener } from 'common/Types';
|
||||
import { Disposable } from 'common/Lifecycle';
|
||||
|
||||
export class EventEmitter extends Disposable implements IEventEmitter, IDisposable {
|
||||
private _events: {[type: string]: XtermListener[]};
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
// Restore the previous events if available, this will happen if the
|
||||
// constructor is called multiple times on the same object (terminal reset).
|
||||
this._events = (<any>this)._events || {};
|
||||
}
|
||||
|
||||
public on(type: string, listener: XtermListener): void {
|
||||
this._events[type] = this._events[type] || [];
|
||||
this._events[type].push(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a disposable listener to the EventEmitter, returning the disposable.
|
||||
* @param type The event type.
|
||||
* @param handler The handler for the listener.
|
||||
*/
|
||||
public addDisposableListener(type: string, handler: XtermListener): IDisposable {
|
||||
// TODO: Rename addDisposableEventListener to more easily disambiguate from Dom listener
|
||||
this.on(type, handler);
|
||||
let disposed = false;
|
||||
return {
|
||||
dispose: () => {
|
||||
if (disposed) {
|
||||
// Already disposed
|
||||
return;
|
||||
}
|
||||
this.off(type, handler);
|
||||
disposed = true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public off(type: string, listener: XtermListener): void {
|
||||
if (!this._events[type]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const obj = this._events[type];
|
||||
let i = obj.length;
|
||||
|
||||
while (i--) {
|
||||
if (obj[i] === listener) {
|
||||
obj.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public removeAllListeners(type: string): void {
|
||||
if (this._events[type]) {
|
||||
delete this._events[type];
|
||||
}
|
||||
}
|
||||
|
||||
public emit(type: string, ...args: any[]): void {
|
||||
if (!this._events[type]) {
|
||||
return;
|
||||
}
|
||||
const obj = this._events[type];
|
||||
for (let i = 0; i < obj.length; i++) {
|
||||
obj[i].apply(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
public emitMayRemoveListeners(type: string, ...args: any[]): void {
|
||||
if (!this._events[type]) {
|
||||
return;
|
||||
}
|
||||
const obj = this._events[type];
|
||||
let length = obj.length;
|
||||
for (let i = 0; i < obj.length; i++) {
|
||||
obj[i].apply(this, args);
|
||||
i -= length - obj.length;
|
||||
length = obj.length;
|
||||
}
|
||||
}
|
||||
|
||||
public listeners(type: string): XtermListener[] {
|
||||
return this._events[type] || [];
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
super.dispose();
|
||||
this._events = {};
|
||||
}
|
||||
}
|
||||
@@ -43,27 +43,6 @@ export class Terminal implements ITerminalApi {
|
||||
public focus(): void {
|
||||
this._core.focus();
|
||||
}
|
||||
public on(type: 'blur' | 'focus' | 'linefeed' | 'selection', listener: () => void): void;
|
||||
public on(type: 'data', listener: (...args: any[]) => void): void;
|
||||
public on(type: 'key', listener: (key?: string, event?: KeyboardEvent) => void): void;
|
||||
public on(type: 'keypress' | 'keydown', listener: (event?: KeyboardEvent) => void): void;
|
||||
public on(type: 'refresh', listener: (data?: { start: number; end: number; }) => void): void;
|
||||
public on(type: 'resize', listener: (data?: { cols: number; rows: number; }) => void): void;
|
||||
public on(type: 'scroll', listener: (ydisp?: number) => void): void;
|
||||
public on(type: 'title', listener: (title?: string) => void): void;
|
||||
public on(type: string, listener: (...args: any[]) => void): void;
|
||||
public on(type: any, listener: any): void {
|
||||
this._core.on(type, listener);
|
||||
}
|
||||
public off(type: string, listener: (...args: any[]) => void): void {
|
||||
this._core.off(type, listener);
|
||||
}
|
||||
public emit(type: string, data?: any): void {
|
||||
this._core.emit(type, data);
|
||||
}
|
||||
public addDisposableListener(type: string, handler: (...args: any[]) => void): IDisposable {
|
||||
return this._core.addDisposableListener(type, handler);
|
||||
}
|
||||
public resize(columns: number, rows: number): void {
|
||||
this._core.resize(columns, rows);
|
||||
}
|
||||
|
||||
Vendored
+1
-98
@@ -268,13 +268,6 @@ declare module 'xterm' {
|
||||
willLinkActivate?: (event: MouseEvent, uri: string) => boolean;
|
||||
}
|
||||
|
||||
export interface IEventEmitter {
|
||||
on(type: string, listener: (...args: any[]) => void): void;
|
||||
off(type: string, listener: (...args: any[]) => void): void;
|
||||
emit(type: string, data?: any): void;
|
||||
addDisposableListener(type: string, handler: (...args: any[]) => void): IDisposable;
|
||||
}
|
||||
|
||||
/**
|
||||
* An object that can be disposed via a dispose function.
|
||||
*/
|
||||
@@ -305,7 +298,7 @@ declare module 'xterm' {
|
||||
/**
|
||||
* The class that represents an xterm.js terminal.
|
||||
*/
|
||||
export class Terminal implements IEventEmitter, IDisposable {
|
||||
export class Terminal implements IDisposable {
|
||||
/**
|
||||
* The element containing the terminal.
|
||||
*/
|
||||
@@ -429,96 +422,6 @@ declare module 'xterm' {
|
||||
*/
|
||||
focus(): void;
|
||||
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: 'blur' | 'focus' | 'linefeed' | 'selection', listener: () => void): void;
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: 'data', listener: (...args: any[]) => void): void;
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: 'key', listener: (key: string, event: KeyboardEvent) => void): void;
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: 'keypress' | 'keydown', listener: (event: KeyboardEvent) => void): void;
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: 'refresh', listener: (data: {start: number, end: number}) => void): void;
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: 'resize', listener: (data: {cols: number, rows: number}) => void): void;
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: 'scroll', listener: (ydisp: number) => void): void;
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: 'title', listener: (title: string) => void): void;
|
||||
/**
|
||||
* Registers an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
on(type: string, listener: (...args: any[]) => void): void;
|
||||
|
||||
/**
|
||||
* Deregisters an event listener.
|
||||
* @param type The type of the event.
|
||||
* @param listener The listener.
|
||||
* @deprecated use `Terminal.onEvent(listener).dispose()` instead.
|
||||
*/
|
||||
off(type: 'blur' | 'focus' | 'linefeed' | 'selection' | 'data' | 'key' | 'keypress' | 'keydown' | 'refresh' | 'resize' | 'scroll' | 'title' | string, listener: (...args: any[]) => void): void;
|
||||
|
||||
/**
|
||||
* Emits an event on the terminal.
|
||||
* @param type The type of event
|
||||
* @param data data associated with the event.
|
||||
* @deprecated This is being removed from the API with no replacement, see
|
||||
* issue #1505.
|
||||
*/
|
||||
emit(type: string, data?: any): void;
|
||||
|
||||
/**
|
||||
* Adds an event listener to the Terminal, returning an IDisposable that can
|
||||
* be used to conveniently remove the event listener.
|
||||
* @param type The type of event.
|
||||
* @param handler The event handler.
|
||||
* @deprecated use `Terminal.onEvent(listener)` instead.
|
||||
*/
|
||||
addDisposableListener(type: string, handler: (...args: any[]) => void): IDisposable;
|
||||
|
||||
/**
|
||||
* Resizes the terminal. It's best practice to debounce calls to resize,
|
||||
* this will help ensure that the pty can respond to the resize event
|
||||
|
||||
Reference in New Issue
Block a user