From 3f01e2f7484eb5b72884aa96a151ee664041c18e Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 15 May 2014 11:50:25 -0600 Subject: [PATCH] Implement shouldComponentUpdate for SelectionsComponent --- src/lines-component.coffee | 2 +- src/selection-component.coffee | 4 ++-- src/selections-component.coffee | 38 +++++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/lines-component.coffee b/src/lines-component.coffee index 3fe9e741c..89d992db3 100644 --- a/src/lines-component.coffee +++ b/src/lines-component.coffee @@ -22,7 +22,7 @@ LinesComponent = React.createClass WebkitTransform: "translate3d(#{-scrollLeft}px, #{-scrollTop}px, 0px)" div {className: 'lines', style}, - SelectionsComponent({editor, lineHeight}) if @isMounted + SelectionsComponent({editor, lineHeight}) if @isMounted() componentWillMount: -> @measuredLines = new WeakSet diff --git a/src/selection-component.coffee b/src/selection-component.coffee index 57eb6d0b8..8f4a1f20a 100644 --- a/src/selection-component.coffee +++ b/src/selection-component.coffee @@ -6,8 +6,8 @@ SelectionComponent = React.createClass displayName: 'SelectionComponent' render: -> - {editor, selection, lineHeight} = @props - {start, end} = selection.getScreenRange() + {editor, screenRange, lineHeight} = @props + {start, end} = screenRange rowCount = end.row - start.row + 1 startPixelPosition = editor.pixelPositionForScreenPosition(start) endPixelPosition = editor.pixelPositionForScreenPosition(end) diff --git a/src/selections-component.coffee b/src/selections-component.coffee index 014600e4f..524f7236a 100644 --- a/src/selections-component.coffee +++ b/src/selections-component.coffee @@ -7,11 +7,37 @@ SelectionsComponent = React.createClass displayName: 'SelectionsComponent' render: -> + div className: 'selections', @renderSelections() + + renderSelections: -> {editor, lineHeight} = @props - div className: 'selections', - if @isMounted() - for selection, index in editor.getSelections() - # Rendering artifacts occur on the lines GPU layer if we remove the last selection - if index is 0 or (not selection.isEmpty() and editor.selectionIntersectsVisibleRowRange(selection)) - SelectionComponent({key: selection.id, selection, editor, lineHeight}) + selectionComponents = [] + for selectionId, screenRange of @selectionRanges + selectionComponents.push(SelectionComponent({key: selectionId, screenRange, editor, lineHeight})) + selectionComponents + + componentWillMount: -> + @selectionRanges = {} + + shouldComponentUpdate: -> + {editor} = @props + oldSelectionRanges = @selectionRanges + newSelectionRanges = {} + @selectionRanges = newSelectionRanges + + for selection, index in editor.getSelections() + # Rendering artifacts occur on the lines GPU layer if we remove the last selection + if index is 0 or (not selection.isEmpty() and editor.selectionIntersectsVisibleRowRange(selection)) + newSelectionRanges[selection.id] = selection.getScreenRange() + + for id, range of newSelectionRanges + if oldSelectionRanges.hasOwnProperty(id) + return true unless range.isEqual(oldSelectionRanges[id]) + else + return true + + for id of oldSelectionRanges + return true unless newSelectionRanges.hasOwnProperty(id) + + false