import React from 'react'; import _ from 'underscore'; import { Flexbox, RetinaImg, EditableList, Contenteditable, MultiselectDropdown, } from 'nylas-component-kit'; import {AccountStore, SignatureStore, Actions} from 'nylas-exports'; export default class PreferencesSignatures extends React.Component { static displayName = 'PreferencesSignatures'; constructor() { super() this.state = this._getStateFromStores() } componentDidMount() { this.unsubscribers = [ SignatureStore.listen(this._onChange), ] } componentWillUnmount() { this.unsubscribers.forEach(unsubscribe => unsubscribe()); } _onChange = () => { this.setState(this._getStateFromStores()) } _getStateFromStores() { const signatures = SignatureStore.getSignatures() const accountsAndAliases = AccountStore.aliases() const selected = SignatureStore.selectedSignature() const defaults = SignatureStore.getDefaults() return { signatures: signatures, selectedSignature: selected, defaults: defaults, accountsAndAliases: accountsAndAliases, editAsHTML: this.state ? this.state.editAsHTML : false, } } _onCreateButtonClick = () => { this._onAddSignature() } _onAddSignature = () => { Actions.addSignature() } _onDeleteSignature = (signature) => { Actions.removeSignature(signature) } _onEditSignature = (edit) => { let editedSig; if (typeof edit === "object") { editedSig = { title: this.state.selectedSignature.title, body: edit.target.value, } } else { editedSig = { title: edit, body: this.state.selectedSignature.body, } } Actions.updateSignature(editedSig, this.state.selectedSignature.id) } _onSelectSignature = (sig) => { Actions.selectSignature(sig.id) } _onToggleAccount = (account) => { Actions.toggleAccount(account.email) } _onToggleEditAsHTML = () => { const toggled = !this.state.editAsHTML this.setState({editAsHTML: toggled}) } _renderListItemContent = (sig) => { return sig.title } _renderSignatureToolbar() { return (
Default for: {this._renderAccountPicker()}
) } _selectItemKey = (accountOrAlias) => { return accountOrAlias.clientId } _isChecked = (accountOrAlias) => { if (this.state.defaults[accountOrAlias.email] === this.state.selectedSignature.id) return true return false } _numSelected() { const sel = _.filter(this.state.accountsAndAliases, (accountOrAlias) => { return this._isChecked(accountOrAlias) }) const numSelected = sel.length return numSelected.toString() + (numSelected === 1 ? " Account" : " Accounts") } _getEmail = (accountOrAlias) => { return accountOrAlias.email } _renderAccountPicker() { const buttonText = this._numSelected() return ( ) } _renderEditableSignature() { const selectedBody = this.state.selectedSignature ? this.state.selectedSignature.body : "" return ( ) } _renderHTMLSignature() { return (