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.

144 lines
5.3 KiB

7 months ago
  1. <!doctype html>
  2. <title>CodeMirror: Version 4 upgrade guide</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="docs.css">
  5. <script src="activebookmark.js"></script>
  6. <div id=nav>
  7. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="logo.png"></a>
  8. <ul>
  9. <li><a href="../index.html">Home</a>
  10. <li><a href="manual.html">Manual</a>
  11. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  12. </ul>
  13. <ul>
  14. <li><a class=active href="#upgrade">Upgrade guide</a>
  15. <li><a href="#multisel">Multiple selections</a>
  16. <li><a href="#beforeSelectionChange">The beforeSelectionChange event</a>
  17. <li><a href="#replaceSelection">replaceSelection and collapsing</a>
  18. <li><a href="#changeEvent">change event data</a>
  19. <li><a href="#showIfHidden">showIfHidden option to line widgets</a>
  20. <li><a href="#module">Module loaders</a>
  21. <li><a href="#shareddata">Mutating shared data structures</a></li>
  22. <li><a href="#deprecated">Deprecated interfaces dropped</a>
  23. </ul>
  24. </div>
  25. <article>
  26. <h2 id=upgrade>Upgrading to version 4</h2>
  27. <p>CodeMirror 4's interface is <em>very</em> close version 3, but it
  28. does fix a few awkward details in a backwards-incompatible ways. At
  29. least skim the text below before upgrading.</p>
  30. <section id=multisel><h2>Multiple selections</h2>
  31. <p>The main new feature in version 4 is multiple selections. The
  32. single-selection variants of methods are still there, but now
  33. typically act only on the <em>primary</em> selection (usually the last
  34. one added).</p>
  35. <p>The exception to this
  36. is <a href="manual.html#getSelection"><strong><code>getSelection</code></strong></a>,
  37. which will now return the content of <em>all</em> selections
  38. (separated by newlines, or whatever <code>lineSep</code> parameter you passed
  39. it).</p>
  40. </section>
  41. <section id=beforeSelectionChange><h2>The beforeSelectionChange event</h2>
  42. <p>This event still exists, but the object it is passed has
  43. a <a href="manual.html#event_beforeSelectionChange">completely new
  44. interface</a>, because such changes now concern multiple
  45. selections.</p>
  46. </section>
  47. <section id=replaceSelection><h2>replaceSelection's collapsing behavior</h2>
  48. <p>By
  49. default, <a href="manual.html#replaceSelection"><code>replaceSelection</code></a>
  50. would leave the newly inserted text selected. This is only rarely what
  51. you want, and also (slightly) more expensive in the new model, so the
  52. default was changed to <code>"end"</code>, meaning the old behavior
  53. must be explicitly specified by passing a second argument
  54. of <code>"around"</code>.</p>
  55. </section>
  56. <section id=changeEvent><h2>change event data</h2>
  57. <p>Rather than forcing client code to follow <code>next</code>
  58. pointers from one change object to the next, the library will now
  59. simply fire
  60. multiple <a href="manual.html#event_change"><code>"change"</code></a>
  61. events. Existing code will probably continue to work unmodified.</p>
  62. </section>
  63. <section id=showIfHidden><h2>showIfHidden option to line widgets</h2>
  64. <p>This option, which conceptually caused line widgets to be visible
  65. even if their line was hidden, was never really well-defined, and was
  66. buggy from the start. It would be a rather expensive feature, both in
  67. code complexity and run-time performance, to implement properly. It
  68. has been dropped entirely in 4.0.</p>
  69. </section>
  70. <section id=module><h2>Module loaders</h2>
  71. <p>All modules in the CodeMirror distribution are now wrapped in a
  72. shim function to make them compatible with both AMD
  73. (<a href="http://requirejs.org">requirejs</a>) and CommonJS (as used
  74. by <a href="http://nodejs.org/">node</a>
  75. and <a href="http://browserify.org/">browserify</a>) module loaders.
  76. When neither of these is present, they fall back to simply using the
  77. global <code>CodeMirror</code> variable.</p>
  78. <p>If you have a module loader present in your environment, CodeMirror
  79. will attempt to use it, and you might need to change the way you load
  80. CodeMirror modules.</p>
  81. </section>
  82. <section id=shareddata><h2>Mutating shared data structures</h2>
  83. <p>Data structures produced by the library should not be mutated
  84. unless explicitly allowed, in general. This is slightly more strict in
  85. 4.0 than it was in earlier versions, which copied the position objects
  86. returned by <a href="manual.html#getCursor"><code>getCursor</code></a>
  87. for nebulous, historic reasons. In 4.0, mutating these
  88. objects <em>will</em> corrupt your editor's selection.</p>
  89. </section>
  90. <section id=deprecated><h2>Deprecated interfaces dropped</h2>
  91. <p>A few properties and methods that have been deprecated for a while
  92. are now gone. Most notably, the <code>onKeyEvent</code>
  93. and <code>onDragEvent</code> options (use the
  94. corresponding <a href="manual.html#event_dom">events</a> instead).</p>
  95. <p>Two silly methods, which were mostly there to stay close to the 0.x
  96. API, <code>setLine</code> and <code>removeLine</code> are now gone.
  97. Use the more
  98. flexible <a href="manual.html#replaceRange"><code>replaceRange</code></a>
  99. method instead.</p>
  100. <p>The long names for folding and completing functions
  101. (<code>CodeMirror.braceRangeFinder</code>, <code>CodeMirror.javascriptHint</code>,
  102. etc) are also gone
  103. (use <code>CodeMirror.fold.brace</code>, <code>CodeMirror.hint.javascript</code>).</p>
  104. <p>The <code>className</code> property in the return value
  105. of <a href="manual.html#getTokenAt"><code>getTokenAt</code></a>, which
  106. has been superseded by the <code>type</code> property, is also no
  107. longer present.</p>
  108. </section>
  109. </article>