简体   繁体   English

MarkItUp:制表符/缩进,设置选择

[英]MarkItUp: Tab/Indent, Set Selection

I see that theres is no build in way for doing tabs/indents in MarkItUp? 我看到在MarkItUp中没有制表符/缩进的构建方式吗? So I did something like 所以我做了类似的事情

onTab: { 
  keepDefault: false, 
  replaceWith: function(markItUp) { 
    return miu.openEachLineWith(markItUp, '  '); 
  }
},
openEachLineWith: function(markItUp, openingStr) {
  var textarea = markItUp.textarea,
      selStart = textarea.selectionStart,
      selEnd = textarea.selectionEnd,
      selText = textarea.value.substring(selStart, selEnd),
      lines = [], 
      charsAdded = 0;

  lines = selText.split(/\r?\n/);
  for (var i = 0, len = lines.length; i < len; i++) {
    lines[i] = openingStr + lines[i];
    charsAdded += openingStr.length;
  }
  textarea.selectionEnd = selEnd + charsAdded;
  return lines.join('\n');
}

which works but, how can I set the selection after replacing the the text, I want it to select the tabbed text, also I prefer the way the editor here on SO works where when I bold some text, it selects the bolded text instead of moving the cursor to the end, can I do that with markItUp too? 可以,但是,如何在替换文本后设置选择,我希望它选择选项卡式文本,我也更喜欢SO上的编辑器的工作方式,当我加粗一些文本时,它选择的是加粗文本而不是将光标移到末尾,我也可以使用markItUp做到这一点吗?

您必须在afterInsert回调中设置选择(而不是在replaceWith中

I've been working on a script to do this. 我一直在研究脚本来做到这一点。 Here's an example: http://jsfiddle.net/timdown/dp2WL/2/ 这是一个示例: http : //jsfiddle.net/timdown/dp2WL/2/

It indents when Tab is pressed and outdents when Shift + Tab is pressed and doesn't require any library. 按下Tab时缩进,按下Shift + Tab时缩进,不需要任何库。 It hasn't had as much testing as I'd like, but seems to work well in all major browsers, including IE 6. The main code comes from an open source project I'm working on . 它没有经过我想要的测试,但是似乎可以在包括IE 6在内的所有主要浏览器中很好地工作。主要代码来自我正在研究的一个开源项目 The bit that enables tab indentation is at the bottom: 启用制表符缩进的位在底部:

window.onload = function() {
    rangyInputs.init();
    rangyInputs.enableTabIndentation(document.getElementById("test"), "    ");
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM