From c5d8fc9a408c098dcc094dc0894b9a7ceab7f0d7 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Tue, 10 Nov 2026 21:37:56 -0500 Subject: [PATCH 1/2] Bring the bundled Card API controller up to the converged baseline Replace js/cards.js with the canonical controller (md5 20694b4c), byte-identical with core and the other plugins. mactrack adopted the chrome early (#412), so its bundled controller was an older lineage missing the request-generation guard, confirmed card removal, keyboard refocus and the new swap-time expand/collapse preservation. css/cards.css already matched; the controller API is a backward- compatible superset so the mactrack adapter is unaffected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- js/cards.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/js/cards.js b/js/cards.js index 5005c420..e8d4d1eb 100644 --- a/js/cards.js +++ b/js/cards.js @@ -165,6 +165,12 @@ window.CactiCards = window.CactiCards || (function() { if (!target || !target.parentNode) { return null; } + // The in-flight response carries the expansion state captured when the + // request started, but the user may have expanded/collapsed the live + // card since. Carry the live state onto the fresh markup so the swap + // (refresh, resize or refresh-all) does not undo that toggle; bindCard's + // normalizeState applies the class from this data-expanded value. + fresh.setAttribute('data-expanded', target.classList.contains('ccCardExpanded') ? '1' : '0'); target.parentNode.replaceChild(fresh, target); bindCard(grid, adapter, fresh); if (typeof adapter.applyTheme === 'function') { @@ -173,6 +179,22 @@ window.CactiCards = window.CactiCards || (function() { return fresh; } + /* A per-card-id request generation shared by refresh and resize so a slow + * response cannot overwrite the markup of a newer request for the same card + * (for example a refresh that lands after a later grow). The most recently + * started request for an id wins; earlier in-flight responses are discarded. */ + function startRequest(grid, id) { + if (!grid.__ccReq) { + grid.__ccReq = {}; + } + grid.__ccReq[id] = (grid.__ccReq[id] || 0) + 1; + return grid.__ccReq[id]; + } + + function requestSuperseded(grid, id, token) { + return !!grid.__ccReq && grid.__ccReq[id] !== token; + } + function refreshCard(grid, adapter, card) { var id = cardId(card, adapter); // Hosts that inject into an inner body (rather than replacing the whole @@ -188,8 +210,11 @@ window.CactiCards = window.CactiCards || (function() { expanded: card.classList.contains('ccCardExpanded'), height: Number.parseInt(card.getAttribute('data-height') || '1', 10) }; + var token = startRequest(grid, id); Promise.resolve(adapter.fetchCard(id, state)).then(function(html) { - if (html != null) { + // A newer refresh/resize for this card superseded us; discard the + // stale markup instead of reverting the card to an older state. + if (html != null && !requestSuperseded(grid, id, token)) { replaceCard(grid, adapter, card, html); } }).catch(function() { /* fire-and-forget: the host surfaces adapter errors */ }); @@ -197,17 +222,35 @@ window.CactiCards = window.CactiCards || (function() { function removeCard(grid, adapter, card) { var id = cardId(card, adapter); + // Only drop the live card and persist the new layout once the host has + // confirmed the deletion. Re-resolve by id so an overlapping refresh that + // swapped the node does not leave a detached reference behind. + var commit = function() { + var target = grid.contains(card) ? card : findCard(grid, adapter, id); + if (target && target.parentNode) { + target.parentNode.removeChild(target); + saveLayout(grid, adapter); + } + }; if (typeof adapter.onRemoveCard === 'function') { - adapter.onRemoveCard(id, card); + Promise.resolve(adapter.onRemoveCard(id, card)).then(commit).catch(function() { + /* rejected: leave the card and its saved layout untouched */ + }); + } else { + commit(); } - card.parentNode.removeChild(card); - saveLayout(grid, adapter); } function resizeCard(grid, adapter, card, dir) { var id = cardId(card, adapter); if (typeof adapter.onResize === 'function') { + var token = startRequest(grid, id); Promise.resolve(adapter.onResize(id, dir, card)).then(function(html) { + // A newer refresh/resize for this card has superseded us; leave + // its markup and layout in place rather than reverting them. + if (requestSuperseded(grid, id, token)) { + return; + } if (typeof html === 'string') { replaceCard(grid, adapter, card, html); } @@ -218,6 +261,19 @@ window.CactiCards = window.CactiCards || (function() { } } + /* After expand/collapse hides the button that was activated (see cards.css), + * move keyboard focus to the now-visible counterpart so the control is not + * lost when the card is toggled from the keyboard. */ + function refocusToolCounterpart(card, tool, counterpart) { + if (document.activeElement !== tool) { + return; + } + var next = card.querySelector('.ccTool[data-tool="' + counterpart + '"]'); + if (next) { + next.focus(); + } + } + /* Wire a single card: its drag handle (mouse + touch + keyboard) and drag * events. Handle interactions are DELEGATED from the persistent card element * so they survive hosts that replace the header markup on refresh/reload. */ @@ -348,11 +404,13 @@ window.CactiCards = window.CactiCards || (function() { case 'expand': card.classList.add('ccCardExpanded'); card.setAttribute('data-expanded', '1'); + refocusToolCounterpart(card, tool, 'collapse'); saveLayout(grid, adapter); break; case 'collapse': card.classList.remove('ccCardExpanded'); card.setAttribute('data-expanded', '0'); + refocusToolCounterpart(card, tool, 'expand'); saveLayout(grid, adapter); break; case 'grow': resizeCard(grid, adapter, card, 'grow'); break; From b147941ca895a6d29bd98f5775bfb06dd70b5af3 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Wed, 11 Nov 2026 09:39:47 -0500 Subject: [PATCH 2/2] Sync cards.js: scope refresh toggle preservation to real user toggles Corrected shared controller (review on core #8700): replaceCard() carries the live expand/collapse state onto refreshed markup only when the user toggled during the request and the fresh card is still expandable; host markup stays authoritative otherwise. Byte-identical with core and the other plugins. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- js/cards.js | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/js/cards.js b/js/cards.js index e8d4d1eb..82503b9f 100644 --- a/js/cards.js +++ b/js/cards.js @@ -145,8 +145,32 @@ window.CactiCards = window.CactiCards || (function() { return rowMatch || belowMatch; } - /* Swap a card's markup in place (after refresh/add) and re-bind it. */ - function replaceCard(grid, adapter, card, html) { + /* Decide, mirroring normalizeState(), whether a fresh card will be + * expandable, so a preserved toggle is never forced onto a card that cannot + * expand (which would leave normalizeState() marking it ccCardExpanded and + * persisting expanded:true for a non-expandable card). */ + function willExpand(card) { + var expandable = card.getAttribute('data-expandable'); + if (expandable === '1' || expandable === 'true') { + return true; + } + if (expandable != null) { + return false; + } + return card.classList.contains('ccCardExpandable'); + } + + /* Swap a card's markup in place (after refresh/add) and re-bind it. + * + * capturedExpanded (optional) is the card's expanded state when an internal + * refresh/resize request started. When it is supplied and the live card has + * since been toggled by the user, the incoming markup reflects the stale + * captured state, so the live state is carried onto the fresh markup to avoid + * reverting that toggle - but only when the fresh card is still expandable. + * When it is omitted (direct callers) or the state is unchanged, the host + * markup's own data-expanded stays authoritative (the normalizeState + * contract, so a host may still drive expansion through returned markup). */ + function replaceCard(grid, adapter, card, html, capturedExpanded) { var tmp = document.createElement('div'); tmp.innerHTML = (html || '').trim(); var fresh = tmp.querySelector(cardSel(adapter)) || tmp.firstElementChild; @@ -165,12 +189,12 @@ window.CactiCards = window.CactiCards || (function() { if (!target || !target.parentNode) { return null; } - // The in-flight response carries the expansion state captured when the - // request started, but the user may have expanded/collapsed the live - // card since. Carry the live state onto the fresh markup so the swap - // (refresh, resize or refresh-all) does not undo that toggle; bindCard's - // normalizeState applies the class from this data-expanded value. - fresh.setAttribute('data-expanded', target.classList.contains('ccCardExpanded') ? '1' : '0'); + if (capturedExpanded != null) { + var liveExpanded = target.classList.contains('ccCardExpanded'); + if (liveExpanded !== capturedExpanded && willExpand(fresh)) { + fresh.setAttribute('data-expanded', liveExpanded ? '1' : '0'); + } + } target.parentNode.replaceChild(fresh, target); bindCard(grid, adapter, fresh); if (typeof adapter.applyTheme === 'function') { @@ -215,7 +239,7 @@ window.CactiCards = window.CactiCards || (function() { // A newer refresh/resize for this card superseded us; discard the // stale markup instead of reverting the card to an older state. if (html != null && !requestSuperseded(grid, id, token)) { - replaceCard(grid, adapter, card, html); + replaceCard(grid, adapter, card, html, state.expanded); } }).catch(function() { /* fire-and-forget: the host surfaces adapter errors */ }); } @@ -245,6 +269,7 @@ window.CactiCards = window.CactiCards || (function() { var id = cardId(card, adapter); if (typeof adapter.onResize === 'function') { var token = startRequest(grid, id); + var capturedExpanded = card.classList.contains('ccCardExpanded'); Promise.resolve(adapter.onResize(id, dir, card)).then(function(html) { // A newer refresh/resize for this card has superseded us; leave // its markup and layout in place rather than reverting them. @@ -252,7 +277,7 @@ window.CactiCards = window.CactiCards || (function() { return; } if (typeof html === 'string') { - replaceCard(grid, adapter, card, html); + replaceCard(grid, adapter, card, html, capturedExpanded); } saveLayout(grid, adapter); }).catch(function() { /* fire-and-forget: the host surfaces adapter errors */ });