Give highlight decorations unique id's to avoid potential React errors

Using the marker's id plus the decoration class can cause an error in
the event we apply a decoration with the same class twice to the same
marker. This is admittedly unlikely, but I think it's cleaner to just
allocate unique id's for decoration objects.
This commit is contained in:
Nathan Sobo
2014-06-17 15:03:45 -06:00
parent 89be77b0a9
commit edadedce7b
3 changed files with 12 additions and 1 deletions

View File

@@ -856,6 +856,12 @@ describe "EditorComponent", ->
regions = node.querySelectorAll('.test-highlight .region')
expect(regions.length).toBe 2
it "allows the same marker to be decorated with multiple highlights of the same class", ->
editor.addDecorationForMarker(marker, type: 'highlight', class: 'test-highlight')
waitsFor -> not component.decorationChangedImmediate?
runs ->
expect(node.querySelectorAll('.test-highlight').length).toBe 2
describe "hidden input field", ->
it "renders the hidden input field at the position of the last cursor if the cursor is on screen and the editor is focused", ->
editor.setVerticalScrollMargin(0)

View File

@@ -1,5 +1,7 @@
_ = require 'underscore-plus'
IdCounter = 1
module.exports =
class Decoration
@isType: (decoration, decorationType) ->
@@ -9,6 +11,7 @@ class Decoration
decorationType is decoration.type
constructor: (@marker, properties) ->
@id = IdCounter++
_.extend(this, properties)
getScreenRange: ->
@@ -22,6 +25,7 @@ class Decoration
toObject: ->
copy = {}
copy.id = @id
copy.valid = @isValid()
copy.screenRange = @getScreenRange().copy()

View File

@@ -17,7 +17,8 @@ HighlightsComponent = React.createClass
for markerId, decorations of highlightDecorations
if decorations?
for decoration in decorations
highlightComponents.push(HighlightComponent({key: "#{markerId}-#{decoration.class}", decoration, editor, lineHeightInPixels}))
highlightComponents.push(HighlightComponent({key: decoration.id, decoration, editor, lineHeightInPixels}))
highlightComponents
shouldComponentUpdate: (newProps) ->