import PropTypes from 'prop-types' import React from 'react' import CSSModules from 'browser/lib/CSSModules' import styles from './SnippetNoteDetail.styl' import CodeEditor from 'browser/components/CodeEditor' import MarkdownEditor from 'browser/components/MarkdownEditor' import StarButton from './StarButton' import TagSelect from './TagSelect' import FolderSelect from './FolderSelect' import dataApi from 'browser/main/lib/dataApi' import {hashHistory} from 'react-router' import ee from 'browser/main/lib/eventEmitter' import CodeMirror from 'codemirror' import 'codemirror-mode-elixir' import SnippetTab from 'browser/components/SnippetTab' import StatusBar from '../StatusBar' import context from 'browser/lib/context' import ConfigManager from 'browser/main/lib/ConfigManager' import _ from 'lodash' import {findNoteTitle} from 'browser/lib/findNoteTitle' import AwsMobileAnalyticsConfig from 'browser/main/lib/AwsMobileAnalyticsConfig' import TrashButton from './TrashButton' import PermanentDeleteButton from './PermanentDeleteButton' import InfoButton from './InfoButton' import InfoPanel from './InfoPanel' import InfoPanelTrashed from './InfoPanelTrashed' import {formatDate} from 'browser/lib/date-formatter' function pass (name) { switch (name) { case 'ejs': return 'Embedded Javascript' case 'html_ruby': return 'Embedded Ruby' case 'objectivec': return 'Objective C' case 'text': return 'Plain Text' default: return name } } const electron = require('electron') const { remote } = electron const { Menu, MenuItem, dialog } = remote class SnippetNoteDetail extends React.Component { constructor (props) { super(props) this.state = { isMovingNote: false, snippetIndex: 0, showArrows: false, enableLeftArrow: false, enableRightArrow: false, note: Object.assign({ description: '' }, props.note, { snippets: props.note.snippets.map((snippet) => Object.assign({}, snippet)) }) } } componentDidMount () { const visibleTabs = this.visibleTabs const allTabs = this.allTabs if (visibleTabs.offsetWidth < allTabs.offsetWidth) { this.setState({ showArrows: visibleTabs.offsetWidth < allTabs.offsetWidth, enableRightArrow: allTabs.offsetLeft !== visibleTabs.offsetWidth - allTabs.offsetWidth, enableLeftArrow: allTabs.offsetLeft !== 0 }) } } componentWillReceiveProps (nextProps) { if (nextProps.note.key !== this.props.note.key && !this.isMovingNote) { if (this.saveQueue != null) this.saveNow() const nextNote = Object.assign({ description: '' }, nextProps.note, { snippets: nextProps.note.snippets.map((snippet) => Object.assign({}, snippet)) }) this.setState({ snippetIndex: 0, note: nextNote }, () => { const { snippets } = this.state.note snippets.forEach((snippet, index) => { this.refs['code-' + index].reload() }) if (this.refs.tags) this.refs.tags.reset() }) } } componentWillUnmount () { if (this.saveQueue != null) this.saveNow() } handleChange (e) { const { note } = this.state if (this.refs.tags) note.tags = this.refs.tags.value note.description = this.refs.description.value note.updatedAt = new Date() note.title = findNoteTitle(note.description) this.setState({ note }, () => { this.save() }) } save () { clearTimeout(this.saveQueue) this.saveQueue = setTimeout(() => { this.saveNow() }, 1000) } saveNow () { const { note, dispatch } = this.props clearTimeout(this.saveQueue) this.saveQueue = null dataApi .updateNote(note.storage, note.key, this.state.note) .then((note) => { dispatch({ type: 'UPDATE_NOTE', note: note }) AwsMobileAnalyticsConfig.recordDynamicCustomEvent('EDIT_NOTE') }) } handleFolderChange (e) { const { note } = this.state const value = this.refs.folder.value const splitted = value.split('-') const newStorageKey = splitted.shift() const newFolderKey = splitted.shift() dataApi .moveNote(note.storage, note.key, newStorageKey, newFolderKey) .then((newNote) => { this.setState({ isMovingNote: true, note: Object.assign({}, newNote) }, () => { const { dispatch, location } = this.props dispatch({ type: 'MOVE_NOTE', originNote: note, note: newNote }) hashHistory.replace({ pathname: location.pathname, query: { key: newNote.storage + '-' + newNote.key } }) this.setState({ isMovingNote: false }) }) }) } handleStarButtonClick (e) { const { note } = this.state if (!note.isStarred) AwsMobileAnalyticsConfig.recordDynamicCustomEvent('ADD_STAR') note.isStarred = !note.isStarred this.setState({ note }, () => { this.save() }) } exportAsFile () { } handleTrashButtonClick (e) { const { note } = this.state const { isTrashed } = note const { confirmDeletion } = this.props if (isTrashed) { if (confirmDeletion(true)) { const {note, dispatch} = this.props dataApi .deleteNote(note.storage, note.key) .then((data) => { const dispatchHandler = () => { dispatch({ type: 'DELETE_NOTE', storageKey: data.storageKey, noteKey: data.noteKey }) } ee.once('list:next', dispatchHandler) }) .then(() => ee.emit('list:next')) } } else { if (confirmDeletion()) { note.isTrashed = true this.setState({ note }, () => { this.save() }) ee.emit('list:next') } } } handleUndoButtonClick (e) { const { note } = this.state note.isTrashed = false this.setState({ note }, () => { this.save() ee.emit('list:next') }) } handleFullScreenButton (e) { ee.emit('editor:fullscreen') } handleTabMoveLeftButtonClick (e) { { const left = Math.abs(this.allTabs.offsetLeft) const tabs = this.allTabs.querySelectorAll('div') const lastVisibleTab = Array.from(tabs).find((tab) => { return tab.offsetLeft + tab.offsetWidth >= left }) const visiblePart = lastVisibleTab.offsetWidth - left + lastVisibleTab.offsetLeft const showTab = (visiblePart > lastVisibleTab.offsetWidth * 0.75) ? lastVisibleTab.previousSibling : lastVisibleTab let newLeft = showTab.offsetLeft if (showTab.previousSibling) { newLeft -= showTab.previousSibling.offsetWidth * 0.6 } this.moveTabBarBy(-newLeft) } } handleTabMoveRightButtonClick (e) { const left = this.allTabs.offsetLeft const width = this.visibleTabs.offsetWidth const tabs = this.allTabs.querySelectorAll('div') const lastVisibleTab = Array.from(tabs).find((tab) => { return tab.offsetLeft + tab.offsetWidth >= width - left }) if (lastVisibleTab) { let newLeft = lastVisibleTab.offsetLeft + lastVisibleTab.offsetWidth - width if (width - left - lastVisibleTab.offsetLeft > lastVisibleTab.offsetWidth * 0.75) { newLeft += lastVisibleTab.nextSibling.offsetWidth } this.moveTabBarBy(-newLeft) } } handleTabPlusButtonClick (e) { this.addSnippet() } handleTabButtonClick (e, index) { this.setState({ snippetIndex: index }) } handleTabDragStart (e, index) { e.dataTransfer.setData('text/plain', index) } handleTabDrop (e, index) { const oldIndex = parseInt(e.dataTransfer.getData('text')) const snippets = this.state.note.snippets.slice() const draggedSnippet = snippets[oldIndex] snippets[oldIndex] = snippets[index] snippets[index] = draggedSnippet const snippetIndex = index const note = Object.assign({}, this.state.note, {snippets}) this.setState({ note, snippetIndex }, () => { this.save() this.refs['code-' + index].reload() this.refs['code-' + oldIndex].reload() }) } handleTabDeleteButtonClick (e, index) { if (this.state.note.snippets.length > 1) { if (this.state.note.snippets[index].content.trim().length > 0) { const dialogIndex = dialog.showMessageBox(remote.getCurrentWindow(), { type: 'warning', message: 'Delete a snippet', detail: 'This work cannot be undone.', buttons: ['Confirm', 'Cancel'] }) if (dialogIndex === 0) { this.deleteSnippetByIndex(index) } } else { this.deleteSnippetByIndex(index) } } } deleteSnippetByIndex (index) { const snippets = this.state.note.snippets.slice() snippets.splice(index, 1) const note = Object.assign({}, this.state.note, {snippets}) const snippetIndex = this.state.snippetIndex >= snippets.length ? snippets.length - 1 : this.state.snippetIndex this.setState({ note, snippetIndex }, () => { this.save() this.refs['code-' + this.state.snippetIndex].reload() }) } renameSnippetByIndex (index, name) { const snippets = this.state.note.snippets.slice() snippets[index].name = name const syntax = CodeMirror.findModeByFileName(name.trim()) const mode = syntax != null ? syntax.name : null if (mode != null) { snippets[index].mode = mode AwsMobileAnalyticsConfig.recordDynamicCustomEvent('SNIPPET_LANG', { name: mode }) } this.setState({note: Object.assign(this.state.note, {snippets: snippets})}) this.setState({ note: this.state.note }, () => { this.save() }) } handleModeOptionClick (index, name) { return (e) => { const snippets = this.state.note.snippets.slice() snippets[index].mode = name this.setState({note: Object.assign(this.state.note, {snippets: snippets})}) this.setState({ note: this.state.note }, () => { this.save() }) AwsMobileAnalyticsConfig.recordDynamicCustomEvent('SELECT_LANG', { name }) } } handleCodeChange (index) { return (e) => { const snippets = this.state.note.snippets.slice() snippets[index].content = this.refs['code-' + index].value this.setState({note: Object.assign(this.state.note, {snippets: snippets})}) this.setState({ note: this.state.note }, () => { this.save() }) } } handleKeyDown (e) { switch (e.keyCode) { case 9: if (e.ctrlKey && !e.shiftKey) { e.preventDefault() this.jumpNextTab() } else if (e.ctrlKey && e.shiftKey) { e.preventDefault() this.jumpPrevTab() } else if (!e.ctrlKey && !e.shiftKey && e.target === this.refs.description) { e.preventDefault() this.focusEditor() } break case 76: { const isSuper = global.process.platform === 'darwin' ? e.metaKey : e.ctrlKey if (isSuper) { e.preventDefault() this.focus() } } break case 84: { const isSuper = global.process.platform === 'darwin' ? e.metaKey : e.ctrlKey if (isSuper) { e.preventDefault() this.addSnippet() } } break } } handleModeButtonClick (e, index) { const menu = new Menu() CodeMirror.modeInfo.sort(function (a, b) { return a.name.localeCompare(b.name) }).forEach((mode) => { menu.append(new MenuItem({ label: mode.name, click: (e) => this.handleModeOptionClick(index, mode.name)(e) })) }) menu.popup(remote.getCurrentWindow()) } handleIndentTypeButtonClick (e) { context.popup([ { label: 'tab', click: (e) => this.handleIndentTypeItemClick(e, 'tab') }, { label: 'space', click: (e) => this.handleIndentTypeItemClick(e, 'space') } ]) } handleIndentSizeButtonClick (e) { context.popup([ { label: '2', click: (e) => this.handleIndentSizeItemClick(e, 2) }, { label: '4', click: (e) => this.handleIndentSizeItemClick(e, 4) }, { label: '8', click: (e) => this.handleIndentSizeItemClick(e, 8) } ]) } handleIndentSizeItemClick (e, indentSize) { const { config, dispatch } = this.props const editor = Object.assign({}, config.editor, { indentSize }) ConfigManager.set({ editor }) dispatch({ type: 'SET_CONFIG', config: { editor } }) } handleIndentTypeItemClick (e, indentType) { const { config, dispatch } = this.props const editor = Object.assign({}, config.editor, { indentType }) ConfigManager.set({ editor }) dispatch({ type: 'SET_CONFIG', config: { editor } }) } focus () { this.refs.description.focus() } moveTabBarBy (x) { this.allTabs.addEventListener('transitionend', () => { this.setState({ enableRightArrow: this.allTabs.offsetLeft !== this.visibleTabs.offsetWidth - this.allTabs.offsetWidth, enableLeftArrow: this.allTabs.offsetLeft !== 0 }) }, {once: true}) this.allTabs.style.left = `${x}px` } addSnippet () { const { note } = this.state note.snippets = note.snippets.concat([{ name: '', mode: 'Plain Text', content: '' }]) const snippetIndex = note.snippets.length - 1 this.setState({ note, snippetIndex }, () => { const newLeft = this.visibleTabs.offsetWidth - this.allTabs.offsetWidth this.moveTabBarBy(newLeft) this.refs['tab-' + snippetIndex].startRenaming() }) } jumpNextTab () { this.setState({ snippetIndex: (this.state.snippetIndex + 1) % this.state.note.snippets.length }, () => { this.focusEditor() }) } jumpPrevTab () { this.setState({ snippetIndex: (this.state.snippetIndex - 1 + this.state.note.snippets.length) % this.state.note.snippets.length }, () => { this.focusEditor() }) } focusEditor () { console.log('code-' + this.state.snippetIndex) this.refs['code-' + this.state.snippetIndex].focus() } handleInfoButtonClick (e) { const infoPanel = document.querySelector('.infoPanel') if (infoPanel.style) infoPanel.style.display = infoPanel.style.display === 'none' ? 'inline' : 'none' } showWarning () { dialog.showMessageBox(remote.getCurrentWindow(), { type: 'warning', message: 'Sorry!', detail: 'md/text import is available only a markdown note.', buttons: ['OK'] }) } render () { const { data, config, location } = this.props const { note } = this.state const storageKey = note.storage const folderKey = note.folder 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 const tabList = note.snippets.map((snippet, index) => { const isActive = this.state.snippetIndex === index return this.handleTabButtonClick(e, index)} onDelete={(e) => this.handleTabDeleteButtonClick(e, index)} onRename={(name) => this.renameSnippetByIndex(index, name)} isDeletable={note.snippets.length > 1} onDragStart={(e) => this.handleTabDragStart(e, index)} onDrop={(e) => this.handleTabDrop(e, index)} /> }) const viewList = note.snippets.map((snippet, index) => { const isActive = this.state.snippetIndex === index let syntax = CodeMirror.findModeByName(pass(snippet.mode)) if (syntax == null) syntax = CodeMirror.findModeByName('Plain Text') return
{snippet.mode === 'Markdown' || snippet.mode === 'GitHub Flavored Markdown' ? this.handleCodeChange(index)(e)} ref={'code-' + index} ignorePreviewPointerEvents={this.props.ignorePreviewPointerEvents} storageKey={storageKey} /> : this.handleCodeChange(index)(e)} ref={'code-' + index} /> }
}) const options = [] data.storageMap.forEach((storage, index) => { storage.folders.forEach((folder) => { options.push({ storage: storage, folder: folder }) }) }) const currentOption = options.filter((option) => option.storage.key === storageKey && option.folder.key === folderKey)[0] const trashTopBar =
this.handleUndoButtonClick(e)} />
this.handleTrashButtonClick(e)} /> this.handleInfoButtonClick(e)} />
const detailTopBar =
this.handleFolderChange(e)} />
this.handleChange(e)} />
this.handleStarButtonClick(e)} isActive={note.isStarred} /> this.handleTrashButtonClick(e)} /> this.handleInfoButtonClick(e)} />
return (
this.handleKeyDown(e)} > {location.pathname === '/trashed' ? trashTopBar : detailTopBar}