widget.html 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!doctype html>
  2. <title>CodeMirror: Inline Widget Demo</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../doc/docs.css">
  5. <link rel="stylesheet" href="../lib/codemirror.css">
  6. <script src="../lib/codemirror.js"></script>
  7. <script src="../mode/javascript/javascript.js"></script>
  8. <script src="https://cdnjs.cloudflare.com/ajax/libs/jshint/2.9.5/jshint.min.js"></script>
  9. <style>
  10. .CodeMirror {border: 1px solid black;}
  11. .lint-error {font-family: arial; font-size: 70%; background: #ffa; color: #a00; padding: 2px 5px 3px; }
  12. .lint-error-icon {color: white; background-color: red; font-weight: bold; border-radius: 50%; padding: 0 3px; margin-right: 7px;}
  13. </style>
  14. <div id=nav>
  15. <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
  16. <ul>
  17. <li><a href="../index.html">Home</a>
  18. <li><a href="../doc/manual.html">Manual</a>
  19. <li><a href="https://github.com/codemirror/codemirror">Code</a>
  20. </ul>
  21. <ul>
  22. <li><a class=active href="#">Inline Widget</a>
  23. </ul>
  24. </div>
  25. <article>
  26. <h2>Inline Widget Demo</h2>
  27. <div id=code></div>
  28. <script id="script">var widgets = []
  29. function updateHints() {
  30. editor.operation(function(){
  31. for (var i = 0; i < widgets.length; ++i)
  32. editor.removeLineWidget(widgets[i]);
  33. widgets.length = 0;
  34. JSHINT(editor.getValue());
  35. for (var i = 0; i < JSHINT.errors.length; ++i) {
  36. var err = JSHINT.errors[i];
  37. if (!err) continue;
  38. var msg = document.createElement("div");
  39. var icon = msg.appendChild(document.createElement("span"));
  40. icon.innerHTML = "!!";
  41. icon.className = "lint-error-icon";
  42. msg.appendChild(document.createTextNode(err.reason));
  43. msg.className = "lint-error";
  44. widgets.push(editor.addLineWidget(err.line - 1, msg, {coverGutter: false, noHScroll: true}));
  45. }
  46. });
  47. var info = editor.getScrollInfo();
  48. var after = editor.charCoords({line: editor.getCursor().line + 1, ch: 0}, "local").top;
  49. if (info.top + info.clientHeight < after)
  50. editor.scrollTo(null, after - info.clientHeight + 3);
  51. }
  52. window.onload = function() {
  53. var sc = document.getElementById("script");
  54. var content = sc.textContent || sc.innerText || sc.innerHTML;
  55. window.editor = CodeMirror(document.getElementById("code"), {
  56. lineNumbers: true,
  57. mode: "javascript",
  58. value: content
  59. });
  60. var waiting;
  61. editor.on("change", function() {
  62. clearTimeout(waiting);
  63. waiting = setTimeout(updateHints, 500);
  64. });
  65. setTimeout(updateHints, 100);
  66. };
  67. "long line to create a horizontal scrollbar, in order to test whether the (non-inline) widgets stay in place when scrolling to the right";
  68. </script>
  69. <p>This demo runs <a href="http://jshint.com">JSHint</a> over the code
  70. in the editor (which is the script used on this page), and
  71. inserts <a href="../doc/manual.html#addLineWidget">line widgets</a> to
  72. display the warnings that JSHint comes up with.</p>
  73. </article>