Textarea for key handling works

Copy and paste need rework.
This commit is contained in:
paris
2014-03-26 19:38:00 +00:00
parent 5fd1948b9e
commit 731ffe1a75
2 changed files with 49 additions and 108 deletions
+9 -2
View File
@@ -35,17 +35,24 @@
* Default style for xterm.js * Default style for xterm.js
*/ */
.terminal { .xterm {
background-color: #000; background-color: #000;
color: #fff; color: #fff;
outline: none; outline: none;
font-family: courier-new, courier, monospace; font-family: courier-new, courier, monospace;
} }
.terminal .terminal-cursor { .xterm .terminal-cursor {
background-color: #fff; background-color: #fff;
} }
.xterm .xterm-helper-textarea {
opacity: 0;
width: 0;
height: 0;
z-index: -10;
}
/* /*
* Determine default colors for xterm.js * Determine default colors for xterm.js
*/ */
+41 -107
View File
@@ -384,20 +384,17 @@ each(keys(Terminal.defaults), function(key) {
* Focused Terminal * Focused Terminal
*/ */
Terminal.prototype.focus = function() { Terminal.prototype.focus = function() {
if (Terminal.focus === this) { if (document.activeElement === this.textarea) {
return; return;
} }
if (Terminal.focus) {
Terminal.focus.blur();
}
if (this.sendFocus) { if (this.sendFocus) {
this.send('\x1b[I'); this.send('\x1b[I');
} }
this.showCursor();
Terminal.focus = this; this.showCursor();
this.textarea.focus();
this.emit('focus', {terminal: this}); this.emit('focus', {terminal: this});
}; };
@@ -417,130 +414,63 @@ Terminal.prototype.blur = function() {
}; };
/** /**
* Initialize global behavior * Initialize default behavior
*/ */
Terminal.prototype.initGlobal = function() { Terminal.prototype.initGlobal = function() {
var document = this.document; Terminal.bindPaste(this);
Terminal.bindKeys(this);
Terminal._boundDocs = Terminal._boundDocs || []; Terminal.bindCopy(this);
if (~indexOf(Terminal._boundDocs, document)) {
return;
}
Terminal._boundDocs.push(document);
Terminal.bindPaste(document);
Terminal.bindKeys(document);
Terminal.bindCopy(document);
if (this.isIpad || this.isIphone) {
Terminal.fixIpad(document);
}
}; };
/** /**
* Bind to paste event * Bind to paste event
*/ */
Terminal.bindPaste = function(document) { Terminal.bindPaste = function(term) {
// This seems to work well for ctrl-V and middle-click, on([term.textarea, term.element], 'paste', function(ev) {
// even without the contentEditable workaround.
var window = document.defaultView;
on(window, 'paste', function(ev) {
var term = Terminal.focus;
if (!term) return;
if (ev.clipboardData) { if (ev.clipboardData) {
term.send(ev.clipboardData.getData('text/plain')); term.send(ev.clipboardData.getData('text/plain'));
} else if (term.context.clipboardData) { } else if (term.context.clipboardData) {
term.send(term.context.clipboardData.getData('Text')); term.send(term.context.clipboardData.getData('Text'));
} }
// Not necessary. Do it anyway for good measure. term.textarea.value = '';
term.element.contentEditable = 'inherit';
return term.cancel(ev); return term.cancel(ev);
}); });
}; };
/** /*
* Global Events for key handling * Apply key handling to the terminal's textarea
*/ */
Terminal.bindKeys = function(document) { Terminal.bindKeys = function(term) {
// We should only need to check `target === body` below, on(term.textarea, 'keydown', function(ev) {
// but we can check everything for good measure. term.keyDown(ev);
on(document, 'keydown', function(ev) {
if (!Terminal.focus) return;
var target = ev.target || ev.srcElement;
if (!target) return;
if (target === Terminal.focus.element
|| target === Terminal.focus.context
|| target === Terminal.focus.document
|| target === Terminal.focus.body
|| target === Terminal._textarea
|| target === Terminal.focus.parent) {
return Terminal.focus.keyDown(ev);
}
}, true); }, true);
on(document, 'keypress', function(ev) { on(term.textarea, 'keypress', function(ev) {
if (!Terminal.focus) return; term.keyPress(ev);
var target = ev.target || ev.srcElement;
if (!target) return; /*
if (target === Terminal.focus.element * Truncate the textarea's value, since it is not needed
|| target === Terminal.focus.context */
|| target === Terminal.focus.document this.value = '';
|| target === Terminal.focus.body
|| target === Terminal._textarea
|| target === Terminal.focus.parent) {
return Terminal.focus.keyPress(ev);
}
}, true); }, true);
// If we click somewhere other than a /*
// terminal, unfocus the terminal. * When mousedown happens on the terminal element,
on(document, 'mousedown', function(ev) { * focus on its textarea
if (!Terminal.focus) return; */
on(term.element, 'mouseup', function(ev) {
var el = ev.target || ev.srcElement; term.focus();
if (!el) return;
do {
if (el === Terminal.focus.element) return;
} while (el = el.parentNode);
Terminal.focus.blur();
}); });
}; };
/** /*
* Copy Selection w/ Ctrl-C (Select Mode) * Bind copy event
*/ */
Terminal.bindCopy = function(document) { Terminal.bindCopy = function(term) {
var window = document.defaultView; on(term.element, 'copy', function(ev) {
// if (!('onbeforecopy' in document)) {
// // Copies to *only* the clipboard.
// on(window, 'copy', function fn(ev) {
// var term = Terminal.focus;
// if (!term) return;
// if (!term._selected) return;
// var text = term.grabText(
// term._selected.x1, term._selected.x2,
// term._selected.y1, term._selected.y2);
// term.emit('copy', text);
// ev.clipboardData.setData('text/plain', text);
// });
// return;
// }
// Copies to primary selection *and* clipboard.
// NOTE: This may work better on capture phase,
// or using the `beforecopy` event.
on(window, 'copy', function(ev) {
var term = Terminal.focus;
if (!term) return;
if (!term._selected) return;
var textarea = term.getCopyTextarea(); var textarea = term.getCopyTextarea();
var text = term.grabText( var text = term.grabText(
term._selected.x1, term._selected.x2, term._selected.x1, term._selected.x2,
@@ -551,7 +481,6 @@ Terminal.bindCopy = function(document) {
textarea.value = text; textarea.value = text;
textarea.setSelectionRange(0, text.length); textarea.setSelectionRange(0, text.length);
setTimeout(function() { setTimeout(function() {
term.element.focus();
term.focus(); term.focus();
}, 1); }, 1);
}); });
@@ -607,7 +536,7 @@ Terminal.prototype.open = function(parent) {
* Create main element container * Create main element container
*/ */
this.element = this.document.createElement('div'); this.element = this.document.createElement('div');
this.element.classList.add('terminal', 'xterm-theme-' + this.theme); this.element.classList.add('terminal', 'xterm', 'xterm-theme-' + this.theme);
this.element.setAttribute('tabindex', 0); this.element.setAttribute('tabindex', 0);
/* /*
@@ -626,7 +555,7 @@ Terminal.prototype.open = function(parent) {
this.helperContainer = document.createElement('div'); this.helperContainer = document.createElement('div');
this.helperContainer.classList.add('xterm-helpers'); this.helperContainer.classList.add('xterm-helpers');
this.element.appendChild(this.helperContainer); this.element.appendChild(this.helperContainer);
this.textarea = document.createElement('div'); this.textarea = document.createElement('textarea');
this.textarea.classList.add('xterm-helper-textarea'); this.textarea.classList.add('xterm-helper-textarea');
this.helperContainer.appendChild(this.textarea); this.helperContainer.appendChild(this.textarea);
@@ -5328,7 +5257,12 @@ Terminal.charsets.ISOLatin = null; // /A
*/ */
function on(el, type, handler, capture) { function on(el, type, handler, capture) {
el.addEventListener(type, handler, capture || false); if (!Array.isArray(el)) {
el = [el];
}
el.forEach(function (element) {
element.addEventListener(type, handler, capture || false);
});
} }
function off(el, type, handler, capture) { function off(el, type, handler, capture) {