Skip to content

Commit fc45277

Browse files
authored
Merge pull request #1 from clear-code/add-support-for-line-break-on-body-param
Add support for line break in the body parameter.
2 parents 3fedd2e + 25128dc commit fc45277

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

src/web/app.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ async function onTypicalReplyButtonClicked(event) {
6262
}
6363
window.onTypicalReplyButtonClicked = onTypicalReplyButtonClicked;
6464

65+
function plainTextToHtml(text) {
66+
if (!text) return "";
67+
68+
return text
69+
.replace(/&/g, "&")
70+
.replace(/</g, "&lt;")
71+
.replace(/>/g, "&gt;")
72+
.replace(/"/g, "&quot;")
73+
.replace(/'/g, "&#039;")
74+
.replace(/\n/g, "<br>");
75+
}
76+
6577
async function onNewMessageComposeCreated(event) {
6678
const conversationId = Office.context.mailbox.item.conversationId;
6779
const buttonConfigString = Office.context.roamingSettings.get("buttonconfig")?.trim() ?? "";
@@ -95,7 +107,15 @@ async function onNewMessageComposeCreated(event) {
95107
await OfficeDataAccessHelper.setBodyAsync("");
96108
}
97109
if (buttonConfig.body) {
98-
await OfficeDataAccessHelper.prependBodyAsync(`${buttonConfig.body} \n`);
110+
const coercionType = await OfficeDataAccessHelper.getBodyTypeAsync();
111+
let prependBody = `${buttonConfig.body} \n`;
112+
if (coercionType === Office.CoercionType.Html) {
113+
prependBody = plainTextToHtml(prependBody);
114+
}
115+
await OfficeDataAccessHelper.prependBodyAsync(prependBody, coercionType);
116+
const body = await OfficeDataAccessHelper.getBodyAsync();
117+
// prependBodyAsync sometimes does not update the body immediately, so we set body again to make sure the body is updated.
118+
await OfficeDataAccessHelper.setBodyAsync(body, coercionType);
99119
}
100120
event.completed();
101121
}

src/web/office-data-access-helper.mjs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,28 @@ export class OfficeDataAccessHelper {
297297
});
298298
}
299299

300-
static setBodyAsync(body) {
300+
static getBodyTypeAsync() {
301301
return new Promise((resolve, reject) => {
302302
try {
303-
Office.context.mailbox.item.body.setAsync(body, (asyncResult) => {
303+
Office.context.mailbox.item.body.getTypeAsync((asyncResult) => {
304+
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
305+
resolve(asyncResult.value);
306+
} else {
307+
console.log(`Error while getting body type: ${asyncResult.error.message}`);
308+
reject(false);
309+
}
310+
});
311+
} catch (error) {
312+
console.log(`Error while getting body type: ${error}`);
313+
reject(error);
314+
}
315+
});
316+
}
317+
318+
static setBodyAsync(body, coercionType = Office.CoercionType.Html) {
319+
return new Promise((resolve, reject) => {
320+
try {
321+
Office.context.mailbox.item.body.setAsync(body, { coercionType }, (asyncResult) => {
304322
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
305323
resolve(true);
306324
} else {
@@ -315,10 +333,10 @@ export class OfficeDataAccessHelper {
315333
});
316334
}
317335

318-
static prependBodyAsync(body) {
336+
static prependBodyAsync(body, coercionType = Office.CoercionType.Html) {
319337
return new Promise((resolve, reject) => {
320338
try {
321-
Office.context.mailbox.item.body.prependAsync(body, (asyncResult) => {
339+
Office.context.mailbox.item.body.prependAsync(body, { coercionType }, (asyncResult) => {
322340
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
323341
resolve(true);
324342
} else {

0 commit comments

Comments
 (0)