X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/e77f342a73c940f16c37f57ffa50edce9caef8f0..613c90833cd963f769500b79b59e9986bc8970ad:/redakcja/static/js/button_scripts.js diff --git a/redakcja/static/js/button_scripts.js b/redakcja/static/js/button_scripts.js index 2a585dc5..953383ce 100644 --- a/redakcja/static/js/button_scripts.js +++ b/redakcja/static/js/button_scripts.js @@ -48,11 +48,10 @@ function ScriptletCenter() { this.scriptlets = {}; - this.scriptlets['insert_tag'] = function(context, params, done) + this.scriptlets['insert_tag'] = function(context, params, text, move_forward, done) { - var text = this.XMLEditorSelectedText(context); 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] + '"'; @@ -84,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) { @@ -91,22 +93,18 @@ function ScriptletCenter() } else { output = start_tag + end_tag; - move_cursor = true; + cursor_inside = true; } } - this.XMLEditorReplaceSelectedText(context, output); - - try { - if (move_cursor) { - this.XMLEditorMoveCursorForward(context, params.tag.length+2); - } - } catch(e) {} + if (cursor_inside) { + move_forward -= params.tag.length+3; + } - done(); + done(output, move_forward); }.bind(this); - this.scriptlets['lineregexp'] = function(context, params, done) { + this.scriptlets['lineregexp'] = function(context, params, text, move_forward, done) { var self = this; var exprs = $.map(params.exprs, function(expr) { @@ -119,9 +117,7 @@ function ScriptletCenter() }; }); - var partial = true; - var text = this.XMLEditorSelectedText(context); - if(!text) return done(); + if(!text) done(text, move_forward); var changed = 0; var lines = text.split('\n'); @@ -138,15 +134,15 @@ function ScriptletCenter() if(old_line != line) changed += 1; return line; }, function(newlines) { - if(changed > 0) { - self.XMLEditorReplaceSelectedText(context, newlines.join('\n') ); - }; + if(changed > 0) { + text = newlines.join('\n'); + }; - done(); + done(text, move_forward); }); }.bind(this); - this.scriptlets['fulltextregexp'] = function(context, params, done) { + this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, done) { var self = this; var exprs = $.map(params.exprs, function(expr) { @@ -160,72 +156,42 @@ function ScriptletCenter() }; }); - var text = this.XMLEditorSelectedText(context); - if(!text) return done(); - var original = text;$ + if(!text) done(text, move_forward); nblck_each(exprs, function(expr, index) { $progress.html(600 + index); text = text.replace(expr.rx, expr.repl); }, function() { - if( original != text) { - self.XMLEditorReplaceSelectedText(context, text); - } - - done(); + done(text, move_forward); }); }.bind(this); - this.scriptlets['macro'] = function(context, params, done) { + this.scriptlets['macro'] = function(context, params, text, move_forward, done) { var self = this; var i = 0; - function next() { + function next(text, move_forward) { if (i < params.length) { var e = params[i]; i = i + 1; - self.scriptlets[e[0]](context, e[1], next); + self.scriptlets[e[0]](context, e[1], text, move_forward, next); } else { - done(); + done(text, move_forward); } }; - next(); + next(text, move_forward); }.bind(this); - this.scriptlets['lowercase'] = function(context, params, done) + this.scriptlets['lowercase'] = function(context, params, text, move_forward, done) { - var text = this.XMLEditorSelectedText(context); - - if(!text) return; - - var repl = ''; - var lcase = text.toLowerCase(); - var ucase = text.toUpperCase(); - - if(lcase == text) repl = ucase; /* was lowercase */ - else if(ucase != text) repl = lcase; /* neither lower- or upper-case */ - else { /* upper case -> camel-case */ - var words = $(lcase.split(/\s/)).map(function() { - if(this.length > 0) { - return this[0].toUpperCase() + this.slice(1); - } else { - return ''; - } - }); - repl = words.join(' '); - } - - if(repl != text) this.XMLEditorReplaceSelectedText(context, repl); - - done(); + if(!text) done(text, move_forward); + done(text.toLowerCase(), move_forward); }.bind(this); - this.scriptlets["insert_stanza"] = function(context, params, done) { - var text = this.XMLEditorSelectedText(context); - + this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, done) { if(text) { var verses = text.split('\n'); text = ''; var buf = ''; var ebuf = ''; @@ -243,14 +209,52 @@ function ScriptletCenter() } } text = text + buf + '\n' + ebuf; - this.XMLEditorReplaceSelectedText(context, text); } + else { + text = "" + move_forward -= "".length; + } + + done(text, move_forward); + }.bind(this); + - if (!text) { - this.XMLEditorMoveCursorForward(context, params.tag.length + 2); + 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; } - done(); + 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); } @@ -267,10 +271,20 @@ ScriptletCenter.prototype.callInteractive = function(opts) { $.blockUI({message: $progress, showOverlay: false}); - self.scriptlets[opts.action](opts.context, opts.extra, function(){ + var input = self.XMLEditorSelectedText(opts.context); + self.scriptlets[opts.action](opts.context, opts.extra, input, 0, function(output, move_forward){ /*if(timer) clearTimeout(timer); else */ + if (input != output) { + self.XMLEditorReplaceSelectedText(opts.context, output) + } + if (move_forward) { + try { + self.XMLEditorMoveCursorForward(opts.context, move_forward) + } + catch(e) {} + } $.unblockUI(); // done }); }