Mailspring/app/internal_packages/composer-templates/lib/preferences-templates.jsx
Ben Gotow 1a3cca8d0a
Totally overhauled composer based on Slate (#524)
* Remove the composer contenteditable, replace with basic <textarea>

* Beginning broader cleanup of draft session

* DraftJS composer with color, style support

* Serialization/unserialization of basic styles, toolbar working

* WIP

* Switch to draft-js-plugins approach, need to revisit HTML

* Move HTML conversion functionality into plugins

* Add spellcheck context menu to editor

* Initial work on quoted text

* Further work on quoted text

* BLOCK approach

* Entity approach - better, does not bump out to top level

* Hiding and showing quoted text via CSS

* Get rid of ability to inject another subject line component

* Clean up specs, DraftFactory to ES6

* Remove old initial focus hack

* Fix focusing, initial text selection

* Remove participant “collapsing” support, it can be confusing

* Correctly terminate links on carriage returns

* Initial signature support, allow removal of uneditable blocks

* Sync body string with body editorstate

* Simplify draft editor session, finish signatures

* Templates

* Minor fixes

* Simplify link/open tracking, ensure it works

* Reorg composer, rework template editor

* Omg the slowness is all the stupid emoji button

* Polish and small fixes

* Performance improvements, new templates UI

* Don’t assume nodes are elements

* Fix for sending drafts twice due to back-to-back saves

* Fix order of operations on app quit to save drafts reliably

* Improve DraftJS-Convert whitespace handling

* Use contentID throughout attachment lifecycle

* Try to fix images

* Switch to Slate instead of DraftJS… much better

* Fix newline handling

* Bug fixes

* Cleanup

* Finish templates plugin

* Clean up text editing / support for Gmail email styles

* Support for color + size on the same node, clean trailing whitespace

* Restore emoji typeahead / emoji picker

* Fix scrolling in template editor

* Fix specs

* Fix newlines

* Re-implement spellcheck to be faster

* Make spellcheck decorator changes invisible to the undo/redo stack

* Remove comment

* Polish themplates panel

* Fix #521
2018-01-11 15:55:56 -08:00

169 lines
4.7 KiB
JavaScript

import fs from 'fs';
import { Flexbox, EditableList, ComposerEditor, ComposerSupport } from 'mailspring-component-kit';
import { React, ReactDOM } from 'mailspring-exports';
import { shell } from 'electron';
import TemplateStore from './template-store';
import TemplateActions from './template-actions';
const { Conversion: { convertFromHTML, convertToHTML } } = ComposerSupport;
class TemplateEditor extends React.Component {
constructor(props) {
super(props);
if (this.props.template) {
const inHTML = fs.readFileSync(props.template.path).toString();
this.state = {
editorState: convertFromHTML(inHTML),
readOnly: false,
};
} else {
this.state = {
editorState: convertFromHTML(''),
readOnly: true,
};
}
}
_onSave = () => {
if (!this.state.readOnly) {
const outHTML = convertToHTML(this.state.editorState);
fs.writeFileSync(this.props.template.path, outHTML);
}
};
_onFocusEditor = e => {
if (e.target === ReactDOM.findDOMNode(this._composer)) {
this._composer.focusEndAbsolute();
}
};
render() {
const { onEditTitle, template } = this.props;
const { readOnly, editorState } = this.state;
return (
<div className={`template-wrap ${readOnly && 'empty'}`}>
<div className="section">
<input
type="text"
id="title"
placeholder="Name"
style={{ maxWidth: 400 }}
defaultValue={template ? template.name : ''}
onBlur={e => onEditTitle(e.target.value)}
/>
</div>
<div className="section editor" onClick={this._onFocusEditor}>
<ComposerEditor
ref={c => (this._composer = c)}
readOnly={readOnly}
value={editorState}
propsForPlugins={{ inTemplateEditor: true }}
onChange={change => this.setState({ editorState: change.value })}
onBlur={this._onSave}
/>
</div>
<div className="section note">
Changes are saved automatically. View the{' '}
<a href="https://foundry376.zendesk.com/hc/en-us/articles/115001875231-Using-quick-reply-templates">
Templates Guide
</a>{' '}
for tips and tricks.
</div>
</div>
);
}
}
export default class PreferencesTemplates extends React.Component {
static displayName = 'PreferencesTemplates';
constructor() {
super();
this.state = this._getStateFromStores();
}
componentDidMount() {
this.unsubscribers = [
TemplateStore.listen(() => {
this.setState(this._getStateFromStores());
}),
];
}
componentWillUnmount() {
this.unsubscribers.forEach(unsubscribe => unsubscribe());
}
_getStateFromStores() {
let lastSelName = null;
let lastSelIndex = null;
if (this.state) {
lastSelName = this.state.selected && this.state.selected.name;
lastSelIndex = this.state.templates.findIndex(t => t.name === lastSelName);
}
const templates = TemplateStore.items();
const selected = templates.find(t => t.name === lastSelName) || templates[lastSelIndex] || null;
return {
templates,
selected,
};
}
_onAdd = () => {
TemplateActions.createTemplate({ name: 'Untitled', contents: 'Insert content here!' });
};
_onDelete = () => {
TemplateActions.deleteTemplate(this.state.selected.name);
};
_onEditTitle = newName => {
TemplateActions.renameTemplate(this.state.selected.name, newName);
};
_onSelect = item => {
this.setState({ selected: item });
};
render() {
const { selected } = this.state;
return (
<div className="preferences-templates-container">
<section>
<Flexbox>
<div>
<EditableList
showEditIcon
className="template-list"
items={this.state.templates}
itemContent={template => template.name}
onCreateItem={this._onAdd}
onDeleteItem={this._onDelete}
onItemEdited={this._onEditTitle}
onSelectItem={this._onSelect}
selected={this.state.selected}
/>
<a
style={{ marginTop: 10, display: 'block' }}
onClick={() => shell.showItemInFolder(TemplateStore.directory())}
>
Show Templates Folder...
</a>
</div>
<TemplateEditor
onEditTitle={this._onEditTitle}
key={selected ? selected.name : 'empty'}
template={selected}
/>
</Flexbox>
</section>
</div>
);
}
}