#951: verse-out-of-stanza issue
[redakcja.git] / redakcja / static / js / button_scripts.js
index de3b5ee..953383c 100644 (file)
@@ -51,7 +51,7 @@ function ScriptletCenter()
     this.scriptlets['insert_tag'] = function(context, params, text, move_forward, done)
     {
         var start_tag = '<'+params.tag;
-        var move_cursor = false;
+        var cursor_inside = false;
 
         for (var attr in params.attrs) {
             start_tag += ' '+attr+'="' + params.attrs[attr] + '"';
@@ -83,6 +83,9 @@ function ScriptletCenter()
                 output += end_tag;
             }
             output += token;
+
+            // keep cursor inside tag if some previous scriptlet has already moved it
+            cursor_inside = move_forward != 0;
         }
         else {
             if(params.nocontent) {
@@ -90,11 +93,11 @@ function ScriptletCenter()
             }
             else {
                 output = start_tag + end_tag;
-                move_cursor = true;
+                cursor_inside = true;
             }
         }
 
-        if (move_cursor) {
+        if (cursor_inside) {
             move_forward -= params.tag.length+3;
         }
 
@@ -114,7 +117,6 @@ function ScriptletCenter()
                 };
         });
 
-        var partial = true;
         if(!text) done(text, move_forward);
 
         var changed = 0;
@@ -155,7 +157,6 @@ function ScriptletCenter()
         });
 
         if(!text) done(text, move_forward);
-        var original = text;$
 
                nblck_each(exprs, function(expr, index) {
                        $progress.html(600 + index);
@@ -217,6 +218,45 @@ function ScriptletCenter()
         done(text, move_forward);
     }.bind(this);
 
+
+    this.scriptlets['autotag'] = function(context, params, text, move_forward, done)
+    {
+        if(!text.match(/^\n+$/)) done(text, move_forward);
+
+        function insert_done(output, mf) {
+            text += output;
+        }
+
+        if (!params.split) params.split = 2;
+        if (!params.padding) params.padding = 3;
+
+        chunks = text.replace(/^\n+|\n+$/, '').split(new RegExp("\\n{"+params.split+",}"));
+        text = text.match(/^\n+/);
+        if (!text)
+            text = '';
+        padding = '';
+        for(; params.padding; params.padding--) {
+            padding += "\n";
+        }
+
+        if (params.tag == 'strofa')
+            tagger = this.scriptlets['insert_stanza'];
+        else
+            tagger = this.scriptlets['insert_tag'];
+
+        for (i in chunks) {
+            if (chunks[i]) {
+                if (params.tag == 'akap' && chunks[i].match(/^---/))
+                    tag = 'akap_dialog';
+                else tag = params.tag;
+                tagger(context, {tag: tag}, chunks[i], 0, insert_done);
+                text += padding;
+            }
+        }
+
+        done(text, move_forward);
+    }.bind(this);
+
 }
 
 ScriptletCenter.prototype.callInteractive = function(opts) {