Created
October 10, 2014 13:58
-
-
Save iclems/31b44bb7aba9bf7713a8 to your computer and use it in GitHub Desktop.
Firepad Copy/Paste
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was a great help, thank you.