pinokiod 8.3.26 → 8.3.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/kernel/shell.js +3 -0
  2. package/kernel/shell_size.js +99 -0
  3. package/kernel/shells.js +2 -0
  4. package/package.json +1 -1
  5. package/server/index.js +7 -3
  6. package/server/lib/app_run_tabs.js +29 -17
  7. package/server/public/agent-bar-about.css +67 -0
  8. package/server/public/agent-bar-about.js +205 -0
  9. package/server/public/agent-bar-compose.js +4 -1
  10. package/server/public/agent-bar-modes.css +78 -0
  11. package/server/public/agent-bar-modes.js +97 -0
  12. package/server/public/agent-bar-panel.js +41 -13
  13. package/server/public/agent-bar.css +7 -5
  14. package/server/public/agent-bar.js +37 -9
  15. package/server/public/app-run-tabs.js +0 -10
  16. package/server/public/files-app/open-folder.js +58 -41
  17. package/server/public/files-app/toolbar.css +27 -5
  18. package/server/public/git-page.css +0 -1
  19. package/server/public/git-page.js +269 -149
  20. package/server/public/home-actions-menu.css +188 -0
  21. package/server/public/home-actions-menu.js +135 -0
  22. package/server/public/home-shell.css +121 -0
  23. package/server/public/home-shell.js +290 -0
  24. package/server/routes/git_page.js +4 -6
  25. package/server/views/app.ejs +19 -5
  26. package/server/views/file_browser.ejs +3 -4
  27. package/server/views/index.ejs +42 -3
  28. package/server/views/partials/home_action_modal.ejs +1 -1
  29. package/server/views/partials/home_ask_button.ejs +15 -0
  30. package/server/views/partials/home_shell_button.ejs +21 -0
  31. package/server/views/partials/main_sidebar.ejs +5 -1
  32. package/server/views/pre.ejs +1 -1
  33. package/test/agent-bar-about.test.js +238 -0
  34. package/test/agent-bar-modes.test.js +191 -0
  35. package/test/agent-bar-new-tab.test.js +60 -0
  36. package/test/app-git-files-tabs.test.js +84 -0
  37. package/test/home-actions-menu.test.js +187 -0
  38. package/test/home-shell.test.js +358 -0
  39. package/test/shell-size.test.js +166 -0
@@ -6,8 +6,14 @@
6
6
  // appear in the section itself. Nothing opens as a popup.
7
7
  //
8
8
  // The status is checked when the page opens, each time its tab is brought to the
9
- // front, when Refresh is pressed, and after an action that changes it. Never on a
10
- // timer, and never by watching files.
9
+ // front, when Refresh is pressed, and around every action. Never on a timer, and
10
+ // never by watching files.
11
+ //
12
+ // One thing at a time. Reading is always possible. Every action (commit,
13
+ // discard, checkout, fork, create, publish) goes through act(): the page is
14
+ // blocked, the status is read again, and the action only goes ahead if the
15
+ // repository still looks the way it did when the button was pressed. The page
16
+ // stays blocked until the action is over and the status has been read once more.
11
17
  (() => {
12
18
  const root = document.getElementById('git-page')
13
19
  if (!root) {
@@ -20,15 +26,18 @@
20
26
  const state = {
21
27
  repos: [], // from the last check
22
28
  checkedAt: 0,
23
- checking: null,
24
- error: '',
29
+ checking: null, // the check in flight; resolves to whether the status could be read
30
+ error: '', // the repositories could not be loaded
31
+ notice: '', // what became of the last action, when it did not simply succeed
25
32
  repoKey: null, // selected repository
26
33
  section: root.dataset.publish === '1' ? 'publish' : 'changes',
27
34
  info: new Map(), // repoKey -> history data (commits, branches, remote, GitHub state)
28
- changes: null, // the Changes section: { repoKey, selected:Set, open:index, message, messageEdited, confirmReset, error }
29
- commit: null, // the History section's open commit: { oid, title, changes, open, cwd }
30
- publishSkipCommit: new Set(),
31
- run: null, // a running script: { title, url, finished, afterClose }
35
+ views: new Map(), // repoKey -> its Changes section: { listed, ticked:Set|null, open:index, message, messageEdited, confirmReset: the look the discard was asked for }
36
+ commit: null, // the History section's open commit: { key, oid, title, changes, open }
37
+ forms: new Map(), // repoKey -> what was typed into its Fork and Publish forms
38
+ consent: null, // "Continue without committing", for one publish: { key, seen }
39
+ diffs: new Map(), // diff address -> its request, kept until the next check
40
+ work: null, // the action in progress: { title, note, url (a script's terminal), finished }
32
41
  }
33
42
 
34
43
  // ---------------------------------------------------------------- helpers
@@ -39,7 +48,7 @@
39
48
  if (key === 'class') node.className = value
40
49
  else if (key === 'text') node.textContent = value
41
50
  else if (key.startsWith('on')) node.addEventListener(key.slice(2), value)
42
- else if (key in node && key !== 'list') node[key] = value
51
+ else if (key in node) node[key] = value
43
52
  else node.setAttribute(key, value)
44
53
  })
45
54
  children.flat().forEach((child) => {
@@ -65,6 +74,15 @@
65
74
  const isOid = (value) => typeof value === 'string' && /^[0-9a-f]{7,40}$/i.test(value)
66
75
  const isUntracked = (status) => /new|add|untracked/i.test(status || '')
67
76
  const shellQuote = (value) => `"${String(value).replace(/(["`$\\])/g, '\\$1')}"`
