Merge pull request #371 from Tyriar/335_CompositionHelper_ts
Convert CompositionHelper to TypeScript
This commit is contained in:
@@ -1,213 +0,0 @@
|
|||||||
/**
|
|
||||||
* xterm.js: xterm, in the browser
|
|
||||||
* Copyright (c) 2014-2016, SourceLair Private Company (www.sourcelair.com (MIT License)
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encapsulates the logic for handling compositionstart, compositionupdate and compositionend
|
|
||||||
* events, displaying the in-progress composition to the UI and forwarding the final composition
|
|
||||||
* to the handler.
|
|
||||||
* @param {HTMLTextAreaElement} textarea The textarea that xterm uses for input.
|
|
||||||
* @param {HTMLElement} compositionView The element to display the in-progress composition in.
|
|
||||||
* @param {Terminal} terminal The Terminal to forward the finished composition to.
|
|
||||||
*/
|
|
||||||
function CompositionHelper(textarea, compositionView, terminal) {
|
|
||||||
this.textarea = textarea;
|
|
||||||
this.compositionView = compositionView;
|
|
||||||
this.terminal = terminal;
|
|
||||||
|
|
||||||
// Whether input composition is currently happening, eg. via a mobile keyboard, speech input
|
|
||||||
// or IME. This variable determines whether the compositionText should be displayed on the UI.
|
|
||||||
this.isComposing = false;
|
|
||||||
|
|
||||||
// The input currently being composed, eg. via a mobile keyboard, speech input or IME.
|
|
||||||
this.compositionText = null;
|
|
||||||
|
|
||||||
// The position within the input textarea's value of the current composition.
|
|
||||||
this.compositionPosition = { start: null, end: null };
|
|
||||||
|
|
||||||
// Whether a composition is in the process of being sent, setting this to false will cancel
|
|
||||||
// any in-progress composition.
|
|
||||||
this.isSendingComposition = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the compositionstart event, activating the composition view.
|
|
||||||
*/
|
|
||||||
CompositionHelper.prototype.compositionstart = function() {
|
|
||||||
this.isComposing = true;
|
|
||||||
this.compositionPosition.start = this.textarea.value.length;
|
|
||||||
this.compositionView.textContent = '';
|
|
||||||
this.compositionView.classList.add('active');
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the compositionupdate event, updating the composition view.
|
|
||||||
* @param {CompositionEvent} ev The event.
|
|
||||||
*/
|
|
||||||
CompositionHelper.prototype.compositionupdate = function(ev) {
|
|
||||||
this.compositionView.textContent = ev.data;
|
|
||||||
this.updateCompositionElements();
|
|
||||||
var self = this;
|
|
||||||
setTimeout(function() {
|
|
||||||
self.compositionPosition.end = self.textarea.value.length;
|
|
||||||
}, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the compositionend event, hiding the composition view and sending the composition to
|
|
||||||
* the handler.
|
|
||||||
*/
|
|
||||||
CompositionHelper.prototype.compositionend = function() {
|
|
||||||
this.finalizeComposition(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the keydown event, routing any necessary events to the CompositionHelper functions.
|
|
||||||
* @return Whether the Terminal should continue processing the keydown event.
|
|
||||||
*/
|
|
||||||
CompositionHelper.prototype.keydown = function(ev) {
|
|
||||||
if (this.isComposing || this.isSendingComposition) {
|
|
||||||
if (ev.keyCode === 229) {
|
|
||||||
// Continue composing if the keyCode is the "composition character"
|
|
||||||
return false;
|
|
||||||
} else if (ev.keyCode === 16 || ev.keyCode === 17 || ev.keyCode === 18) {
|
|
||||||
// Continue composing if the keyCode is a modifier key
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
// Finish composition immediately. This is mainly here for the case where enter is
|
|
||||||
// pressed and the handler needs to be triggered before the command is executed.
|
|
||||||
this.finalizeComposition(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ev.keyCode === 229) {
|
|
||||||
// If the "composition character" is used but gets to this point it means a non-composition
|
|
||||||
// character (eg. numbers and punctuation) was pressed when the IME was active.
|
|
||||||
this.handleAnyTextareaChanges();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Finalizes the composition, resuming regular input actions. This is called when a composition
|
|
||||||
* is ending.
|
|
||||||
* @param {boolean} waitForPropogation Whether to wait for events to propogate before sending
|
|
||||||
* the input. This should be false if a non-composition keystroke is entered before the
|
|
||||||
* compositionend event is triggered, such as enter, so that the composition is send before
|
|
||||||
* the command is executed.
|
|
||||||
*/
|
|
||||||
CompositionHelper.prototype.finalizeComposition = function(waitForPropogation) {
|
|
||||||
this.compositionView.classList.remove('active');
|
|
||||||
this.isComposing = false;
|
|
||||||
this.clearTextareaPosition();
|
|
||||||
|
|
||||||
if (!waitForPropogation) {
|
|
||||||
// Cancel any delayed composition send requests and send the input immediately.
|
|
||||||
this.isSendingComposition = false;
|
|
||||||
var input = this.textarea.value.substring(this.compositionPosition.start, this.compositionPosition.end);
|
|
||||||
this.terminal.handler(input);
|
|
||||||
} else {
|
|
||||||
// Make a deep copy of the composition position here as a new compositionstart event may
|
|
||||||
// fire before the setTimeout executes.
|
|
||||||
var currentCompositionPosition = {
|
|
||||||
start: this.compositionPosition.start,
|
|
||||||
end: this.compositionPosition.end,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since composition* events happen before the changes take place in the textarea on most
|
|
||||||
// browsers, use a setTimeout with 0ms time to allow the native compositionend event to
|
|
||||||
// complete. This ensures the correct character is retrieved, this solution was used
|
|
||||||
// because:
|
|
||||||
// - The compositionend event's data property is unreliable, at least on Chromium
|
|
||||||
// - The last compositionupdate event's data property does not always accurately describe
|
|
||||||
// the character, a counter example being Korean where an ending consonsant can move to
|
|
||||||
// the following character if the following input is a vowel.
|
|
||||||
var self = this;
|
|
||||||
this.isSendingComposition = true;
|
|
||||||
setTimeout(function () {
|
|
||||||
// Ensure that the input has not already been sent
|
|
||||||
if (self.isSendingComposition) {
|
|
||||||
self.isSendingComposition = false;
|
|
||||||
var input;
|
|
||||||
if (self.isComposing) {
|
|
||||||
// Use the end position to get the string if a new composition has started.
|
|
||||||
input = self.textarea.value.substring(currentCompositionPosition.start, currentCompositionPosition.end);
|
|
||||||
} else {
|
|
||||||
// Don't use the end position here in order to pick up any characters after the
|
|
||||||
// composition has finished, for example when typing a non-composition character
|
|
||||||
// (eg. 2) after a composition character.
|
|
||||||
input = self.textarea.value.substring(currentCompositionPosition.start);
|
|
||||||
}
|
|
||||||
self.terminal.handler(input);
|
|
||||||
}
|
|
||||||
}, 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply any changes made to the textarea after the current event chain is allowed to complete.
|
|
||||||
* This should be called when not currently composing but a keydown event with the "composition
|
|
||||||
* character" (229) is triggered, in order to allow non-composition text to be entered when an
|
|
||||||
* IME is active.
|
|
||||||
*/
|
|
||||||
CompositionHelper.prototype.handleAnyTextareaChanges = function() {
|
|
||||||
var oldValue = this.textarea.value;
|
|
||||||
var self = this;
|
|
||||||
setTimeout(function() {
|
|
||||||
// Ignore if a composition has started since the timeout
|
|
||||||
if (!self.isComposing) {
|
|
||||||
var newValue = self.textarea.value;
|
|
||||||
var diff = newValue.replace(oldValue, '');
|
|
||||||
if (diff.length > 0) {
|
|
||||||
self.terminal.handler(diff);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Positions the composition view on top of the cursor and the textarea just below it (so the
|
|
||||||
* IME helper dialog is positioned correctly).
|
|
||||||
*/
|
|
||||||
CompositionHelper.prototype.updateCompositionElements = function(dontRecurse) {
|
|
||||||
if (!this.isComposing) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var cursor = this.terminal.element.querySelector('.terminal-cursor');
|
|
||||||
if (cursor) {
|
|
||||||
// Take .xterm-rows offsetTop into account as well in case it's positioned absolutely within
|
|
||||||
// the .xterm element.
|
|
||||||
var xtermRows = this.terminal.element.querySelector('.xterm-rows');
|
|
||||||
var cursorTop = xtermRows.offsetTop + cursor.offsetTop;
|
|
||||||
|
|
||||||
this.compositionView.style.left = cursor.offsetLeft + 'px';
|
|
||||||
this.compositionView.style.top = cursorTop + 'px';
|
|
||||||
this.compositionView.style.height = cursor.offsetHeight + 'px';
|
|
||||||
this.compositionView.style.lineHeight = cursor.offsetHeight + 'px';
|
|
||||||
// Sync the textarea to the exact position of the composition view so the IME knows where the
|
|
||||||
// text is.
|
|
||||||
var compositionViewBounds = this.compositionView.getBoundingClientRect();
|
|
||||||
this.textarea.style.left = cursor.offsetLeft + 'px';
|
|
||||||
this.textarea.style.top = cursorTop + 'px';
|
|
||||||
this.textarea.style.width = compositionViewBounds.width + 'px';
|
|
||||||
this.textarea.style.height = compositionViewBounds.height + 'px';
|
|
||||||
this.textarea.style.lineHeight = compositionViewBounds.height + 'px';
|
|
||||||
}
|
|
||||||
if (!dontRecurse) {
|
|
||||||
setTimeout(this.updateCompositionElements.bind(this, true), 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clears the textarea's position so that the cursor does not blink on IE.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
CompositionHelper.prototype.clearTextareaPosition = function() {
|
|
||||||
this.textarea.style.left = '';
|
|
||||||
this.textarea.style.top = '';
|
|
||||||
};
|
|
||||||
|
|
||||||
export { CompositionHelper };
|
|
||||||
@@ -1,20 +1,20 @@
|
|||||||
var assert = require('chai').assert;
|
import { assert } from 'chai';
|
||||||
var Terminal = require('../xterm');
|
import { CompositionHelper } from './CompositionHelper';
|
||||||
|
|
||||||
describe('CompositionHelper', function () {
|
describe('CompositionHelper', () => {
|
||||||
var terminal;
|
var terminal;
|
||||||
var compositionHelper;
|
var compositionHelper;
|
||||||
var compositionView;
|
var compositionView;
|
||||||
var textarea;
|
var textarea;
|
||||||
var handledText;
|
var handledText;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(() => {
|
||||||
compositionView = {
|
compositionView = {
|
||||||
classList: {
|
classList: {
|
||||||
add: function () {},
|
add: () => {},
|
||||||
remove: function () {},
|
remove: () => {},
|
||||||
},
|
},
|
||||||
getBoundingClientRect: function () {
|
getBoundingClientRect: () => {
|
||||||
return { width: 0 };
|
return { width: 0 };
|
||||||
},
|
},
|
||||||
style: {
|
style: {
|
||||||
@@ -32,7 +32,7 @@ describe('CompositionHelper', function () {
|
|||||||
};
|
};
|
||||||
terminal = {
|
terminal = {
|
||||||
element: {
|
element: {
|
||||||
querySelector: function () {
|
querySelector: () => {
|
||||||
return { offsetLeft: 0, offsetTop: 0 };
|
return { offsetLeft: 0, offsetTop: 0 };
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -41,53 +41,26 @@ describe('CompositionHelper', function () {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
handledText = '';
|
handledText = '';
|
||||||
compositionHelper = new Terminal.CompositionHelper(textarea, compositionView, terminal);
|
compositionHelper = new CompositionHelper(textarea, compositionView, terminal);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Public API', function () {
|
describe('Input', () => {
|
||||||
it('should define CompositionHelper.prototype.compositionstart', function () {
|
|
||||||
assert.isDefined(Terminal.CompositionHelper.prototype.compositionstart);
|
|
||||||
});
|
|
||||||
it('should define CompositionHelper.prototype.compositionupdate', function () {
|
|
||||||
assert.isDefined(Terminal.CompositionHelper.prototype.compositionupdate);
|
|
||||||
});
|
|
||||||
it('should define CompositionHelper.prototype.compositionend', function () {
|
|
||||||
assert.isDefined(Terminal.CompositionHelper.prototype.compositionend);
|
|
||||||
});
|
|
||||||
it('should define CompositionHelper.prototype.finalizeComposition', function () {
|
|
||||||
assert.isDefined(Terminal.CompositionHelper.prototype.finalizeComposition);
|
|
||||||
});
|
|
||||||
it('should define CompositionHelper.prototype.handleAnyTextareaChanges', function () {
|
|
||||||
assert.isDefined(Terminal.CompositionHelper.prototype.handleAnyTextareaChanges);
|
|
||||||
});
|
|
||||||
it('should define CompositionHelper.prototype.updateCompositionElements', function () {
|
|
||||||
assert.isDefined(Terminal.CompositionHelper.prototype.updateCompositionElements);
|
|
||||||
});
|
|
||||||
it('should define CompositionHelper.isComposing', function () {
|
|
||||||
assert.isDefined(compositionHelper.isComposing);
|
|
||||||
});
|
|
||||||
it('should define CompositionHelper.isSendingComposition', function () {
|
|
||||||
assert.isDefined(compositionHelper.isSendingComposition);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Input', function () {
|
|
||||||
it('Should insert simple characters', function (done) {
|
it('Should insert simple characters', function (done) {
|
||||||
// First character 'ㅇ'
|
// First character 'ㅇ'
|
||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
||||||
textarea.value = 'ㅇ';
|
textarea.value = 'ㅇ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, 'ㅇ');
|
assert.equal(handledText, 'ㅇ');
|
||||||
// Second character 'ㅇ'
|
// Second character 'ㅇ'
|
||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
||||||
textarea.value = 'ㅇㅇ';
|
textarea.value = 'ㅇㅇ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, 'ㅇㅇ');
|
assert.equal(handledText, 'ㅇㅇ');
|
||||||
done();
|
done();
|
||||||
}, 0);
|
}, 0);
|
||||||
@@ -101,29 +74,29 @@ describe('CompositionHelper', function () {
|
|||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
||||||
textarea.value = 'ㅇ';
|
textarea.value = 'ㅇ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionupdate({ data: '아' });
|
compositionHelper.compositionupdate({ data: '아' });
|
||||||
textarea.value = '아';
|
textarea.value = '아';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionupdate({ data: '앙' });
|
compositionHelper.compositionupdate({ data: '앙' });
|
||||||
textarea.value = '앙';
|
textarea.value = '앙';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, '앙');
|
assert.equal(handledText, '앙');
|
||||||
// Second character '앙'
|
// Second character '앙'
|
||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
||||||
textarea.value = '앙ㅇ';
|
textarea.value = '앙ㅇ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionupdate({ data: '아' });
|
compositionHelper.compositionupdate({ data: '아' });
|
||||||
textarea.value = '앙아';
|
textarea.value = '앙아';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionupdate({ data: '앙' });
|
compositionHelper.compositionupdate({ data: '앙' });
|
||||||
textarea.value = '앙앙';
|
textarea.value = '앙앙';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, '앙앙');
|
assert.equal(handledText, '앙앙');
|
||||||
done();
|
done();
|
||||||
}, 0);
|
}, 0);
|
||||||
@@ -141,21 +114,21 @@ describe('CompositionHelper', function () {
|
|||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
||||||
textarea.value = 'ㅇ';
|
textarea.value = 'ㅇ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionupdate({ data: '아' });
|
compositionHelper.compositionupdate({ data: '아' });
|
||||||
textarea.value = '아';
|
textarea.value = '아';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
// Start second character '아' in first character
|
// Start second character '아' in first character
|
||||||
compositionHelper.compositionupdate({ data: '앙' });
|
compositionHelper.compositionupdate({ data: '앙' });
|
||||||
textarea.value = '앙';
|
textarea.value = '앙';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: '아' });
|
compositionHelper.compositionupdate({ data: '아' });
|
||||||
textarea.value = '아아'
|
textarea.value = '아아'
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, '아아');
|
assert.equal(handledText, '아아');
|
||||||
done();
|
done();
|
||||||
}, 0);
|
}, 0);
|
||||||
@@ -170,16 +143,16 @@ describe('CompositionHelper', function () {
|
|||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'd' });
|
compositionHelper.compositionupdate({ data: 'd' });
|
||||||
textarea.value = 'd';
|
textarea.value = 'd';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionupdate({ data: 'だ' });
|
compositionHelper.compositionupdate({ data: 'だ' });
|
||||||
textarea.value = 'だ';
|
textarea.value = 'だ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
// Second character 'あ'
|
// Second character 'あ'
|
||||||
compositionHelper.compositionupdate({ data: 'だあ' });
|
compositionHelper.compositionupdate({ data: 'だあ' });
|
||||||
textarea.value = 'だあ';
|
textarea.value = 'だあ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, 'だあ');
|
assert.equal(handledText, 'だあ');
|
||||||
done();
|
done();
|
||||||
}, 0);
|
}, 0);
|
||||||
@@ -193,20 +166,20 @@ describe('CompositionHelper', function () {
|
|||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'd' });
|
compositionHelper.compositionupdate({ data: 'd' });
|
||||||
textarea.value = 'd';
|
textarea.value = 'd';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionupdate({ data: 'だ' });
|
compositionHelper.compositionupdate({ data: 'だ' });
|
||||||
textarea.value = 'だ';
|
textarea.value = 'だ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
// Second character 'ー'
|
// Second character 'ー'
|
||||||
compositionHelper.compositionupdate({ data: 'だー' });
|
compositionHelper.compositionupdate({ data: 'だー' });
|
||||||
textarea.value = 'だー';
|
textarea.value = 'だー';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
// Convert to katakana 'ダー'
|
// Convert to katakana 'ダー'
|
||||||
compositionHelper.compositionupdate({ data: 'ダー' });
|
compositionHelper.compositionupdate({ data: 'ダー' });
|
||||||
textarea.value = 'ダー';
|
textarea.value = 'ダー';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, 'ダー');
|
assert.equal(handledText, 'ダー');
|
||||||
done();
|
done();
|
||||||
}, 0);
|
}, 0);
|
||||||
@@ -221,20 +194,20 @@ describe('CompositionHelper', function () {
|
|||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'い' });
|
compositionHelper.compositionupdate({ data: 'い' });
|
||||||
textarea.value = 'い';
|
textarea.value = 'い';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
// Second character 'ま'
|
// Second character 'ま'
|
||||||
compositionHelper.compositionupdate({ data: 'いm' });
|
compositionHelper.compositionupdate({ data: 'いm' });
|
||||||
textarea.value = 'いm';
|
textarea.value = 'いm';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionupdate({ data: 'いま' });
|
compositionHelper.compositionupdate({ data: 'いま' });
|
||||||
textarea.value = 'いま';
|
textarea.value = 'いま';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
// Convert to kanji '今'
|
// Convert to kanji '今'
|
||||||
compositionHelper.compositionupdate({ data: '今' });
|
compositionHelper.compositionupdate({ data: '今' });
|
||||||
textarea.value = '今';
|
textarea.value = '今';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, '今');
|
assert.equal(handledText, '今');
|
||||||
done();
|
done();
|
||||||
}, 0);
|
}, 0);
|
||||||
@@ -249,11 +222,11 @@ describe('CompositionHelper', function () {
|
|||||||
compositionHelper.compositionstart();
|
compositionHelper.compositionstart();
|
||||||
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
compositionHelper.compositionupdate({ data: 'ㅇ' });
|
||||||
textarea.value = 'ㅇ';
|
textarea.value = 'ㅇ';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
compositionHelper.compositionend();
|
compositionHelper.compositionend();
|
||||||
// Second character '1' (a non-composition character)
|
// Second character '1' (a non-composition character)
|
||||||
textarea.value = 'ㅇ1';
|
textarea.value = 'ㅇ1';
|
||||||
setTimeout(function() { // wait for any textarea updates
|
setTimeout(() => { // wait for any textarea updates
|
||||||
assert.equal(handledText, 'ㅇ1');
|
assert.equal(handledText, 'ㅇ1');
|
||||||
done();
|
done();
|
||||||
}, 0);
|
}, 0);
|
||||||
@@ -0,0 +1,233 @@
|
|||||||
|
/**
|
||||||
|
* xterm.js: xterm, in the browser
|
||||||
|
* Copyright (c) 2014-2016, SourceLair Private Company (www.sourcelair.com (MIT License)
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ITerminal } from './Interfaces';
|
||||||
|
|
||||||
|
interface IPosition {
|
||||||
|
start: number;
|
||||||
|
end: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates the logic for handling compositionstart, compositionupdate and compositionend
|
||||||
|
* events, displaying the in-progress composition to the UI and forwarding the final composition
|
||||||
|
* to the handler.
|
||||||
|
*/
|
||||||
|
export class CompositionHelper {
|
||||||
|
/**
|
||||||
|
* Whether input composition is currently happening, eg. via a mobile keyboard, speech input or
|
||||||
|
* IME. This variable determines whether the compositionText should be displayed on the UI.
|
||||||
|
*/
|
||||||
|
private isComposing: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The position within the input textarea's value of the current composition.
|
||||||
|
*/
|
||||||
|
private compositionPosition: IPosition;;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether a composition is in the process of being sent, setting this to false will cancel any
|
||||||
|
* in-progress composition.
|
||||||
|
*/
|
||||||
|
private isSendingComposition: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new CompositionHelper.
|
||||||
|
* @param textarea The textarea that xterm uses for input.
|
||||||
|
* @param compositionView The element to display the in-progress composition in.
|
||||||
|
* @param terminal The Terminal to forward the finished composition to.
|
||||||
|
*/
|
||||||
|
constructor(
|
||||||
|
private textarea: HTMLTextAreaElement,
|
||||||
|
private compositionView: HTMLElement,
|
||||||
|
private terminal: ITerminal
|
||||||
|
) {
|
||||||
|
this.isComposing = false;
|
||||||
|
this.isSendingComposition = false;
|
||||||
|
this.compositionPosition = { start: null, end: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the compositionstart event, activating the composition view.
|
||||||
|
*/
|
||||||
|
public compositionstart() {
|
||||||
|
this.isComposing = true;
|
||||||
|
this.compositionPosition.start = this.textarea.value.length;
|
||||||
|
this.compositionView.textContent = '';
|
||||||
|
this.compositionView.classList.add('active');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the compositionupdate event, updating the composition view.
|
||||||
|
* @param {CompositionEvent} ev The event.
|
||||||
|
*/
|
||||||
|
public compositionupdate(ev: CompositionEvent) {
|
||||||
|
this.compositionView.textContent = ev.data;
|
||||||
|
this.updateCompositionElements();
|
||||||
|
var self = this;
|
||||||
|
setTimeout(function() {
|
||||||
|
self.compositionPosition.end = self.textarea.value.length;
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the compositionend event, hiding the composition view and sending the composition to
|
||||||
|
* the handler.
|
||||||
|
*/
|
||||||
|
public compositionend() {
|
||||||
|
this.finalizeComposition(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the keydown event, routing any necessary events to the CompositionHelper functions.
|
||||||
|
* @param ev The keydown event.
|
||||||
|
* @return Whether the Terminal should continue processing the keydown event.
|
||||||
|
*/
|
||||||
|
public keydown(ev: KeyboardEvent) {
|
||||||
|
if (this.isComposing || this.isSendingComposition) {
|
||||||
|
if (ev.keyCode === 229) {
|
||||||
|
// Continue composing if the keyCode is the "composition character"
|
||||||
|
return false;
|
||||||
|
} else if (ev.keyCode === 16 || ev.keyCode === 17 || ev.keyCode === 18) {
|
||||||
|
// Continue composing if the keyCode is a modifier key
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
// Finish composition immediately. This is mainly here for the case where enter is
|
||||||
|
// pressed and the handler needs to be triggered before the command is executed.
|
||||||
|
this.finalizeComposition(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ev.keyCode === 229) {
|
||||||
|
// If the "composition character" is used but gets to this point it means a non-composition
|
||||||
|
// character (eg. numbers and punctuation) was pressed when the IME was active.
|
||||||
|
this.handleAnyTextareaChanges();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finalizes the composition, resuming regular input actions. This is called when a composition
|
||||||
|
* is ending.
|
||||||
|
* @param waitForPropogation Whether to wait for events to propogate before sending
|
||||||
|
* the input. This should be false if a non-composition keystroke is entered before the
|
||||||
|
* compositionend event is triggered, such as enter, so that the composition is send before
|
||||||
|
* the command is executed.
|
||||||
|
*/
|
||||||
|
private finalizeComposition(waitForPropogation: boolean) {
|
||||||
|
this.compositionView.classList.remove('active');
|
||||||
|
this.isComposing = false;
|
||||||
|
this.clearTextareaPosition();
|
||||||
|
|
||||||
|
if (!waitForPropogation) {
|
||||||
|
// Cancel any delayed composition send requests and send the input immediately.
|
||||||
|
this.isSendingComposition = false;
|
||||||
|
var input = this.textarea.value.substring(this.compositionPosition.start, this.compositionPosition.end);
|
||||||
|
this.terminal.handler(input);
|
||||||
|
} else {
|
||||||
|
// Make a deep copy of the composition position here as a new compositionstart event may
|
||||||
|
// fire before the setTimeout executes.
|
||||||
|
var currentCompositionPosition = {
|
||||||
|
start: this.compositionPosition.start,
|
||||||
|
end: this.compositionPosition.end,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since composition* events happen before the changes take place in the textarea on most
|
||||||
|
// browsers, use a setTimeout with 0ms time to allow the native compositionend event to
|
||||||
|
// complete. This ensures the correct character is retrieved, this solution was used
|
||||||
|
// because:
|
||||||
|
// - The compositionend event's data property is unreliable, at least on Chromium
|
||||||
|
// - The last compositionupdate event's data property does not always accurately describe
|
||||||
|
// the character, a counter example being Korean where an ending consonsant can move to
|
||||||
|
// the following character if the following input is a vowel.
|
||||||
|
var self = this;
|
||||||
|
this.isSendingComposition = true;
|
||||||
|
setTimeout(function () {
|
||||||
|
// Ensure that the input has not already been sent
|
||||||
|
if (self.isSendingComposition) {
|
||||||
|
self.isSendingComposition = false;
|
||||||
|
var input;
|
||||||
|
if (self.isComposing) {
|
||||||
|
// Use the end position to get the string if a new composition has started.
|
||||||
|
input = self.textarea.value.substring(currentCompositionPosition.start, currentCompositionPosition.end);
|
||||||
|
} else {
|
||||||
|
// Don't use the end position here in order to pick up any characters after the
|
||||||
|
// composition has finished, for example when typing a non-composition character
|
||||||
|
// (eg. 2) after a composition character.
|
||||||
|
input = self.textarea.value.substring(currentCompositionPosition.start);
|
||||||
|
}
|
||||||
|
self.terminal.handler(input);
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply any changes made to the textarea after the current event chain is allowed to complete.
|
||||||
|
* This should be called when not currently composing but a keydown event with the "composition
|
||||||
|
* character" (229) is triggered, in order to allow non-composition text to be entered when an
|
||||||
|
* IME is active.
|
||||||
|
*/
|
||||||
|
private handleAnyTextareaChanges() {
|
||||||
|
var oldValue = this.textarea.value;
|
||||||
|
var self = this;
|
||||||
|
setTimeout(function() {
|
||||||
|
// Ignore if a composition has started since the timeout
|
||||||
|
if (!self.isComposing) {
|
||||||
|
var newValue = self.textarea.value;
|
||||||
|
var diff = newValue.replace(oldValue, '');
|
||||||
|
if (diff.length > 0) {
|
||||||
|
self.terminal.handler(diff);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Positions the composition view on top of the cursor and the textarea just below it (so the
|
||||||
|
* IME helper dialog is positioned correctly).
|
||||||
|
* @param dontRecurse Whether to use setTimeout to recursively trigger another update, this is
|
||||||
|
* necessary as the IME events across browsers are not consistently triggered.
|
||||||
|
*/
|
||||||
|
public updateCompositionElements(dontRecurse?: boolean) {
|
||||||
|
if (!this.isComposing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var cursor = <HTMLElement>this.terminal.element.querySelector('.terminal-cursor');
|
||||||
|
if (cursor) {
|
||||||
|
// Take .xterm-rows offsetTop into account as well in case it's positioned absolutely within
|
||||||
|
// the .xterm element.
|
||||||
|
var xtermRows = <HTMLElement>this.terminal.element.querySelector('.xterm-rows');
|
||||||
|
var cursorTop = xtermRows.offsetTop + cursor.offsetTop;
|
||||||
|
|
||||||
|
this.compositionView.style.left = cursor.offsetLeft + 'px';
|
||||||
|
this.compositionView.style.top = cursorTop + 'px';
|
||||||
|
this.compositionView.style.height = cursor.offsetHeight + 'px';
|
||||||
|
this.compositionView.style.lineHeight = cursor.offsetHeight + 'px';
|
||||||
|
// Sync the textarea to the exact position of the composition view so the IME knows where the
|
||||||
|
// text is.
|
||||||
|
var compositionViewBounds = this.compositionView.getBoundingClientRect();
|
||||||
|
this.textarea.style.left = cursor.offsetLeft + 'px';
|
||||||
|
this.textarea.style.top = cursorTop + 'px';
|
||||||
|
this.textarea.style.width = compositionViewBounds.width + 'px';
|
||||||
|
this.textarea.style.height = compositionViewBounds.height + 'px';
|
||||||
|
this.textarea.style.lineHeight = compositionViewBounds.height + 'px';
|
||||||
|
}
|
||||||
|
if (!dontRecurse) {
|
||||||
|
setTimeout(this.updateCompositionElements.bind(this, true), 0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the textarea's position so that the cursor does not blink on IE.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private clearTextareaPosition() {
|
||||||
|
this.textarea.style.left = '';
|
||||||
|
this.textarea.style.top = '';
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -4,11 +4,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export interface ITerminal {
|
export interface ITerminal {
|
||||||
|
element: HTMLElement;
|
||||||
rowContainer: HTMLElement;
|
rowContainer: HTMLElement;
|
||||||
ydisp: number;
|
ydisp: number;
|
||||||
lines: string[];
|
lines: string[];
|
||||||
rows: number;
|
rows: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emit the 'data' event and populate the given data.
|
||||||
|
* @param data The data to populate in the event.
|
||||||
|
*/
|
||||||
|
handler(data: string);
|
||||||
on(event: string, callback: () => void);
|
on(event: string, callback: () => void);
|
||||||
scrollDisp(disp: number, suppressScrollEvent: boolean);
|
scrollDisp(disp: number, suppressScrollEvent: boolean);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5135,7 +5135,6 @@ var wcwidth = (function(opts) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Terminal.EventEmitter = EventEmitter;
|
Terminal.EventEmitter = EventEmitter;
|
||||||
Terminal.CompositionHelper = CompositionHelper;
|
|
||||||
Terminal.inherits = inherits;
|
Terminal.inherits = inherits;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user