1- import { Component } from 'substance'
1+ import { CustomSurface } from 'substance'
22import { renderNode } from '../../kit'
33import { getPos } from './nodeHelpers'
44
5- export default class ReferenceListComponent extends Component {
5+ export default class ReferenceListComponent extends CustomSurface {
66 didMount ( ) {
7- this . context . appState . addObserver ( [ 'document' ] , this . rerender , this , { stage : 'render' , document : { path : [ 'article' , 'references' ] } } )
7+ super . didMount ( )
8+
9+ const appState = this . context . appState
10+ appState . addObserver ( [ 'document' ] , this . rerender , this , { stage : 'render' , document : { path : [ 'article' , 'references' ] } } )
11+ appState . addObserver ( [ 'selection' ] , this . rerender , this , { stage : 'render' } )
812 }
913
1014 dispose ( ) {
15+ super . dispose ( )
1116 // TODO: as we have a node for references now, we should turn this into a NodeComponent instead
1217 this . context . appState . removeObserver ( this )
1318 }
@@ -20,6 +25,7 @@ export default class ReferenceListComponent extends Component {
2025 }
2126
2227 render ( $$ ) {
28+ const sel = this . context . appState . selection
2329 const bibliography = this . _getBibliography ( )
2430
2531 let el = $$ ( 'div' ) . addClass ( 'sc-reference-list' )
@@ -31,19 +37,41 @@ export default class ReferenceListComponent extends Component {
3137 }
3238
3339 bibliography . forEach ( ref => {
34- el . append (
35- renderNode ( $$ , this , ref )
36- )
40+ const referenceEl = renderNode ( $$ , this , ref )
41+ . ref ( ref . id )
42+ . on ( 'click' , this . _selectReference . bind ( this , ref . id ) )
43+
44+ if ( sel && sel . customType === 'reference' && sel . data . referenceId === ref . id ) {
45+ referenceEl . addClass ( 'sm-selected' )
46+ }
47+
48+ el . append ( referenceEl )
3749 } )
3850
3951 return el
4052 }
4153
54+ _getCustomResourceId ( ) {
55+ return 'reference-list'
56+ }
57+
4258 _getBibliography ( ) {
4359 let references = this . props . model . getItems ( )
4460 references . sort ( ( a , b ) => {
4561 return getPos ( a ) - getPos ( b )
4662 } )
4763 return references
4864 }
65+
66+ _selectReference ( referenceId ) {
67+ const newSel = {
68+ type : 'custom' ,
69+ customType : 'reference' ,
70+ nodeId : referenceId ,
71+ data : {
72+ referenceId
73+ }
74+ }
75+ this . context . editorSession . setSelection ( newSel )
76+ }
4977}
0 commit comments