Skip to content

Instantly share code, notes, and snippets.

@iclems
Created October 10, 2014 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iclems/31b44bb7aba9bf7713a8 to your computer and use it in GitHub Desktop.
Save iclems/31b44bb7aba9bf7713a8 to your computer and use it in GitHub Desktop.
Firepad Copy/Paste
var MIME_TYPE = {
PLAIN: 'text/plain',
HTML: window.clipboardData ? 'Text' : 'text/html'
};
var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent);
function selectInput(node) {
if (ios) { // Mobile Safari apparently has a bug where select() is broken.
node.selectionStart = 0;
node.selectionEnd = node.value.length;
} else {
// Suppress mysterious IE10 errors
try { node.select(); }
catch(_e) {}
}
}
var onFirepadCopyEvent = function(e) {
var input = self._cm.getInputField();
var clipboardData = e.clipboardData || null;
var copyText = input.value.replace(new RegExp('['+LineSentinelCharacter+EntitySentinelCharacter+']', 'g'), '');
selectInput(input);
if (clipboardData) {
e.preventDefault();
var html = self._fp.getHtmlFromSelection();
clipboardData.setData(MIME_TYPE.PLAIN, copyText);
clipboardData.setData(MIME_TYPE.HTML, html);
}
};
this._fp.on('ready', function() {
this._cm.getInputField().addEventListener('copy', onFirepadCopyEvent);
this._cm.getInputField().addEventListener('cut', function(e) {
onFirepadCopyEvent(e);
this._cm.replaceSelection('');
}.bind(this));
this._cm.getInputField().addEventListener('paste', function(e) {
var clipboardData = e.clipboardData || null;
if (clipboardData && clipboardData.getData && clipboardData.getData(MIME_TYPE.HTML) {
var html = clipboardData.getData(MIME_TYPE.HTML) || '';
this._cm.replaceSelection('');
this._fp.insertHtmlAtCursor(html);
e.preventDefault();
}
}.bind(this));
}
@q-state
Copy link

q-state commented Dec 5, 2014

This was a great help, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment