import React, { PropTypes } from 'react' import ReactDOM from 'react-dom' import { connect, Provider } from 'react-redux' import reducer from './reducer' import { createStore } from 'redux' import FinderInput from './FinderInput' import FinderList from './FinderList' import FinderDetail from './FinderDetail' import actions, { selectArticle, searchArticle } from './actions' import _ from 'lodash' import dataStore from 'browser/lib/dataStore' import fetchConfig from '../lib/fetchConfig' const electron = require('electron') const { clipboard, ipcRenderer, remote } = electron const path = require('path') let config = fetchConfig() applyConfig(config) ipcRenderer.on('config-apply', function (e, newConfig) { config = newConfig applyConfig(config) }) function applyConfig () { let body = document.body body.setAttribute('data-theme', config['theme-ui']) let hljsCss = document.getElementById('hljs-css') hljsCss.setAttribute('href', '../node_modules/highlight.js/styles/' + config['theme-code'] + '.css') } if (process.env.NODE_ENV !== 'production') { window.addEventListener('keydown', function (e) { if (e.keyCode === 73 && e.metaKey && e.altKey) { remote.getCurrentWindow().toggleDevTools() } }) } function hideFinder () { ipcRenderer.send('hide-finder') } function notify (title, options) { if (process.platform === 'win32') { options.icon = path.join('file://', global.__dirname, '../../resources/app.png') } return new window.Notification(title, options) } require('!!style!css!stylus?sourceMap!../styles/finder/index.styl') const FOLDER_FILTER = 'FOLDER_FILTER' const FOLDER_EXACT_FILTER = 'FOLDER_EXACT_FILTER' const TEXT_FILTER = 'TEXT_FILTER' const TAG_FILTER = 'TAG_FILTER' class FinderMain extends React.Component { constructor (props) { super(props) } componentDidMount () { this.keyDownHandler = e => this.handleKeyDown(e) document.addEventListener('keydown', this.keyDownHandler) ReactDOM.findDOMNode(this.refs.finderInput.refs.input).focus() this.focusHandler = e => { let { dispatch } = this.props dispatch(searchArticle('')) dispatch(selectArticle(null)) } window.addEventListener('focus', this.focusHandler) } componentWillUnmount () { document.removeEventListener('keydown', this.keyDownHandler) window.removeEventListener('focus', this.focusHandler) } handleKeyDown (e) { if (e.keyCode === 38) { this.selectPrevious() e.preventDefault() } if (e.keyCode === 40) { this.selectNext() e.preventDefault() } if (e.keyCode === 13) { this.saveToClipboard() e.preventDefault() } if (e.keyCode === 27) { hideFinder() e.preventDefault() } if (e.keyCode === 91 || e.metaKey) { return } ReactDOM.findDOMNode(this.refs.finderInput.refs.input).focus() } saveToClipboard () { let { activeArticle } = this.props clipboard.writeText(activeArticle.content) ipcRenderer.send('copy-finder') notify('Saved to Clipboard!', { body: 'Paste it wherever you want!', silent: true }) hideFinder() } handleSearchChange (e) { let { dispatch } = this.props dispatch(searchArticle(e.target.value)) } selectArticle (article) { this.setState({currentArticle: article}) } selectPrevious () { let { activeArticle, dispatch } = this.props let index = this.refs.finderList.props.articles.indexOf(activeArticle) let previousArticle = this.refs.finderList.props.articles[index - 1] if (previousArticle != null) dispatch(selectArticle(previousArticle.key)) } selectNext () { let { activeArticle, dispatch } = this.props let index = this.refs.finderList.props.articles.indexOf(activeArticle) let previousArticle = this.refs.finderList.props.articles[index + 1] if (previousArticle != null) dispatch(selectArticle(previousArticle.key)) } render () { let { articles, activeArticle, status, dispatch } = this.props let saveToClipboard = () => this.saveToClipboard() return (