Skip to content

Commit 4acd462

Browse files
committed
Add implementation for loading config
1 parent 0f5aa57 commit 4acd462

6 files changed

Lines changed: 307 additions & 94 deletions

File tree

src/web/app.js

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,66 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
66
Copyright (c) 2026 ClearCode Inc.
77
*/
8-
//import { ConfigLoader } from "./config-loader.mjs";
8+
import { ConfigLoader } from "./config-loader.mjs";
9+
import { MailDataCreator } from "./mail-data-creator.mjs";
910

10-
let locale;
11-
let language;
11+
Office.onReady(() => {});
1212

13-
Office.onReady(() => {
14-
language = Office.context.displayLanguage;
15-
document.documentElement.setAttribute("lang", language);
16-
});
13+
// function createNewMail() {
14+
// try {
15+
// const currentItemId = Office.context.mailbox.item.itemId;
16+
// MailDataCreator.CreateReplyMailData();
17+
// Office.context.mailbox.displayNewMessageFormAsync({
18+
// toRecipients: Office.context.mailbox.item.to, // Copies the To line from current item
19+
// ccRecipients: ["sam@contoso.com"],
20+
// subject: "Outlook add-ins are cool!",
21+
// htmlBody: 'Hello <b>World</b>!<br/><img src="cid:image.png"></i>',
22+
// attachments: [
23+
// {
24+
// name: Office.context.mailbox.item.subject,
25+
// type: Office.MailboxEnums.AttachmentType.Item,
26+
// itemId: currentItemId,
27+
// },
28+
// ],
29+
// });
30+
// } catch (e) {
31+
// console.log("createNewMail Failed:", e);
32+
// }
33+
// }
1734