77
+ // a changed file's identity, a repository's list of them, and how the
78
+ // repository looks: the same list with each file's status
79
+ const keyOf = (change) => change.path || change.file
80
+ const listedOf = (entry) => (Array.isArray(entry.changes) ? entry.changes : []).map(keyOf).join('\n')
81
+ const lookOf = (entry) => (Array.isArray(entry.changes) ? entry.changes : []).map((change) => `${keyOf(change)}\t${change.status}`).join('\n')
82
+ const formOf = (entry) => {
83
+ if (!state.forms.has(entry.repoParam)) state.forms.set(entry.repoParam, {})
84
+ return state.forms.get(entry.repoParam)
85
+ }
68
86
  const getJson = async (url) => {
69
87
  const response = await fetch(url, { cache: 'no-store' })
70
88
  if (!response.ok) throw new Error(`HTTP ${response.status}`)
@@ -95,7 +113,7 @@
95
113
  const raw = typeof remoteUrl === 'string' ? remoteUrl.trim() : ''
96
114
  if (!raw) return ''
97
115
  if (/^https?:\/\//i.test(raw)) return raw.replace(/\.git$/i, '')
98
- const ssh = raw.match(/^ssh:\/\/(?:[^@/]+@)?([^/]+)\/(.+?)(?:\.git)?$/i)
116
+ const ssh = raw.match(/^(?:ssh|git):\/\/(?:[^@/]+@)?([^/:]+)(?::\d+)?\/(.+?)(?:\.git)?$/i)
99
117
  if (ssh) return `https://${ssh[1]}/${ssh[2]}`
100
118
  const scp = raw.match(/^(?:[^@]+@)?([^:/]+):(.+?)(?:\.git)?$/)
101
119
  if (scp) return `https://${scp[1]}/${scp[2]}`
@@ -129,25 +147,39 @@
129
147
  }
130
148
 
131
149
  // ------------------------------------------------------------------ data
150
+ // A repository's commit draft is its ticks and its typed message. After every
151
+ // read of the status each draft is brought in line with the files, whichever
152
+ // section is showing. A draft carries over while there are files left to
153
+ // commit; once there are none it starts over. Ticks are null until a tick is
154
+ // changed or a message is typed, which means "every file". From then on they
155
+ // are the files that were ticked, so a file that turns up later is never
156
+ // ticked by itself.
157
+ const syncDrafts = () => state.repos.forEach((entry) => {
158
+ const changes = Array.isArray(entry.changes) ? entry.changes : []
159
+ const listed = listedOf(entry)
160
+ const before = state.views.get(entry.repoParam)
161
+ if (before && before.listed === listed) return
162
+ const draft = before && changes.length ? before : null
163
+ const present = new Set(changes.map(keyOf))
164
+ state.views.set(entry.repoParam, {
165
+ listed,
166
+ ticked: draft && draft.ticked ? new Set([...draft.ticked].filter((key) => present.has(key))) : null,
167
+ open: changes.length ? 0 : -1,
168
+ message: draft && draft.messageEdited ? draft.message : '',
169
+ messageEdited: Boolean(draft && draft.messageEdited),
170
+ confirmReset: '',
171
+ })
172
+ })
132
173
  const check = () => {
133
174
  if (state.checking) return state.checking
134
175
  state.checking = (async () => {
176
+ let read = false
135
177
  try {
136
178
  const data = await getJson(`/info/gitstatus/${encodeURIComponent(appName)}?force=1`)
137
179
  let repos = Array.isArray(data.repos) ? data.repos : []
138
180
  if (repos.length === 0) {
139
- // a folder with no repository yet: its files, ready for a first commit
140
- const fallback = await getJson(`/gitcommit/HEAD/${encodeRepoPath(appName)}`)
141
- const changes = Array.isArray(fallback.changes) ? fallback.changes : []
142
- repos = [{
143
- name: appName,
144
- main: true,
145
- repoParam: appName,
146
- changeCount: changes.length,
147
- changes,
148
- git_commit_url: fallback.git_commit_url || null,
149
- url: null,
150
- }]
181
+ // a folder with no repository in it
182
+ repos = [{ name: appName, main: true, repoParam: appName, changeCount: 0, changes: [], url: null, noRepo: true }]
151
183
  }
152
184
  repos.sort((a, b) => (a.main === b.main ? String(a.name).localeCompare(String(b.name)) : (a.main ? -1 : 1)))
153
185
  state.repos = repos
@@ -155,86 +187,148 @@
155
187
  if (!repos.some((entry) => entry.repoParam === state.repoKey)) {
156
188
  state.repoKey = repos[0] ? repos[0].repoParam : null
157
189
  }
190
+ // a repository that is gone takes everything remembered about it with it,
191
+ // so one that later turns up under the same name starts clean
192
+ const present = new Set(repos.map((entry) => entry.repoParam))
193
+ ;[state.views, state.forms].forEach((map) => [...map.keys()].forEach((key) => present.has(key) || map.delete(key)))
194
+ if (state.consent && !present.has(state.consent.key)) state.consent = null
195
+ if (state.commit && !present.has(state.commit.key)) state.commit = null
196
+ read = true
158
197
  } catch (error) {
159
- state.error = 'Could not load the repositories.'
198
+ try {
199
+ // the scan of every repository failed: the app's own repository on its own, to look at
200
+ const own = await getJson(`/gitcommit/HEAD/${encodeRepoPath(appName)}`)
201
+ const changes = Array.isArray(own.changes) ? own.changes : []
202
+ state.repos = [{ name: appName, main: true, repoParam: appName, changeCount: changes.length, changes, git_commit_url: own.git_commit_url || null, url: null }]
203
+ state.repoKey = appName
204
+ state.error = ''
205
+ } catch (_) {
206
+ // nothing could be read: what was on the page is no longer known to be true, so it goes
207
+ state.repos = []
208
+ state.error = 'Could not load the repositories.'
209
+ }
160
210
  }
211
+ syncDrafts()
161
212
  state.info.clear()
213
+ state.diffs.clear()
162
214
  state.checkedAt = Date.now()
163
215
  state.checking = null
164
216
  render()
217
+ return read
165
218
  })()
166
219
  render()
167
220
  return state.checking
168
221
  }
169
222
 
170
- // History data for the selected repository: commits, branches, remote and
171
- // whether GitHub is connected. Loaded when a section needs it, kept until the
172
- // next check.
173
- const loadInfo = (key) => {
223
+ // History data for a repository: commits, branches, remote and whether GitHub
224
+ // is connected. Loaded when a section first needs it, null until it arrives,
225
+ // kept until the next check.
226
+ const infoOf = (key) => {
174
227
  const known = state.info.get(key)
175
- if (known) return known.pending || Promise.resolve(known)
176
- const pending = getJson(`/info/git/HEAD/${encodeRepoPath(key)}`)
228
+ if (known) return known.pending ? null : known
229
+ const marker = { pending: true }
230
+ state.info.set(key, marker)
231
+ getJson(`/info/git/HEAD/${encodeRepoPath(key)}`)
177
232
  .catch(() => ({ failed: true }))
178
233
  .then((data) => {
179
- if (state.info.get(key) && state.info.get(key).pending === pending) {
234
+ // a check since then has thrown this request away
235
+ if (state.info.get(key) === marker) {
180
236
  state.info.set(key, data)
181
237
  render()
182
238
  }
183
- return data
184
239
  })
185
- state.info.set(key, { pending })
186
- return pending
187
- }
188
- const infoOf = (key) => {
189
- const data = state.info.get(key)
190
- if (!data) {
191
- loadInfo(key)
192
- return null
193
- }
194
- return data.pending ? null : data
240
+ return null
195
241
  }
196
242
 
197
- // ------------------------------------------------------- running a script
198
- // A git script runs in Pinokio's terminal view, shown in the section. Scripts
199
- // that report back (commit, checkout) close it themselves; the others show
200
- // their output until Close is pressed.
201
- const run = (title, url, afterClose) => {
202
- state.run = { title, url, finished: false, afterClose: afterClose || null }
243
+ // ------------------------------------------------------------ taking action
244
+ // The status has been read again after an action; the page is released.
245
+ const finish = async (problem) => {
246
+ state.work = { title: state.work.title, note: 'Refreshing...' }
247
+ render()
248
+ await check()
249
+ state.work = null
250
+ state.consent = null
251
+ state.notice = problem || ''
203
252
  render()
204
253
  }
205
- const closeRun = () => {
206
- const finished = state.run
207
- state.run = null
208
- const done = check()
209
- if (finished && finished.afterClose) {
210
- done.then(finished.afterClose)
254
+ // The one way to do anything. `perform(entry)` gets the repository as it is
255
+ // now. It returns, or resolves to, the address of a git script, which then
256
+ // runs in Pinokio's terminal view in the section; or to nothing, when it made
257
+ // its request directly. If it throws, nothing more is done and its message is shown.
258
+ const act = async (title, entry, perform) => {
259
+ if (state.work) return
260
+ const key = entry.repoParam
261
+ const seen = lookOf(entry)
262
+ state.work = { title, note: 'Checking...' }
263
+ state.notice = ''
264
+ render()
265
+ // a check already under way began before the button was pressed: it does not count
266
+ await state.checking
267
+ const read = await check()
268
+ const now = state.repos.find((item) => item.repoParam === key)
269
+ if (!read || !now || now.error || now.noRepo) {
270
+ state.work = null
271
+ state.notice = 'The repository could not be checked, so nothing was done.'
272
+ } else if (lookOf(now) !== seen) {
273
+ state.work = null
274
+ state.consent = null
275
+ state.notice = 'The files changed since this was shown, so nothing was done. Look again, then retry.'
276
+ } else {
277
+ state.work = { title, note: 'Working...' }
278
+ render()
279
+ let started
280
+ try {
281
+ started = await perform(now)
282
+ } catch (error) {
283
+ return finish(error.message)
284
+ }
285
+ if (typeof started !== 'string') return finish()
286
+ state.work = { title, url: started, finished: false }
211
287
  }
288
+ render()
289
+ }
290
+ // For publish and checkout: the history is read again, and the folder to run
291
+ // in is the one it names. A folder whose repository cannot be read is refused,
292
+ // because git would go on to use whatever repository contains it. Publish
293
+ // also needs the current branch to have a commit; checkout does not, since it
294
+ // is how a branch with none is left.
295
+ const folderOf = async (now, needsCommit) => {
296
+ const info = await getJson(`/info/git/HEAD/${encodeRepoPath(now.repoParam)}`).catch(() => null)
297
+ const hasHead = info && (typeof info.hasHead === 'boolean' ? info.hasHead : Array.isArray(info.log) && info.log.length > 0)
298
+ if (!info || info.gitDirExists === false || (needsCommit && !hasHead)) {
299
+ throw new Error('This repository could not be read, so nothing was done.')
300
+ }
301
+ return info.dir || cwdOf(now)
212
302
  }
213
303
  window.addEventListener('message', (event) => {
214
304
  const data = event.data
215
305
  if (!data) return
216
306
  if (event.source === window.parent && window.parent !== window) {
217
307
  // the app page tells the tab it brings to the front
218
- if (data.action === 'foreground' && !state.run && Date.now() - state.checkedAt > 2000) {
308
+ if (data.action === 'foreground' && !state.work && Date.now() - state.checkedAt > 2000) {
219
309
  check()
220
310
  }
221
311
  return
222
312
  }
223
- if (!state.run) return
313
+ if (!state.work || !state.work.url) return
224
314
  if (typeof data.callback === 'string' && data.callback.startsWith('$')) {
225
- closeRun()
226
- } else if (data.type === 'pinokio:socket-closed') {
227
- state.run.finished = true
315
+ // a script that reports back (commit, checkout) is over
316
+ finish()
317
+ } else if (data.type === 'disconnect') {
318
+ // the others show their output until Close is pressed
319
+ state.work.finished = true
228
320
  const footer = root.querySelector('.git-run-status')
229
321
  if (footer) footer.textContent = 'Finished.'
230
322
  }
231
323
  })
232
- const runPanel = () => h('div', { class: 'git-run' },
233
- h('div', { class: 'git-run-header' },
234
- h('div', { class: 'git-run-title', text: state.run.title }),
235
- h('div', { class: 'git-run-status', text: state.run.finished ? 'Finished.' : '' }),
236
- button('Close', closeRun)),
237
- h('iframe', { class: 'git-run-frame', src: state.run.url, allow: 'fullscreen *' }))
324
+ const workPanel = () => (state.work.url
325
+ ? h('div', { class: 'git-run' },
326
+ h('div', { class: 'git-run-header' },
327
+ h('div', { class: 'git-run-title', text: state.work.title }),
328
+ h('div', { class: 'git-run-status', text: state.work.finished ? 'Finished.' : '' }),
329
+ button('Close', () => finish())),
330
+ h('iframe', { class: 'git-run-frame', src: state.work.url, allow: 'fullscreen *' }))
331
+ : h('div', { class: 'git-empty', text: `${state.work.title}: ${state.work.note}` }))
238
332
 
239
333
  // ------------------------------------------------------------- file diffs
240
334
  const diffLines = (data) => {
@@ -252,7 +346,10 @@
252
346
  // The diff of one changed file, with a button that opens the file itself.
253
347
  const diffViewer = (change, repoKey) => {
254
348
  const body = h('div', { class: 'git-diff-body' }, h('div', { class: 'git-empty', text: 'Loading...' }))
255
- getJson(change.diffpath)
349
+ if (!state.diffs.has(change.diffpath)) {
350
+ state.diffs.set(change.diffpath, getJson(change.diffpath))
351
+ }
352
+ state.diffs.get(change.diffpath)
256
353
  .then((data) => body.replaceChildren(diffLines(data)))
257
354
  .catch(() => body.replaceChildren(h('div', { class: 'git-empty', text: 'Could not load the diff' })))
258
355
  const relative = String(change.file || '').split('/').map(encodeURIComponent).join('/')
@@ -265,35 +362,31 @@
265
362
 
266
363
  // ---------------------------------------------------------------- Changes
267
364
  const changesSection = (entry) => {
268
- const changes = Array.isArray(entry.changes) ? entry.changes : []
269
- if (!state.changes || state.changes.repoKey !== entry.repoParam || state.changes.count !== changes.length) {
270
- state.changes = {
271
- repoKey: entry.repoParam,
272
- count: changes.length,
273
- selected: new Set(changes.map((_, index) => index)),
274
- open: changes.length ? 0 : -1,
275
- message: '',
276
- messageEdited: false,
277
- confirmReset: false,
278
- error: '',
279
- }
365
+ if (entry.noRepo) {
366
+ return h('div', { class: 'git-empty', text: 'This folder is not a Git repository yet.' })
280
367
  }
281
- const view = state.changes
368
+ if (entry.error) {
369
+ return h('div', { class: 'git-error', text: `Could not read this repository's status: ${entry.error}` })
370
+ }
371
+ const changes = Array.isArray(entry.changes) ? entry.changes : []
372
+ const view = state.views.get(entry.repoParam)
282
373
  if (changes.length === 0) {
283
374
  return h('div', { class: 'git-empty', text: 'No changes. Everything is committed.' })
284
375
  }
285
- const picked = changes.filter((_, index) => view.selected.has(index))
376
+ const isTicked = (change) => !view.ticked || view.ticked.has(keyOf(change))
377
+ const picked = changes.filter(isTicked)
286
378
  if (!view.messageEdited) {
287
379
  view.message = suggestMessage(picked)
288
380
  }
289
- const tracked = picked.filter((change) => !isUntracked(change.status)).map((change) => change.path || change.file)
290
- const untracked = picked.filter((change) => isUntracked(change.status)).map((change) => change.path || change.file)
381
+ const tracked = picked.filter((change) => !isUntracked(change.status)).map(keyOf)
382
+ const untracked = picked.filter((change) => isUntracked(change.status)).map(keyOf)
291
383
 
292
384
  const selectAll = h('input', {
293
385
  type: 'checkbox',
294
386
  checked: picked.length === changes.length,
295
387
  onchange: (event) => {
296
- view.selected = new Set(event.target.checked ? changes.map((_, index) => index) : [])
388
+ view.ticked = new Set(event.target.checked ? changes.map(keyOf) : [])
389
+ view.confirmReset = ''
297
390
  render()
298
391
  },
299
392
  })
@@ -307,40 +400,39 @@
307
400
  },
308
401
  h('input', {
309
402
  type: 'checkbox',
310
- checked: view.selected.has(index),
403
+ checked: isTicked(change),
311
404
  onclick: (event) => event.stopPropagation(),
312
405
  onchange: (event) => {
313
- if (event.target.checked) view.selected.add(index)
314
- else view.selected.delete(index)
406
+ view.ticked = new Set(picked.map(keyOf))
407
+ if (event.target.checked) view.ticked.add(keyOf(change))
408
+ else view.ticked.delete(keyOf(change))
409
+ view.confirmReset = ''
410
+ view.open = index
315
411
  render()
316
412
  },
317
413
  }),
318
414
  h('span', { class: 'git-file-name', text: change.file, title: change.file }),
319
415
  h('span', { class: 'git-file-status', text: change.status }))),
320
416
  h('div', { class: 'git-files-actions' },
321
- view.confirmReset
417
+ view.confirmReset && view.confirmReset === lookOf(entry)
322
418
  ? h('div', { class: 'git-confirm' },
323
419
  h('div', { class: 'git-confirm-text', text: `Discard the changes in ${picked.length} file${picked.length === 1 ? '' : 's'}? This cannot be undone.` }),
324
- button('Discard', async () => {
325
- view.confirmReset = false
326
- view.error = ''
327
- try {
420
+ button('Discard', () => {
421
+ view.confirmReset = ''
422
+ act(`Discarding ${picked.length} file${picked.length === 1 ? '' : 's'}`, entry, async (now) => {
328
423
  const response = await fetch('/terminals/git/reset-files', {
329
424
  method: 'POST',
330
425
  headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
331
- body: JSON.stringify({ cwd: cwdOf(entry), tracked_paths: tracked, untracked_paths: untracked }),
426
+ body: JSON.stringify({ cwd: cwdOf(now), tracked_paths: tracked, untracked_paths: untracked }),
332
427
  })
333
428
  const payload = await response.json().catch(() => null)
334
429
  if (!response.ok || !payload || payload.ok !== true) {
335
430
  throw new Error(payload && payload.error ? payload.error : 'Failed to reset selected files.')
336
431
  }
337
- } catch (error) {
338
- view.error = error.message
339
- }
340
- check()
432
+ })
341
433
  }, { danger: true }),
342
- button('Cancel', () => { view.confirmReset = false; render() }))
343
- : button(`Reset selected (${picked.length})`, () => { view.confirmReset = true; render() }, {
434
+ button('Cancel', () => { view.confirmReset = ''; render() }))
435
+ : button(`Reset selected (${picked.length})`, () => { view.confirmReset = lookOf(entry); render() }, {
344
436
  icon: 'fa-solid fa-arrow-rotate-left',
345
437
  disabled: picked.length === 0,
346
438
  })))
@@ -348,24 +440,26 @@
348
440
  const messageInput = h('input', {
349
441
  type: 'text',
350
442
  class: 'git-input',
443
+ id: 'git-commit-message',
351
444
  value: view.message,
352
445
  placeholder: 'Commit message',
353
446
  'aria-label': 'Commit message',
354
447
  oninput: (event) => {
355
448
  view.message = event.target.value
356
449
  view.messageEdited = true
450
+ view.ticked = new Set(picked.map(keyOf))
357
451
  },
358
452
  })
359
- const commit = () => {
360
- const message = view.message.trim() || suggestMessage(picked)
361
- const params = { message }
453
+ const commit = () => act(`Committing ${picked.length} file${picked.length === 1 ? '' : 's'}`, entry, (now) => {
454
+ const params = { message: view.message.trim() || suggestMessage(picked) }
362
455
  if (tracked.length) params.paths = tracked.map(shellQuote).join(' ')
363
456
  if (untracked.length) params.untracked_paths = untracked.map(shellQuote).join(' ')
364
- state.changes = null
365
- run(`Committing ${picked.length} file${picked.length === 1 ? '' : 's'}`, callbackUrl('commit_files', cwdOf(entry), params))
366
- }
457
+ // the message is used up
458
+ view.message = ''
459
+ view.messageEdited = false
460
+ return callbackUrl('commit_files', cwdOf(now), params)
461
+ })
367
462
  return h('div', { class: 'git-changes' },
368
- view.error ? h('div', { class: 'git-error', text: view.error }) : null,
369
463
  h('div', { class: 'git-split' },
370
464
  files,
371
465
  view.open >= 0 && changes[view.open] ? diffViewer(changes[view.open], entry.repoParam) : h('div', { class: 'git-empty', text: 'Select a file to view its changes' })),
@@ -377,10 +471,11 @@
377
471
  // ---------------------------------------------------------------- History
378
472
  const commitView = (entry) => {
379
473
  const view = state.commit
380
- const checkout = () => {
474
+ const checkout = () => act(`Checking out ${view.oid.slice(0, 7)}`, entry, async (now) => {
475
+ const cwd = await folderOf(now, false)
381
476
  state.commit = null
382
- run(`Checking out ${view.oid.slice(0, 7)}`, callbackUrl('checkout', view.cwd, { commit: view.oid }))
383
- }
477
+ return callbackUrl('checkout', cwd, { commit: view.oid })
478
+ })
384
479
  return h('div', { class: 'git-commit' },
385
480
  h('div', { class: 'git-commit-header' },
386
481
  button('History', () => { state.commit = null; render() }, { icon: 'fa-solid fa-arrow-left' }),
@@ -400,8 +495,8 @@
400
495
  h('span', { class: 'git-file-status', text: change.status })))),
401
496
  diffViewer(view.changes[view.open], entry.repoParam)))
402
497
  }
403
- const openCommit = (entry, info, item) => {
404
- const view = { oid: item.oid, title: String(item.commit.message || '').split('\n')[0], changes: null, open: 0, cwd: info.dir || cwdOf(entry) }
498
+ const openCommit = (entry, item) => {
499
+ const view = { key: entry.repoParam, oid: item.oid, title: String(item.commit.message || '').split('\n')[0], changes: null, open: 0 }
405
500
  state.commit = view
406
501
  render()
407
502
  getJson(item.info)
@@ -410,20 +505,20 @@
410
505
  .then(() => { if (state.commit === view) render() })
411
506
  }
412
507
  const historySection = (entry) => {
413
- if (state.commit) return commitView(entry)
508
+ if (state.commit && state.commit.key === entry.repoParam) return commitView(entry)
414
509
  const info = infoOf(entry.repoParam)
415
510
  if (!info) return h('div', { class: 'git-empty', text: 'Loading history...' })
416
511
  if (info.failed) return h('div', { class: 'git-empty', text: 'Could not load the history' })
417
512
  const commits = Array.isArray(info.log) ? info.log : []
418
- const cwd = info.dir || cwdOf(entry)
419
513
  const branches = (Array.isArray(info.branches) ? info.branches : [])
420
514
  .map((item) => item && item.branch)
421
515
  .filter((name) => typeof name === 'string' && name && !isOid(name))
422
516
  const current = typeof info.branch === 'string' && !isOid(info.branch) && info.branch !== 'HEAD' ? info.branch : null
423
- const checkout = (target) => run(`Checking out ${target}`, callbackUrl('checkout', cwd, { commit: target }))
517
+ const checkout = (target) => act(`Checking out ${target}`, entry, async (now) => callbackUrl('checkout', await folderOf(now, false), { commit: target }))
424
518
  const newest = current || ['main', 'master', 'develop', 'dev'].find((name) => branches.includes(name)) || branches[0] || null
425
519
  const select = branches.length
426
- ? h('select', { class: 'git-input git-input--select', 'aria-label': 'Select branch' },
520
+ ? h('select', { class: 'git-input git-input--select', 'aria-label': 'Switch branch', title: 'Switch branch', onchange: (event) => checkout(event.target.value) },
521
+ branches.includes(current) ? null : h('option', { value: '', selected: true, disabled: true, text: 'Switch branch...' }),
427
522
  branches.map((name) => h('option', { value: name, selected: name === current, text: name })))
428
523
  : null
429
524
  const web = remoteWebUrl(info.remote)
@@ -437,14 +532,14 @@
437
532
  web ? h('a', { class: 'git-link', href: web, text: web.replace(/^https?:\/\//, ''), onclick: (event) => { event.preventDefault(); openExternal(web) } }) : null),
438
533
  h('div', { class: 'git-history-viewing', text: `Currently viewing ${current || info.ref || 'HEAD'}` }),
439
534
  button('Return to newest commit', () => checkout(newest), { icon: 'fa-solid fa-arrow-rotate-left', disabled: !newest }),
440
- select,
441
- select ? button('Switch', () => checkout(select.value), { icon: 'fa-solid fa-code-branch' }) : null),
535
+ select),
442
536
  commits.length === 0
443
537
  ? h('div', { class: 'git-empty', text: info.logError ? 'No commits yet' : 'No commits' })
444
538
  : h('div', { class: 'git-commits' }, commits.map((item) => h('button', {
445
539
  type: 'button',
446
540
  class: 'git-commit-row',
447
- onclick: () => openCommit(entry, info, item),
541
+ title: String(item.commit.message || '').trim(),
542
+ onclick: () => openCommit(entry, item),
448
543
  },
449
544
  h('div', { class: 'git-commit-message', text: String(item.commit.message || '').split('\n')[0] }),
450
545
  h('div', { class: 'git-commit-meta' },
@@ -457,19 +552,25 @@
457
552
  const loginNotice = (purpose) => notice(
458
553
  'Log in to GitHub',
459
554
  `Connect your GitHub account to ${purpose}.`,
460
- button('Log in', () => { (window.parent || window).location.href = '/github' }, { primary: true, icon: 'fa-brands fa-github' }))
555
+ button('Log in', () => { window.parent.location.href = '/github' }, { primary: true, icon: 'fa-brands fa-github' }))
556
+
557
+ // whether GitHub is connected could not be found out
558
+ const unknownNotice = (entry) => notice('Unable to check repository state', 'The request for it failed.',
559
+ button('Try again', () => { state.info.delete(entry.repoParam); render() }, { primary: true }))
461
560
 
462
561
  const forkSection = (entry) => {
463
562
  const info = infoOf(entry.repoParam)
464
563
  if (!info) return h('div', { class: 'git-empty', text: 'Loading...' })
564
+ if (info.failed) return unknownNotice(entry)
465
565
  if (!info.connected) return loginNotice('fork this repository')
466
566
  if (!hasRemote(entry)) {
467
567
  return notice('Remote required', 'This repository does not have a remote configured, so it cannot be forked.')
468
568
  }
469
- const name = h('input', { type: 'text', class: 'git-input', value: forkDefaultName(entry), id: 'git-fork-name' })
470
- const org = h('input', { type: 'text', class: 'git-input', placeholder: 'Organization name', 'aria-label': 'Organization name' })
471
- const orgRow = h('div', { class: 'git-field', hidden: true }, org)
472
- const toOrg = h('input', { type: 'checkbox', onchange: (event) => { orgRow.hidden = !event.target.checked; if (event.target.checked) org.focus() } })
569
+ const form = formOf(entry)
570
+ const name = h('input', { type: 'text', class: 'git-input', id: 'git-fork-name', value: typeof form.forkName === 'string' ? form.forkName : forkDefaultName(entry), oninput: (event) => { form.forkName = event.target.value } })
571
+ const org = h('input', { type: 'text', class: 'git-input', id: 'git-fork-org', value: form.org || '', placeholder: 'Organization name', 'aria-label': 'Organization name', oninput: (event) => { form.org = event.target.value } })
572
+ const orgRow = h('div', { class: 'git-field', hidden: !form.toOrg }, org)
573
+ const toOrg = h('input', { type: 'checkbox', checked: Boolean(form.toOrg), onchange: (event) => { form.toOrg = event.target.checked; orgRow.hidden = !form.toOrg; if (form.toOrg) org.focus() } })
473
574
  const error = h('div', { class: 'git-error', hidden: true })
474
575
  const submit = () => {
475
576
  const forkName = name.value.trim()
@@ -481,7 +582,7 @@
481
582
  }
482
583
  const params = { name: forkName, ts: Date.now() }
483
584
  if (orgName) params.org = orgName
484
- run(`Forking ${entry.name} as ${forkName}`, scriptUrl('fork', cwdOf(entry), params))
585
+ act(`Forking ${entry.name} as ${forkName}`, entry, (now) => scriptUrl('fork', cwdOf(now), params))
485
586
  }
486
587
  return h('div', { class: 'git-form' },
487
588
  h('div', { class: 'git-form-title', text: 'Fork repository' }),
@@ -497,27 +598,32 @@
497
598
  const publishSection = (entry) => {
498
599
  const info = infoOf(entry.repoParam)
499
600
  if (!info) return h('div', { class: 'git-empty', text: 'Loading...' })
500
- if (info.failed) return notice('Unable to check repository state', 'Refresh and try again.')
601
+ if (info.failed) return unknownNotice(entry)
602
+ // nothing is offered for publishing until the files have been checked as they are now
603
+ if (state.checking) return h('div', { class: 'git-empty', text: 'Checking for changes...' })
501
604
  if (!info.connected) return loginNotice('publish this workspace')
502
- const review = button('Review files', () => { state.section = 'changes'; render() }, { primary: true })
503
- if (info.gitDirExists === false) {
504
- return notice(`${entry.name} isn't a Git repository yet`, 'Initialize git in this folder and save a version before publishing.', review)
605
+ if (entry.noRepo || info.gitDirExists === false) {
606
+ return notice(`${entry.name} isn't a Git repository yet`, 'Initialize git in this folder and save a version before publishing.')
505
607
  }
608
+ const review = button('Review files', () => { state.section = 'changes'; render() }, { primary: true })
506
609
  const hasHead = typeof info.hasHead === 'boolean' ? info.hasHead : (Array.isArray(info.log) && info.log.length > 0)
507
610
  if (!hasHead) {
508
611
  return notice(`Create your first commit for ${entry.name}`, 'Save a version on the main branch before publishing to GitHub.', review)
509
612
  }
510
613
  const count = Number(entry.changeCount) || 0
511
- if (count > 0 && !state.publishSkipCommit.has(entry.repoParam)) {
614
+ // "Continue without committing" holds for one publish, of the files exactly as they were when it was pressed
615
+ const consented = state.consent && state.consent.key === entry.repoParam && state.consent.seen === lookOf(entry)
616
+ if (count > 0 && !consented) {
512
617
  return notice(
513
618
  `${count} uncommitted change${count === 1 ? '' : 's'}`,
514
619
  'Commit them first if you want them included in what you publish.',
515
620
  button('Review and commit', () => { state.section = 'changes'; render() }, { primary: true }),
516
- button('Continue without committing', () => { state.publishSkipCommit.add(entry.repoParam); render() }))
621
+ button('Continue without committing', () => { state.consent = { key: entry.repoParam, seen: lookOf(entry) }; render() }))
517
622
  }
518
623
  if (!hasRemote(entry)) {
519
- const name = h('input', { type: 'text', class: 'git-input', placeholder: 'Enter repository name', id: 'git-create-name' })
520
- const isPrivate = h('input', { type: 'checkbox' })
624
+ const form = formOf(entry)
625
+ const name = h('input', { type: 'text', class: 'git-input', id: 'git-create-name', value: form.createName || '', placeholder: 'Enter repository name', oninput: (event) => { form.createName = event.target.value } })
626
+ const isPrivate = h('input', { type: 'checkbox', checked: Boolean(form.isPrivate), onchange: (event) => { form.isPrivate = event.target.checked } })
521
627
  const error = h('div', { class: 'git-error', hidden: true, text: 'Repository name is required' })
522
628
  const submit = () => {
523
629
  const repoName = name.value.trim()
@@ -525,7 +631,8 @@
525
631
  error.hidden = false
526
632
  return
527
633
  }
528
- run(`Creating ${repoName} on GitHub`, scriptUrl('create', cwdOf(entry), { name: repoName, visibility: isPrivate.checked ? 'private' : 'public' }))
634
+ const visibility = isPrivate.checked ? 'private' : 'public'
635
+ act(`Creating ${repoName} on GitHub`, entry, (now) => scriptUrl('create', cwdOf(now), { name: repoName, visibility }))
529
636
  }
530
637
  return h('div', { class: 'git-form' },
531
638
  h('div', { class: 'git-form-title', text: 'Create a GitHub repository' }),
@@ -542,22 +649,26 @@
542
649
  h('div', { class: 'git-form-remote', text: entry.url }),
543
650
  h('div', { class: 'git-notice-text', text: 'Pushes your committed changes to this remote.' }),
544
651
  h('div', { class: 'git-form-actions' },
545
- button('Publish', () => run(`Publishing ${entry.name}`, scriptUrl('push', cwdOf(entry), { ts: Date.now() })), { primary: true, icon: 'fa-brands fa-github' }),
652
+ button('Publish', () => act(`Publishing ${entry.name}`, entry, async (now) => scriptUrl('push', await folderOf(now, true), { ts: Date.now() })), { primary: true, icon: 'fa-brands fa-github' }),
546
653
  web ? button('View on GitHub', () => openExternal(web)) : null))
547
654
  }
548
655
 
549
656
  // ----------------------------------------------------------------- render
550
657
  const sectionBody = (entry) => {
551
- if (state.run) return runPanel()
658
+ if (state.work) return workPanel()
552
659
  if (state.section === 'history') return historySection(entry)
553
660
  if (state.section === 'fork') return forkSection(entry)
554
661
  if (state.section === 'publish') return publishSection(entry)
555
662
  return changesSection(entry)
556
663
  }
557
664
  const render = () => {
558
- // a field being typed in keeps its text and the cursor across a redraw
665
+ // a running script's terminal is left alone: drawing it again would start the script again
666
+ if (state.work && state.work.url && root.querySelector('.git-run-frame')) return
667
+ // the field being typed in keeps the focus and the cursor across a redraw (its text is in the state)
559
668
  const active = document.activeElement
560
- const typing = active && root.contains(active) && active.tagName === 'INPUT' && active.type === 'text' ? active : null
669
+ const typing = active && root.contains(active) && active.id && active.type === 'text'
670
+ ? { id: active.id, start: active.selectionStart, end: active.selectionEnd }
671
+ : null
561
672
  const entry = repo()
562
673
  const checked = state.checking
563
674
  ? 'Checking...'
@@ -567,21 +678,25 @@
567
678
  const side = h('aside', { class: 'git-side' },
568
679
  h('div', { class: 'git-side-top' },
569
680
  h('div', { class: 'git-checked', text: checked }),
570
- h('button', { type: 'button', class: 'git-btn git-btn--icon', title: 'Refresh', 'aria-label': 'Refresh', disabled: Boolean(state.checking), onclick: () => { check() } }, icon('fa-solid fa-rotate'))),
681
+ h('button', { type: 'button', class: 'git-btn git-btn--icon', title: 'Refresh', 'aria-label': 'Refresh', disabled: Boolean(state.checking || state.work), onclick: () => { state.notice = ''; check() } }, icon('fa-solid fa-rotate'))),
571
682
  state.repos.map((item) => {
572
683
  const count = Number(item.changeCount) || 0
573
684
  return h('button', {
574
685
  type: 'button',
575
686
  class: `git-repo${item.repoParam === state.repoKey ? ' git-repo--selected' : ''}`,
576
687
  onclick: () => {
577
- if (state.run) return
688
+ if (state.work) return
578
689
  state.repoKey = item.repoParam
579
690
  state.commit = null
691
+ state.notice = ''
580
692
  render()
581
693
  },
582
694
  },
583
695
  h('div', { class: 'git-repo-name', text: item.name, title: item.name }),
584
- h('div', { class: `git-repo-status ${count > 0 ? 'git-repo-status--dirty' : 'git-repo-status--clean'}`, text: count > 0 ? `${count} change${count === 1 ? '' : 's'}` : 'Clean' }))
696
+ h('div', {
697
+ class: `git-repo-status ${count > 0 || item.error || item.noRepo ? 'git-repo-status--dirty' : 'git-repo-status--clean'}`,
698
+ text: item.noRepo ? 'Not a repository' : item.error ? 'Could not check' : count > 0 ? `${count} change${count === 1 ? '' : 's'}` : 'Clean',
699
+ }))
585
700
  }))
586
701
  let main
587
702
  if (!entry) {
@@ -599,25 +714,30 @@
599
714
  role: 'tab',
600
715
  class: `git-tab${state.section === name ? ' git-tab--selected' : ''}`,
601
716
  'aria-selected': state.section === name ? 'true' : 'false',
602
- disabled: Boolean(state.run),
717
+ disabled: Boolean(state.work),
603
718
  text: name.charAt(0).toUpperCase() + name.slice(1),
604
719
  onclick: () => {
605
720
  state.section = name
606
721
  state.commit = null
607
- render()
722
+ state.notice = ''
723
+ state.consent = null
724
+ if (name === 'publish') {
725
+ // what gets published depends on the files as they are now
726
+ check()
727
+ } else {
728
+ render()
729
+ }
608
730
  },
609
731
  }))),
610
- h('div', { class: 'git-section' }, state.error ? h('div', { class: 'git-error', text: state.error }) : null, sectionBody(entry)))
732
+ h('div', { class: 'git-section' },
733
+ state.notice || state.error ? h('div', { class: 'git-error', text: state.notice || state.error }) : null,
734
+ sectionBody(entry)))
611
735
  }
612
- const typedValue = typing ? { id: typing.id, value: typing.value, start: typing.selectionStart, end: typing.selectionEnd } : null
613
736
  root.replaceChildren(side, h('main', { class: 'git-main' }, main))
614
- if (typedValue) {
615
- const again = typedValue.id ? document.getElementById(typedValue.id) : root.querySelector('.git-commit-bar .git-input')
616
- if (again) {
617
- again.value = typedValue.value
618
- again.focus()
619
- try { again.setSelectionRange(typedValue.start, typedValue.end) } catch (_) {}
620
- }
737
+ const again = typing && document.getElementById(typing.id)
738
+ if (again) {
739
+ again.focus()
740
+ try { again.setSelectionRange(typing.start, typing.end) } catch (_) {}
621
741
  }
622
742
  }
623
743