You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2015/09/10 00:06:51 UTC

[8/8] allura git commit: [#7955] wip: dedent working

[#7955] wip: dedent working


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/d42c3364
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/d42c3364
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/d42c3364

Branch: refs/heads/db/7955
Commit: d42c3364fd897f00172b76f23a789a1bf8262e8d
Parents: 60bc7e0
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Wed Sep 9 22:06:20 2015 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Wed Sep 9 22:06:20 2015 +0000

----------------------------------------------------------------------
 .../lib/widgets/resources/js/sf_markitup.js     | 123 +++++++++++++------
 1 file changed, 88 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/d42c3364/Allura/allura/lib/widgets/resources/js/sf_markitup.js
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/resources/js/sf_markitup.js b/Allura/allura/lib/widgets/resources/js/sf_markitup.js
index 1180ce7..c66562d 100644
--- a/Allura/allura/lib/widgets/resources/js/sf_markitup.js
+++ b/Allura/allura/lib/widgets/resources/js/sf_markitup.js
@@ -97,20 +97,22 @@ $(window).load(function() {
                 cur_end = cm.getCursor('end'),
                 tok = cm.getTokenAt(cur_start),
                 types = tok.type ? tok.type.split(' ') : [],
-                is_code = types.indexOf('comment') !== -1,
-                is_code_block_indented = tok.state.base.indentedCode,
-                is_code_single_line = tok.state.base.code,
+                line = cm.getLineHandle(cur_start.line),
+                lastTok = !!line.text && cm.getTokenAt({line: cur_start.line, ch: line.text.length-1}),
+                is_code_block_indented = lastTok && lastTok.state.base.indentedCode, // have to check last char, since first chars of first line aren't marked as indented
+                is_code = types.indexOf('comment') !== -1 || is_code_block_indented, // have to also check for indented, since first chars of first indented line aren't marked
+                is_code_single_line = tok.state.base.code, // FIXME false-positive for ``` line, check for formatting-code-block
                 is_code_block_fenced = !is_code_block_indented && !is_code_single_line;
 
-              console.log(tok);
-              console.log(cm.getLineTokens(start.line));
+              console.log(lastTok);
+              console.log('code:', is_code, 'indented', is_code_block_indented, 'fenced', is_code_block_fenced, 'single', is_code_single_line);
+              //console.log(cm.getLineTokens(start.line));
 
               if (is_code) {
                 if (is_code_single_line) {
                   // similar to some SimpleMDE _toggleBlock logic
-                  var text = cm.getLine(cur_start.line),
-                    start = text.slice(0, cur_start.ch).replace('`',''),
-                    end = text.slice(cur_start.ch).replace('`', '');
+                  var start = line.text.slice(0, cur_start.ch).replace('`',''),
+                    end = line.text.slice(cur_start.ch).replace('`', '');
                   cm.replaceRange(start + end, {
                     line: cur_start.line,
                     ch: 0
@@ -122,40 +124,91 @@ $(window).load(function() {
                   cur_end.ch--;
                   cm.setSelection(cur_start, cur_end);
                   cm.focus();
-                } else {
+                } else if (is_code_block_fenced) {
+                  /*
+                  DEBUG
+                  */
+                  for (var i = 0; i < cm.lineCount(); i++) {
+                    tok0 = cm.getTokenAt({line: i, ch: 0});
+                    tok1 = cm.getTokenAt({line: i, ch: 1});
+                    //types0 = cm.getTokenTypeAt({line: i, ch:0});
+                    //types1 = cm.getTokenTypeAt({line: i, ch:1});
+                    line = cm.getLineHandle(i);
+                    //lineToks = cm.getLineTokens(i);
+                    //lastTok = lineToks.length && lineToks[lineToks.length-1]
+                    lastTok = line.text && cm.getTokenAt({line: i, ch: line.text.length-1});
+                    console.log('line', i,
+                      tok0.type, ',', tok1.type, ',',
+                      //tok0.state.base.indentedCode, tok1.state.base.indentedCode,
+                      'last tok indented:', lastTok && lastTok.state.base.indentedCode,
+                      'first tok indented:', tok1.state.base.indentedCode,
+                      'last tok inline code:', lastTok && lastTok.state.base.code,
+                      'first tok inline code:', tok1.state.base.code,
+                      'last tok type:', lastTok && lastTok.type,
+                      'styles:', line && line.styles, line && line.text);
+                    //console.log(cm.getLineTokens(i));
+                  }
+                  return;
+
+                  // look for formatting-code-block with highlightFormatting turned on  :(
+                  // or look for whole line being "comment" and not tok.state.base.code (which means inline)
+                  // FIXME what about selection?
+                  cm.replaceRange('', {line: block_start-1, ch:0}, {line: block_start, ch:0});
+                  cm.replaceRange('', {line: block_end, ch:0}, {line: block_end+1, ch:0});
+                } else if (is_code_block_indented) {
                   var block_start, block_end;
-                  /* FIXME need a way to find the block start and end
-                   getTokenTypeAt doesn't work for indented blocks since first char is just a space
-                   getLineHandle(i).styles doesn't work since there are multiple styles per line.
-                      or could it?
-
-                    */
-                  for (block_start = cur_start.line; block_start >= 0; block_start--) {
-                    types = cm.getTokenTypeAt({line: block_start, ch:1});
-                    console.log(block_start, types);
-                    if (types && types.split(' ').indexOf('comment') !== -1) {
-                      block_start += 1;
-                      break;
+                  if (cur_start.line !== cur_end.line || cur_start.ch !== cur_end.ch) {
+                    // use selection
+                    block_start = cur_start.line;
+                    block_end = cur_end.line;
+                    if (cur_end.ch === 0) {
+                      block_end--;
                     }
-                  }
-                  lineCount = cm.lineCount();
-                  for (block_end = cur_start.line; block_end < lineCount; block_end++) {
-                    if (types && types.split(' ').indexOf('comment') !== -1) {
-                      block_end -= 1;
-                      break;
+                  } else {
+                    // no selection, search for ends of this indented block
+                    for (block_start = cur_start.line; block_start >= 0; block_start--) {
+                      line = cm.getLineHandle(block_start);
+                      if (line.text.match(/^\s*$/)) {
+                        // empty or all whitespace - keep going
+                        continue;
+                      } else {
+                        lastTok = cm.getTokenAt({line: block_start, ch: line.text.length-1});
+                        if (!lastTok.state.base.indentedCode) {
+                          block_start += 1;
+                          break;
+                        }
+                      }
                     }
-                  }
-                  console.log(block_start, block_end);
-                  if (is_code_block_indented) {
-                    for (var line = block_start; line <= block_end; line++) {
-                      cm.indentLine(line, 'subtract');
+                    lineCount = cm.lineCount();
+                    for (block_end = cur_start.line; block_end < lineCount; block_end++) {
+                      line = cm.getLineHandle(block_end);
+                      if (line.text.match(/^\s*$/)) {
+                        // empty or all whitespace - keep going
+                        continue;
+                      } else {
+                        lastTok = cm.getTokenAt({line: block_end, ch: line.text.length-1});
+                        if (!lastTok.state.base.indentedCode) {
+                          block_end -= 1;
+                          break;
+                        }
+                      }
                     }
-                  } else if (is_code_block_fenced) {
-                    cm.replaceRange('', {line: block_start-1, ch:0}, {line: block_start, ch:0});
-                    cm.replaceRange('', {line: block_end, ch:0}, {line: block_end+1, ch:0});
+                  }
+                  // if we are going to un-indent based on a selected set of lines, and the next line is indented too, we need to
+                  // insert a blank line so that the next line(s) continue to be indented code
+                  var next_line = cm.getLineHandle(block_end+1),
+                    next_line_last_tok = next_line && cm.getTokenAt({line: block_end+1, ch: next_line.text.length-1}),
+                    next_line_indented = next_line_last_tok && next_line_last_tok.state.base.indentedCode;
+                  if (next_line_indented) {
+                    cm.replaceRange('\n', {line: block_end+1, ch:0});
+                  }
+
+                  for (var i = block_start; i <= block_end; i++) {
+                    cm.indentLine(i, 'subtract'); // TODO: this doesn't get tracked in the history, so can't be undone :(
                   }
                 }
               } else {
+                // insert code formatting
                 var no_sel_and_starting_of_line = (cur_start.line === cur_end.line && cur_start.ch === cur_end.ch && cur_start.ch === 0);
                 var sel_multi = cur_start.line !== cur_end.line;
                 if (no_sel_and_starting_of_line || sel_multi) {