import React from 'react' import CSSModules from 'browser/lib/CSSModules' import styles from './SnippetTab.styl' import fs from 'fs' import SnippetEditor from './SnippetEditor' import i18n from 'browser/lib/i18n' import dataApi from 'browser/main/lib/dataApi' import consts from 'browser/lib/consts' const { remote } = require('electron') const { Menu, MenuItem } = remote class SnippetTab extends React.Component { constructor (props) { super(props) this.state = { snippets: [], currentSnippet: null } } componentDidMount () { this.snippets = JSON.parse(fs.readFileSync(consts.SNIPPET_FILE, 'utf8')) this.setState({snippets: this.snippets}) } handleSnippetClick (snippet) { const currentSnippet = Object.assign({}, snippet) currentSnippet.prefix = currentSnippet.prefix.join(', ') this.setState({currentSnippet}) } handleSnippetContextMenu (snippet) { const menu = new Menu() menu.append(new MenuItem({ label: 'Delete', click: () => { this.deleteSnippet(snippet.id) } })) menu.popup() } deleteSnippet (id) { dataApi.deleteSnippet(this.snippets, id).then((snippets) => { this.snippets = snippets this.setState(this.snippets) }).catch(err => { throw err }) } createSnippet () { dataApi.createSnippet(this.snippets).then((snippets) => { this.snippets = snippets this.setState(this.snippets) // scroll to end of list when added new snippet const snippetList = document.getElementById('snippets') snippetList.scrollTop = snippetList.scrollHeight }).catch(err => { throw err }) } renderSnippetList () { const { snippets } = this.state return (