forked from snario/allhands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting.ts
248 lines (217 loc) · 7.29 KB
/
formatting.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { TEXT_COLOR_SECONDARY } from "../constants";
import Linear, { Initiative, Project } from "./linear";
export const HIGHLIGHT_COLOR = "#FFFF99";
// Define colors for health statuses
export const BACKGROUND_COLOR_AT_RISK = "#f6eacb";
export const FONT_COLOR_AT_RISK = "#d9a800";
export const BACKGROUND_COLOR_OFF_TRACK = "#f3cece";
export const FONT_COLOR_OFF_TRACK = "#d24044";
export const BACKGROUND_COLOR_ON_TRACK = "#c8e1ca";
export const FONT_COLOR_ON_TRACK = "#009030";
export const BACKGROUND_COLOR_UNKNOWN_HEALTH = "#d6d6d6";
export const FONT_COLOR_UNKNOWN_HEALTH = "#a0a0a2";
// Define colors for project statuses
export const BACKGROUND_COLOR_COMPLETED = "#aac4fd";
export const FONT_COLOR_COMPLETED = "#5d6ad2";
export const BACKGROUND_COLOR_IN_PROGRESS = "#f6eacb";
export const FONT_COLOR_IN_PROGRESS = "#d9a800";
export const BACKGROUND_COLOR_PLANNED = "#d9d9d9";
export const FONT_COLOR_PLANNED = "#666666";
export const BACKGROUND_COLOR_CANCELED = "#000000";
export const FONT_COLOR_CANCELED = "#ea9999";
export const FONT_COLOR_UNKNOWN_STATUS = "#595959";
export function rightPad(str?: string | null) {
return str ? str + " " : "";
}
export type HexColor = `#${string}`;
export function getFirstName(name: string) {
return name.split(" ")[0];
}
export function formatDate(date: string) {
return new Intl.DateTimeFormat("en-US", {
timeZone: "GMT",
month: "short",
day: "2-digit",
}).format(new Date(date));
}
function defaultFormatting(): TextFormatting {
return {
fontFamily: "Inter",
};
}
export function applyFormattingToTextStyle(
textStyle: GoogleAppsScript.Slides.TextStyle,
formatting: Omit<
TextFormatting,
"alignment" | "paragraphAlignment"
> = defaultFormatting(),
) {
const {
fontFamily,
fontColor,
backgroundColor,
fontSize,
bold,
italic,
highlightColor,
} = formatting;
if (!textStyle) return textStyle;
if (fontFamily) textStyle.setFontFamily(fontFamily);
if (fontSize) textStyle.setFontSize(fontSize);
if (bold) textStyle.setBold(true);
if (italic) textStyle.setItalic(true);
if (fontColor) textStyle.setForegroundColor(fontColor);
if (backgroundColor) textStyle.setBackgroundColor(backgroundColor);
if (backgroundColor === null) textStyle.setBackgroundColorTransparent();
if (highlightColor) textStyle.setBackgroundColor(highlightColor);
return textStyle;
}
export function getHealthFormatting(
healthStatus: Project["health"],
): TextFormatting {
switch (healthStatus) {
case "atRisk":
return {
backgroundColor: BACKGROUND_COLOR_AT_RISK,
fontColor: FONT_COLOR_AT_RISK,
};
case "offTrack":
return {
backgroundColor: BACKGROUND_COLOR_OFF_TRACK,
fontColor: FONT_COLOR_OFF_TRACK,
};
case "onTrack":
return {
backgroundColor: BACKGROUND_COLOR_ON_TRACK,
fontColor: FONT_COLOR_ON_TRACK,
};
default:
return {
backgroundColor: BACKGROUND_COLOR_UNKNOWN_HEALTH,
fontColor: FONT_COLOR_UNKNOWN_HEALTH,
};
}
}
export function getStatusFormatting(
status: Initiative["status"],
): TextFormatting {
switch (status) {
case "Completed":
return {
backgroundColor: BACKGROUND_COLOR_COMPLETED,
fontColor: FONT_COLOR_COMPLETED,
}; // Dark indigo
case "In Progress":
return {
backgroundColor: BACKGROUND_COLOR_IN_PROGRESS,
fontColor: FONT_COLOR_IN_PROGRESS,
}; // Yellow background
case "Planned":
return {
backgroundColor: BACKGROUND_COLOR_PLANNED,
fontColor: FONT_COLOR_PLANNED,
}; // Grey background
case "Canceled":
return {
backgroundColor: BACKGROUND_COLOR_CANCELED,
fontColor: FONT_COLOR_CANCELED,
}; // Black background
default:
return {
backgroundColor: undefined,
fontColor: FONT_COLOR_UNKNOWN_STATUS,
}; // Transparent background
}
}
export function getDateFormatting(
targetDate: string,
completed: boolean,
): TextFormatting &
Required<Pick<TextFormatting, "bold" | "fontColor" | "backgroundColor">> {
const isOverdue = Linear.isProjectOverdue(targetDate);
const isDueSoon = Linear.isProjectDueSoon(targetDate);
if (completed) {
return {
backgroundColor: BACKGROUND_COLOR_COMPLETED,
fontColor: FONT_COLOR_COMPLETED,
bold: false,
}; // Completed (Dark indigo)
} else if (isOverdue) {
return {
backgroundColor: BACKGROUND_COLOR_OFF_TRACK,
fontColor: FONT_COLOR_OFF_TRACK,
bold: false,
}; // Overdue (Dark red)
} else if (isDueSoon) {
return {
backgroundColor: BACKGROUND_COLOR_AT_RISK,
fontColor: FONT_COLOR_AT_RISK,
bold: false,
}; // Due Soon (Dark yellow)
} else {
return {
backgroundColor: null,
fontColor: TEXT_COLOR_SECONDARY,
bold: false,
}; // Transparent background
}
}
export type TextFormatting = {
fontColor?: HexColor;
backgroundColor?: HexColor | null;
highlightColor?: HexColor;
bold?: boolean;
italic?: boolean;
alignment?: GoogleAppsScript.Slides.ContentAlignment;
paragraphAlignment?: GoogleAppsScript.Slides.ParagraphAlignment;
fontFamily?: string;
fontSize?: number;
};
export function adjustFontSizeToFit(
textBox: GoogleAppsScript.Slides.Shape,
defaultFontSize: number,
) {
const textRange = textBox.getText();
let currentFontSize = defaultFontSize;
const maxHeight = textBox.getHeight();
if (textRange.getLength() < 10) return;
while (currentFontSize > 1) {
textRange.getTextStyle().setFontSize(currentFontSize);
const lineCount = getAdjustedLineCount(textRange, currentFontSize);
const lineHeight = currentFontSize * 1.8;
const contentHeight = lineCount * lineHeight;
if (contentHeight <= maxHeight) {
break;
}
currentFontSize -= 1;
}
textRange.getTextStyle().setFontSize(currentFontSize);
}
export function getAdjustedLineCount(
textRange: GoogleAppsScript.Slides.TextRange,
currentFontSize: number,
) {
// Calculate the maximum number of characters that can fit on one line
const maxCharsPerLine = Math.floor(1280 / currentFontSize);
// Split the text by newlines and use reduce to calculate the total line count
// For each line, add the necessary number of lines based on its length
// and maxCharsPerLine
return textRange
.asString()
.split("\n")
.reduce(
(count, line) =>
// Math.ceil ensures that any partial line is counted as a full line
count + Math.ceil(line.length / maxCharsPerLine),
0,
);
}
export default {
rightPad,
formatDate,
applyFormattingToTextStyle,
getHealthFormatting,
getStatusFormatting,
getDateFormatting,
adjustFontSizeToFit,
};