studio-engine 0.143.2 → 0.144.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -0
- data/Gemfile +7 -0
- data/README.md +66 -12
- data/app/assets/tailwind/studio_engine/engine.css +9 -0
- data/app/assets/tailwind/studio_engine/host-variant-25559-c64c1299.css +56 -0
- data/app/components/previews/studio/button_component_preview.rb +2 -2
- data/app/components/previews/studio/table_component_preview/rows.html.erb +1 -1
- data/app/components/studio/button_component.rb +13 -4
- data/app/components/studio/table_component.html.erb +3 -2
- data/app/components/studio/table_component.rb +39 -5
- data/app/controllers/studio/modals_controller.rb +32 -1
- data/app/javascript/studio/modal_host.js +291 -32
- data/app/views/studio/modals/_host.html.erb +23 -8
- data/app/views/style/_primitives.html.erb +3 -2
- data/lib/studio/modals.rb +57 -5
- data/lib/studio/system_test_case.rb +236 -0
- data/lib/studio/version.rb +1 -1
- metadata +4 -2
|
@@ -306,15 +306,50 @@ function focusTrap(env, held) {
|
|
|
306
306
|
// 'error' (the host's error card) and forgets the fetch, so the next open
|
|
307
307
|
// retries. So for the first LOAD_DELAY_MS of a first open, isOpen(id) is false.
|
|
308
308
|
//
|
|
309
|
-
// THE
|
|
310
|
-
//
|
|
309
|
+
// THE TIMEOUT. A fetch that has not answered in cfg.timeout_ms (the server's
|
|
310
|
+
// Studio.modals.fetch_timeout_ms, else DEFAULT_FETCH_TIMEOUT_MS) is aborted and
|
|
311
|
+
// shows the error card. Its Try again (retryRemote) re-runs the fetch with the
|
|
312
|
+
// card in place; its Close calls close() directly, so a card opened
|
|
313
|
+
// dismissible: false still has a way out.
|
|
314
|
+
//
|
|
315
|
+
// THE CACHE: fragments by id, beside the store, not on it, keyed on the
|
|
316
|
+
// session. A Turbo visit brings a fresh host without the inserted templates
|
|
311
317
|
// (or a snapshot clone with them), so insertion checks the host every time and
|
|
312
318
|
// inserts only what is missing; that check is what keeps a Turbo visit from
|
|
313
319
|
// inserting a modal twice. A fetch that resolves after a Turbo visit replaced
|
|
314
320
|
// the body (doc.body changed) opens nothing on the new page.
|
|
321
|
+
//
|
|
322
|
+
// THE SESSION KEY. A fragment can carry the viewer's data and a form's CSRF
|
|
323
|
+
// token, and a Turbo visit across sign-in, sign-out or a user switch keeps the
|
|
324
|
+
// document, and with it this cache. So the host advertises cfg.session, a
|
|
325
|
+
// digest of the session id and the signed-in user id (Studio::Modals.
|
|
326
|
+
// session_marker), and every open and every turbo:load compares it: a change
|
|
327
|
+
// drops every cached fragment, every fetch in flight, and every template
|
|
328
|
+
// already inserted on this page, except the template of a modal open now,
|
|
329
|
+
// which goes when it closes (a card is never emptied in place).
|
|
330
|
+
// studio/session's session:changed drops them too, but only on a real
|
|
331
|
+
// identity change (sessionIdentityChange), never on the first seating or a
|
|
332
|
+
// stale -> authenticated revalidation. Why that signal: the session id is what
|
|
333
|
+
// reset_session rotates, and the CSRF secret rotates with it, so a cached form
|
|
334
|
+
// token goes stale exactly when it changes; the user id catches a sign-in
|
|
335
|
+
// that keeps the session. The page's csrf-token meta would not do: it is
|
|
336
|
+
// masked afresh on every render, so it would empty the cache on every visit.
|
|
337
|
+
//
|
|
338
|
+
// THE SCRIPT RULE. A fragment containing a <script> is refused, never
|
|
339
|
+
// inserted: an inserted script would run (or not) by accident of how Alpine
|
|
340
|
+
// clones the template, and it carries no CSP nonce. It shows the error card;
|
|
341
|
+
// with cfg.debug (development and test) the card names the reason. There is
|
|
342
|
+
// no console output in either case: the endpoint records it server-side in an
|
|
343
|
+
// ErrorLog row (Studio::ModalsController).
|
|
344
|
+
//
|
|
345
|
+
// PREFETCH runs when the browser idles after the first load AND after every
|
|
346
|
+
// Turbo visit (turbo:load), so a page reached by Turbo prefetches what it
|
|
347
|
+
// flags, for the viewer it was rendered for.
|
|
315
348
|
|
|
316
349
|
export const REMOTE_MODAL_ATTR = 'data-studio-remote-modal'
|
|
317
350
|
export const LOAD_DELAY_MS = 150
|
|
351
|
+
export const DEFAULT_FETCH_TIMEOUT_MS = 8000
|
|
352
|
+
export const REMOTE_ERROR_TEXT = 'Check your connection and try again.'
|
|
318
353
|
|
|
319
354
|
// The host template advertising on-demand modals, if this page has one.
|
|
320
355
|
export function remoteHost(doc) {
|
|
@@ -329,6 +364,8 @@ export function remoteConfig(doc) {
|
|
|
329
364
|
var cfg = JSON.parse(el.getAttribute('data-modal-host-remote-value'))
|
|
330
365
|
if (!cfg || typeof cfg.url !== 'string' || !Array.isArray(cfg.ids)) return null
|
|
331
366
|
if (!Array.isArray(cfg.prefetch)) cfg.prefetch = []
|
|
367
|
+
if (!(cfg.timeout_ms > 0)) cfg.timeout_ms = DEFAULT_FETCH_TIMEOUT_MS
|
|
368
|
+
if (typeof cfg.session !== 'string') cfg.session = ''
|
|
332
369
|
return cfg
|
|
333
370
|
} catch (e) {
|
|
334
371
|
return null
|
|
@@ -381,18 +418,61 @@ export function remoteModalUrl(cfg, id) {
|
|
|
381
418
|
return cfg.url.replace('__id__', encodeURIComponent(id))
|
|
382
419
|
}
|
|
383
420
|
|
|
421
|
+
// A refused fragment's error. Its message is shown only with cfg.debug.
|
|
422
|
+
function remoteError(message) {
|
|
423
|
+
var err = new Error(message)
|
|
424
|
+
err.studioModal = true
|
|
425
|
+
return err
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// True when the markup carries a <script> element. A text match, not a parse:
|
|
429
|
+
// the fragment is server-rendered, so this guards an app's own mistake, and a
|
|
430
|
+
// false positive (the word in an attribute) errs on the safe side.
|
|
431
|
+
export function fragmentHasScript(markup) {
|
|
432
|
+
return /<script[\s>\/]/i.test(String(markup))
|
|
433
|
+
}
|
|
434
|
+
|
|
384
435
|
// One fetch. Rejects unless the answer is the fragment the endpoint sends
|
|
385
436
|
// (X-Studio-Modal names the id): a host filter's redirect to a full page, or a
|
|
386
|
-
// 401, is an error, never a modal.
|
|
437
|
+
// 401, is an error, never a modal. Rejects too when nothing answers inside
|
|
438
|
+
// cfg.timeout_ms (the request is aborted where the browser can), and when the
|
|
439
|
+
// fragment carries a <script>.
|
|
387
440
|
export function fetchRemoteModal(win, cfg, id) {
|
|
388
|
-
|
|
441
|
+
var ms = cfg.timeout_ms > 0 ? cfg.timeout_ms : DEFAULT_FETCH_TIMEOUT_MS
|
|
442
|
+
var controller = typeof win.AbortController === 'function' ? new win.AbortController() : null
|
|
443
|
+
var init = {
|
|
389
444
|
credentials: 'same-origin',
|
|
390
445
|
headers: { 'Accept': 'text/html', 'X-Requested-With': 'XMLHttpRequest' }
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
|
|
446
|
+
}
|
|
447
|
+
if (controller) init.signal = controller.signal
|
|
448
|
+
|
|
449
|
+
return new Promise(function (resolve, reject) {
|
|
450
|
+
var settled = false
|
|
451
|
+
var timer = win.setTimeout(function () {
|
|
452
|
+
if (settled) return
|
|
453
|
+
settled = true
|
|
454
|
+
if (controller) { try { controller.abort() } catch (e) {} }
|
|
455
|
+
reject(remoteError('studio/modal_host: ' + id + ' did not answer in ' + ms + 'ms'))
|
|
456
|
+
}, ms)
|
|
457
|
+
function finish(fn, value) {
|
|
458
|
+
if (settled) return
|
|
459
|
+
settled = true
|
|
460
|
+
if (win.clearTimeout) win.clearTimeout(timer)
|
|
461
|
+
fn(value)
|
|
394
462
|
}
|
|
395
|
-
|
|
463
|
+
|
|
464
|
+
win.fetch(remoteModalUrl(cfg, id), init).then(function (res) {
|
|
465
|
+
if (!res.ok || !res.headers || res.headers.get('X-Studio-Modal') !== id) {
|
|
466
|
+
throw remoteError('studio/modal_host: ' + id + ' answered ' + res.status)
|
|
467
|
+
}
|
|
468
|
+
return res.text()
|
|
469
|
+
}).then(function (markup) {
|
|
470
|
+
if (fragmentHasScript(markup)) {
|
|
471
|
+
throw remoteError('studio/modal_host: ' + id + ' was refused: its fragment carries a <script>. ' +
|
|
472
|
+
'Move the behaviour to a module or a Stimulus controller.')
|
|
473
|
+
}
|
|
474
|
+
finish(resolve, markup)
|
|
475
|
+
}).catch(function (err) { finish(reject, err) })
|
|
396
476
|
})
|
|
397
477
|
}
|
|
398
478
|
|
|
@@ -402,21 +482,142 @@ export function whenIdle(win, fn) {
|
|
|
402
482
|
else win.setTimeout(fn, 1500)
|
|
403
483
|
}
|
|
404
484
|
|
|
405
|
-
//
|
|
406
|
-
|
|
485
|
+
// Removes the inserted registrations from this page's cards (the session
|
|
486
|
+
// changed, so what they carry is stale), except those whose id is in keep:
|
|
487
|
+
// the modals on the stack now, which must never be emptied in place. Answers
|
|
488
|
+
// the ids it kept.
|
|
489
|
+
function removeRemoteModals(doc, keep) {
|
|
490
|
+
keep = keep || []
|
|
491
|
+
var kept = []
|
|
492
|
+
var cards = remoteCards(doc)
|
|
493
|
+
for (var i = 0; i < cards.length; i++) {
|
|
494
|
+
var kids = Array.prototype.slice.call(cards[i].children || [])
|
|
495
|
+
for (var k = 0; k < kids.length; k++) {
|
|
496
|
+
var kidId = kids[k].getAttribute ? kids[k].getAttribute(REMOTE_MODAL_ATTR) : null
|
|
497
|
+
if (kidId !== null && keep.indexOf(kidId) !== -1) {
|
|
498
|
+
if (kept.indexOf(kidId) === -1) kept.push(kidId)
|
|
499
|
+
continue
|
|
500
|
+
}
|
|
501
|
+
if (kidId !== null) {
|
|
502
|
+
if (typeof cards[i].removeChild === 'function') cards[i].removeChild(kids[k])
|
|
503
|
+
else if (typeof kids[k].remove === 'function') kids[k].remove()
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return kept
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// THE SETTLED STATES of studio/session (docs/SESSION_DRIFT.md). A settled
|
|
511
|
+
// event whose fingerprint (user + bound identities) differs from the last is an
|
|
512
|
+
// identity change; rehydrated is one ("possibly as someone new"); stale is not.
|
|
513
|
+
var SETTLED_SESSION_STATES = ['anonymous', 'authenticated', 'signed_out', 'changed', 'rehydrated']
|
|
514
|
+
|
|
515
|
+
// Is this session:changed detail a REAL change of who is viewing? studio/session
|
|
516
|
+
// sends the event on every transition, including the first seating on page
|
|
517
|
+
// load (previous null) and stale -> authenticated when a hidden tab returns,
|
|
518
|
+
// and neither may throw the cache away. lastIdentity is the identity string
|
|
519
|
+
// the previous settled event carried (null before the first). Answers
|
|
520
|
+
// { real, identity }: identity is the string to remember.
|
|
521
|
+
export function sessionIdentityChange(detail, lastIdentity) {
|
|
522
|
+
var d = detail || {}
|
|
523
|
+
var snap = d.snapshot || {}
|
|
524
|
+
var identity = snap.state === 'anonymous' || snap.state === 'signed_out' || d.state === 'anonymous' || d.state === 'signed_out'
|
|
525
|
+
? 'anonymous'
|
|
526
|
+
: (snap.fingerprint || JSON.stringify(snap.identities || {}))
|
|
527
|
+
// First seating, or the store has no state yet: remember, never drop.
|
|
528
|
+
if (d.previous === null || d.previous === undefined || !d.state) return { real: false, identity: identity }
|
|
529
|
+
// Someone else observed (changed) or the server reports nobody (signed_out).
|
|
530
|
+
if (d.state === 'changed' || (d.state === 'signed_out' && d.previous !== 'signed_out')) {
|
|
531
|
+
return { real: true, identity: identity }
|
|
532
|
+
}
|
|
533
|
+
// A settled state with a different identity than the last one seen.
|
|
534
|
+
if (SETTLED_SESSION_STATES.indexOf(d.state) !== -1 && lastIdentity !== null && identity !== lastIdentity) {
|
|
535
|
+
return { real: true, identity: identity }
|
|
536
|
+
}
|
|
537
|
+
// stale, or a return to the same identity: revalidation.
|
|
538
|
+
return { real: false, identity: SETTLED_SESSION_STATES.indexOf(d.state) !== -1 ? identity : lastIdentity }
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// The loader one shared store keeps in its closure. openIds() answers the ids
|
|
542
|
+
// on the store's stack now: their templates survive a drop until they close.
|
|
543
|
+
export function createRemoteModals(env, openIds) {
|
|
407
544
|
var doc = env.doc
|
|
408
545
|
var win = env.win
|
|
546
|
+
var openNowIds = typeof openIds === 'function' ? openIds : function () { return [] }
|
|
547
|
+
var staleOpen = [] // ids whose stale template was kept because they were open
|
|
409
548
|
var pending = {} // id -> Promise<markup>, while in flight or resolved
|
|
410
549
|
var ready = {} // id -> markup, once resolved
|
|
550
|
+
var session = null // the cfg.session the cache was filled under
|
|
551
|
+
var generation = 0 // bumped by a drop, so a fetch from before it caches nothing
|
|
552
|
+
|
|
553
|
+
// Drops the cache at once. The templates go too, except those of modals on
|
|
554
|
+
// the stack now: a card is never emptied in place (a dismissible: false one
|
|
555
|
+
// would leave the viewer with no way out). Those go when they close (flush).
|
|
556
|
+
function drop() {
|
|
557
|
+
pending = {}
|
|
558
|
+
ready = {}
|
|
559
|
+
generation++
|
|
560
|
+
staleOpen = removeRemoteModals(doc, openNowIds())
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// Removes each kept stale template whose modal has left the stack. Called
|
|
564
|
+
// whenever the stack changes.
|
|
565
|
+
function flush() {
|
|
566
|
+
if (staleOpen.length === 0) return
|
|
567
|
+
var open = openNowIds()
|
|
568
|
+
var keep = staleOpen.filter(function (id) { return open.indexOf(id) !== -1 })
|
|
569
|
+
if (keep.length === staleOpen.length) return
|
|
570
|
+
// Only the kept ids are stale; every other inserted template is fresh.
|
|
571
|
+
var gone = staleOpen.filter(function (id) { return keep.indexOf(id) === -1 })
|
|
572
|
+
var cards = remoteCards(doc)
|
|
573
|
+
for (var i = 0; i < cards.length; i++) {
|
|
574
|
+
var kids = Array.prototype.slice.call(cards[i].children || [])
|
|
575
|
+
for (var k = 0; k < kids.length; k++) {
|
|
576
|
+
var kidId = kids[k].getAttribute ? kids[k].getAttribute(REMOTE_MODAL_ATTR) : null
|
|
577
|
+
if (kidId !== null && gone.indexOf(kidId) !== -1) {
|
|
578
|
+
if (typeof cards[i].removeChild === 'function') cards[i].removeChild(kids[k])
|
|
579
|
+
else if (typeof kids[k].remove === 'function') kids[k].remove()
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
staleOpen = keep
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Drops the cache when the advertised session differs from the one it was
|
|
587
|
+
// filled under. The first config seen only sets the key.
|
|
588
|
+
function sync(cfg) {
|
|
589
|
+
if (!cfg) return
|
|
590
|
+
if (session === null) { session = cfg.session; return }
|
|
591
|
+
if (cfg.session === session) return
|
|
592
|
+
session = cfg.session
|
|
593
|
+
drop()
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// studio/session's session:changed drops the cache too, for a change that
|
|
597
|
+
// happens without a page render to carry a new marker, but ONLY on a real
|
|
598
|
+
// identity change (sessionIdentityChange): the event also fires on the
|
|
599
|
+
// first seating at every load and on stale -> authenticated revalidation,
|
|
600
|
+
// which must keep a finished prefetch and an open card. The host's marker
|
|
601
|
+
// (sync, on every open and turbo:load) stays the primary signal.
|
|
602
|
+
var lastIdentity = null
|
|
603
|
+
if (doc && typeof doc.addEventListener === 'function') {
|
|
604
|
+
doc.addEventListener('session:changed', function (event) {
|
|
605
|
+
var verdict = sessionIdentityChange(event && event.detail, lastIdentity)
|
|
606
|
+
lastIdentity = verdict.identity
|
|
607
|
+
if (verdict.real) drop()
|
|
608
|
+
})
|
|
609
|
+
}
|
|
411
610
|
|
|
412
611
|
function load(cfg, id) {
|
|
612
|
+
sync(cfg)
|
|
413
613
|
if (ready[id] !== undefined) return Promise.resolve(ready[id])
|
|
414
614
|
if (!pending[id]) {
|
|
415
|
-
|
|
416
|
-
|
|
615
|
+
var gen = generation
|
|
616
|
+
var mine = pending[id] = fetchRemoteModal(win, cfg, id).then(function (markup) {
|
|
617
|
+
if (gen === generation) ready[id] = markup
|
|
417
618
|
return markup
|
|
418
619
|
}, function (err) {
|
|
419
|
-
delete pending[id]
|
|
620
|
+
if (pending[id] === mine) delete pending[id]
|
|
420
621
|
throw err
|
|
421
622
|
})
|
|
422
623
|
}
|
|
@@ -424,10 +625,11 @@ export function createRemoteModals(env) {
|
|
|
424
625
|
}
|
|
425
626
|
|
|
426
627
|
return {
|
|
427
|
-
// The config when id is advertised here, else null.
|
|
628
|
+
// The config when id is advertised here, else null. Checks the session.
|
|
428
629
|
handles: function (id) {
|
|
429
630
|
if (typeof id !== 'string') return null
|
|
430
631
|
var cfg = remoteConfig(doc)
|
|
632
|
+
sync(cfg)
|
|
431
633
|
return (cfg && cfg.ids.indexOf(id) !== -1) ? cfg : null
|
|
432
634
|
},
|
|
433
635
|
load: load,
|
|
@@ -437,8 +639,10 @@ export function createRemoteModals(env) {
|
|
|
437
639
|
return ready[id] !== undefined && insertRemoteModal(doc, id, ready[id])
|
|
438
640
|
},
|
|
439
641
|
insert: function (id, markup) { return insertRemoteModal(doc, id, markup) },
|
|
642
|
+
flush: flush,
|
|
440
643
|
prefetch: function () {
|
|
441
644
|
var cfg = remoteConfig(doc)
|
|
645
|
+
sync(cfg)
|
|
442
646
|
if (!cfg || typeof win.fetch !== 'function') return
|
|
443
647
|
cfg.prefetch.forEach(function (id) {
|
|
444
648
|
// A failed prefetch is retried by the open that needs it.
|
|
@@ -460,7 +664,19 @@ export function createModalStore(env) {
|
|
|
460
664
|
// id -> 'loading' | 'error' for an on-demand modal; read by the host's
|
|
461
665
|
// loading and error cards and by each fetched registration's x-if.
|
|
462
666
|
store._remote = {}
|
|
463
|
-
|
|
667
|
+
// id -> the reason the last load failed, shown on the error card only when
|
|
668
|
+
// the host advertises debug (development and test).
|
|
669
|
+
store._remoteReason = {}
|
|
670
|
+
var remote = createRemoteModals(env, function () {
|
|
671
|
+
return store.stack.map(function (entry) { return entry.id })
|
|
672
|
+
})
|
|
673
|
+
// Every stack change (open, close, swap, sweep) goes through _sync, so a
|
|
674
|
+
// stale template kept for an open card is removed once that card leaves.
|
|
675
|
+
var baseSync = store._sync
|
|
676
|
+
store._sync = function () {
|
|
677
|
+
baseSync.call(this)
|
|
678
|
+
remote.flush()
|
|
679
|
+
}
|
|
464
680
|
|
|
465
681
|
// open(id, props, opts)
|
|
466
682
|
// opts.replace: swap the top entry. With a visible card, a directional
|
|
@@ -470,52 +686,95 @@ export function createModalStore(env) {
|
|
|
470
686
|
// An on-demand id not yet in the host loads first (openRemote below).
|
|
471
687
|
store.open = function (id, props, opts) {
|
|
472
688
|
var cfg = remote.handles(id)
|
|
473
|
-
if (cfg && !remote.insertIfReady(id)) return openRemote(this, cfg, id, props, opts)
|
|
689
|
+
if (cfg && !remote.insertIfReady(id)) return openRemote(this, cfg, id, props, opts, false)
|
|
474
690
|
return openNow.call(this, id, props, opts)
|
|
475
691
|
}
|
|
476
692
|
|
|
477
|
-
// The
|
|
478
|
-
//
|
|
479
|
-
function
|
|
693
|
+
// The error card's Try again: re-runs the current entry's fetch with the
|
|
694
|
+
// entry left where it is, showing the loading card meanwhile.
|
|
695
|
+
store.retryRemote = function () {
|
|
696
|
+
var entry = this.current()
|
|
697
|
+
if (!entry || this._remote[entry.id] !== 'error') return
|
|
698
|
+
var cfg = remote.handles(entry.id)
|
|
699
|
+
if (!cfg) return
|
|
700
|
+
// Try again unmounts the focused button, so focus goes back to the
|
|
701
|
+
// backdrop each time the card's content changes (here and in openRemote).
|
|
702
|
+
if (remote.insertIfReady(entry.id)) { delete this._remote[entry.id]; this.refocus(); return }
|
|
703
|
+
openRemote(this, cfg, entry.id, entry.props, null, true)
|
|
704
|
+
this.refocus()
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// The error card's message: the generic line, or with cfg.debug the reason.
|
|
708
|
+
store.remoteErrorText = function () {
|
|
709
|
+
var entry = this.current()
|
|
710
|
+
var reason = entry && this._remoteReason[entry.id]
|
|
711
|
+
return reason || REMOTE_ERROR_TEXT
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// The first open of an on-demand id (or a retry, when inPlace: the entry is
|
|
715
|
+
// already on the stack). Never throws and never leaves a promise unhandled:
|
|
716
|
+
// every outcome is the modal, the loading card, or the error card.
|
|
717
|
+
function openRemote(self, cfg, id, props, opts, inPlace) {
|
|
480
718
|
var body = env.doc.body
|
|
481
|
-
var shown =
|
|
719
|
+
var shown = !!inPlace
|
|
482
720
|
var hold = null
|
|
483
721
|
var done = false
|
|
722
|
+
var timer = null
|
|
484
723
|
if (self._remote[id] === 'error') delete self._remote[id]
|
|
724
|
+
delete self._remoteReason[id]
|
|
725
|
+
var minLoad = function () { return (win.StudioModals && win.StudioModals.MIN_LOAD_MS) || MIN_LOAD_MS }
|
|
485
726
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
shown = true
|
|
489
|
-
hold = holdAtLeast((win.StudioModals && win.StudioModals.MIN_LOAD_MS) || MIN_LOAD_MS)
|
|
727
|
+
if (inPlace) {
|
|
728
|
+
hold = holdAtLeast(minLoad())
|
|
490
729
|
self._remote[id] = 'loading'
|
|
491
|
-
|
|
492
|
-
|
|
730
|
+
} else {
|
|
731
|
+
timer = win.setTimeout(function () {
|
|
732
|
+
if (done || env.doc.body !== body) return
|
|
733
|
+
shown = true
|
|
734
|
+
hold = holdAtLeast(minLoad())
|
|
735
|
+
self._remote[id] = 'loading'
|
|
736
|
+
openNow.call(self, id, props, opts)
|
|
737
|
+
}, LOAD_DELAY_MS)
|
|
738
|
+
}
|
|
493
739
|
|
|
494
|
-
function
|
|
740
|
+
function stopTimer() { if (timer !== null && win.clearTimeout) win.clearTimeout(timer) }
|
|
741
|
+
// Only while this modal is on top: a late answer must not pull focus off another card.
|
|
742
|
+
function refocusIfTop() { var top = self.current(); if (top && top.id === id) self.refocus() }
|
|
743
|
+
|
|
744
|
+
function fail(err) {
|
|
495
745
|
if (done) return
|
|
496
746
|
done = true
|
|
497
|
-
|
|
747
|
+
stopTimer()
|
|
498
748
|
if (env.doc.body !== body) return
|
|
749
|
+
if (cfg.debug && err && err.studioModal) self._remoteReason[id] = err.message
|
|
499
750
|
self._remote[id] = 'error'
|
|
500
751
|
if (!shown) openNow.call(self, id, props, opts)
|
|
752
|
+
else refocusIfTop()
|
|
501
753
|
}
|
|
502
754
|
|
|
503
755
|
remote.load(cfg, id).then(function (markup) {
|
|
504
756
|
if (done) return
|
|
505
|
-
if (env.doc.body !== body) { done = true;
|
|
757
|
+
if (env.doc.body !== body) { done = true; stopTimer(); return }
|
|
506
758
|
if (!remote.insert(id, markup)) { fail(); return }
|
|
507
759
|
done = true
|
|
508
|
-
|
|
760
|
+
stopTimer()
|
|
509
761
|
if (!shown) { openNow.call(self, id, props, opts); return }
|
|
510
762
|
hold.then(function () {
|
|
511
|
-
if (self._remote[id] === 'loading') delete self._remote[id]
|
|
763
|
+
if (self._remote[id] === 'loading') { delete self._remote[id]; refocusIfTop() }
|
|
512
764
|
})
|
|
513
765
|
}).catch(fail)
|
|
514
766
|
}
|
|
515
767
|
|
|
516
|
-
// Schedules the advertised prefetches
|
|
768
|
+
// Schedules the advertised prefetches when the browser idles: once now, for
|
|
769
|
+
// the first load, and again after every Turbo visit. load() shares one
|
|
770
|
+
// request per id, so the first load's turbo:load adds no second fetch.
|
|
517
771
|
if (env.doc && typeof env.doc.querySelector === 'function') {
|
|
518
772
|
whenIdle(win, function () { remote.prefetch() })
|
|
773
|
+
if (typeof env.doc.addEventListener === 'function') {
|
|
774
|
+
env.doc.addEventListener('turbo:load', function () {
|
|
775
|
+
whenIdle(win, function () { remote.prefetch() })
|
|
776
|
+
})
|
|
777
|
+
}
|
|
519
778
|
}
|
|
520
779
|
|
|
521
780
|
function openNow(id, props, opts) {
|
|
@@ -219,10 +219,19 @@
|
|
|
219
219
|
fetches a registered id the first time it opens and inserts its
|
|
220
220
|
<template x-if> into the card. See the README, "Modals on demand". %>
|
|
221
221
|
<% studio_remote_modals = Studio.modals.any? %>
|
|
222
|
+
<% if studio_remote_modals
|
|
223
|
+
studio_remote_session = respond_to?(:controller) && controller.respond_to?(:session) ? controller.session : nil
|
|
224
|
+
studio_remote_config = Studio.modals.client_config(
|
|
225
|
+
url_template: studio_modal_path(id: "0").delete_suffix("0") + "__id__",
|
|
226
|
+
logged_in: respond_to?(:logged_in?) && logged_in?,
|
|
227
|
+
session: Studio::Modals.session_marker(studio_remote_session),
|
|
228
|
+
debug: defined?(Rails) && Rails.respond_to?(:env) && (Rails.env.development? || Rails.env.test?)
|
|
229
|
+
)
|
|
230
|
+
end %>
|
|
222
231
|
<template x-if="$store.modals.current()"
|
|
223
232
|
data-studio-controller="modal-host"
|
|
224
233
|
data-modal-host-store-value="modals"<% if studio_remote_modals %>
|
|
225
|
-
data-modal-host-remote-value="<%=
|
|
234
|
+
data-modal-host-remote-value="<%= studio_remote_config.to_json %>"<% end %>>
|
|
226
235
|
<%# Backdrop + card use CSS keyframe animations (NOT Alpine
|
|
227
236
|
x-transition), all shipped in the inline style block above. On
|
|
228
237
|
mount, the .modal-backdrop-mount + card mount classes fire fade-in
|
|
@@ -312,22 +321,28 @@
|
|
|
312
321
|
<%# The on-demand states, keyed on $store.modals._remote[id], which only
|
|
313
322
|
studio/modal_host sets and only for a registered id: 'loading' while a
|
|
314
323
|
slow first fetch runs (the processing card, held MIN_LOAD_MS so it
|
|
315
|
-
never flashes
|
|
316
|
-
|
|
324
|
+
never flashes; role="status", so a screen reader hears it politely),
|
|
325
|
+
'error' when the fetch fails, times out (Studio.modals.fetch_timeout_ms)
|
|
326
|
+
or is refused (the error card: Try again re-runs the fetch in place,
|
|
327
|
+
Close closes). Close calls close() directly, so it works on a card
|
|
328
|
+
opened dismissible: false too, which Escape and the backdrop do not
|
|
329
|
+
close. Fetched registrations are appended after these. %>
|
|
317
330
|
<% if studio_remote_modals %>
|
|
318
331
|
<template x-if="$store.modals._remote && $store.modals._remote[$store.modals.current()?.id] === 'loading'">
|
|
319
|
-
<div data-modal-remote-state="loading">
|
|
332
|
+
<div data-modal-remote-state="loading" role="status" aria-live="polite">
|
|
320
333
|
<%= render "studio/modals/blocks/processing_card", title: "Loading…" %>
|
|
321
334
|
</div>
|
|
322
335
|
</template>
|
|
323
336
|
<template x-if="$store.modals._remote && $store.modals._remote[$store.modals.current()?.id] === 'error'">
|
|
324
|
-
<div data-modal-remote-state="error">
|
|
337
|
+
<div data-modal-remote-state="error" @studio-modal-retry="$store.modals.retryRemote()">
|
|
325
338
|
<%= render "studio/modals/blocks/error_card",
|
|
326
339
|
title: "Couldn't open this",
|
|
327
|
-
|
|
340
|
+
message_key: "$store.modals.remoteErrorText()",
|
|
328
341
|
icon_emoji: "⚠️",
|
|
329
|
-
cta_label: "
|
|
330
|
-
|
|
342
|
+
cta_label: "Try again",
|
|
343
|
+
cta_event: "studio-modal-retry",
|
|
344
|
+
secondary_label: "Close",
|
|
345
|
+
secondary_close: true %>
|
|
331
346
|
</div>
|
|
332
347
|
</template>
|
|
333
348
|
<% end %>
|
|
@@ -93,14 +93,15 @@
|
|
|
93
93
|
<h3 class="type-h4 text-heading">Table · sorted by member, a badge cell</h3>
|
|
94
94
|
<div class="card overflow-hidden">
|
|
95
95
|
<%= render Studio::TableComponent.new(rows: people, columns: columns, sort: "name", direction: "asc",
|
|
96
|
-
sort_url: sort_url, min_width: 520) %>
|
|
96
|
+
sort_url: sort_url, min_width: 520, caption: "Team members, sorted by member") %>
|
|
97
97
|
</div>
|
|
98
98
|
</div>
|
|
99
99
|
|
|
100
100
|
<div class="space-y-3" data-specimen="table-empty">
|
|
101
101
|
<h3 class="type-h4 text-heading">Table · no rows</h3>
|
|
102
102
|
<div class="card overflow-hidden">
|
|
103
|
-
<%= render Studio::TableComponent.new(rows: [], columns: columns, sort_url: sort_url, empty: "No members yet."
|
|
103
|
+
<%= render Studio::TableComponent.new(rows: [], columns: columns, sort_url: sort_url, empty: "No members yet.",
|
|
104
|
+
aria_label: "Team members") %>
|
|
104
105
|
</div>
|
|
105
106
|
</div>
|
|
106
107
|
</div>
|
data/lib/studio/modals.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
3
5
|
module Studio
|
|
4
6
|
# The on-demand modal registry: modals the shared host (studio/modals/_host)
|
|
5
7
|
# fetches the first time they open, instead of the layout rendering every
|
|
@@ -22,11 +24,14 @@ module Studio
|
|
|
22
24
|
# (one root element; see the README's "Modal host" rules).
|
|
23
25
|
# auth: :any (default) or :user. A :user modal answers 401 to a
|
|
24
26
|
# signed-out request, and is never prefetched for one.
|
|
25
|
-
# prefetch: true fetches the modal when the browser idles after load
|
|
26
|
-
# modal that should open with no
|
|
27
|
+
# prefetch: true fetches the modal when the browser idles after load and
|
|
28
|
+
# after every Turbo visit, for a modal that should open with no
|
|
29
|
+
# wait (the sign-in card).
|
|
27
30
|
# locals: the partial's locals. A Hash, a Symbol naming a helper the view
|
|
28
31
|
# answers (Turf's existing *_modal_locals helpers), or a callable
|
|
29
|
-
# taking the view. Resolved per request, inside the view
|
|
32
|
+
# taking the view. Resolved per request, inside the view of
|
|
33
|
+
# Studio::ModalsController: helpers and the session are there,
|
|
34
|
+
# the page's instance variables (@contest) are not.
|
|
30
35
|
module Modals
|
|
31
36
|
AUTH_LEVELS = %i[any user].freeze
|
|
32
37
|
|
|
@@ -54,8 +59,23 @@ module Studio
|
|
|
54
59
|
end
|
|
55
60
|
|
|
56
61
|
class Registry
|
|
62
|
+
# How long studio/modal_host waits for a fragment before it gives up and
|
|
63
|
+
# shows the error card (with Try again and Close), in milliseconds.
|
|
64
|
+
DEFAULT_FETCH_TIMEOUT_MS = 8000
|
|
65
|
+
|
|
66
|
+
# Settable from the initializer: Studio.modals.fetch_timeout_ms = 12_000.
|
|
67
|
+
attr_reader :fetch_timeout_ms
|
|
68
|
+
|
|
57
69
|
def initialize
|
|
58
70
|
@entries = {}
|
|
71
|
+
@fetch_timeout_ms = DEFAULT_FETCH_TIMEOUT_MS
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def fetch_timeout_ms=(value)
|
|
75
|
+
ms = Integer(value)
|
|
76
|
+
raise ArgumentError, "Studio.modals.fetch_timeout_ms must be positive, got #{value.inspect}" unless ms.positive?
|
|
77
|
+
|
|
78
|
+
@fetch_timeout_ms = ms
|
|
59
79
|
end
|
|
60
80
|
|
|
61
81
|
def register(id, partial:, auth: :any, prefetch: false, locals: nil)
|
|
@@ -85,16 +105,48 @@ module Studio
|
|
|
85
105
|
# What the host template advertises to studio/modal_host. Every id, so a
|
|
86
106
|
# signed-out open of a :user modal still reaches the endpoint and shows
|
|
87
107
|
# the error card; prefetch only what this viewer may load.
|
|
88
|
-
|
|
108
|
+
#
|
|
109
|
+
# session: the marker studio/modal_host keys its fragment cache on (see
|
|
110
|
+
# Modals.session_marker). debug: true in development and test, where a
|
|
111
|
+
# refused fragment names its reason on the error card.
|
|
112
|
+
def client_config(url_template:, logged_in:, session: nil, debug: false)
|
|
89
113
|
{
|
|
90
114
|
url: url_template,
|
|
91
115
|
ids: ids,
|
|
92
|
-
prefetch: entries.select { |e| e.prefetch && (!e.user_only? || logged_in) }.map(&:id)
|
|
116
|
+
prefetch: entries.select { |e| e.prefetch && (!e.user_only? || logged_in) }.map(&:id),
|
|
117
|
+
timeout_ms: fetch_timeout_ms,
|
|
118
|
+
session: session.to_s,
|
|
119
|
+
debug: debug == true
|
|
93
120
|
}
|
|
94
121
|
end
|
|
95
122
|
end
|
|
96
123
|
end
|
|
97
124
|
|
|
125
|
+
module Modals
|
|
126
|
+
# THE SESSION MARKER. A fetched fragment can carry the viewer's data and a
|
|
127
|
+
# form's CSRF token, and studio/modal_host keeps it for the page's life. A
|
|
128
|
+
# Turbo visit across sign-in, sign-out or a user switch keeps that page, so
|
|
129
|
+
# the browser compares this marker on every open and every turbo:load and
|
|
130
|
+
# drops its cache when it changes.
|
|
131
|
+
#
|
|
132
|
+
# It digests two things the request already has:
|
|
133
|
+
# - the session id, which reset_session rotates (sign-out, and the
|
|
134
|
+
# sign-ins that reset), and with it the session's CSRF secret: a cached
|
|
135
|
+
# form token stops being valid exactly when this changes;
|
|
136
|
+
# - the signed-in user id under Studio.session_key, which a sign-in that
|
|
137
|
+
# does NOT reset the session (an OAuth or wallet sign-in) still changes.
|
|
138
|
+
# Digested so neither value reaches the page. A request with no session
|
|
139
|
+
# (a view rendered outside a controller) answers the digest of nothing:
|
|
140
|
+
# a constant, so the cache simply lives for the page, as before.
|
|
141
|
+
def self.session_marker(session)
|
|
142
|
+
sid = session.respond_to?(:id) ? session.id.to_s : ""
|
|
143
|
+
uid = session.respond_to?(:[]) ? session[Studio.session_key].to_s : ""
|
|
144
|
+
Digest::SHA256.hexdigest("#{sid}|#{uid}")[0, 16]
|
|
145
|
+
rescue StandardError
|
|
146
|
+
Digest::SHA256.hexdigest("|")[0, 16]
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
98
150
|
def self.modals
|
|
99
151
|
@modals ||= Modals::Registry.new
|
|
100
152
|
end
|