Repository navigation
Expand file tree
/
Copy pathZenTweak.js
More file actions
302 lines (264 loc) · 9.92 KB
/
Copy pathZenTweak.js
File metadata and controls
302 lines (264 loc) · 9.92 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: sliders-h;
/**
* ZenTweak.js - Configuration Editor for ZenTrate
*
* Features:
* - Fullscreen editor page with a live widget preview and time scrubber
* - Add, edit, delete (with undo), and drag items between columns
* - Find item URLs by catalog search, Claude, or shortcut name (also from Siri)
* - Time and day constraints, sort method, usage training
* - Pick, create and edit the suite's themes (?focus=tema opens at Tema)
* - Every change is saved as soon as it's made
*/
const Theme = importModule("lib/theme")
const Widget = importModule("lib/widget")
const Validate = importModule("lib/validate")
const ZenTrateConfig = importModule("config/zentrate")
const Editor = importModule("config/zentrate-editor")
const Schemes = importModule("lib/schemes")
// ============================================
// CONFIGURATION
// ============================================
const loadConfig = ZenTrateConfig.loadConfigForEditor
const saveConfig = ZenTrateConfig.saveConfig
function saveStatsIfChanged(before, after) {
if (JSON.stringify(before) !== JSON.stringify(after)) ZenTrateConfig.saveStats(after)
}
/** The theme strip is never empty: ship Noir when ZenThemes/ has nothing. */
function ensureThemes() {
if (Theme.loadAllThemes().length === 0) Theme.saveThemeToFolder(Theme.DEFAULT_THEME, "noir.json")
}
function loadThemes(session) {
session.themes = Theme.loadAllThemes()
session.activeTheme = Theme.loadTheme()
}
function pageState(session, now) {
return Editor.buildState(session.config, session.stats, now, { themes: session.themes, active: session.activeTheme })
}
// ============================================
// EDITOR PAGE
// ============================================
// After this many failed ZT.next() calls in a row the page is assumed gone
const MAX_BRIDGE_FAILURES = 5
const CLOSED = Symbol("closed")
/**
* Presents the editor and serves its messages until the user closes it.
* Scriptable can't dismiss a WebView from code, so every change is saved
* immediately and the closed page simply ends the loop.
* @param {string|null} focus - "tema" opens the page scrolled to the themes
*/
async function openEditor(focus) {
ensureThemes()
const session = { config: loadConfig(), stats: ZenTrateConfig.loadStats(), undo: null, themeUndo: null }
loadThemes(session)
const webView = new WebView()
let open = true
const closed = webView.present(true).then(() => {
open = false
return CLOSED
})
// Loaded only once on screen: a page loaded into an unpresented view can stay blank
await webView.loadHTML(Editor.buildHTML({
theme: session.activeTheme,
state: pageState(session, new Date()),
focus
}))
let failures = 0
while (open) {
let raw
try {
raw = await Promise.race([webView.evaluateJavaScript("ZT.next()", true), closed])
failures = 0
} catch (error) {
if (++failures >= MAX_BRIDGE_FAILURES) break
continue
}
if (raw === CLOSED || !open) break
const reply = await handleMessage(raw, session)
if (reply && open) {
await webView.evaluateJavaScript(`ZT.receive(${JSON.stringify(reply)})`).catch(error => {
console.error(`ZenTweak: Could not reply to the page: ${error.message}`)
})
}
}
}
/**
* Applies one page message.
* @param {*} raw - Message JSON from the page
* @param {Object} session - { config, stats, undo, themes, activeTheme, themeUndo }, updated in place on success
* @returns {Promise<Object|null>} Reply for ZT.receive, or null for none
*/
async function handleMessage(raw, session) {
const parsed = Editor.parseMessage(raw)
if (!parsed.ok) return { type: "error", error: parsed.error }
const msg = parsed.msg
const id = msg.id
switch (msg.type) {
case "idle":
return null
case "op": {
if (Editor.isThemeOp(msg)) return handleThemeOp(msg, session)
const now = new Date()
const result = Editor.applyOp(session, msg, now)
if (!result.ok) return { type: "error", id, error: result.error, field: result.field }
saveConfig(result.state.config)
saveStatsIfChanged(session.stats, result.state.stats)
Object.assign(session, result.state)
session.themeUndo = null
const reply = { type: "state", id, state: pageState(session, now) }
if (result.deleted) reply.deleted = result.deleted
return reply
}
case "search-claude":
return { type: "claude", id, ...await askClaude(msg.query) }
case "set-key": {
if (!Schemes.saveApiKey(msg.key)) return { type: "claude", id, needKey: true, error: "Pegá una clave primero" }
const query = typeof msg.query === "string" ? msg.query.trim() : ""
return query ? { type: "claude", id, ...await askClaude(query) } : { type: "claude", id, results: [] }
}
case "test": {
const url = Validate.validateURL(msg.url)
if (!url) return { type: "error", id, error: `«${msg.url}» no es una URL válida` }
Safari.open(url)
return { type: "tested", id }
}
}
return { type: "error", id, error: "Mensaje no válido" }
}
/**
* Applies one theme op: Editor decides what to write, lib/theme writes it.
* Ending any item undo, like every other change does.
* @returns {Object} Reply for ZT.receive
*/
function handleThemeOp(msg, session) {
const id = msg.id
const result = Editor.applyThemeOp({ themes: session.themes, active: session.activeTheme, undo: session.themeUndo }, msg)
if (!result.ok) {
const reply = { type: "error", id, error: result.error, field: result.field }
if (result.errors) reply.errors = result.errors
return reply
}
const effect = result.effect
let created = null
switch (effect.kind) {
case "apply":
if (!Theme.applyTheme(effect.filename)) return { type: "error", id, error: "No se pudo leer ese tema." }
break
case "update":
Theme.updateTheme(effect.filename, effect.theme)
break
case "create":
created = Theme.saveAsNew(effect.theme)
break
case "remove": {
const removed = Theme.deleteTheme(effect.filename)
if (!removed.ok) return { type: "error", id, error: removed.error }
break
}
case "saveActive":
Theme.saveTheme(effect.theme)
break
}
session.themeUndo = result.undo
session.undo = null
loadThemes(session)
const reply = { type: "state", id, state: pageState(session, new Date()) }
if (created) reply.created = created
if (result.deleted) reply.deleted = result.deleted
return reply
}
/**
* Asks Claude for URLs without any alert; the page asks for the key itself.
* @param {string} query
* @returns {Promise<Object>} { results } | { needKey, error? } | { error }
*/
async function askClaude(query) {
const clean = typeof query === "string" ? query.trim() : ""
if (!clean) return { error: "Escribí un nombre primero" }
const apiKey = await Schemes.getApiKey()
if (!apiKey) return { needKey: true }
try {
return { results: await Schemes.askClaude(clean, apiKey) }
} catch (error) {
if (error.keyError) return { needKey: true, error: `Claude rechazó la clave: ${error.message}` }
return { error: `Error de Claude: ${error.message}` }
}
}
// ============================================
// SIRI / SHORTCUTS
// ============================================
/**
* Reads text passed in from a Shortcut ("Run Script" parameter)
* @returns {string|null}
*/
function getShortcutQuery() {
const param = args.shortcutParameter
if (typeof param === "string" && param.trim()) return param.trim()
const texts = args.plainTexts || []
if (texts.length && texts[0].trim()) return texts[0].trim()
return null
}
/**
* Adds an item without any UI, for Siri. Replies through the Shortcut output.
* Apps go to the left column, shortcuts to the right, matching the current layout.
* @param {string} query - e.g. "Spotify" or "shortcut Leer QR"
* @returns {Promise<string>} Message for Siri to speak
*/
async function addFromSiri(query) {
const request = ZenTrateConfig.parseSiriQuery(query)
let picked = null
if (request.kind === "shortcut") {
picked = Schemes.shortcutItem(request.name)
} else {
try {
picked = await Schemes.resolve(request.query)
} catch (error) {
return `Claude error: ${error.message}`
}
}
if (!picked) return `I couldn't find a URL for "${query}".`
const config = loadConfig()
if (config.items.some(i => i.name === picked.name)) {
return `${picked.name} is already in ZenTrate.`
}
const result = ZenTrateConfig.addItem(config, { name: picked.name, scheme: picked.scheme, column: request.column })
if (!result.ok) return `Couldn't add ${picked.name}: ${result.error}`
const stats = ZenTrateConfig.loadStats()
const trained = ZenTrateConfig.startTraining(result.config, stats, new Date())
saveConfig(trained.config)
saveStatsIfChanged(stats, trained.stats)
return `Added ${picked.name} to ZenTrate.`
}
// ============================================
// WIDGET
// ============================================
function createLauncherWidget() {
const themeConfig = Theme.loadTheme()
const widget = Widget.createWidget({ theme: themeConfig, url: Widget.buildActionURL(Script.name()) })
widget.addSpacer()
const label = widget.addText("Editar ZenTrate")
label.font = Theme.getFont(16, { theme: themeConfig })
label.textColor = Theme.getTextColor(themeConfig)
label.centerAlignText()
widget.addSpacer()
return widget
}
// ============================================
// MAIN
// ============================================
if (Widget.isWidget()) {
Script.setWidget(createLauncherWidget())
} else {
await ZenTrateConfig.downloadFiles()
await Theme.downloadThemes()
const shortcutQuery = getShortcutQuery()
if (shortcutQuery) {
Script.setShortcutOutput(await addFromSiri(shortcutQuery))
} else {
const focus = (args.queryParameters || {}).focus
await openEditor(focus === "tema" ? "tema" : null)
}
}
Script.complete()