0

I am trying to stop a paste event and re-issue it with different data (the if statement is for preventing constant re-issuing, ignore it):

  @HostListener('paste', ['$event']) onPaste(e) {
    if (this.doNotReissuePaste) {
      this.doNotReissuePaste = false;
    } else {
      this.reformatPasteText(e);
      e.preventDefault();
    }
  }

My goal is to pretend the first event never happened, because I need Angular2 to process the field "normally". This part is just for context, the question is how do I re-issue the event without the defaultPrevented property being true? I can't change it because it's read only, and I can't find docs on creating a brand new paste event anywhere (which is why I resort to copying it).

  private reformatPasteText(pasteEvent) {

    this.doNotReissuePaste = true;

    let pastedText: string = pasteEvent.clipboardData.getData('Text');
    let formattedPaste: string = formattingMagicWheeee(pastedText); 

    //pasteEvent.defaultPrevented = false; //Cannot change it this way 

    setTimeout(() => {

      pasteEvent.clipboardData.setData('text/plain', formattedPaste);

      this.target.dispatchEvent(pasteEvent);

    }, 1);
  }

P.S. Ignore the Angular2 part - no need to think about it, this is purely a Javascript question on creating/editing Javascript events.

VSO
  • 11,546
  • 25
  • 99
  • 187
  • _I can't find docs on creating a brand new paste event.._ In the same way as any other event is creating - [dispatchEvent](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent) – hindmost Nov 28 '16 at 15:38
  • So what's the point here? If you catch a `paste` event, and then trigger the same event in the event handler, you basically have an infinite loop, the only thing stopping the browser from totally crashing is probably the timeout ? – adeneo Nov 28 '16 at 15:39
  • @adeneo The point is to format the text and remove "illegal" characters before it is paste. For a really simple example, lets say someone pasted some profanity, and you want to formatMagic function to filter it. Anyway, the if statement prevents the infinite loop. – VSO Nov 28 '16 at 15:44
  • @hindmost Looking at that now, thanks. – VSO Nov 28 '16 at 15:44
  • Why would you need a profanity filter on the clientside that removes the values on the fly, does it really matter if the client posts swearwords only they can see? – adeneo Nov 28 '16 at 15:48
  • @adeneo: That was just an example. We are writing a credit card display UI, so the directive does stuff like adding spaces on every key input so the CC # is easier to read for the user, e.g. xxxx xxxx xxxx xxxx instead of xxxxxxxxxxxxxx, etc. – VSO Nov 28 '16 at 15:59
  • 1
    https://jsfiddle.net/szcweg4o/ – adeneo Nov 28 '16 at 16:01
  • @adeneo: Lol, thanks, but I had that part figured out, I did stress that it was just an example initially. I really do need to reformat the string, not filter it. – VSO Nov 28 '16 at 16:04
  • 1
    [here](https://www.w3.org/TR/clipboard-apis/#clipboard-event-interfaces) you will see that dispatching the event will do absolutely nothing, sorry... –  Nov 28 '16 at 16:18
  • to clarify, "Synthetic events do not have default actions. In other words, while the script above will fire a paste event, the data will not actually be pasted into the document". even if you use the `new ClipboardEvent('paste', { ... })` syntax, the event will do nothing –  Nov 28 '16 at 16:25
  • Thanks. If that's truly the case, I am pretty screwed. – VSO Nov 28 '16 at 16:26
  • 1
    maybe you can try to use [this](http://stackoverflow.com/questions/3964710/replacing-selected-text-in-the-textarea) –  Nov 28 '16 at 16:33
  • Will mess with it. – VSO Nov 28 '16 at 16:36

0 Answers0