18-
function createNewMail() {
35+
async function onTypicalReplyButtonClicked(event) {
36+
const actionId = event.source.id;
37+
console.log(actionId);
38+
const config = await ConfigLoader.loadConfigForCurrentLanguage(Office.context.displayLanguage);
39+
const originalMailData = {
40+
toRecipients: Office.context.mailbox.item.to,
41+
ccRecipients: Office.context.mailbox.item.cc,
42+
bccRecipients: Office.context.mailbox.item.bcc,
43+
sender: Office.context.mailbox.item.sender,
44+
body: Office.context.mailbox.item.body,
45+
subject: Office.context.mailbox.item.subject,
46+
id: Office.context.mailbox.item.itemId,
47+
};
1948
try {
20-
const currentItemId = Office.context.mailbox.item.itemId;
21-
Office.context.mailbox.displayNewMessageFormAsync({
22-
toRecipients: Office.context.mailbox.item.to, // Copies the To line from current item
23-
ccRecipients: ["sam@contoso.com"],
24-
subject: "Outlook add-ins are cool!",
25-
htmlBody: 'Hello <b>World</b>!<br/><img src="cid:image.png"></i>',
26-
attachments: [
27-
{
28-
name: Office.context.mailbox.item.subject,
29-
type: Office.MailboxEnums.AttachmentType.Item,
30-
itemId: currentItemId,
31-
},
32-
],
33-
});
49+
const waitComplete = false;
50+
for(const buttonConfig of config.ButtonConfigList) {
51+
if (actionId !== buttonConfig.Id) {
52+
continue;
53+
}
54+
const replyMailData = MailDataCreator.CreateReplyMailData({ config: config.ButtonConfigList[0], originalMailData });
55+
Office.context.mailbox.displayNewMessageFormAsync(replyMailData);
56+
// displayNewMessageFormAsync will be canceled if event.completed() is called
57+
// before finishing displayNewMessageFormAsync. The event will be completed
58+
// automatically after displayNewMessageFormAsync is called.
59+
waitComplete = true;
60+
break;
61+
}
62+
if(!waitComplete) {
63+
event.completed();
64+
}
3465
} catch (e) {
3566
console.log("createNewMail Failed:", e);
3667
}
3768
}
38-
39-
async function onTypicalReplyButtonClicked() {
40-
createNewMail();
41-
}
4269
window.onTypicalReplyButtonClicked = onTypicalReplyButtonClicked;
4370
Office.actions.associate("onTypicalReplyButtonClicked", onTypicalReplyButtonClicked);

src/web/config-loader.mjs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
Copyright (c) 2025 ClearCode Inc.
77
*/
88

9-
import { Config } from "./config.mjs";
9+
import { TypicalReplyConfig } from "./config.mjs";
1010

1111
export class ConfigLoader {
1212
static async loadFile(url) {
@@ -29,8 +29,21 @@ export class ConfigLoader {
2929
const configJsonString = await this.loadFile("configs/TypicalReplyConfig.json");
3030
if (configJsonString) {
3131
const configObject = JSON.parse(configJsonString);
32-
return new Config(configObject);
32+
return new TypicalReplyConfig(configObject);
3333
}
34-
return new Config({});
34+
return new TypicalReplyConfig({});
35+
}
36+
37+
static async loadConfigForCurrentLanguage(culture) {
38+
const typicalReplyConfig = await ConfigLoader.loadFileConfig();
39+
let config = typicalReplyConfig?.ConfigList?.find((_) => (_.Culture ?? null) === culture);
40+
if (!config) {
41+
const lang = culture.split("-")[0];
42+
config = typicalReplyConfig?.ConfigList?.find((_) => (_.Culture ?? null) === lang);
43+
}
44+
if (!config) {
45+
config = typicalReplyConfig?.ConfigList?.[0];
46+
}
47+
return config;
3548
}
3649
}

src/web/config.mjs

Lines changed: 86 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,34 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
66
Copyright (c) 2025 ClearCode Inc.
77
*/
8-
export class Config {
9-
Culture;
10-
GroupLabel = "Typical Reply";
11-
TabMailInsertAfterMso = "GroupMailRespond";
12-
TabReadInsertAfterMso = "GroupRespond";
13-
ContextMenuInsertAfterMso = "Forward";
14-
ButtonConfigList;
158

16-
constructor({
17-
Culture,
18-
GroupLabel,
19-
TabMailInsertAfterMso,
20-
TabReadInsertAfterMso,
21-
ContextMenuInsertAfterMso,
22-
ButtonConfigList,
23-
}) {
24-
this.Culture = Culture;
25-
this.GroupLabel = GroupLabel;
26-
this.TabMailInsertAfterMso = TabMailInsertAfterMso;
27-
this.TabReadInsertAfterMso = TabReadInsertAfterMso;
28-
this.ContextMenuInsertAfterMso = ContextMenuInsertAfterMso;
29-
this.ButtonConfigList = ButtonConfigList;
30-
}
31-
}
9+
export class ButtonConfigEnums {
10+
static ForwardType = {
11+
Unknown: 0,
12+
Attachment: 1,
13+
Inline: 2,
14+
};
3215

33-
const ForwardType = {
34-
Unknown: 0,
35-
Attachment: 1,
36-
Inline: 2,
37-
};
16+
static RecipientsType = {
17+
Unknown: 0,
18+
Blank: 1,
19+
Sender: 2,
20+
All: 3,
21+
SpecifiedByUser: 4,
22+
};
3823

39-
const RecipientsType = {
40-
Unknown: 0,
41-
Blank: 1,
42-
Sender: 2,
43-
All: 3,
44-
SpecifiedByUser: 4,
45-
};
24+
static AllowedDomainsType = {
25+
Unknown: 0,
26+
All: 1,
27+
SpecifiedByUser: 2,
28+
};
4629

47-
const AllowedDomainsType = {
48-
Unknown: 0,
49-
All: 1,
50-
SpecifiedByUser: 2,
51-
};
52-
53-
const ButtonSize = {
54-
Unknown: 0,
55-
Normal: 1,
56-
Large: 2,
57-
};
30+
static ButtonSize = {
31+
Unknown: 0,
32+
Normal: 1,
33+
Large: 2,
34+
};
35+
}
5836

5937
export class ButtonConfig {
6038
Id;
@@ -70,7 +48,7 @@ export class ButtonConfig {
7048
AllowedDomainsType;
7149
ForwardType;
7250
Size;
73-
Image = "logo.png";
51+
Image;
7452

7553
constructor({
7654
Id,
@@ -81,41 +59,84 @@ export class ButtonConfig {
8159
Recipients,
8260
QuoteType,
8361
AllowedDomains,
84-
AllowedDomainsType,
8562
ForwardType,
8663
Size,
8764
Image,
8865
}) {
89-
this.Id = Id;
90-
this.Label = Label;
91-
this.SubjectPrefix = SubjectPrefix;
92-
this.Subject = Subject;
93-
this.Body = Body;
94-
this.Recipients = Recipients;
95-
this.QuoteType = QuoteType;
96-
this.AllowedDomains = AllowedDomains;
97-
this.ForwardType = ForwardType;
98-
this.Size = Size;
99-
this.Image = Image;
66+
this.Id = Id ?? "";
67+
this.Label = Label ?? "";
68+
this.SubjectPrefix = SubjectPrefix ?? "";
69+
this.Subject = Subject ?? "";
70+
this.Body = Body ?? "";
71+
this.Recipients = Recipients ?? [];
72+
this.QuoteType = QuoteType ?? false;
73+
this.AllowedDomains = AllowedDomains ?? [];
74+
this.ForwardType = ForwardType ?? ButtonConfigEnums.ForwardType.Unknown;
75+
this.Size = Size ?? ButtonConfigEnums.ButtonSize.Unknown;
76+
this.Image = Image ?? "logo.png";
10077

10178
if (!Recipients || Recipients.length == 0) {
102-
this.RecipientsType = RecipientsType.Blank;
79+
this.RecipientsType = ButtonConfigEnums.RecipientsType.Blank;
10380
} else {
104-
this.RecipientsType = RecipientsType.SpecifiedByUser;
105-
for (const key of Object.keys(RecipientsType)) {
81+
this.RecipientsType = ButtonConfigEnums.RecipientsType.SpecifiedByUser;
82+
for (const key of Object.keys(ButtonConfigEnums.RecipientsType)) {
10683
const lowerdKey = key.toLowerCase();
10784
const inputRecipientLower = Recipients[0].toLowerCase();
10885
if (lowerdKey === inputRecipientLower) {
109-
this.RecipientsType = RecipientsType[key];
86+
this.RecipientsType = ButtonConfigEnums.RecipientsType[key];
11087
break;
11188
}
11289
}
11390
}
11491

11592
if (!AllowedDomains || AllowedDomains.length == 0 || AllowedDomains[0] === "*") {
116-
this.AllowedDomainsType = AllowedDomainsType.Blank;
93+
this.AllowedDomainsType = ButtonConfigEnums.AllowedDomainsType.Blank;
11794
} else {
118-
this.AllowedDomainsType = AllowedDomainsType.SpecifiedByUser;
95+
this.AllowedDomainsType = ButtonConfigEnums.AllowedDomainsType.SpecifiedByUser;
96+
}
97+
}
98+
}
99+
100+
export class Config {
101+
Culture;
102+
GroupLabel;
103+
TabMailInsertAfterMso;
104+
TabReadInsertAfterMso;
105+
ContextMenuInsertAfterMso;
106+
ButtonConfigList;
107+
108+
constructor({
109+
Culture,
110+
GroupLabel,
111+
TabMailInsertAfterMso,
112+
TabReadInsertAfterMso,
113+
ContextMenuInsertAfterMso,
114+
ButtonConfigList,
115+
}) {
116+
this.Culture = Culture ?? "en-US";
117+
this.GroupLabel = GroupLabel ?? "Typical Reply";
118+
this.TabMailInsertAfterMso = TabMailInsertAfterMso ?? "GroupMailRespond";
119+
this.TabReadInsertAfterMso = TabReadInsertAfterMso ?? "GroupRespond";
120+
this.ContextMenuInsertAfterMso = ContextMenuInsertAfterMso ?? "Forward";
121+
this.ButtonConfigList = [];
122+
if (ButtonConfigList) {
123+
for (const buttonConfig of ButtonConfigList) {
124+
this.ButtonConfigList.push(new ButtonConfig(buttonConfig));
125+
}
126+
}
127+
}
128+
}
129+
130+
export class TypicalReplyConfig {
131+
Priority;
132+
ConfigList;
133+
constructor({ Priority, ConfigList }) {
134+
this.Priority = Priority ?? 0;
135+
this.ConfigList = [];
136+
if (ConfigList) {
137+
for (const config of ConfigList) {
138+
this.ConfigList.push(new Config(config));
139+
}
119140
}
120141
}
121142
}

0 commit comments

Comments
 (0)