You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.6 KiB

7 months ago
  1. <!doctype html>
  2. <title>CodeMirror: XML Autocomplete Demo</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../doc/docs.css">
  5. <link rel="stylesheet" href="../lib/codemirror.css">
  6. <link rel="stylesheet" href="../addon/hint/show-hint.css">
  7. <script src="../lib/codemirror.js"></script>
  8. <script src="../addon/hint/show-hint.js"></script>
  9. <script src="../addon/hint/xml-hint.js"></script>
  10. <script src="../mode/xml/xml.js"></script>
  11. <style>
  12. .CodeMirror { border: 1px solid #eee; }
  13. </style>
  14. <div id=nav>
  15. <a href="https://codemirror.net/5"><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/codemirror5">Code</a>
  20. </ul>
  21. <ul>
  22. <li><a class=active href="#">XML Autocomplete</a>
  23. </ul>
  24. </div>
  25. <article>
  26. <h2>XML Autocomplete Demo</h2>
  27. <form><textarea id="code" name="code"><!-- write some xml below -->
  28. </textarea></form>
  29. <p>Press <strong>ctrl-space</strong>, or type a '&lt;' character to
  30. activate autocompletion. This demo defines a simple schema that
  31. guides completion. The schema can be customized—see
  32. the <a href="../doc/manual.html#addon_xml-hint">manual</a>.</p>
  33. <p>Development of the <code>xml-hint</code> addon was kindly
  34. sponsored
  35. by <a href="http://www.xperiment.mobi">www.xperiment.mobi</a>.</p>
  36. <script>
  37. var dummy = {
  38. attrs: {
  39. color: ["red", "green", "blue", "purple", "white", "black", "yellow"],
  40. size: ["large", "medium", "small"],
  41. description: null
  42. },
  43. children: []
  44. };
  45. var tags = {
  46. "!top": ["top"],
  47. "!attrs": {
  48. id: null,
  49. class: ["A", "B", "C"]
  50. },
  51. top: {
  52. attrs: {
  53. lang: ["en", "de", "fr", "nl"],
  54. freeform: null
  55. },
  56. children: ["animal", "plant"]
  57. },
  58. animal: {
  59. attrs: {
  60. name: null,
  61. isduck: ["yes", "no"]
  62. },
  63. children: ["wings", "feet", "body", "head", "tail"]
  64. },
  65. plant: {
  66. attrs: {name: null},
  67. children: ["leaves", "stem", "flowers"]
  68. },
  69. wings: dummy, feet: dummy, body: dummy, head: dummy, tail: dummy,
  70. leaves: dummy, stem: dummy, flowers: dummy
  71. };
  72. function completeAfter(cm, pred) {
  73. var cur = cm.getCursor();
  74. if (!pred || pred()) setTimeout(function() {
  75. if (!cm.state.completionActive)
  76. cm.showHint({completeSingle: false});
  77. }, 100);
  78. return CodeMirror.Pass;
  79. }
  80. function completeIfAfterLt(cm) {
  81. return completeAfter(cm, function() {
  82. var cur = cm.getCursor();
  83. return cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur) == "<";
  84. });
  85. }
  86. function completeIfInTag(cm) {
  87. return completeAfter(cm, function() {
  88. var tok = cm.getTokenAt(cm.getCursor());
  89. if (tok.type == "string" && (!/['"]/.test(tok.string.charAt(tok.string.length - 1)) || tok.string.length == 1)) return false;
  90. var inner = CodeMirror.innerMode(cm.getMode(), tok.state).state;
  91. return inner.tagName;
  92. });
  93. }
  94. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  95. mode: "xml",
  96. lineNumbers: true,
  97. extraKeys: {
  98. "'<'": completeAfter,
  99. "'/'": completeIfAfterLt,
  100. "' '": completeIfInTag,
  101. "'='": completeIfInTag,
  102. "Ctrl-Space": "autocomplete"
  103. },
  104. hintOptions: {schemaInfo: tags}
  105. });
  106. </script>
  107. </article>