-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMyQuotes.js
More file actions
287 lines (231 loc) · 8.6 KB
/
Copy pathMyQuotes.js
File metadata and controls
287 lines (231 loc) · 8.6 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// icon-color: purple; icon-glyph: quote-right;
// === Start: Param Handling ===
const defaultCategory = "machiavelli";
// const defaultCategory = "kafka";
const defaultSize = config.widgetFamily === "small" ? "s" : config.widgetFamily === "medium" ? "m" : "l";
const validSizes = ["s", "m", "l"];
const validCategories = ["myquotes", "gita", "test", "zen", "machiavelli", "aurelius", "fyodor", "kafka"];
// const param = args.widgetParameter ? args.widgetParameter.trim().toLowerCase() : `${defaultCategory}`;
const param = args.widgetParameter ? args.widgetParameter.trim().toLowerCase() : defaultCategory;
// const param = args.widgetParameter ? args.widgetParameter.trim().toLowerCase() : "540";
const parts = param.split(",");
let category = defaultCategory;
let sizeParam = defaultSize;
let forcedIndex = null;
for (const p of parts) {
const trimmed = p.trim();
if (validCategories.includes(trimmed)) {
category = trimmed;
} else if (validSizes.includes(trimmed)) {
sizeParam = trimmed;
} else if (!isNaN(parseInt(trimmed))) {
forcedIndex = parseInt(trimmed);
}
}
// Determine widget size fallback
let widgetSize;
if (validSizes.includes(sizeParam)) {
widgetSize = sizeParam;
} else if (config.widgetFamily === "medium") {
widgetSize = "m";
} else if (config.widgetFamily === "large") {
widgetSize = "l";
} else {
widgetSize = "s";
}
// If category is invalid, just refresh and exit
if (!validCategories.includes(category)) {
console.warn("⚠️ Invalid category. Refreshing...");
Script.complete();
return;
}
const fm = FileManager.iCloud();
// myQuotes, test ,gita, zen, machiavelli, Aurelius, fyodor, kafka
// const defaultParam = "machiavelli";
// const param = args.widgetParameter ? args.widgetParameter.trim().toLowerCase() : "machiavelli";
// const SOURCE = param || "gita";
const SHEET_ID = "1amFMwf_j83eRLNOAWnqMNfA3ZyE6igqjZF_OrSNww84";
// const SHEET_TAB = param;
// const SHEET_TAB = category.charAt(0).toUpperCase() + category.slice(1);
const SHEET_TAB = category;
const COLOR_PAIRS_PATH = fm.joinPath(fm.documentsDirectory(), ".source/dark_theme_color_pairs.json");
// === Utilities ===
function getColor(hex) {
if (!hex || typeof hex !== "string" || !hex.startsWith("#")) return null;
try {
return new Color(hex);
} catch (_) {
return null;
}
}
function getColorPairFromJSON() {
try {
if (!fm.fileExists(COLOR_PAIRS_PATH)) return null;
const raw = fm.readString(COLOR_PAIRS_PATH);
const pairs = JSON.parse(raw);
const pair = pairs[Math.floor(Math.random() * pairs.length)];
return {
backgroundColor: getColor(pair.background) || new Color("#000000"),
fontColor: getColor(pair.font) || Color.white()
};
} catch (_) {
return {
backgroundColor: new Color("#000000"),
fontColor: Color.white()
};
}
}
async function getQuoteFromSheet(rowNumber = null) {
try {
const url = `https://docs.google.com/spreadsheets/d/${SHEET_ID}/gviz/tq?tqx=out:json&sheet=${category}`;
const req = new Request(url);
const raw = await req.loadString();
const json = JSON.parse(raw.match(/google.visualization.Query.setResponse\((.+)\)/)[1]);
const rows = json.table.rows.map(r => r.c.map(c => (c ? c.v : "")));
const usable = rows.filter(r => r[0] && r[1]);
let row;
if (rowNumber !== null && rowNumber >= 2) {
row = rows[rowNumber - 1]; // Spreadsheet row 544 → rows[542]
}
if (!row || !row[0]) {
console.warn(`⚠️ No valid quote at row ${rowNumber}, falling back to filtered random`);
// Try to find a usable quote that fits the widget size
const fitting = usable.filter(([q, a]) => !isQuoteTooLong(q, a, widgetSize));
if (fitting.length > 0) {
// row = fitting[Math.floor(Math.random() * fitting.length)];
const dailyIndex = getDailyIndex(fitting.length, widgetSize);
row = fitting[dailyIndex];
} else {
// If none fit, fall back to truly random
console.warn("⚠️ No short enough quote found, using random long one");
row = usable[Math.floor(Math.random() * usable.length)];
}
}
const [quote, author, fontHex, bgHex] = row;
return {
quote,
author,
fontColor: getColor(fontHex),
backgroundColor: getColor(bgHex)
};
} catch (err) {
console.error("🔥 Error fetching or parsing sheet data:", err);
return {
quote: "Something went wrong.",
author: ""
};
}
}
// Utility to get repeatable index based on current day
function getDailyIndex(length, sizeKey) {
const today = new Date();
const seed = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate();
const sizeOffset = { s: 1, m: 2, l: 3 };
return (seed + sizeOffset[sizeKey]) % length;
// return 1; // for testing
}
const sfs = 12;
const mfs = 14;
const lfs = 16;
function isQuoteTooLong(quote, author, sizeKey) {
const totalText = `“${quote}”— ${author}`;
const length = totalText.length;
if (sizeKey === "s") {
return length < 1 || length > 140;
}
if (sizeKey === "m") {
return length <= 140 || length > 260;
}
if (sizeKey === "l") {
return length <= 260; // anything above 260 is fine!
}
return false; // fallback safety
}
// === Font Loader ===
function loadCustomFont(fileName, size) {
const fontPath = fm.joinPath(fm.documentsDirectory(), `.fonts/${fileName}`);
if (fm.fileExists(fontPath)) {
return new Font(fileName, size);
} else {
console.warn(`⚠️ Font not found: ${fileName}`);
return Font.systemFont(size);
}
}
// === Widget ===
async function createWidget() {
const widget = new ListWidget();
// const quoteData = await getQuoteFromSheet();
const quoteData = await getQuoteFromSheet(forcedIndex);
const fallback = getColorPairFromJSON();
const bgColor = quoteData.backgroundColor || fallback.backgroundColor;
const fontColor = quoteData.fontColor || fallback.fontColor;
widget.backgroundColor = bgColor;
// Font settings
// const fontSize = config.widgetFamily === "small" ? 13 : 16;
const fontSize = widgetSize === "s" ? sfs : widgetSize === "l" ? lfs : mfs;
// new Font(FONT, fontSize - 3);
// loadCustomFont("Roboto-Bold.ttf", fontSize);
// Font.boldSystemFont(fontSize)
// loadCustomFont("Roboto-Italic.ttf", fontSize - 3);
// Font.italicSystemFont(fontSize - 3)
// // "Avenir-Black" "Avenir-Heavy" Avenir-Oblique Roboto
// 1 style
// const FONT = "Avenir-Heavy";
// const quoteFont = new Font(FONT, fontSize);
// const authorFont = new Font("Avenir-Oblique", fontSize - 3);
// 2 style
// const FONT = "Roboto";
// const quoteFont = new Font(FONT, fontSize);
// const authorFont = new Font(FONT, fontSize - 3);
// 3 style
const quoteFont = Font.boldSystemFont(fontSize);
const authorFont = Font.italicSystemFont(fontSize - 1);
const stack = widget.addStack(); // Create a vertical stack
stack.layoutVertically();
stack.addSpacer();
const textStack = stack.addStack();
textStack.layoutHorizontally();
// textStack.centerAlignContent();
// stack.centerAlignContent();
// textStack.addSpacer();
// Quote: bold
const quoteText = textStack.addText(`“${quoteData.quote}”`);
quoteText.font = quoteFont;
// quoteText.font = loadCustomFont("Roboto-Bold.ttf", fontSize);
quoteText.textColor = fontColor;
// quoteText.minimumScaleFactor = 0.5;
quoteText.leftAlignText();
stack.addSpacer();
// Author: italic
if (quoteData.author) {
const textStack = stack.addStack();
textStack.layoutHorizontally();
textStack.addSpacer();
const authorText = textStack.addText(`— ${quoteData.author}`);
// textStack.addSpacer(0);
// authorText.font = new Font(FONT, fontSize - 3);
// authorText.font = loadCustomFont("Roboto-Italic.ttf", fontSize - 3);
authorText.font = authorFont;
authorText.textColor = fontColor;
// authorText.minimumScaleFactor = 0.5;
authorText.rightAlignText();
}
// stack.addSpacer();
// widget.refreshAfterDate = new Date(Date.now() + 3600000); // refresh hourly
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0, 0, 0, 0); // exactly at 12:00:00 AM (midnight)
widget.refreshAfterDate = tomorrow;
console.log("➡️ Param parts:", parts);
console.log("📂 Category:", category);
console.log("📏 Size:", sizeParam);
console.log("🔢 Forced index:", forcedIndex);
return widget;
}
// === Run ===
const widget = await createWidget();
if (!config.runsInWidget) await widget.presentSmall();
// if (!config.runsInWidget) await widget.presentMedium();
// if (!config.runsInWidget) await widget.presentLarge();
else Script.setWidget(widget);
Script.complete();