X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/483b49a917211353f1707012c77531cc0d06d99f..07689901a9bf30daeccf8a1ceb7193fe771eb3ac:/redakcja/static/js/button_scripts.js diff --git a/redakcja/static/js/button_scripts.js b/redakcja/static/js/button_scripts.js index c3275938..c97a2314 100644 --- a/redakcja/static/js/button_scripts.js +++ b/redakcja/static/js/button_scripts.js @@ -48,10 +48,14 @@ function ScriptletCenter() { this.scriptlets = {}; - this.scriptlets['insert_tag'] = function(context, params, text, move_forward, done) + this.scriptlets['insert_tag'] = function(context, params, text, move_forward, move_up, done) { + var padding_top = ''; + for (var i=params.padding_top; i; i--) + padding_top += '\n'; + 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] + '"'; @@ -60,6 +64,10 @@ function ScriptletCenter() start_tag += '>'; var end_tag = ''; + var padding_bottom = ''; + for (var i=params.padding_bottom; i; i--) + padding_bottom += '\n'; + if(text.length > 0) { // tokenize var output = ''; @@ -71,37 +79,41 @@ function ScriptletCenter() } else { // character output += token; - if(output == token) output += start_tag; + if(output == token) output += padding_top + start_tag; token = ''; output += text[index]; } } if( output[output.length-1] == '\\' ) { - output = output.substr(0, output.length-1) + end_tag + '\\'; + output = output.substr(0, output.length-1) + end_tag + padding_bottom + '\\'; } else { - output += end_tag; + output += end_tag + padding_bottom; } output += token; + + // keep cursor inside tag if some previous scriptlet has already moved it + cursor_inside = move_forward != 0 || move_up != 0; } else { if(params.nocontent) { - output = "<"+params.tag +" />"; + output = padding_top + "<"+params.tag +" />" + padding_bottom; } else { - output = start_tag + end_tag; - move_cursor = true; + output = padding_top + start_tag + end_tag + padding_bottom; + cursor_inside = true; } } - if (move_cursor) { - move_forward -= params.tag.length+3; + if (cursor_inside) { + move_forward -= params.tag.length + 3; + move_up += params.padding_bottom || 0; } - done(output, move_forward); + done(output, move_forward, move_up); }.bind(this); - this.scriptlets['lineregexp'] = function(context, params, text, move_forward, done) { + this.scriptlets['lineregexp'] = function(context, params, text, move_forward, move_up, done) { var self = this; var exprs = $.map(params.exprs, function(expr) { @@ -114,7 +126,7 @@ function ScriptletCenter() }; }); - if(!text) done(text, move_forward); + if(!text) done(text, move_forward, move_up); var changed = 0; var lines = text.split('\n'); @@ -135,11 +147,11 @@ function ScriptletCenter() text = newlines.join('\n'); }; - done(text, move_forward); + done(text, move_forward, move_up); }); }.bind(this); - this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, done) { + this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, move_up, done) { var self = this; var exprs = $.map(params.exprs, function(expr) { @@ -153,42 +165,50 @@ function ScriptletCenter() }; }); - if(!text) done(text, move_forward); + if(!text) done(text, move_forward, move_up); nblck_each(exprs, function(expr, index) { $progress.html(600 + index); text = text.replace(expr.rx, expr.repl); }, function() { - done(text, move_forward); + done(text, move_forward, move_up); }); }.bind(this); - this.scriptlets['macro'] = function(context, params, text, move_forward, done) { + this.scriptlets['macro'] = function(context, params, text, move_forward, move_up, done) { var self = this; var i = 0; - function next(text, move_forward) { + function next(text, move_forward, move_up) { if (i < params.length) { var e = params[i]; i = i + 1; - self.scriptlets[e[0]](context, e[1], text, move_forward, next); + self.scriptlets[e[0]](context, e[1], text, move_forward, move_up, next); } else { - done(text, move_forward); + done(text, move_forward, move_up); } }; - next(text, move_forward); + next(text, move_forward, move_up); }.bind(this); - this.scriptlets['lowercase'] = function(context, params, text, move_forward, done) + this.scriptlets['lowercase'] = function(context, params, text, move_forward, move_up, done) { - if(!text) done(text, move_forward); - done(text.toLowerCase(), move_forward); + if(!text) done(text, move_forward, move_up); + done(text.toLowerCase(), move_forward, move_up); }.bind(this); - this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, done) { + this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, move_up, done) { + var padding_top = ''; + for (var i=params.padding_top; i; i--) + padding_top += '\n'; + + var padding_bottom = ''; + for (var i=params.padding_bottom; i; i--) + padding_bottom += '\n'; + if(text) { var verses = text.split('\n'); text = ''; var buf = ''; var ebuf = ''; @@ -205,20 +225,21 @@ function ScriptletCenter() ebuf += '\n' + verses[i]; } } - text = text + buf + '\n' + ebuf; + text = padding_top + text + buf + '\n' + padding_bottom + ebuf; } else { - text = "" + text = padding_top + "" + padding_bottom; move_forward -= "".length; + move_up += params.padding_bottom || 0; } - done(text, move_forward); + done(text, move_forward, move_up); }.bind(this); - this.scriptlets['autotag'] = function(context, params, text, move_forward, done) + this.scriptlets['autotag'] = function(context, params, text, move_forward, move_up, done) { - if(!text.match(/^\n+$/)) done(text, move_forward); + if(!text.match(/^\n+$/)) done(text, move_forward, move_up); function insert_done(output, mf) { text += output; @@ -246,12 +267,12 @@ function ScriptletCenter() if (params.tag == 'akap' && chunks[i].match(/^---/)) tag = 'akap_dialog'; else tag = params.tag; - tagger(context, {tag: tag}, chunks[i], 0, insert_done); + tagger(context, {tag: tag}, chunks[i], 0, 0, insert_done); text += padding; } } - done(text, move_forward); + done(text, move_forward, move_up); }.bind(this); } @@ -269,16 +290,16 @@ ScriptletCenter.prototype.callInteractive = function(opts) { $.blockUI({message: $progress, showOverlay: false}); var input = self.XMLEditorSelectedText(opts.context); - self.scriptlets[opts.action](opts.context, opts.extra, input, 0, function(output, move_forward){ + self.scriptlets[opts.action](opts.context, opts.extra, input, 0, 0, function(output, move_forward, move_up){ /*if(timer) clearTimeout(timer); else */ if (input != output) { self.XMLEditorReplaceSelectedText(opts.context, output) } - if (move_forward) { + if (move_forward || move_up) { try { - self.XMLEditorMoveCursorForward(opts.context, move_forward) + self.XMLEditorMoveCursorForward(opts.context, move_forward, move_up) } catch(e) {} } @@ -297,9 +318,24 @@ ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replac editor.replaceSelection(replacement); }; -ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) { +ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, right, up) { var pos = panel.cursorPosition(); - panel.selectLines(pos.line, pos.character + n); + if (up) { + line = pos.line; + while (up < 0) { + line = panel.nextLine(line); + ++up; + } + while (up > 0) { + line = panel.prevLine(line); + --up; + } + len = panel.lineContent(line).length; + panel.selectLines(line, len + right); + } + else { + panel.selectLines(pos.line, pos.character + right); + } }; var scriptletCenter;