X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/f938afb8ab4cb091d12e0ef0311eaea14b774798..feee71de06ace01f793c2c8adf9cce65b9df2cec:/redakcja/static/js/button_scripts.js diff --git a/redakcja/static/js/button_scripts.js b/redakcja/static/js/button_scripts.js index 0d38b004..37f37a7d 100644 --- a/redakcja/static/js/button_scripts.js +++ b/redakcja/static/js/button_scripts.js @@ -48,11 +48,19 @@ function ScriptletCenter() { this.scriptlets = {}; - this.scriptlets['insert_tag'] = function(context, params, done) + this.scriptlets['insert_text'] = function(context, params, text, move_forward, move_up, done) { - var text = this.XMLEditorSelectedText(context); + done(params.text, move_forward, move_up); + }.bind(this); + + 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] + '"'; @@ -61,6 +69,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 = ''; @@ -72,41 +84,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; } } - 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; + move_up += params.padding_bottom || 0; + } - done(); + done(output, move_forward, move_up); }.bind(this); - this.scriptlets['lineregexp'] = function(context, params, done) { + this.scriptlets['lineregexp'] = function(context, params, text, move_forward, move_up, done) { var self = this; var exprs = $.map(params.exprs, function(expr) { @@ -119,9 +131,7 @@ function ScriptletCenter() }; }); - var partial = true; - var text = this.XMLEditorSelectedText(context); - if(!text) return done(); + if(!text) done(text, move_forward, move_up); var changed = 0; var lines = text.split('\n'); @@ -138,15 +148,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, move_up); }); }.bind(this); - this.scriptlets['fulltextregexp'] = function(context, params, done) { + this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, move_up, done) { var self = this; var exprs = $.map(params.exprs, function(expr) { @@ -160,71 +170,49 @@ function ScriptletCenter() }; }); - var text = this.XMLEditorSelectedText(context); - if(!text) return done(); - var original = text; + 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() { - if( original != text) { - self.XMLEditorReplaceSelectedText(context, text); - } - - done(); + done(text, move_forward, move_up); }); }.bind(this); - this.scriptlets['macro'] = function(context, params, done) { + this.scriptlets['macro'] = function(context, params, text, move_forward, move_up, done) { var self = this; var i = 0; - function next() { + 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], next); + self.scriptlets[e[0]](context, e[1], text, move_forward, move_up, next); } else { - done(); + done(text, move_forward, move_up); } }; - next(); + next(text, move_forward, move_up); }.bind(this); - this.scriptlets['lowercase'] = function(context, params, done) + this.scriptlets['lowercase'] = function(context, params, text, move_forward, move_up, 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, move_up); + done(text.toLowerCase(), move_forward, move_up); }.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, 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'); @@ -242,15 +230,67 @@ function ScriptletCenter() ebuf += '\n' + verses[i]; } } - text = text + buf + '\n' + ebuf; - this.XMLEditorReplaceSelectedText(context, text); + text = padding_top + text + buf + '\n' + padding_bottom + ebuf; + } + else { + text = padding_top + "" + padding_bottom; + move_forward -= "".length; + move_up += params.padding_bottom || 0; + } + + done(text, move_forward, move_up); + }.bind(this); + + + this.scriptlets['autotag'] = function(context, params, text, move_forward, move_up, done) + { + if(!text.match(/^\n+$/)) done(text, move_forward, move_up); + + var output = ''; + + function insert_done(text, mf, mu) { + output += text; } - if (!text) { - this.XMLEditorMoveCursorForward(context, params.tag.length + 2); + if (!params.split) params.split = 2; + if (!params.padding) params.padding = 3; + + if (params.tag == 'strofa') + tagger = this.scriptlets['insert_stanza']; + else + tagger = this.scriptlets['insert_tag']; + + var padding_top = text.match(/^\n+/) + output = padding_top ? padding_top[0] : ''; + + padding = ''; + for(var i=params.padding; i; --i) { + padding += "\n"; } - done(); + text = text.substr(output.length); + var chunk_reg = new RegExp("^([\\s\\S]+?)(\\n{"+params.split+",}|$)"); + while (match = text.match(chunk_reg)) { + if (params.tag == 'akap' && match[1].match(/^---/)) + tag = 'akap_dialog'; + else tag = params.tag; + tagger(context, {tag: tag}, match[1], 0, 0, insert_done); + if (match[2].length > params.padding) + output += match[2]; + else + output += padding; + text = text.substr(match[0].length) + } + + output += text; + + done(output, move_forward, move_up); + }.bind(this); + + + this.scriptlets['slugify'] = function(context, params, text, move_forward, move_up, done) + { + done(slugify(text.replace(/_/g, '-')), move_forward, move_up); }.bind(this); } @@ -259,15 +299,34 @@ ScriptletCenter.prototype.callInteractive = function(opts) { $progress = $('Executing script'); var self = this; - $.blockUI({ - message: $progress, - - }); - + /* This won't work, 'cause the JS below might be synchronous :( */ + /* var timer = setTimeout(function() { + $.blockUI({message: $progress}); + timer = null; + }, 1000); */ + + $.blockUI({message: $progress, showOverlay: false}); + + $('#save-button').attr('disabled', true); + var input = self.XMLEditorSelectedText(opts.context); + window.setTimeout(function() { + 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 || move_up) { + try { + self.XMLEditorMoveCursorForward(opts.context, move_forward, move_up) + } + catch(e) {} + } + $.unblockUI({onUnblock: function() { $('#save-button').attr('disabled', null)}}); // done + }); + }, 0); - self.scriptlets[opts.action](opts.context, opts.extra, function(){ - $.unblockUI(); // done - }); } ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) { @@ -278,16 +337,31 @@ ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) { ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement) { $progress.html("Replacing text"); - editor.replaceSelection(replacement); + 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; $(function() { scriptletCenter = new ScriptletCenter(); -}); \ No newline at end of file +});