Forum closed. New forum available at http://community.wymeditor.org/

Remove 'bullet-char' from content when converting to list

Support forum for WYMeditor.

Remove 'bullet-char' from content when converting to list

Postby Taffa on Mon Sep 22, 2008 9:47 am

My users need to paste content to WYMeditor in the following format:
Code: Select all
- lorem ipsum
- lorem ipsum
- lorem ipsum

When the content is selected and converted to real unordered list with corresponding button the result is:
Code: Select all
* - lorem ipsum
* - lorem ipsum
* - lorem ipsum

Any ideas how to change the WYMeditor code to automatically remove the extra - character?
Even better would be that every line which starts with - is converted to a list when pasted.
Taffa
 
Posts: 3
Joined: Mon Sep 22, 2008 9:27 am

Re: Remove 'bullet-char' from content when converting to list

Postby mr_lundis on Mon Sep 22, 2008 6:38 pm

Hi Taffa, and welcome to the WYMeditor community!

Taffa wrote:Any ideas how to change the WYMeditor code to automatically remove the extra - character?
Even better would be that every line which starts with - is converted to a list when pasted.

For versions of WYMeditor prior 0.6 you will need to modify the internal paste functionality so that it'll search for anything that might be a list (preferably using an regular expression), and then replace it with an unordered list.

However this will most likely not be worth the trouble, have you considered just to change the format of the source that the user will copy? Doing so will of course only work if the source also is an html document.

Version 0.6 of WYMeditor will be a major rewrite, how pasting is handled from that point on I can't tell.
Jonatan Lundin - designer, developer and forum moderator. You should follow me on Twitter!
mr_lundis
 
Posts: 335
Joined: Sun Dec 02, 2007 10:59 am
Location: Sweden

Re: Remove 'bullet-char' from content when converting to list

Postby Taffa on Fri Sep 26, 2008 12:39 pm

have you considered just to change the format of the source that the user will copy? Doing so will of course only work if the source also is an html document.

Unfortunatelly I have no control over the source material. I did quick-modification to WYMeditor.editor.prototype.paste function and it seems to work with "Paste from Word" dialog. Now if I could find a way to hook this to plain ctrl+v paste as well.
Here's the code if anyone is intrested:
Code: Select all
WYMeditor.editor.prototype.paste = function(sData) {
  var sTmp;
  var container = this.selected();
   
  //split the data, using double newlines as the separator
  var aP = sData.split(this._newLine /*+ this._newLine*/);
  var rExp = new RegExp(this._newLine, "g");
  var bulletChar_rExp = new RegExp("^(-|\\*|#)", "g");
 
  //add a P for each item
  if(container && container.tagName.toLowerCase() != WYMeditor.BODY) {
   
    for(x = aP.length - 1; x >= 0; x--) {
        sTmp = aP[x];
        //automatically convert lines with bullet to unordered list
        if (sTmp.match(bulletChar_rExp)) {
            var sList = "</ul>";
            var inList = true;
            while (inList) {
                var item = aP[x].replace(bulletChar_rExp, "");
                item =  item.replace(/^\s+|\s+$/g, '');
                sList = "<li>" + item + "</li>" + sList;
                if ((aP[x - 1] == undefined) || (aP[x - 1].match(bulletChar_rExp) == null)) {
                    sList = "<ul>" + sList;
                    inList = false;
                }
                else {
                    x--;
                }
            }
            jQuery(container).after(sList);
        }
        else {
            //simple newlines are replaced by a break
            sTmp = sTmp.replace(rExp, "<br />");
            jQuery(container).after("<p>" + sTmp + "</p>");
        }
    }
  } else {
    for(x = 0; x < aP.length; x++) {
        sTmp = aP[x];
        //automatically convert lines with bullet to unordered list
        if (sTmp.match(bulletChar_rExp)) {
            var sList = "<ul>";
            var inList = true;
            while (inList) {
                var item = aP[x].replace(bulletChar_rExp, "");
                item =  item.replace(/^\s+|\s+$/g, '');
                sList += "<li>" + item + "</li>";
                if ((aP[x + 1] == undefined) || (aP[x + 1].match(bulletChar_rExp) == null)) {
                    sList += "</ul>";
                    inList = false;
                }
                else {
                    x++;
                }
            }
            jQuery(this._doc.body).append(sList);
        }
        else {
            //simple newlines are replaced by a break
            sTmp = sTmp.replace(rExp, "<br />");
            jQuery(this._doc.body).append("<p>" + sTmp + "</p>");
        }
    }
  }
};



Taffa
 
Posts: 3
Joined: Mon Sep 22, 2008 9:27 am


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest