0

I have the following code:

<textarea class="content">
    [row]
        [col-md-12]
            text
        [/col-md-12]
    [/row]
    [row]
        text
    [/row]
</textarea>

Now I want to add the [col-md-12] inside (directly after) the [row] and close it before the [/row] with jquery. This should be the result:

<textarea class="content">
    [row]
        [col-md-12]
            text
        [/col-md-12]
    [/row]
    [row]
        [col-md-12]
            text
        [/col-md-12]
    [/row]
</textarea>

(Note: The code above is not bootstrap! I just took bootstrap, because its more clear, that the "col-md-12" is needed!)

Here is a jsfiddle example of the current code: http://jsfiddle.net/Zoker/6mzrj1e7/

Now after "converting" there is a '[vc_column width="1/1"]' missing inside the first '[vc_row]' and a '[/vc_column]' before the closing '[/vc_row]'. And thats the shortcode I need to add. I dont know how to proof, if its missing and add it then...

Also I have to click 3 times on the button to get the content completely updated. How can I make it work after 1 click? (edit: that one fixed with https://stackoverflow.com/a/1145525/2977288)

Community
  • 1
  • 1
Zoker
  • 2,020
  • 5
  • 32
  • 53
  • Where is the javascript that you have attempted to do this with? – charlietfl Mar 03 '15 at 13:26
  • I thought about that problem 2 days and not found any working solution. I dont have any working approach :/ – Zoker Mar 03 '15 at 13:45
  • Really not clear what your higher level issue is. Are you trying to create an editor? – charlietfl Mar 03 '15 at 13:46
  • I want to create an converter, which first converts [shortcode1] into [shortcode2] (I got that working fine) and then add the missing inner shortcode [inner-shortcode1] if its missing. – Zoker Mar 03 '15 at 13:51
  • Without you providing your code i don't know how anyone can help you then – charlietfl Mar 03 '15 at 13:53
  • Here you can see the current code: http://jsfiddle.net/Zoker/6mzrj1e7/ – Zoker Mar 03 '15 at 13:57
  • Now make the question relevant to the code , and the specific part of the code that needs to be worked on and steps to take in UI that cause problems – charlietfl Mar 03 '15 at 14:00
  • Better now? Or is something else missing? – Zoker Mar 03 '15 at 14:15
  • Main issue I see is you can't chain `replace()` that's why you have to click so many times – charlietfl Mar 03 '15 at 14:21
  • I found that solution: http://stackoverflow.com/a/1145525/2977288 So this is fixed, but not the main thing :/ – Zoker Mar 03 '15 at 14:22
  • Would help to have as simple a demo as possible, as well as expected results beside it. Is time consuming to try to see what issues are, especially with chained replace that needs fixing – charlietfl Mar 03 '15 at 14:28
  • Is the demo in the question not simple? I thought it would show the problems quite good. The main problem (for me) is, to check whether the next element to [vc_row *] is a [vc_column *] and if the element before [/vc_row] is a [/vc_column] – Zoker Mar 03 '15 at 14:32
  • Showing expected results always saves time. Not interested in trying to sort out what it should do myself...especially with other bugs like `replace` in it – charlietfl Mar 03 '15 at 14:34
  • I fixed the replace thing in the fiddle. Where should I place the expected result? In the fiddle or in the question? – Zoker Mar 03 '15 at 14:37
  • Done, now it contains input and expected result – Zoker Mar 03 '15 at 14:46

1 Answers1

1

LIVE:

It was very hard to achieve: (Hope it helps)


// auto_descritive
function wrap_string( str, open_param, close_param ){
    var text = $(".content").text();

    // all indexes of str
    var re = new RegExp(str,'gi'), results = [];
    while (re.exec(text)){
        results.push(re.lastIndex);
    }

    // check each parent param of each str
    for (var i=0; results[i]; i++) {
        var y_param = text.substring(0, results[i]).lastIndexOf(']')+1;
        var x_param = y_param - open_param.length;
        var lastParam = text.substring(x_param, y_param);
        var half1 = text.substring(0, y_param);
        var half2 = text.substring(results[i]+str.length, text.length);
        if (lastParam != open_param) {
            text = half1 + open_param + str + close_param + half2;
            $(".content").text( text );
            // return true if one was wrapped
            return true;
        }
    }

    // return false in case nothing was wrapped
    return false;

}


// wrap all "text" with "[col-md-12] /"
while( wrap_string( "text", "[col-md-12]", "[/col-md-12]" ) );

  • Thats a good way to add it :) The only problem is, that I first need to check, if the next element of [row] already is a [col-md-*] and only add it, if not. – Zoker Mar 03 '15 at 16:45
  • I need something like: if text between '[' and ']' = 'row' then check if next element after the ']' is '[col-md-*' – Zoker Mar 03 '15 at 16:47
  • Dude you are awesome!!! Is there any test, if the shortcode is [row]? Only thing is, that the script does not work 100% as expected: http://jsfiddle.net/Zoker/6mzrj1e7/11/ – Zoker Mar 03 '15 at 21:07
  • Here is a extended explanation: http://pastebin.com/Gn29JrdZ Thank you again for your great work! Hope you understand what I want to do :) – Zoker Mar 03 '15 at 21:33
  • But all content is currently replaced with "Text" and also the text is wrapped with [vc_column width="1/1"], but it is already wrapped with [vc_column width="1/3"] (there is just one wrapper more around the main text). Did you read the pastebin text? – Zoker Mar 03 '15 at 21:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72224/discussion-between-washington-guedes-and-zoker). –  Mar 04 '15 at 12:27
  • Awesome! Only some little things: 1. I changed the shortcode to be "col-md-12" instead of "col-md-12 param5 param1='12'", but the closing does not work anymore, its just [/]. 2. i commented on it here: http://jsfiddle.net/Zoker/kp2xy6ge/11/ – Zoker Mar 06 '15 at 14:12
  • @Zoker Updated: http://jsfiddle.net/kp2xy6ge/12/ .. Be aware of key characters like '<' '>' '[' ']' –  Mar 06 '15 at 15:03
  • Now only the second text ("-- This should be also wrapped --") does not work, the others do :) – Zoker Mar 06 '15 at 15:06