2015-09-09 08:10:29 +08:00
---
Title: Extending the Composer
Section: Guides
Order: 6
---
2015-09-30 00:44:30 +08:00
The composer lies at the heart of N1, and many improvements to the mail experience require deep integration with the composer. To enable these sort of plugins, the {DraftStore} exposes an extension API.
2015-09-09 08:10:29 +08:00
This API allows your package to:
- Display warning messages before a draft is sent. (ie: "Are you sure you want to send this without attaching a file?")
- Intercept keyboard and mouse events to the composer's text editor.
- Transform the draft and make additional changes before it is sent.
2015-11-28 03:49:24 +08:00
To create your own composer extensions, subclass {ComposerExtension} and override the methods your extension needs.
2015-09-09 08:10:29 +08:00
2015-11-28 03:49:24 +08:00
In the sample packages repository, [templates]() is an example of a package which uses a ComposerExtension to enhance the composer experience.
2015-09-09 08:10:29 +08:00
### Example
This extension displays a warning before sending a draft that contains the names of competitors' products. If the user proceeds to send the draft containing the words, it appends a disclaimer.
```coffee
2015-11-28 03:49:24 +08:00
{ComposerExtension} = require 'nylas-exports'
2015-09-09 08:10:29 +08:00
2015-11-28 03:49:24 +08:00
class ProductsExtension extends ComposerExtension
2015-09-09 08:10:29 +08:00
2015-12-31 00:36:47 +08:00
@warningsForSending: ({draft}) ->
2015-09-09 08:10:29 +08:00
words = ['acme', 'anvil', 'tunnel', 'rocket', 'dynamite']
body = draft.body.toLowercase()
for word in words
if body.indexOf(word) > 0
return ["with the word '#{word}'?"]
return []
2015-12-31 00:36:47 +08:00
@finalizeSessionBeforeSending: ({session}) ->
2015-09-09 08:10:29 +08:00
draft = session.draft()
2015-12-31 00:36:47 +08:00
if @warningsForSending ({draft})
2015-09-09 08:10:29 +08:00
bodyWithWarning = draft.body += "< br > This email \
contains competitor's product names \
or trademarks used in context."
2015-12-24 09:25:30 +08:00
return session.changes.add(body: bodyWithWarning)
else return Promise.resolve()
2015-09-09 08:10:29 +08:00
```