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 } this.changeDelay = null } componentDidMount () { this.reloadSnippetList() } handleSnippetClick (snippet) { if (this.state.currentSnippet === null || this.state.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}) }) } } 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) this.reloadSnippetList() }, 500) } handleSnippetContextMenu (snippet) { const menu = new Menu() menu.append(new MenuItem({ label: i18n.__('Delete snippet'), click: () => { this.deleteSnippet(snippet) } })) menu.popup() } reloadSnippetList () { dataApi.fetchSnippet().then(snippets => this.setState({snippets})) } deleteSnippet (snippet) { dataApi.deleteSnippet(snippet).then(() => { this.reloadSnippetList() }).catch(err => { throw err }) } createSnippet () { dataApi.createSnippet().then(() => { this.reloadSnippetList() // 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 (