import React, { PropTypes } from 'react' import CSSModules from 'browser/lib/CSSModules' import styles from './SnippetNoteDetail.styl' import CodeEditor from 'browser/components/CodeEditor' import StarButton from './StarButton' import TagSelect from './TagSelect' import FolderSelect from './FolderSelect' import Commander from 'browser/main/lib/Commander' import dataApi from 'browser/main/lib/dataApi' import modes from 'browser/lib/modes' const electron = require('electron') const { remote } = electron const Menu = remote.Menu const MenuItem = remote.MenuItem class SnippetNoteDetail extends React.Component { constructor (props) { super(props) this.state = { snippetIndex: 0, note: Object.assign({ description: '' }, props.note, { snippets: props.note.snippets.map((snippet) => Object.assign({}, snippet)) }) } } componentDidMount () { Commander.bind('note-detail', this) } componentWillUnmount () { Commander.release(this) } fire (command) { switch (command) { case 'focus': this.refs.description.focus() } } componentWillReceiveProps (nextProps) { if (nextProps.note.key !== this.props.note.key) { this.setState({ snippetIndex: 0, note: Object.assign({ description: '' }, nextProps.note, { snippets: nextProps.note.snippets.map((snippet) => Object.assign({}, snippet)) }) }, () => { let { snippets } = this.state.note snippets.forEach((snippet, index) => { this.refs['code-' + index].reload() }) this.refs.tags.reset() }) } } findTitle (value) { let splitted = value.split('\n') let title = null for (let i = 0; i < splitted.length; i++) { let trimmedLine = splitted[i].trim() if (trimmedLine.match(/^# .+/)) { title = trimmedLine.substring(1, trimmedLine.length).trim() break } } if (title == null) { for (let i = 0; i < splitted.length; i++) { let trimmedLine = splitted[i].trim() if (trimmedLine.length > 0) { title = trimmedLine break } } if (title == null) { title = '' } } return title } handleChange (e) { let { note } = this.state note.tags = this.refs.tags.value note.description = this.refs.description.value note.updatedAt = new Date() note.title = this.findTitle(note.description) this.setState({ note }, () => { this.save() }) } save () { let { note, dispatch } = this.props dispatch({ type: 'UPDATE_NOTE', note: this.state.note }) dataApi .updateNote(note.storage, note.folder, note.key, this.state.note) } handleFolderChange (e) { } handleStarButtonClick (e) { let { note } = this.state note.isStarred = !note.isStarred this.setState({ note }, () => { this.save() }) } exportAsFile () { } handleShareButtonClick (e) { let menu = new Menu() menu.append(new MenuItem({ label: 'Export as a File', click: (e) => this.handlePreferencesButtonClick(e) })) menu.append(new MenuItem({ label: 'Export to Web', disabled: true, click: (e) => this.handlePreferencesButtonClick(e) })) menu.popup(remote.getCurrentWindow()) } handleContextButtonClick (e) { let menu = new Menu() menu.append(new MenuItem({ label: 'Delete', click: (e) => this.handlePreferencesButtonClick(e) })) menu.popup(remote.getCurrentWindow()) } handleTabPlusButtonClick (e) { let { note } = this.state note.snippets = note.snippets.concat([{ name: '', mode: 'text', content: '' }]) this.setState({ note }) } handleTabButtonClick (index) { return (e) => { this.setState({ snippetIndex: index }) } } handleTabDeleteButtonClick (index) { return (e) => { if (this.state.note.snippets.length > 1) { let snippets = this.state.note.snippets.slice() snippets.splice(index, 1) this.state.note.snippets = snippets this.setState({ note: this.state.note }) } } } handleNameInputChange (index) { return (e) => { let snippets = this.state.note.snippets.slice() snippets[index].name = e.target.value this.state.note.snippets = snippets this.setState({ note: this.state.note }, () => { this.save() }) } } handleModeButtonClick (index) { return (e) => { let menu = new Menu() modes.forEach((mode) => { menu.append(new MenuItem({ label: mode.label, click: (e) => this.handleModeOptionClick(index, mode.name)(e) })) }) menu.popup(remote.getCurrentWindow()) } } handleModeOptionClick (index, name) { return (e) => { let snippets = this.state.note.snippets.slice() snippets[index].mode = name this.state.note.snippets = snippets this.setState({ note: this.state.note }, () => { this.save() }) } } handleCodeChange (index) { return (e) => { let snippets = this.state.note.snippets.slice() snippets[index].content = this.refs['code-' + index].value this.state.note.snippets = snippets this.setState({ note: this.state.note }, () => { this.save() }) } } render () { let { storages, config } = this.props let { note } = this.state let editorFontSize = parseInt(config.editor.fontSize, 10) if (!(editorFontSize > 0 && editorFontSize < 101)) editorFontSize = 14 let editorIndentSize = parseInt(config.editor.indentSize, 10) if (!(editorFontSize > 0 && editorFontSize < 132)) editorIndentSize = 4 let tabList = note.snippets.map((snippet, index) => { let isActive = this.state.snippetIndex === index return