Mailspring/internal_packages/message-templates/lib/template-store.coffee
Ben Gotow a26b8d4bc4 fix(*): Resolve a variety of small and simple bugs
Summary:
Fix T1822 - saving templates not working, not showing template

Fix T1800 - give composers a minimum size

Fix the bottom bar of the composer so the gray bar goes all the way across in popout mode.

Fix T1825 - switch to a more attractive "June 4, 2015 at 3:10 PM" styling for expanded dates

Remove, rather than hide, react components for text fields in composer. Fixes T1147

Fix specs

Switch to 999+ instead of infinity. Fixes T1768

Fix broken TemplateStore specs

Test Plan: Run tests

Reviewers: evan

Reviewed By: evan

Maniphest Tasks: T1147, T1768, T1822, T1800, T1825

Differential Revision: https://phab.nylas.com/D1601
2015-06-05 11:02:44 -07:00

104 lines
3.2 KiB
CoffeeScript

Reflux = require 'reflux'
_ = require 'underscore'
{DatabaseStore, DraftStore, Actions, Message} = require 'nylas-exports'
shell = require 'shell'
path = require 'path'
fs = require 'fs-plus'
TemplateStore = Reflux.createStore
init: ->
@_setStoreDefaults()
@_registerListeners()
@_templatesDir = path.join(atom.getConfigDirPath(), 'templates')
# I know this is a bit of pain but don't do anything that
# could possibly slow down app launch
fs.exists @_templatesDir, (exists) =>
if exists
@_populate()
fs.watch @_templatesDir, => @_populate()
else
fs.mkdir @_templatesDir, =>
fs.watch @_templatesDir, => @_populate()
########### PUBLIC #####################################################
items: ->
@_items
templatesDirectory: ->
@_templatesDir
########### PRIVATE ####################################################
_setStoreDefaults: ->
@_items = []
_registerListeners: ->
@listenTo Actions.insertTemplateId, @_onInsertTemplateId
@listenTo Actions.createTemplate, @_onCreateTemplate
@listenTo Actions.showTemplates, @_onShowTemplates
_populate: ->
fs.readdir @_templatesDir, (err, filenames) =>
@_items = []
for filename in filenames
continue if filename[0] is '.'
displayname = path.basename(filename, path.extname(filename))
@_items.push
id: filename,
name: displayname,
path: path.join(@_templatesDir, filename)
@trigger(@)
_onCreateTemplate: ({draftLocalId, name, contents} = {}) ->
if draftLocalId
DraftStore.sessionForLocalId(draftLocalId).then (session) =>
draft = session.draft()
name ?= draft.subject
contents ?= draft.body
if not name or name.length is 0
return @_displayError("Give your draft a subject to name your template.")
if not contents or contents.length is 0
return @_displayError("To create a template you need to fill the body of the current draft.")
@_writeTemplate(name, contents)
else
if not name or name.length is 0
return @_displayError("You must provide a name for your template.")
if not contents or contents.length is 0
return @_displayError("You must provide contents for your template.")
@_writeTemplate(name, contents)
_onShowTemplates: ->
shell.showItemInFolder(@_items[0]?.path || @_templatesDir)
_displayError: (message) ->
dialog = require('remote').require('dialog')
dialog.showErrorBox('Template Creation Error', message)
_writeTemplate: (name, contents) ->
filename = "#{name}.html"
templatePath = path.join(@_templatesDir, filename)
fs.writeFile templatePath, contents, (err) =>
@_displayError(err) if err
shell.showItemInFolder(templatePath)
@_items.push
id: filename,
name: name,
path: templatePath
@trigger(@)
_onInsertTemplateId: ({templateId, draftLocalId} = {}) ->
template = _.find @_items, (item) -> item.id is templateId
return unless template
fs.readFile template.path, (err, data) ->
body = data.toString()
DraftStore.sessionForLocalId(draftLocalId).then (session) ->
session.changes.add(body: body)
module.exports = TemplateStore