2015-07-20 155 views
1

我试图在ACE中逐步显示一个字符串,但我遇到了一个问题。ACE编辑器:逐步显示内容

Api documentation中,我可以找到一个函数“setValue”,但它取代了已经添加的内容。我想要做的就是逐渐显示我的字符串,就像有人写道这样通过“addContent”方法添加内容。

下面的代码片段显示了如何实现王牌编辑:

的HTML:

<pre id="editor"></pre> 

的JavaScript:

$(document).ready(function() { 
     var editor = ace.edit("editor"); 

     // Disable warn message in console. Advised by Ace developers. 
     editor.$blockScrolling = Infinity; 

     var options = { 
      animatedScroll: true, 
      autoScrollEditorIntoView: true, 
      fontSize: 16, 
      mode: "ace/mode/php", 
      scrollSpeed: 1, 
      showFoldWidgets: false, 
      showPrintMargin: false, 
      theme: "ace/theme/dreamweaver" 
     }; 

     editor.setOptions(options); 

     getFileRaw(editor); 
     setInterval(function() { getFileRaw(editor); }, 60000) 
    }); 

功能 “getFileRaw” 让我从检索内容一个URL。在那里,它是:

var getFileRaw = function(editor) { 
     var routes = [ 
      {url: 'https://api.github.com/repos/IDCI-Consulting/GenealogyBundle/contents/Controller/ImageController.php',    language: 'php'}, 
      {url: 'https://api.github.com/repos/IDCI-Consulting/GenealogyBundle/contents/Repository/ElementRepository.php',   language: 'php'}, 
      {url: 'https://api.github.com/repos/IDCI-Consulting/ExtraFormBundle/contents/Controller/ApiController.php',    language: 'php'}, 
      {url: 'https://api.github.com/repos/IDCI-Consulting/SimpleMetadataBundle/contents/Resources/views/Form/fields.html.twig', language: 'twig'}, 
      {url: 'https://api.github.com/repos/IDCI-Consulting/BrowserCapabilities/contents/js/codebarReader.js',     language: 'javascript'} 
     ]; 

     // get a random file object 
     var file = routes[Math.floor(Math.random() * routes.length)]; 

     $.ajax({ 
      url: file.url, 
      method: "GET", 
      success: function(fileRaw) { 
       // Github api provides the fileraw's content encoded in base64 format. We need to use atob() function to decode it. 
       var content = atob(fileRaw.content); 

       editor.getSession().setMode("ace/mode/"+file.language); 

       // Here I will call a function 'showText' that display the string letter by letter. 
       // I need to use a method that add content instead of replace it 

       editor.getSession().setValue(content); 
      } 
     }); 
    }; 

最后,这里是我的功能逐步显示我的字符串:

var showText = function (editor, message, index) { 
    if (index < message.length) { 
     editor.addValue(message[index++]); // the desired function 
     setTimeout(function() { showText(target, message, index); }, 500); 
    } 
}; 

如果有人能够告诉我,因为我很坚持。

希望我在发言

回答

1

可以使用editor.session.insert很清楚,这里是用按键

var showText = function (editor, message, index, delay) { 
    if (index < message.length) { 
     // insert at cursor 
     editor.session.insert(editor.getCursorPosition(), message[index++]); 
     // clear selection just in case there was something selected 
     editor.selection.clearSelection(); 
     // make sure cursor is visible 
     editor.renderer.scrollCursorIntoView(); 

     // adjust delay after typing a space 
     if (/\s/.test(message[index - 1])) delay = 0 
     setTimeout(function() { 
      showText(editor, message, index, (delay + 1) || 0); 
     }, 90 - 15 * Math.min(delay||0, 5)); 
    } 
}; 
showText(editor, "\nmessage\nmy message", 0) 
+0

非常感谢之间的动态延迟完成showText!这个功能似乎是很好的解决方案。 – BwaBwa