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.

230 lines
8.3 KiB

2 months ago
  1. <!doctype html>
  2. <title>CodeMirror: Version 3 upgrade guide</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="docs.css">
  5. <script src="../lib/codemirror.js"></script>
  6. <link rel="stylesheet" href="../lib/codemirror.css">
  7. <script src="../addon/runmode/runmode.js"></script>
  8. <script src="../addon/runmode/colorize.js"></script>
  9. <script src="../mode/javascript/javascript.js"></script>
  10. <script src="../mode/xml/xml.js"></script>
  11. <script src="../mode/css/css.js"></script>
  12. <script src="../mode/htmlmixed/htmlmixed.js"></script>
  13. <script src="activebookmark.js"></script>
  14. <div id=nav>
  15. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="logo.png"></a>
  16. <ul>
  17. <li><a href="../index.html">Home</a>
  18. <li><a href="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="#upgrade">Upgrade guide</a>
  23. <li><a href="#dom">DOM structure</a></li>
  24. <li><a href="#gutters">Gutter model</a></li>
  25. <li><a href="#events">Event handling</a></li>
  26. <li><a href="#marktext">markText method arguments</a></li>
  27. <li><a href="#folding">Line folding</a></li>
  28. <li><a href="#lineclass">Line CSS classes</a></li>
  29. <li><a href="#positions">Position properties</a></li>
  30. <li><a href="#matchbrackets">Bracket matching</a></li>
  31. <li><a href="#modes">Mode management</a></li>
  32. <li><a href="#new">New features</a></li>
  33. </ul>
  34. </div>
  35. <article>
  36. <h2 id=upgrade>Upgrading to version 3</h2>
  37. <p>Version 3 does not depart too much from 2.x API, and sites that use
  38. CodeMirror in a very simple way might be able to upgrade without
  39. trouble. But it does introduce a number of incompatibilities. Please
  40. at least skim this text before upgrading.</p>
  41. <p>Note that <strong>version 3 drops full support for Internet
  42. Explorer 7</strong>. The editor will mostly work on that browser, but
  43. it'll be significantly glitchy.</p>
  44. <section id=dom>
  45. <h2>DOM structure</h2>
  46. <p>This one is the most likely to cause problems. The internal
  47. structure of the editor has changed quite a lot, mostly to implement a
  48. new scrolling model.</p>
  49. <p>Editor height is now set on the outer wrapper element (CSS
  50. class <code>CodeMirror</code>), not on the scroller element
  51. (<code>CodeMirror-scroll</code>).</p>
  52. <p>Other nodes were moved, dropped, and added. If you have any code
  53. that makes assumptions about the internal DOM structure of the editor,
  54. you'll have to re-test it and probably update it to work with v3.</p>
  55. <p>See the <a href="manual.html#styling">styling section</a> of the
  56. manual for more information.</p>
  57. </section>
  58. <section id=gutters>
  59. <h2>Gutter model</h2>
  60. <p>In CodeMirror 2.x, there was a single gutter, and line markers
  61. created with <code>setMarker</code> would have to somehow coexist with
  62. the line numbers (if present). Version 3 allows you to specify an
  63. array of gutters, <a href="manual.html#option_gutters">by class
  64. name</a>,
  65. use <a href="manual.html#setGutterMarker"><code>setGutterMarker</code></a>
  66. to add or remove markers in individual gutters, and clear whole
  67. gutters
  68. with <a href="manual.html#clearGutter"><code>clearGutter</code></a>.
  69. Gutter markers are now specified as DOM nodes, rather than HTML
  70. snippets.</p>
  71. <p>The gutters no longer horizontally scrolls along with the content.
  72. The <code>fixedGutter</code> option was removed (since it is now the
  73. only behavior).</p>
  74. <pre data-lang="text/html">
  75. &lt;style>
  76. /* Define a gutter style */
  77. .note-gutter { width: 3em; background: cyan; }
  78. &lt;/style>
  79. &lt;script>
  80. // Create an instance with two gutters -- line numbers and notes
  81. var cm = new CodeMirror(document.body, {
  82. gutters: ["note-gutter", "CodeMirror-linenumbers"],
  83. lineNumbers: true
  84. });
  85. // Add a note to line 0
  86. cm.setGutterMarker(0, "note-gutter", document.createTextNode("hi"));
  87. &lt;/script>
  88. </pre>
  89. </section>
  90. <section id=events>
  91. <h2>Event handling</h2>
  92. <p>Most of the <code>onXYZ</code> options have been removed. The same
  93. effect is now obtained by calling
  94. the <a href="manual.html#on"><code>on</code></a> method with a string
  95. identifying the event type. Multiple handlers can now be registered
  96. (and individually unregistered) for an event, and objects such as line
  97. handlers now also expose events. See <a href="manual.html#events">the
  98. full list here</a>.</p>
  99. <p>(The <code>onKeyEvent</code> and <code>onDragEvent</code> options,
  100. which act more as hooks than as event handlers, are still there in
  101. their old form.)</p>
  102. <pre data-lang="javascript">
  103. cm.on("change", function(cm, change) {
  104. console.log("something changed! (" + change.origin + ")");
  105. });
  106. </pre>
  107. </section>
  108. <section id=marktext>
  109. <h2>markText method arguments</h2>
  110. <p>The <a href="manual.html#markText"><code>markText</code></a> method
  111. (which has gained some interesting new features, such as creating
  112. atomic and read-only spans, or replacing spans with widgets) no longer
  113. takes the CSS class name as a separate argument, but makes it an
  114. optional field in the options object instead.</p>
  115. <pre data-lang="javascript">
  116. // Style first ten lines, and forbid the cursor from entering them
  117. cm.markText({line: 0, ch: 0}, {line: 10, ch: 0}, {
  118. className: "magic-text",
  119. inclusiveLeft: true,
  120. atomic: true
  121. });
  122. </pre>
  123. </section>
  124. <section id=folding>
  125. <h2>Line folding</h2>
  126. <p>The interface for hiding lines has been
  127. removed. <a href="manual.html#markText"><code>markText</code></a> can
  128. now be used to do the same in a more flexible and powerful way.</p>
  129. <p>The <a href="../demo/folding.html">folding script</a> has been
  130. updated to use the new interface, and should now be more robust.</p>
  131. <pre data-lang="javascript">
  132. // Fold a range, replacing it with the text "??"
  133. var range = cm.markText({line: 4, ch: 2}, {line: 8, ch: 1}, {
  134. replacedWith: document.createTextNode("??"),
  135. // Auto-unfold when cursor moves into the range
  136. clearOnEnter: true
  137. });
  138. // Get notified when auto-unfolding
  139. CodeMirror.on(range, "clear", function() {
  140. console.log("boom");
  141. });
  142. </pre>
  143. </section>
  144. <section id=lineclass>
  145. <h2>Line CSS classes</h2>
  146. <p>The <code>setLineClass</code> method has been replaced
  147. by <a href="manual.html#addLineClass"><code>addLineClass</code></a>
  148. and <a href="manual.html#removeLineClass"><code>removeLineClass</code></a>,
  149. which allow more modular control over the classes attached to a line.</p>
  150. <pre data-lang="javascript">
  151. var marked = cm.addLineClass(10, "background", "highlighted-line");
  152. setTimeout(function() {
  153. cm.removeLineClass(marked, "background", "highlighted-line");
  154. });
  155. </pre>
  156. </section>
  157. <section id=positions>
  158. <h2>Position properties</h2>
  159. <p>All methods that take or return objects that represent screen
  160. positions now use <code>{left, top, bottom, right}</code> properties
  161. (not always all of them) instead of the <code>{x, y, yBot}</code> used
  162. by some methods in v2.x.</p>
  163. <p>Affected methods
  164. are <a href="manual.html#cursorCoords"><code>cursorCoords</code></a>, <a href="manual.html#charCoords"><code>charCoords</code></a>, <a href="manual.html#coordsChar"><code>coordsChar</code></a>,
  165. and <a href="manual.html#getScrollInfo"><code>getScrollInfo</code></a>.</p>
  166. </section>
  167. <section id=matchbrackets>
  168. <h2>Bracket matching no longer in core</h2>
  169. <p>The <a href="manual.html#addon_matchbrackets"><code>matchBrackets</code></a>
  170. option is no longer defined in the core editor.
  171. Load <code>addon/edit/matchbrackets.js</code> to enable it.</p>
  172. </section>
  173. <section id=modes>
  174. <h2>Mode management</h2>
  175. <p>The <code>CodeMirror.listModes</code>
  176. and <code>CodeMirror.listMIMEs</code> functions, used for listing
  177. defined modes, are gone. You are now encouraged to simply
  178. inspect <code>CodeMirror.modes</code> (mapping mode names to mode
  179. constructors) and <code>CodeMirror.mimeModes</code> (mapping MIME
  180. strings to mode specs).</p>
  181. </section>
  182. <section id=new>
  183. <h2>New features</h2>
  184. <p>Some more reasons to upgrade to version 3.</p>
  185. <ul>
  186. <li>Bi-directional text support. CodeMirror will now mostly do the
  187. right thing when editing Arabic or Hebrew text.</li>
  188. <li>Arbitrary line heights. Using fonts with different heights
  189. inside the editor (whether off by one pixel or fifty) is now
  190. supported and handled gracefully.</li>
  191. <li>In-line widgets. See <a href="../demo/widget.html">the demo</a>
  192. and <a href="manual.html#addLineWidget">the docs</a>.</li>
  193. <li>Defining custom options
  194. with <a href="manual.html#defineOption"><code>CodeMirror.defineOption</code></a>.</li>
  195. </ul>
  196. </section>
  197. </article>
  198. <script>setTimeout(function(){CodeMirror.colorize();}, 20);</script>