<commit_before>
class InputHandler {

	constructor(input) {

		this.ctx = input;
		this.shiji = this.ctx.ctx.shiji;
		this.astHandler = this.ctx.astHandler;
		this.$inputBox = $('<iframe>')
			.css({
				width: this.shiji.$origin.width(),
				position: 'absolute',
				top: 0,
				left: 0,
//				border: '1px solid orange',
				border: '0px',
				display: 'none'
			});

		this.shiji.$overlay.append(this.$inputBox);

		$(this.$inputBox.get(0).contentWindow).on('load', function() {
			console.log('ready');
		});

		this.$inputBody = this.$inputBox
			.contents()
			.find('body');

		var originContent = null;
		var preeditMode = false;
		this.$inputBody
			.attr('contenteditable', true)
			.on('compositionstart', function(e) {
				// Display input box
				this.$inputBox.css({
					display: ''
				});

				preeditMode = true;
			}.bind(this))
			.on('compositionend', function(e) {

				// Hide input box
				this.$inputBox.css({
					display: 'none'
				});

				preeditMode = false;
			}.bind(this))
			.on('keydown', function(e) {
				var cursor = this.ctx.ctx.caret;

				if (e.which == 229) {

					if (!originContent) {
						originContent = cursor.startNode.text.slice(0);
					} else {
						cursor.startNode.text = originContent.slice(0);
					}

					var buffer = this.$inputBody.text();
					this.astHandler.insert(cursor.startNode, cursor.startOffset, buffer);

					// done everything so we update now
					var task = cursor.startNode.component.refresh();
					task.then(function() {

						// Entered already
						if (!preeditMode) {
							this.$inputBody.empty();

							// clear buffer
							originContent = null;

							// Set new position to caret
							cursor.move(buffer.length);
							cursor.show();

							this.setCursorPosition(cursor.$caret.css('left'), cursor.$caret.css('top'));
						}
					}.bind(this));
				}

			}.bind(this))
			.on('keypress', function(e) {
				var cursor = this.ctx.ctx.caret;

				this.astHandler.insert(cursor.startNode, cursor.startOffset, String.fromCharCode(e.keyCode));

				// done everything so we update now
				var task = cursor.startNode.component.refresh();
				task.then(function() {
					this.$inputBody.empty();

					// Set new position to caret
					cursor.move(1);
					cursor.show();

					this.setCursorPosition(cursor.$caret.css('left'), cursor.$caret.css('top'));<commit_msg>support direction keys in new input handler<commit_after>var Key = {
	Left: 37,
	Up: 38,
	Right: 39,
	Down: 40,
	Backspace: 8,
};

class InputHandler {

	constructor(input) {

		this.ctx = input;
		this.shiji = this.ctx.ctx.shiji;
		this.astHandler = this.ctx.astHandler;
		this.$inputBox = $('<iframe>')
			.css({
				width: this.shiji.$origin.width(),
				position: 'absolute',
				top: 0,
				left: 0,
//				border: '1px solid orange',
				border: '0px',
				display: 'none'
			});

		this.shiji.$overlay.append(this.$inputBox);

		$(this.$inputBox.get(0).contentWindow).on('load', function() {
			console.log('ready');
		});

		this.$inputBody = this.$inputBox
			.contents()
			.find('body');

		var originContent = null;
		var preeditMode = false;
		this.$inputBody
			.attr('contenteditable', true)
			.on('compositionstart', function(e) {
				// Display input box
				this.$inputBox.css({
					display: ''
				});

				preeditMode = true;
			}.bind(this))
			.on('compositionend', function(e) {

				// Hide input box
				this.$inputBox.css({
					display: 'none'
				});

				preeditMode = false;
			}.bind(this))
			.on('keydown', function(e) {
				var cursor = this.ctx.ctx.caret;

				if (e.which == 229) {

					if (!originContent) {
						originContent = cursor.startNode.text.slice(0);
					} else {
						cursor.startNode.text = originContent.slice(0);
					}

					var buffer = this.$inputBody.text();
					this.astHandler.insert(cursor.startNode, cursor.startOffset, buffer);

					// done everything so we update now
					var task = cursor.startNode.component.refresh();
					task.then(function() {

						// Entered already
						if (!preeditMode) {
							this.$inputBody.empty();

							// clear buffer
							originContent = null;

							// Set new position to caret
							cursor.move(buffer.length);
							cursor.show();

							this.setCursorPosition(cursor.$caret.css('left'), cursor.$caret.css('top'));
						}
					}.bind(this));

					return;
				}

				// Direction keys
				switch(e.keyCode) {
				case Key.Left:

					cursor.move(-1);
					cursor.show();

					break;

				case Key.Right:

					cursor.move(1);
					cursor.show();

					break;
				}

			}.bind(this))
			.on('keypress', function(e) {
				var cursor = this.ctx.ctx.caret;

				this.astHandler.insert(cursor.startNode, cursor.startOffset, String.fromCharCode(e.keyCode));

				// done everything so we update now
				var task = cursor.startNode.component.refresh();
				task.then(function() {
					this.$inputBody.empty();

					// Set new position to caret
					cursor.move(1);
					cursor.show();

					this.setCursorPosition(cursor.$caret.css('left'), cursor.$caret.css('top'));
{'fraction_non_alphanumeric': 0.16240333135038668, 'fraction_numerical': 0.006543723973825104, 'mean_word_length': 2.6235632183908044, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5284730', 'n_tokens_mistral': 2215, 'n_tokens_neox': 1625, 'n_words': 322}