Skip to content

Commit 09047d2

Browse files
committed
Merge pull request #31 from dmnd/update-upon-save
Update blame view upon save
2 parents 90bef04 + 26e3e1c commit 09047d2

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

lib/controllers/blameViewController.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ function toggleBlame(projectBlamer) {
3636
editorView.blameView = React.renderComponent(new BlameListView({
3737
projectBlamer: projectBlamer,
3838
remoteRevision: remoteRevision,
39-
filePath: filePath,
40-
lineCount: editor.getLineCount(),
41-
scrollbar: editorView.find('.vertical-scrollbar')
39+
editorView: editorView
4240
}), mountPoint[0]);
4341
} else {
4442
editorView.blameView.toggle();

lib/views/blame-line-view.coffee

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ moment = require 'moment'
66

77
HASH_LENGTH = 7 # github uses this length
88
BLANK_HASH = '-'.repeat(HASH_LENGTH)
9-
DEFAULT_DATE = formatDate moment("2000-01-01T13:17:00 Z")
9+
10+
_defaultDate = null
11+
getDefaultDate = ->
12+
_defaultDate ?= formatDate moment("2014-01-01T13:37:00 Z")
1013

1114

1215
renderLoading = ->
1316
div className: 'blame-line loading',
1417
span className: 'hash', BLANK_HASH
15-
span className: 'date', DEFAULT_DATE
18+
span className: 'date', getDefaultDate()
1619
span className: 'committer', 'Loading'
1720

1821

@@ -50,7 +53,7 @@ BlameLineComponent = React.createClass
5053
componentWillUnmount: ->
5154
$(@getDOMNode()).tooltip "destroy"
5255

53-
shouldComponentUpdate: ->
54-
false
56+
shouldComponentUpdate: ({hash}) ->
57+
hash isnt @props.hash
5558

5659
module.exports = {BlameLineComponent, renderLoading}

lib/views/blame-list-view.coffee

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{React, Reactionary, $} = require 'atom'
2-
{div, span, a} = Reactionary
2+
{div} = Reactionary
33
RP = React.PropTypes
44
_ = require 'underscore'
55
{BlameLineComponent, renderLoading} = require './blame-line-view'
@@ -9,12 +9,12 @@ BlameListLinesComponent = React.createClass
99
propTypes:
1010
annotations: RP.arrayOf(RP.object)
1111
loading: RP.bool.isRequired
12-
filePath: RP.string.isRequired
13-
lineCount: RP.number.isRequired
12+
dirty: RP.bool.isRequired
13+
initialLineCount: RP.number.isRequired
1414
remoteRevision: RP.object.isRequired
1515

1616
renderLoading: ->
17-
lines = [0...@props.lineCount].map renderLoading
17+
lines = [0...@props.initialLineCount].map renderLoading
1818
div null, lines
1919

2020
# makes background color alternate by commit
@@ -49,28 +49,34 @@ BlameListLinesComponent = React.createClass
4949
else
5050
@renderLoaded()
5151

52-
shouldComponentUpdate: ({loading}) ->
53-
loading isnt @props.loading
54-
52+
shouldComponentUpdate: ({loading, dirty}) ->
53+
finishedInitialLoad = @props.loading and not loading and not @props.dirty
54+
finishedEdit = @props.dirty and not dirty
55+
finishedInitialLoad or finishedEdit
5556

5657
BlameListView = React.createClass
5758
propTypes:
5859
projectBlamer: RP.object.isRequired
5960
remoteRevision: RP.object.isRequired
60-
filePath: RP.string.isRequired
61-
lineCount: RP.number.isRequired
62-
scrollbar: RP.object.isRequired
61+
editorView: RP.object.isRequired
6362

6463
getInitialState: ->
6564
{
6665
# TODO: get this from the parent component somehow?
67-
scrollTop: @props.scrollbar.scrollTop()
66+
scrollTop: @scrollbar().scrollTop()
6867
# TODO: be intelligent about persisting this so it doesn't reset
6968
width: 210
7069
loading: true
7170
visible: true
71+
dirty: false
7272
}
7373

74+
scrollbar: ->
75+
@_scrollbar ?= @props.editorView.find('.vertical-scrollbar')
76+
77+
editor: ->
78+
@_editor ?= @props.editorView.getModel()
79+
7480
render: ->
7581
display = if @state.visible then 'inline-block' else 'none'
7682

@@ -85,10 +91,9 @@ BlameListView = React.createClass
8591
BlameListLinesComponent
8692
annotations: @state.annotations
8793
loading: @state.loading
88-
filePath: @props.filePath
89-
lineCount: @props.lineCount
94+
dirty: @state.dirty
95+
initialLineCount: @editor().getLineCount()
9096
remoteRevision: @props.remoteRevision
91-
9297
div
9398
className: 'git-blame'
9499
style: width: @state.width, display: display
@@ -107,38 +112,54 @@ BlameListView = React.createClass
107112

108113
componentWillMount: ->
109114
# kick off async request for blame data
110-
@loadBlame true
111-
112-
loadBlame: (force) ->
113-
return if @state.loading and not force
115+
@loadBlame()
116+
@editor().on 'contents-modified', @contentsModified
117+
@editor().buffer.on 'saved', @saved
114118

119+
loadBlame: ->
115120
@setState loading: true
116-
@props.projectBlamer.blame @props.filePath, (err, data) =>
121+
@props.projectBlamer.blame @editor().getPath(), (err, data) =>
117122
if err
118123
@setState
119124
loading: false
120125
error: true
126+
dirty: false
121127
else
122128
@setState
123129
loading: false
124130
error: false
131+
dirty: false
125132
annotations: data
126133

134+
contentsModified: ->
135+
return unless @isMounted()
136+
@setState dirty: true unless @state.dirty
137+
138+
saved: ->
139+
return unless @isMounted()
140+
@loadBlame() if @state.visible and @state.dirty
141+
127142
toggle: ->
128-
@setState visible: !@state.visible
143+
if @state.visible
144+
@setState visible: false
145+
else
146+
@loadBlame() if @state.dirty
147+
@setState visible: true
129148

130149
componentDidMount: ->
131150
# Bind to scroll event on vertical-scrollbar to sync up scroll position of
132151
# blame gutter.
133-
@props.scrollbar.on 'scroll', @matchScrollPosition
152+
@scrollbar().on 'scroll', @matchScrollPosition
134153

135154
componentWillUnmount: ->
136-
@props.scrollbar.off 'scroll', @matchScrollPosition
155+
@scrollbar().off 'scroll', @matchScrollPosition
156+
@editor().off 'contents-modified', @contentsModified
157+
@editor().buffer.off 'saved', @saved
137158

138159
# Makes the view arguments scroll position match the target elements scroll
139160
# position
140161
matchScrollPosition: ->
141-
@setState scrollTop: @props.scrollbar.scrollTop()
162+
@setState scrollTop: @scrollbar().scrollTop()
142163

143164
resizeStarted: ({pageX}) ->
144165
@setState dragging: true, initialPageX: pageX, initialWidth: @state.width

0 commit comments

Comments
 (0)