@@ -10,6 +10,7 @@ import dataApi from 'browser/main/lib/dataApi'
1010import { hashHistory } from 'react-router'
1111import ee from 'browser/main/lib/eventEmitter'
1212import CodeMirror from 'codemirror'
13+ import SnippetTab from './SnippetTab'
1314
1415function pass ( name ) {
1516 switch ( name ) {
@@ -238,16 +239,24 @@ class SnippetNoteDetail extends React.Component {
238239 }
239240
240241 handleTabPlusButtonClick ( e ) {
242+ this . addSnippet ( )
243+ }
244+
245+ addSnippet ( ) {
241246 let { note } = this . state
242247
243248 note . snippets = note . snippets . concat ( [ {
244249 name : '' ,
245250 mode : 'text' ,
246251 content : ''
247252 } ] )
253+ let snippetIndex = note . snippets . length - 1
248254
249255 this . setState ( {
250- note
256+ note,
257+ snippetIndex
258+ } , ( ) => {
259+ this . refs [ 'tab-' + snippetIndex ] . startRenaming ( )
251260 } )
252261 }
253262
@@ -279,15 +288,19 @@ class SnippetNoteDetail extends React.Component {
279288 let snippets = this . state . note . snippets . slice ( )
280289 snippets . splice ( index , 1 )
281290 this . state . note . snippets = snippets
291+ let snippetIndex = this . state . snippetIndex >= snippets . length
292+ ? snippets . length - 1
293+ : this . state . snippetIndex
282294 this . setState ( {
283- note : this . state . note
295+ note : this . state . note ,
296+ snippetIndex
284297 } )
285298 }
286299
287- handleNameInputChange ( e , index ) {
300+ renameSnippetByIndex ( index , name ) {
288301 let snippets = this . state . note . snippets . slice ( )
289- snippets [ index ] . name = e . target . value
290- let syntax = CodeMirror . findModeByFileName ( e . target . value . trim ( ) )
302+ snippets [ index ] . name = name
303+ let syntax = CodeMirror . findModeByFileName ( name . trim ( ) )
291304 let mode = syntax != null ? syntax . name : null
292305 if ( mode != null ) snippets [ index ] . mode = mode
293306 this . state . note . snippets = snippets
@@ -339,8 +352,62 @@ class SnippetNoteDetail extends React.Component {
339352 }
340353 }
341354
342- handleDeleteKeyDown ( e ) {
343- if ( e . keyCode === 27 ) this . handleDeleteCancelButtonClick ( e )
355+ handleKeyDown ( e ) {
356+ switch ( e . keyCode ) {
357+ case 9 :
358+ if ( e . ctrlKey && ! e . shiftKey ) {
359+ e . preventDefault ( )
360+ this . jumpNextTab ( )
361+ } else if ( e . ctrlKey && e . shiftKey ) {
362+ e . preventDefault ( )
363+ this . jumpPrevTab ( )
364+ } else if ( ! e . ctrlKey && ! e . shiftKey && e . target === this . refs . description ) {
365+ e . preventDefault ( )
366+ this . focusEditor ( )
367+ }
368+ break
369+ case 76 :
370+ let shouldFocus = global . process . platform === 'darwin'
371+ ? e . metaKey
372+ : e . ctrlKey
373+ if ( shouldFocus ) {
374+ e . preventDefault ( )
375+ this . focus ( )
376+ }
377+ break
378+ case 84 :
379+ {
380+ let shouldFocus = global . process . platform === 'darwin'
381+ ? e . metaKey
382+ : e . ctrlKey
383+ if ( e . shouldFocus ) {
384+ e . preventDefault ( )
385+ this . addSnippet ( )
386+ }
387+ }
388+ }
389+
390+ }
391+
392+ jumpNextTab ( ) {
393+ this . setState ( {
394+ snippetIndex : ( this . state . snippetIndex + 1 ) % this . state . note . snippets . length
395+ } , ( ) => {
396+ this . focusEditor ( )
397+ } )
398+ }
399+
400+ jumpPrevTab ( ) {
401+ this . setState ( {
402+ snippetIndex : ( this . state . snippetIndex - 1 + this . state . note . snippets . length ) % this . state . note . snippets . length
403+ } , ( ) => {
404+ this . focusEditor ( )
405+ } )
406+ }
407+
408+ focusEditor ( ) {
409+ console . log ( 'code-' + this . state . snippetIndex )
410+ this . refs [ 'code-' + this . state . snippetIndex ] . focus ( )
344411 }
345412
346413 render ( ) {
@@ -354,31 +421,19 @@ class SnippetNoteDetail extends React.Component {
354421
355422 let tabList = note . snippets . map ( ( snippet , index ) => {
356423 let isActive = this . state . snippetIndex === index
357- return < div styleName = { isActive
358- ? 'tabList-item--active'
359- : 'tabList-item'
360- }
424+
425+ return < SnippetTab
361426 key = { index }
362- >
363- < button styleName = 'tabList-item-button'
364- onClick = { ( e ) => this . handleTabButtonClick ( e , index ) }
365- >
366- { snippet . name . trim ( ) . length > 0
367- ? snippet . name
368- : < span styleName = 'tabList-item-unnamed' >
369- Unnamed
370- </ span >
371- }
372- </ button >
373- { note . snippets . length > 1 &&
374- < button styleName = 'tabList-item-deleteButton'
375- onClick = { ( e ) => this . handleTabDeleteButtonClick ( e , index ) }
376- >
377- < i className = 'fa fa-times' />
378- </ button >
379- }
380- </ div >
427+ ref = { 'tab-' + index }
428+ snippet = { snippet }
429+ isActive = { isActive }
430+ onClick = { ( e ) => this . handleTabButtonClick ( e , index ) }
431+ onDelete = { ( e ) => this . handleTabDeleteButtonClick ( e , index ) }
432+ onRename = { ( name ) => this . renameSnippetByIndex ( index , name ) }
433+ isDeletable = { note . snippets . length > 1 }
434+ />
381435 } )
436+
382437 let viewList = note . snippets . map ( ( snippet , index ) => {
383438 let isActive = this . state . snippetIndex === index
384439
@@ -389,22 +444,6 @@ class SnippetNoteDetail extends React.Component {
389444 key = { index }
390445 style = { { zIndex : isActive ? 5 : 4 } }
391446 >
392- < div styleName = 'tabView-top' >
393- < input styleName = 'tabView-top-name'
394- placeholder = 'Filename including extensions...'
395- value = { snippet . name }
396- onChange = { ( e ) => this . handleNameInputChange ( e , index ) }
397- />
398- < button styleName = 'tabView-top-mode'
399- onClick = { ( e ) => this . handleModeButtonClick ( index ) ( e ) }
400- >
401- { snippet . mode == null
402- ? 'Select Syntax...'
403- : syntax . name
404- }
405- < i className = 'fa fa-caret-down' />
406- </ button >
407- </ div >
408447 { snippet . mode === 'markdown'
409448 ? < MarkdownEditor styleName = 'tabView-content'
410449 value = { snippet . content }
@@ -432,6 +471,7 @@ class SnippetNoteDetail extends React.Component {
432471 < div className = 'NoteDetail'
433472 style = { this . props . style }
434473 styleName = 'root'
474+ onKeyDown = { ( e ) => this . handleKeyDown ( e ) }
435475 >
436476 < div styleName = 'info' >
437477 < div styleName = 'info-left' >
@@ -478,8 +518,8 @@ class SnippetNoteDetail extends React.Component {
478518 </ div >
479519
480520 < div styleName = 'body' >
481- < div styleName = 'body- description' >
482- < textarea styleName = 'body-description-textarea'
521+ < div styleName = 'description' >
522+ < textarea
483523 style = { {
484524 fontFamily : config . preview . fontFamily ,
485525 fontSize : parseInt ( config . preview . fontSize , 10 )
@@ -491,8 +531,10 @@ class SnippetNoteDetail extends React.Component {
491531 />
492532 </ div >
493533 < div styleName = 'tabList' >
494- { tabList }
495- < button styleName = 'tabList-plusButton'
534+ < div styleName = 'list' >
535+ { tabList }
536+ </ div >
537+ < button styleName = 'plusButton'
496538 onClick = { ( e ) => this . handleTabPlusButtonClick ( e ) }
497539 >
498540 < i className = 'fa fa-plus' />
0 commit comments