2016-04-05 06:05:48 +08:00
|
|
|
import React from 'react'
|
|
|
|
import {PLUGIN_ID} from '../scheduler-constants'
|
|
|
|
import ProposedTimeList from './proposed-time-list'
|
2016-04-10 09:19:01 +08:00
|
|
|
import {Event, Actions, RegExpUtils, ComposerExtension} from 'nylas-exports'
|
2016-04-05 06:05:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts the set of Proposed Times into the body of the HTML email.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export default class SchedulerComposerExtension extends ComposerExtension {
|
|
|
|
|
|
|
|
static listRegex() {
|
|
|
|
return new RegExp(/<proposed-time-list>.*<\/proposed-time-list>/)
|
|
|
|
}
|
|
|
|
|
|
|
|
static _findInsertionPoint(body) {
|
|
|
|
const checks = [
|
|
|
|
/<!-- <signature> -->/,
|
|
|
|
RegExpUtils.signatureRegex(),
|
|
|
|
RegExpUtils.n1QuoteStartRegex(),
|
|
|
|
]
|
|
|
|
|
|
|
|
let insertionPoint = -1
|
|
|
|
for (const check of checks) {
|
|
|
|
insertionPoint = body.search(check);
|
|
|
|
if (insertionPoint >= 0) { break; }
|
|
|
|
}
|
|
|
|
if (insertionPoint === -1) { insertionPoint = body.length }
|
|
|
|
return insertionPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
static _insertInBody(body, markup) {
|
|
|
|
// Remove any existing signature in the body
|
|
|
|
const re = SchedulerComposerExtension.listRegex()
|
|
|
|
const cleanBody = body.replace(re, "");
|
|
|
|
|
|
|
|
const insertionPoint = SchedulerComposerExtension._findInsertionPoint(cleanBody)
|
|
|
|
|
|
|
|
const contentBefore = cleanBody.slice(0, insertionPoint);
|
|
|
|
const contentAfter = cleanBody.slice(insertionPoint);
|
|
|
|
const wrapS = "<proposed-time-list>"
|
|
|
|
const wrapE = "</proposed-time-list>"
|
|
|
|
|
|
|
|
return contentBefore + wrapS + markup + wrapE + contentAfter
|
|
|
|
}
|
|
|
|
|
2016-04-10 09:19:01 +08:00
|
|
|
static _prepareEvent(inEvent, draft, metadata) {
|
2016-04-05 06:05:48 +08:00
|
|
|
const event = inEvent
|
|
|
|
if (!event.title || event.title.length === 0) {
|
|
|
|
event.title = draft.subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.participants = draft.participants().map((contact) => {
|
|
|
|
return {
|
|
|
|
name: contact.name,
|
|
|
|
email: contact.email,
|
|
|
|
status: "noreply",
|
|
|
|
}
|
|
|
|
})
|
2016-04-10 09:19:01 +08:00
|
|
|
|
|
|
|
if (metadata.proposals) {
|
|
|
|
event.end = null
|
|
|
|
event.start = null
|
|
|
|
}
|
2016-04-05 06:05:48 +08:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2016-04-10 09:19:01 +08:00
|
|
|
// We must set the `preparedEvent` to be exactly what could be posted to
|
|
|
|
// the /events endpoint of the API.
|
|
|
|
static _cleanEventJSON(rawJSON) {
|
|
|
|
const json = rawJSON;
|
|
|
|
delete json.client_id;
|
|
|
|
delete json.id;
|
|
|
|
json.when = {
|
2016-04-10 23:44:24 +08:00
|
|
|
start_time: json._start,
|
|
|
|
end_time: json._end,
|
2016-04-10 09:19:01 +08:00
|
|
|
}
|
|
|
|
delete json._start
|
|
|
|
delete json._end
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2016-04-05 06:05:48 +08:00
|
|
|
static _insertProposalsIntoBody(draft, metadata) {
|
|
|
|
const nextDraft = draft;
|
|
|
|
if (metadata && metadata.proposals) {
|
|
|
|
const el = React.createElement(ProposedTimeList,
|
|
|
|
{
|
|
|
|
draft: nextDraft,
|
2016-04-10 09:19:01 +08:00
|
|
|
event: metadata.pendingEvent,
|
2016-04-05 06:05:48 +08:00
|
|
|
inEmail: true,
|
|
|
|
proposals: metadata.proposals,
|
|
|
|
});
|
|
|
|
const markup = React.renderToStaticMarkup(el);
|
|
|
|
const nextBody = SchedulerComposerExtension._insertInBody(nextDraft.body, markup)
|
|
|
|
nextDraft.body = nextBody;
|
|
|
|
}
|
|
|
|
return nextDraft
|
|
|
|
}
|
|
|
|
|
|
|
|
static applyTransformsToDraft({draft}) {
|
|
|
|
const self = SchedulerComposerExtension
|
|
|
|
let nextDraft = draft.clone();
|
|
|
|
const metadata = draft.metadataForPluginId(PLUGIN_ID)
|
2016-04-10 09:19:01 +08:00
|
|
|
if (metadata && metadata.pendingEvent) {
|
|
|
|
nextDraft = self._insertProposalsIntoBody(nextDraft, metadata);
|
|
|
|
const nextEvent = new Event().fromJSON(metadata.pendingEvent);
|
|
|
|
const nextEventPrepared = self._prepareEvent(nextEvent, draft, metadata);
|
|
|
|
metadata.pendingEvent = self._cleanEventJSON(nextEventPrepared.toJSON());
|
2016-04-05 06:05:48 +08:00
|
|
|
Actions.setMetadata(nextDraft, PLUGIN_ID, metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextDraft;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unapplyTransformsToDraft({draft}) {
|
|
|
|
const nextDraft = draft.clone();
|
|
|
|
const re = SchedulerComposerExtension.listRegex()
|
|
|
|
const body = nextDraft.body.replace(re, "");
|
|
|
|
nextDraft.body = body;
|
|
|
|
return nextDraft
|
|
|
|
}
|
|
|
|
}
|