T14.3 (D26, BL-061): the sync badge shows the true state and gets out of the way

The badge words live in one function, badgeModel(). States: sending, retrying,
held for sign-in, not saved on this device, rejected (with the reason), read and
queue, another person's changes, and all sent. Every state names the project.
Sign in is a real link to login.html?next=. Dismiss is a real button and removes
only the changes the project refused. Both are 44px or more with a focus ring.

BL-061: the console pages (Admin, User Directory) never show the badge. All sent
shows only after real work. The badge picks a free spot on the screen. One very
dense screen case is left and logged as BL-072.

tests/sync_badge_check.py (87 checks). autosave_check reads the new badge words.
This commit is contained in:
2026-10-17 15:12:25 -07:00
parent b78b9b3707
commit 6c2e457225
8 changed files with 551 additions and 71 deletions

View File

@@ -58,7 +58,7 @@
}
</style>
</head>
<body>
<body data-wp-no-sync-badge>
<!-- SHARED DARK APP BAR -->
<header class="wp-appbar">

View File

@@ -423,79 +423,212 @@
renderSyncBadge(c);
}
// Tiny sync indicator (bottom-left). This was gated on being the top-level
// window so it was not drawn twice - once by the suite page and again inside
// the embedded creator. B7/T7.1 dissolved that frame, so there is one document
// and one badge. The 'storage' listener below stays: it is what keeps two
// TABS in step, which is a different thing and still happens.
var _badgeHideTimer = null;
// The sync badge (D26, BL-061). It reports the OUTBOX: whether saved records have
// reached the project. This was gated on being the top-level window so it was not
// drawn twice (the suite page and the embedded creator); B7/T7.1 dissolved that
// frame, so there is one document and one badge. The 'storage' listener below
// stays: it is what keeps two TABS in step, which is a different thing.
var _badgeHideTimer = null, _badgePlaceTimer = null, _hadWork = false;
// BL-011 (fixed at T9.9): the badge used to mount on the FIRST SYNC EVENT,
// which is async, so the three fixed overlays on the SOP page landed in a
// different DOM order run to run and every index-keyed comparison saw
// phantom diffs. Mounting the (hidden) holder at DOMContentLoaded puts the
// three in script order, deterministically.
document.addEventListener('DOMContentLoaded', function () { renderSyncBadge(null); });
// BL-061: the console pages (Admin, User Directory) never queue project work, and
// the badge used to sit over their bottom-right buttons. They opt out with
// data-wp-no-sync-badge on <body>.
function badgeExempt() { return !!(document.body && document.body.hasAttribute('data-wp-no-sync-badge')); }
// Discard the changes the project refused. The person has read the reason and
// chose to dismiss it; nothing else ever removes a queued change by itself.
function dismissRejected() {
_mem = _mem.filter(function (o) { return !o.dead; });
qWrite(qRead().filter(function (o) { return !o.dead; }));
notifySync();
}
function signInUrl() {
return 'login.html?next=' + encodeURIComponent(location.pathname + location.search);
}
// One place decides what the badge says. B5: every string names the PROJECT, so it
// cannot be read as the draft indicator (WPAutosave.mountIndicator), which speaks
// for the form. tone is one of neutral, ok, warn, error.
function badgeModel(c) {
var n;
if (c.deviceFull) {
n = c.unsaved;
return { tone: 'error', text: '✕ ' + n + ' change' + (n === 1 ? ' is' : 's are') + ' not saved on this device. Keep this page open until it is sent to the project.' };
}
if (readQueueMode()) {
return { tone: 'warn', text: 'Working offline. Sign in again when you have a signal to send your changes to the project.', signIn: true };
}
if (c.held) {
return { tone: 'warn', text: '⚠ Sign in again to send your changes to the project. They are kept on this device.', signIn: true };
}
if (c.dead) {
return { tone: 'error', text: '✕ ' + c.dead + ' change' + (c.dead === 1 ? '' : 's') + ' rejected by the project. Not saved.', reason: c.reason, dismiss: true };
}
if (c.failed) {
return { tone: 'warn', text: '⚠ ' + c.failed + ' change' + (c.failed === 1 ? '' : 's') + ' not yet sent to the project. Retrying.' };
}
if (c.pending) {
return { tone: 'neutral', text: '↻ Sending ' + c.pending + ' change' + (c.pending === 1 ? '' : 's') + ' to the project…' };
}
if (c.others) {
return { tone: 'warn', text: '⚠ ' + c.others + ' change' + (c.others === 1 ? ' belongs' : 's belong') + ' to ' + c.otherOwner + '. ' + (c.others === 1 ? 'It goes' : 'They go') + ' to the project when ' + c.otherOwner + ' signs in.' };
}
// Nothing to report. "Everything sent" follows real work only: on a page that
// never queued anything it is noise that sits over the controls (BL-061).
if (!_hadWork) return null;
return { tone: 'ok', text: '✓ Everything sent to the project', autoHide: true };
}
var TONES = {
neutral: ['var(--cds-text-secondary)', 'var(--cds-border-subtle)', 'var(--cds-layer)'],
ok: ['var(--wp-status-success-text)', 'var(--wp-status-success-border-a)', 'var(--wp-status-success-bg)'],
warn: ['var(--wp-status-warning-text)', 'var(--cds-support-warning)', 'var(--wp-status-warning-bg)'],
error: ['var(--wp-status-error-text)', 'var(--wp-status-error-border-a)', 'var(--wp-status-error-bg)']
};
function ensureBadgeStyle() {
if (document.getElementById('wp-sync-badge-style')) return;
var st = document.createElement('style');
st.id = 'wp-sync-badge-style';
// Controls are real <a> and <button> elements: reachable by keyboard and touch,
// at least 44px high, with a focus ring from the shared focus token.
st.textContent =
'#wp-sync-badge{max-width:min(420px,calc(100vw - 24px));box-sizing:border-box}' +
'@media (max-width:480px){#wp-sync-badge{max-width:min(300px,calc(100vw - 24px))}}' +
'#wp-sync-badge .wp-sb-msg{display:flex;flex-direction:column;gap:2px;min-width:0}' +
'#wp-sync-badge .wp-sb-reason{font-weight:400}' +
'#wp-sync-badge .wp-sb-ctl{display:inline-flex;align-items:center;justify-content:center;box-sizing:border-box;' +
'min-width:44px;min-height:44px;padding:0 10px;margin:-6px -12px -6px -4px;border:0;background:transparent;color:inherit;' +
'font-family:inherit;font-weight:600;font-size:12px;line-height:1.3;text-decoration:underline;cursor:pointer}' +
'#wp-sync-badge .wp-sb-ctl:focus-visible{outline:2px solid var(--cds-focus);outline-offset:-2px}';
(document.head || document.documentElement).appendChild(st);
}
// BL-061: keep the badge clear of page controls. Measure every visible control,
// then put the badge in the first free spot: the four corners first, then any gap
// on the screen that is big enough, scanning from the bottom up. Page headers and
// bottom bars fill the width at 390px, so fixed corners alone cannot clear them.
// If no spot is free, take the one that covers the fewest controls.
var CONTROLS = 'a[href],button,input,select,textarea,summary,[role="button"],[tabindex]:not([tabindex="-1"])';
function controlRects(el) {
var out = [], list = document.querySelectorAll(CONTROLS);
for (var i = 0; i < list.length; i++) {
var o = list[i];
if (o === el || el.contains(o)) continue;
var b = o.getBoundingClientRect();
if (!b.width || !b.height || !o.getClientRects().length) continue;
var cs = window.getComputedStyle(o);
if (cs.visibility === 'hidden' || cs.display === 'none') continue;
out.push(b);
}
return out;
}
function hitCount(rects, x, y, w, h) {
var n = 0;
for (var i = 0; i < rects.length; i++) {
var b = rects[i];
if (b.left < x + w && b.right > x && b.top < y + h && b.bottom > y) n++;
}
return n;
}
function placeBadge(el) {
if (el.style.display === 'none') return;
var vw = document.documentElement.clientWidth, vh = window.innerHeight;
// Measure at the left edge, where the box has all the room it will ever have,
// then pin that width: a box measured near the right edge wraps and grows.
el.style.width = ''; el.style.right = 'auto'; el.style.bottom = 'auto';
el.style.left = '0px'; el.style.top = '0px';
var r = el.getBoundingClientRect(), w = Math.ceil(r.width), h = Math.ceil(r.height), m = 12;
el.style.width = w + 'px';
var rects = controlRects(el);
var spots = [
[vw - w - m, vh - h - m], [m, vh - h - m], [vw - w - m, m], [m, m]
];
// Then every gap, bottom to top, right to left, in 8px steps.
for (var y = vh - h - m; y >= m; y -= 8) {
for (var x = vw - w - m; x >= m; x -= 8) spots.push([x, y]);
}
var best = spots[0], bestN = Infinity;
for (var i = 0; i < spots.length; i++) {
var n = hitCount(rects, spots[i][0], spots[i][1], w, h);
if (n < bestN) { bestN = n; best = spots[i]; }
if (n === 0) break;
}
el.style.right = 'auto'; el.style.bottom = 'auto';
el.style.left = Math.max(0, Math.round(best[0])) + 'px';
el.style.top = Math.max(0, Math.round(best[1])) + 'px';
}
function renderSyncBadge(c) {
if (!document.body) return;
var el = document.getElementById('wp-sync-badge');
if (!el) {
ensureBadgeStyle();
el = document.createElement('div');
el.id = 'wp-sync-badge';
// S10. Polite: this reports background syncing, and interrupting someone to
// say a queue drained is exactly the noise that gets aria-live turned off.
el.setAttribute('role', 'status');
el.style.cssText = 'position:fixed;right:12px;bottom:12px;z-index:9998;pointer-events:none;display:none;align-items:center;gap:7px;' +
el.style.cssText = 'position:fixed;right:12px;bottom:12px;z-index:9998;pointer-events:none;display:none;align-items:center;gap:12px;' +
'font:500 12px/1.3 "IBM Plex Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;' +
'padding:6px 12px;border:1px solid var(--cds-border-subtle);background:var(--cds-layer);color:var(--cds-text-secondary);box-shadow:0 1px 4px rgba(0,0,0,.12);transition:opacity .2s;';
document.body.appendChild(el);
window.addEventListener('resize', function () { placeBadge(el); });
}
if (!c) return; // the eager DOMContentLoaded mount: holder only, no state yet
if (_badgeHideTimer) { clearTimeout(_badgeHideTimer); _badgeHideTimer = null; }
// A dead op is a refusal, not a hiccup — "retrying" would be a lie, and the
// reason is the only thing that tells the user what to do (e.g. the project is
// archived). Stack it under the headline; the badge never hides in this state.
el.style.flexDirection = c.dead ? 'column' : 'row';
el.style.alignItems = c.dead ? 'flex-start' : 'center';
el.style.maxWidth = c.dead ? 'min(340px, calc(100vw - 32px))' : 'none';
// B5. This badge reports the OUTBOX — whether saved records have reached the
// project — and it used to say "✓ All changes saved", which is what a draft
// autosave says. So the app was already making the promise B5 says it does not
// keep: the badge went green when the queue emptied, whether or not anything in
// the form had been saved at all.
//
// Every string here now names the project explicitly. The draft indicator
// (WPAutosave.mountIndicator) is the one that speaks for the form, and the two
// can no longer be read as each other.
// D26. These three are the states that used to be invisible or wrong. T14.3
// gives them their final wording and controls; until then they are plain text.
if (c.deviceFull) {
el.textContent = '✕ ' + c.unsaved + ' change' + (c.unsaved === 1 ? ' is' : 's are') + ' not saved on this device. Keep this page open until it is sent to the project.';
el.style.color = 'var(--wp-status-error-text)'; el.style.borderColor = 'var(--wp-status-error-border-a)'; el.style.background = 'var(--wp-status-error-bg)'; el.style.display = 'inline-flex';
} else if (readQueueMode()) {
// D27. The one line for read-and-queue mode. T14.3 owns the final wording
// and the sign-in control.
el.textContent = 'Working offline. Sign in again when you have a signal to send your changes.';
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
} else if (c.held) {
el.textContent = '⚠ Sign in again to send your changes to the project. They are kept on this device.';
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
} else if (c.dead) {
el.innerHTML = '<span>✕ ' + c.dead + ' change' + (c.dead === 1 ? '' : 's') + ' rejected by the project — not saved</span>' +
(c.reason ? '<span style="font-weight:400">' + esc(c.reason) + '</span>' : '');
el.style.color = 'var(--wp-status-error-text)'; el.style.borderColor = 'var(--wp-status-error-border-a)'; el.style.background = 'var(--wp-status-error-bg)'; el.style.display = 'inline-flex';
} else if (c.failed) {
el.textContent = '⚠ ' + c.failed + ' change' + (c.failed === 1 ? '' : 's') + ' not yet sent to the project — retrying';
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
} else if (c.pending) {
el.textContent = '↻ Sending ' + c.pending + ' change' + (c.pending === 1 ? '' : 's') + ' to the project…';
el.style.color = 'var(--cds-text-secondary)'; el.style.borderColor = 'var(--cds-border-subtle)'; el.style.background = 'var(--cds-layer)'; el.style.display = 'inline-flex';
} else if (c.others) {
el.textContent = '⚠ ' + c.others + ' change' + (c.others === 1 ? ' belongs' : 's belong') + ' to ' + c.otherOwner + '. ' + (c.others === 1 ? 'It goes' : 'They go') + ' to the project when ' + c.otherOwner + ' signs in.';
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
} else {
el.textContent = '✓ Everything sent to the project';
el.style.color = 'var(--wp-status-success-text)'; el.style.borderColor = 'var(--wp-status-success-border-a)'; el.style.background = 'var(--wp-status-success-bg)'; el.style.display = 'inline-flex';
if (_badgePlaceTimer) { clearInterval(_badgePlaceTimer); _badgePlaceTimer = null; }
if (c.pending || c.failed || c.held || c.others || c.deviceFull) _hadWork = true;
var m = badgeExempt() ? null : badgeModel(c);
if (!m) { el.style.display = 'none'; el.style.pointerEvents = 'none'; return; }
var t = TONES[m.tone] || TONES.neutral;
el.style.color = t[0]; el.style.borderColor = t[1]; el.style.background = t[2];
el.style.pointerEvents = (m.signIn || m.dismiss) ? 'auto' : 'none';
// Rebuild the contents. The container keeps role="status", so a change of text
// is announced; the controls inside it are ordinary focusable elements.
while (el.firstChild) el.removeChild(el.firstChild);
var msg = document.createElement('span');
msg.className = 'wp-sb-msg';
var head = document.createElement('span');
head.textContent = m.text;
msg.appendChild(head);
if (m.reason) {
var why = document.createElement('span');
why.className = 'wp-sb-reason';
why.textContent = String(m.reason);
msg.appendChild(why);
}
el.appendChild(msg);
if (m.signIn) {
var a = document.createElement('a');
a.className = 'wp-sb-ctl';
a.href = signInUrl();
a.textContent = 'Sign in';
el.appendChild(a);
}
if (m.dismiss) {
var d = document.createElement('button');
d.type = 'button';
d.className = 'wp-sb-ctl';
d.textContent = 'Dismiss';
d.addEventListener('click', dismissRejected);
el.appendChild(d);
}
el.style.display = 'inline-flex';
placeBadge(el);
if (m.autoHide) {
_hadWork = false; // the next "sent" needs new work
_badgeHideTimer = setTimeout(function () { if (el) el.style.display = 'none'; }, 1800);
} else {
// A state that stays: re-check the spot now and then, since the page under it changes.
_badgePlaceTimer = setInterval(function () { placeBadge(el); }, 2000);
}
}

View File

@@ -77,7 +77,7 @@
}
</style>
</head>
<body>
<body data-wp-no-sync-badge>
<!-- SHARED DARK APP BAR -->
<header class="wp-appbar">