Mailspring/docs/ComposerExtensions.md
Ben Gotow 18d294c42f feat(transforms): Replace regexp body transforms with DOM approach
Summary:
We originally didn't do this because creating a DOM tree was loading images.
Using range.createContextualFragment seems to do it without the tree ever
being attached.

Accompanying changes to src/pro are here:
https://phab.nylas.com/D3300
https://github.com/nylas/edgehill/compare/bengotow/draft-dom-transformations?expand=1

Also rename applyTransformsToDraft => applyTransformsForSending. Needed
a new name because the function signature has changed. AFAIK there are no
open source plugins using the old functions.

Test Plan: All specs updated

Reviewers: evan, juan

Reviewed By: evan, juan

Differential Revision: https://phab.nylas.com/D3299
2016-09-23 16:36:08 -07:00

43 lines
1.6 KiB
Markdown

---
Title: Extending the Composer
Section: Guides
Order: 6
---
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.
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.
To create your own composer extensions, subclass {ComposerExtension} and override the methods your extension needs.
In the sample packages repository, [templates]() is an example of a package which uses a ComposerExtension to enhance the composer experience.
### 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
{ComposerExtension} = require 'nylas-exports'
class ProductsExtension extends ComposerExtension
@warningsForSending: ({draft}) ->
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 []
@applyTransformsForSending: ({draftBodyRootNode, draft}) ->
if @warningsForSending({draft})
el = document.createElement('p');
el.innerText = "This email contains competitor's product names or trademarks used in context."
draftBodyRootNode.appendChild(el)
```