import React from 'react' import CSSModules from 'browser/lib/CSSModules' import styles from './SnippetTab.styl' import SnippetEditor from './SnippetEditor' import i18n from 'browser/lib/i18n' import dataApi from 'browser/main/lib/dataApi' import SnippetList from './SnippetList' import eventEmitter from 'browser/main/lib/eventEmitter' class SnippetTab extends React.Component { constructor (props) { super(props) this.state = { currentSnippet: null } this.changeDelay = null } handleSnippetNameOrPrefixChange () { clearTimeout(this.changeDelay) this.changeDelay = setTimeout(() => { // notify the snippet editor that the name or prefix of snippet has been changed this.snippetEditor.onSnippetNameOrPrefixChanged(this.state.currentSnippet) eventEmitter.emit('snippetList:reload') }, 500) } handleSnippetClick (snippet) { const { currentSnippet } = this.state if (currentSnippet === null || currentSnippet.id !== snippet.id) { dataApi.fetchSnippet(snippet.id).then(changedSnippet => { // notify the snippet editor to load the content of the new snippet this.snippetEditor.onSnippetChanged(changedSnippet) this.setState({currentSnippet: changedSnippet}) }) } } onSnippetNameOrPrefixChanged (e, type) { const newSnippet = Object.assign({}, this.state.currentSnippet) if (type === 'name') { newSnippet.name = e.target.value } else { newSnippet.prefix = e.target.value } this.setState({ currentSnippet: newSnippet }) this.handleSnippetNameOrPrefixChange() } handleDeleteSnippet (snippet) { // prevent old snippet still display when deleted if (snippet.id === this.state.currentSnippet.id) { this.setState({currentSnippet: null}) } } render () { const { config, storageKey } = this.props const { currentSnippet } = 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 return (