mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-02-22 23:23:54 +08:00
Summary: This diff replaces "finalizeSessionBeforeSending" with a plugin hook that is bidirectional and allows us to put the draft in the "ready to send" state every time we save it, and restore it to the "ready to edit" state every time a draft session is created to edit it. This diff also significantly restructures the draft tasks: 1. SyncbackDraftUploadsTask: - ensures that `uploads` are converted to `files` and that any existing files on the draft are part of the correct account. 1. SyncbackDraftTask: - saves the draft, nothing else. 3. SendDraftTask - sends the draft, nothing else. - deletes the entire uploads directory for the draft Test Plan: WIP Reviewers: juan, evan Reviewed By: evan Differential Revision: https://phab.nylas.com/D2753
42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
import {ComposerExtension, QuotedHTMLTransformer} from 'nylas-exports';
|
|
import {PLUGIN_ID, PLUGIN_URL} from './open-tracking-constants';
|
|
|
|
|
|
class DraftBody {
|
|
constructor(draft) {this._body = draft.body}
|
|
get unquoted() {return QuotedHTMLTransformer.removeQuotedHTML(this._body);}
|
|
set unquoted(text) {this._body = QuotedHTMLTransformer.appendQuotedHTML(text, this._body);}
|
|
get body() {return this._body}
|
|
}
|
|
|
|
export default class OpenTrackingComposerExtension extends ComposerExtension {
|
|
|
|
static applyTransformsToDraft({draft}) {
|
|
// grab message metadata, if any
|
|
const nextDraft = draft.clone();
|
|
const metadata = draft.metadataForPluginId(PLUGIN_ID);
|
|
if (!metadata) {
|
|
return nextDraft;
|
|
}
|
|
|
|
if (!metadata.uid) {
|
|
NylasEnv.reportError(new Error("Open tracking composer extension could not find 'uid' in metadata!"));
|
|
return nextDraft;
|
|
}
|
|
|
|
// insert a tracking pixel <img> into the message
|
|
const serverUrl = `${PLUGIN_URL}/open/${draft.accountId}/${metadata.uid}`;
|
|
const img = `<img class="n1-open" width="0" height="0" style="border:0; width:0; height:0;" src="${serverUrl}">`;
|
|
const draftBody = new DraftBody(draft);
|
|
|
|
draftBody.unquoted = `${draftBody.unquoted}${img}`;
|
|
nextDraft.body = draftBody.body;
|
|
return nextDraft;
|
|
}
|
|
|
|
static unapplyTransformsToDraft({draft}) {
|
|
const nextDraft = draft.clone();
|
|
nextDraft.body = draft.body.replace(/<img class="n1-open"[^>]*>/g, '');
|
|
return nextDraft;
|
|
}
|
|
}
|