nytimes / ice

track changes with javascript
Other
1.71k stars 194 forks source link

IceCopyPastePlugin, before/afterPasteClean #98

Closed jamienk closed 10 years ago

jamienk commented 10 years ago

I'm not sure how to use these callbacks?

I have

{
   name: 'IceCopyPastePlugin',
   settings: {
   pasteType : 'formattedClean',
   preserve: 'p,a[href]',
   beforePasteClean : function (ev)
   {
      //do something here?
   }
}

...can I manipulate the pasted contents inside the before/afterPasteClean function?

delambo commented 10 years ago

Yes, both callbacks receive the html before and after it was pasted, and expects that you return some modified version of the html. Let me know if you have any more questions.

jamienk commented 10 years ago

Got it -- I'm using this (from http://sujeetgholap.github.io/typesmart.js/) to make pasted quotes turn "smart":

afterPasteClean : function (ev)
{
    ev=ev+' ';//this turns it into a string, sometimes it's not a string?

    // Strategy :
    // 1) First replace all quotes following whitespace into opening ones.
    // 2) Then all those which are after any character into closing ones.
    // 3) Then remaining into opening ones.
    newev=ev.replace(/(\s)'/g, "$1\u2018") // Opening singles
        .replace(/(\s)"/g, "$1\u201c") // Opening doubles
        // After one or more whitespace.
        .replace(/(.)'/g, "$1\u2019") // Closing singles
        .replace(/(.)"/g, "$1\u201d") // Closing doubles
        .replace(/'/g, "\u2018") // Opening singles
        .replace(/"/g, "\u201c"); // Opening doubles

    return newev;   
}