11import React , { PropTypes } from 'react'
22import { findWhere } from 'lodash'
3- import { setSearchFilter , switchFolder , saveArticle } from '../actions'
3+ import { setSearchFilter , switchFolder , uncacheArticle , saveAllArticles , switchArticle , clearSearch } from '../actions'
44import { openModal , isModalOpen } from 'browser/lib/modal'
55import FolderMark from 'browser/components/FolderMark'
66import Preferences from '../modal/Preferences'
77import CreateNewFolder from '../modal/CreateNewFolder'
8- import keygen from 'browser/lib/keygen'
8+ import _ from 'lodash'
9+ import ModeIcon from 'browser/components/ModeIcon'
910
1011const ipc = require ( 'electron' ) . ipcRenderer
1112
1213const BRAND_COLOR = '#18AF90'
14+ const OSX = global . process . platform === 'darwin'
1315
1416const preferenceTutorialElement = (
1517 < svg width = '300' height = '300' className = 'tutorial' >
@@ -29,7 +31,7 @@ c-4,0-7.9,0-11.9-0.1C164,294,164,297,165.9,297L165.9,297z'/>
2931const newPostTutorialElement = (
3032 < svg width = '900' height = '900' className = 'tutorial' >
3133 < text x = '290' y = '155' fill = { BRAND_COLOR } fontSize = '24' > Create a new post!!</ text >
32- < text x = '300' y = '180' fill = { BRAND_COLOR } fontSize = '16' children = { `press \`${ process . platform === 'darwin' ? '⌘' : '^' } + Enter\` or \`a\`` } />
34+ < text x = '300' y = '180' fill = { BRAND_COLOR } fontSize = '16' children = { `press \`${ OSX === 'darwin' ? '⌘' : '^' } + Enter\` or \`a\`` } />
3335 < svg x = '130' y = '-20' width = '400' height = '400' >
3436 < path fill = 'white' d = 'M56.2,132.5c11.7-2.9,23.9-6,36.1-4.1c8.7,1.4,16.6,5.5,23.7,10.5c13.3,9.4,24.5,21.5,40.2,27
3537 c1.8,0.6,2.6-2.3,0.8-2.9c-17.1-6-28.9-20.3-44-29.7c-7-4.4-14.8-7.4-23-8.2c-11.7-1.1-23.3,1.7-34.5,4.5
@@ -62,54 +64,24 @@ c-3.4-6.1-8.2-11.3-13.8-15.4C50.2,11.6,31,10.9,15.3,19C13.6,19.8,15.1,22.4,16.8,
6264export default class ArticleNavigator extends React . Component {
6365 constructor ( props ) {
6466 super ( props )
65- this . newPostHandler = e => {
66- if ( isModalOpen ( ) ) return true
67- this . handleNewPostButtonClick ( e )
68- }
6967 this . newFolderHandler = e => {
7068 if ( isModalOpen ( ) ) return true
7169 this . handleNewFolderButton ( e )
7270 }
7371 }
7472
7573 componentDidMount ( ) {
76- ipc . on ( 'nav-new-post' , this . newPostHandler )
7774 ipc . on ( 'nav-new-folder' , this . newFolderHandler )
7875 }
7976
8077 componentWillUnmount ( ) {
81- ipc . removeListener ( 'nav-new-post' , this . newPostHandler )
8278 ipc . removeListener ( 'nav-new-folder' , this . newFolderHandler )
8379 }
8480
8581 handlePreferencesButtonClick ( e ) {
8682 openModal ( Preferences )
8783 }
8884
89- handleNewPostButtonClick ( e ) {
90- let { dispatch, folders, status } = this . props
91- let { targetFolders } = status
92-
93- let isFolderFilterApplied = targetFolders . length > 0
94- let FolderKey = isFolderFilterApplied
95- ? targetFolders [ 0 ] . key
96- : folders [ 0 ] . key
97-
98- let newArticle = {
99- key : keygen ( ) ,
100- title : '' ,
101- content : '' ,
102- mode : 'markdown' ,
103- tags : [ ] ,
104- FolderKey : FolderKey ,
105- craetedAt : new Date ( ) ,
106- updatedAt : new Date ( )
107- }
108-
109- dispatch ( saveArticle ( newArticle . key , newArticle , true ) )
110- if ( isFolderFilterApplied ) dispatch ( switchFolder ( targetFolders [ 0 ] . name ) )
111- }
112-
11385 handleNewFolderButton ( e ) {
11486 let { user } = this . props
11587 openModal ( CreateNewFolder , { user : user } )
@@ -127,11 +99,52 @@ export default class ArticleNavigator extends React.Component {
12799 dispatch ( setSearchFilter ( '' ) )
128100 }
129101
102+ handleUnsavedItemClick ( article ) {
103+ let { dispatch } = this . props
104+ return e => {
105+ let { articles } = this . props
106+ let isInArticleList = articles . some ( _article => _article . key === article . key )
107+ if ( ! isInArticleList ) dispatch ( clearSearch ( ) )
108+ dispatch ( switchArticle ( article . key ) )
109+ }
110+ }
111+
112+ handleUncacheButtonClick ( article ) {
113+ let { dispatch } = this . props
114+ return e => {
115+ dispatch ( uncacheArticle ( article . key ) )
116+ }
117+ }
118+
119+ handleSaveAllClick ( e ) {
120+ let { dispatch } = this . props
121+ dispatch ( saveAllArticles ( ) )
122+ }
123+
130124 render ( ) {
131- let { status, user, folders, allArticles } = this . props
125+ let { status, user, folders, allArticles, modified , activeArticle } = this . props
132126 let { targetFolders } = status
133127 if ( targetFolders == null ) targetFolders = [ ]
134128
129+ let modifiedElements = modified . map ( modifiedArticle => {
130+ let originalArticle = _ . findWhere ( allArticles , { key : modifiedArticle . key } )
131+ if ( originalArticle == null ) return false
132+ let combinedArticle = Object . assign ( { } , originalArticle , modifiedArticle )
133+
134+ let className = 'ArticleNavigator-unsaved-list-item'
135+ if ( activeArticle && activeArticle . key === combinedArticle . key ) className += ' active'
136+
137+ return (
138+ < div key = { modifiedArticle . key } onClick = { e => this . handleUnsavedItemClick ( combinedArticle ) ( e ) } className = { className } >
139+ < div className = 'ArticleNavigator-unsaved-list-item-label' >
140+ < ModeIcon mode = { combinedArticle . mode } />
141+ { combinedArticle . title }
142+ </ div >
143+ < button onClick = { e => this . handleUncacheButtonClick ( combinedArticle ) ( e ) } className = 'ArticleNavigator-unsaved-list-item-discard-button' > < i className = 'fa fa-times' /> </ button >
144+ </ div >
145+ )
146+ } ) . filter ( modifiedArticle => modifiedArticle ) . sort ( ( a , b ) => a . updatedAt - b . updatedAt )
147+
135148 let folderElememts = folders . map ( ( folder , index ) => {
136149 let isActive = findWhere ( targetFolders , { key : folder . key } )
137150 let articleCount = allArticles . filter ( article => article . FolderKey === folder . key && article . status !== 'NEW' ) . length
@@ -157,22 +170,27 @@ export default class ArticleNavigator extends React.Component {
157170
158171 </ div >
159172
160- < div className = 'controlSection' >
161- < button onClick = { e => this . handleNewPostButtonClick ( e ) } className = 'newPostBtn' >
162- New Post
163- < span className = 'tooltip' > Create a new Post ({ process . platform === 'darwin' ? '⌘' : '^' } + n)</ span >
164- </ button >
165-
166- { status . isTutorialOpen ? newPostTutorialElement : null }
167-
173+ < div className = 'ArticleNavigator-unsaved' >
174+ < div className = 'ArticleNavigator-unsaved-header' > Work in progress</ div >
175+ < div className = 'ArticleNavigator-unsaved-list' >
176+ { modifiedElements . length > 0
177+ ? modifiedElements
178+ : (
179+ < div className = 'ArticleNavigator-unsaved-list-empty' > Empty list</ div >
180+ )
181+ }
182+ </ div >
183+ < div className = 'ArticleNavigator-unsaved-control' >
184+ < button onClick = { e => this . handleSaveAllClick ( ) } className = 'ArticleNavigator-unsaved-control-save-all-button' disabled = { modifiedElements . length === 0 } > Save all</ button >
185+ </ div >
168186 </ div >
169187
170- < div className = 'folders' >
171- < div className = 'header' >
188+ < div className = 'ArticleNavigator- folders' >
189+ < div className = 'ArticleNavigator-folders- header' >
172190 < div className = 'title' > Folders</ div >
173191 < button onClick = { e => this . handleNewFolderButton ( e ) } className = 'addBtn' >
174192 < i className = 'fa fa-fw fa-plus' />
175- < span className = 'tooltip' > Create a new folder ({ process . platform === 'darwin' ? '⌘' : '^' } + Shift + n)</ span >
193+ < span className = 'tooltip' > Create a new folder ({ OSX === 'darwin' ? '⌘' : '^' } + Shift + n)</ span >
176194 </ button >
177195
178196 { status . isTutorialOpen ? newFolderTutorialElement : null }
@@ -189,12 +207,17 @@ export default class ArticleNavigator extends React.Component {
189207}
190208
191209ArticleNavigator . propTypes = {
192- user : PropTypes . object ,
193- folders : PropTypes . array ,
194- allArticles : PropTypes . array ,
210+ dispatch : PropTypes . func ,
195211 status : PropTypes . shape ( {
196212 folderId : PropTypes . number
197213 } ) ,
198- dispatch : PropTypes . func
214+ user : PropTypes . object ,
215+ folders : PropTypes . array ,
216+ allArticles : PropTypes . array ,
217+ articles : PropTypes . array ,
218+ modified : PropTypes . array ,
219+ activeArticle : PropTypes . shape ( {
220+ key : PropTypes . string
221+ } )
199222}
200223
0 commit comments