layerssss / paste.js

read image/text data from clipboard (cross-browser)
http://layerssss.github.io/paste.js/
MIT License
463 stars 94 forks source link

About textarea can not be paste when I use ctrl+v to paste text in textarea (using firfox) #19

Closed iamkevinwang closed 8 years ago

iamkevinwang commented 8 years ago

Hi, I have some question. following is my situation: I copy some plain text and paste plain text to textarea on your example web site (http://micy.in/paste.js/) . but I found out that when I use Firfox(IE 10 also),I can't paste plain text to textarea. It's only trigger pasteText Event and append result after textarea.(see pic 1)

default *pic 1

But it's work on chrome.(see pic 2)

2 *pic 2

I trace the pastejs source code and found pastejs would change it's behaviour between chrome and other browser.(see pic 3)

3 *pic 3

I have tried to make all browser to behave as the same as possible, but after trial and error, I still could not make different browsers to work and behave the same.

Can you give me some advice?

layerssss commented 8 years ago

Thanks for reporting, this is certainly an issue of paste.js. I tried to make behaviours across different browsers the same, but most of my time have been focused on image pasting, so text pasting could have been missed out.

xiwc commented 8 years ago

I have the same problem, is there any fix solution? 3ks!

pkaminski commented 8 years ago

Here's the workaround I use:

var inChrome = navigator.userAgent.toLowerCase().indexOf('chrome') !== -1;
element.pastableTextarea().on('pasteText', function(ev, data) {
  if (inChrome) return;  // default paste event will work
  var cursorStart = element.prop('selectionStart');
  var cursorEnd = element.prop('selectionEnd');
  var contents = element.val();
  element.val(contents.slice(0, cursorStart) + data.text + contents.slice(cursorEnd));
  element[0].setSelectionRange(cursorStart + data.text.length, cursorStart + data.text.length);
  element.trigger('change');
});
xiwc commented 8 years ago

@pkaminski 3ks, i will try.

layerssss commented 8 years ago

Hi, people, this issue is fixed using @pkaminski 's workaround. Now all browsers (including IE10) should have un-affected textareas, like in Chrome. Image-pasting still won't work under IE10, but text-pasting is not "broken" anymore.