From 8985062d3489cc423891bdcae8769e033b072d3d Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Mon, 14 Aug 2017 09:14:51 +0900 Subject: [PATCH] :recycle: Refactor --- browser/main/NewNoteButton/NewNoteButton.styl | 10 +- browser/main/NewNoteButton/index.js | 139 ++++-------------- 2 files changed, 35 insertions(+), 114 deletions(-) diff --git a/browser/main/NewNoteButton/NewNoteButton.styl b/browser/main/NewNoteButton/NewNoteButton.styl index db5b4ca8..b53ff892 100644 --- a/browser/main/NewNoteButton/NewNoteButton.styl +++ b/browser/main/NewNoteButton/NewNoteButton.styl @@ -20,7 +20,7 @@ $control-height = 34px overflow hidden display flex -.control-newPostButton +.control-newNoteButton display block width 32px height $control-height - 2 @@ -30,10 +30,10 @@ $control-height = 34px padding 0 &:active border-color $ui-button--active-backgroundColor - &:hover .control-newPostButton-tooltip + &:hover .control-newNoteButton-tooltip opacity 1 -.control-newPostButton-tooltip +.control-newNoteButton-tooltip tooltip() position fixed pointer-events none @@ -53,7 +53,7 @@ body[data-theme="dark"] .control border-color $ui-dark-borderColor - .control-newPostButton + .control-newNoteButton color $ui-inactive-text-color border-color $ui-dark-borderColor background-color $ui-dark-noteList-backgroundColor @@ -64,5 +64,5 @@ body[data-theme="dark"] background-color alpha($ui-dark-button--active-backgroundColor, 20%) border-color $ui-dark-button--active-backgroundColor - .control-newPostButton-tooltip + .control-newNoteButton-tooltip darkTooltip() diff --git a/browser/main/NewNoteButton/index.js b/browser/main/NewNoteButton/index.js index 6e8674d7..d2269fa0 100644 --- a/browser/main/NewNoteButton/index.js +++ b/browser/main/NewNoteButton/index.js @@ -6,7 +6,6 @@ import modal from 'browser/main/lib/modal' import NewNoteModal from 'browser/main/modals/NewNoteModal' import { hashHistory } from 'react-router' import ee from 'browser/main/lib/eventEmitter' -import ConfigManager from 'browser/main/lib/ConfigManager' import dataApi from 'browser/main/lib/dataApi' const { remote } = require('electron') @@ -22,7 +21,7 @@ class NewNoteButton extends React.Component { } this.newNoteHandler = () => { - this.handleNewPostButtonClick() + this.handleNewNoteButtonClick() } } @@ -35,38 +34,16 @@ class NewNoteButton extends React.Component { ee.off('top:new-note', this.newNoteHandler) } - handleNewPostButtonClick (e) { - let { config, location } = this.props + handleNewNoteButtonClick (e) { + let { config, location, dispatch } = this.props + let { storage, folder } = this.resolveTargetFolder() - if (location.pathname === '/trashed') { - dialog.showMessageBox(remote.getCurrentWindow(), { - type: 'warning', - message: 'Cannot create new note', - detail: 'You cannot create new note in trash box.', - buttons: ['OK'] - }) - return - } - - switch (config.ui.defaultNote) { - case 'MARKDOWN_NOTE': - this.createNote('MARKDOWN_NOTE') - break - case 'SNIPPET_NOTE': - this.createNote('SNIPPET_NOTE') - break - case 'ALWAYS_ASK': - default: - let { dispatch, location } = this.props - let { storage, folder } = this.resolveTargetFolder() - - modal.open(NewNoteModal, { - storage: storage.key, - folder: folder.key, - dispatch, - location - }) - } + modal.open(NewNoteModal, { + storage: storage.key, + folder: folder.key, + dispatch, + location + }) } resolveTargetFolder () { @@ -91,87 +68,31 @@ class NewNoteButton extends React.Component { } } - createNote (noteType) { - let { dispatch, location } = this.props - if (noteType !== 'MARKDOWN_NOTE' && noteType !== 'SNIPPET_NOTE') throw new Error('Invalid note type.') - - let { storage, folder } = this.resolveTargetFolder() - - let newNote = noteType === 'MARKDOWN_NOTE' - ? { - type: 'MARKDOWN_NOTE', - folder: folder.key, - title: '', - content: '' - } - : { - type: 'SNIPPET_NOTE', - folder: folder.key, - title: '', - description: '', - snippets: [{ - name: '', - mode: 'text', - content: '' - }] - } - - dataApi - .createNote(storage.key, newNote) - .then((note) => { - dispatch({ - type: 'UPDATE_NOTE', - note: note - }) - hashHistory.push({ - pathname: location.pathname, - query: {key: note.storage + '-' + note.key} - }) - ee.emit('detail:focus') - }) - } - - setDefaultNote (defaultNote) { - let { config, dispatch } = this.props - let ui = Object.assign(config.ui) - ui.defaultNote = defaultNote - ConfigManager.set({ - ui - }) - - dispatch({ - type: 'SET_UI', - config: ConfigManager.get() - }) - } - render () { - let { config, style, data } = this.props - return ( -
-
- + let { config, style, data, location } = this.props + if (location.pathname === '/trashed') { + return '' + } else { + return ( +
+
+ +
-
- ) + ) + } } } -NewNoteButton.contextTypes = { - router: PropTypes.shape({ - push: PropTypes.func - }) -} - NewNoteButton.propTypes = { dispatch: PropTypes.func, config: PropTypes.shape({