Skip to content

Instantly share code, notes, and snippets.

@risacher
Last active December 7, 2016 17:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save risacher/7837761 to your computer and use it in GitHub Desktop.
Save risacher/7837761 to your computer and use it in GitHub Desktop.
tty.js/static/user.js to enable context-menu pasting in Firefox/gecko with tty.js web terminal.
var on = window.Terminal.on;
var isGecko = navigator.userAgent.indexOf('WebKit')==-1 && navigator.product == 'Gecko';
var isChromium = window.chrome;
// replace bindPaste with a variant that:
// 1. doesn't set contentEditable back to 'inherit'
// 2. replaces newlines with carriage returns in the clip
// 3. moves the caret (i.e. the contentEditable cursor) out of sight.
window.Terminal.bindPaste = function(document) {
// This seems to work well for ctrl-V and middle-click,
// even without the contentEditable workaround.
var window = document.defaultView;
on(window, 'paste', function(ev) {
//console.log('paste handler called');
var term = Terminal.focus;
if (!term) return;
var clip;
if (ev.clipboardData) {
clip = ev.clipboardData.getData('text/plain');
} else if (term.context.clipboardData) {
clip = term.context.clipboardData.getData('Text');
}
// Its a safe bet that newlines in the clipboard should be sent as ^m
clip = clip.replace(/\n/g, "\r");
term.send(clip);
// consider redraw the terminal since the pasted text may be cluttering it up.
// term.refresh(0, term.rows - 1);
placeCaretAtEnd( term.element );
// If we do this, we can't paste any more...
// term.element.contentEditable = 'inherit';
term.cancel(ev);
});
};
// patchup Terminal.open to add a new mousedown handler that pretty aggressively
// sets contentEditable to true. Also disables spellcheck (HTML5 browsers).
var oldopen = window.Terminal.prototype.open;
window.Terminal.prototype.open = function (parent) {
var self = this;
oldopen.apply(this, arguments);
if (isGecko || isChromium) {
on(this.element, 'mousedown', function(ev) {
console.log('new mousedown code reached');
var button = ev.button != null
? +ev.button
: ev.which != null
? ev.which - 1
: null;
if (button !== 2) return;
// ev.target.addEventListener('DOMCharacterDataModified', _handler, false);
ev.target.contentEditable = true;
}, true);
self.element.contentEditable = true;
}
self.element.spellcheck = false;
};
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment