# HG changeset patch # User neko259 # Date 2013-12-14 15:41:16 # Node ID 4330ff5a2bf6c543d8aaae8a43de1dc062f3bd13 # Parent 2ad39e0490d02eb0e316c8da830563dc9202980a Added proper formatting when several lines are selected diff --git a/boards/mdx_neboard.py b/boards/mdx_neboard.py --- a/boards/mdx_neboard.py +++ b/boards/mdx_neboard.py @@ -143,6 +143,14 @@ class BoldPattern(TextFormatter): format_right = '__' +class CodePattern(TextFormatter): + name = 'code' + preview_left = '' + preview_right = '' + + format_left = ' ' + + class NeboardMarkdown(markdown.Extension): def extendMarkdown(self, md, md_globals): self._add_neboard_patterns(md) @@ -186,4 +194,5 @@ formatters = [ BoldPattern, CommentPattern, StrikeThroughPattern, + CodePattern, ] diff --git a/boards/static/js/panel.js b/boards/static/js/panel.js --- a/boards/static/js/panel.js +++ b/boards/static/js/panel.js @@ -23,7 +23,17 @@ for the JavaScript code in this page. */ +/** + * Add the desired characters to the start and end of selection. + * @param start Start (left) text + * @param end End (right) text + * @returns {boolean} + */ function addMarkToMsg(start, end) { + if (end.length == 0) { + return addTextToEachLineOfSelection(start); + } + var textarea = document.getElementsByTagName('textarea')[0]; if(!textarea) return; if( document.selection ) { @@ -39,4 +49,68 @@ function addMarkToMsg(start, end) { textarea.value += start + end; } return false; +} + +/** + * Add text to the beginning of each selected line. Partially selected lines + * are included + * @param textToAdd + * @returns {*} + */ +function addTextToEachLineOfSelection(textToAdd) { + var editor, end, newValue, start, value, _ref, _ref1; + editor = document.getElementsByTagName('textarea')[0]; + _ref = [editor.selectionStart, editor.selectionEnd], start = _ref[0], end = _ref[1]; + if (start == null) { + return; + } + if (start === end) { + return; + } + console.log("Selection range: start=" + start + " end=" + end); + value = editor.value; + _ref1 = getLinesRange(start, end, value), start = _ref1[0], end = _ref1[1]; + newValue = replaceLines(start, end, value, textToAdd); + return editor.value = newValue; +} + +function replaceLines(start, end, value, textToAdd) { + var line, replacedText, text; + text = value.slice(start, end); + replacedText = ((function() { + var _i, _len, _ref, _results; + _ref = text.split("\n"); + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + line = _ref[_i]; + _results.push(textToAdd + line); + } + return _results; + })()).join("\n"); + return replaceSubstring(start, end, value, replacedText); +} + +function replaceSubstring(start, end, string, replacingString) { + return string.slice(0, start) + replacingString + string.slice(end); +} + +function getLinesRange(start, end, value) { + var i, rangeEnd, rangeStart, _i, _j, _ref, _ref1; + if (value[start] === "\n") { + start = start - 1; + } + _ref = [start, end], rangeStart = _ref[0], rangeEnd = _ref[1]; + for (i = _i = start; start <= 0 ? _i <= 0 : _i >= 0; i = start <= 0 ? ++_i : --_i) { + if (value[i] === "\n") { + break; + } + rangeStart = i; + } + for (i = _j = end, _ref1 = value.length; end <= _ref1 ? _j < _ref1 : _j > _ref1; i = end <= _ref1 ? ++_j : --_j) { + if (value[i] === "\n") { + break; + } + rangeEnd = i; + } + return [rangeStart, rangeEnd]; } \ No newline at end of file