@justai/core 0.8.0 → 0.9.0
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.
- package/package.json +2 -2
- package/src/index.d.ts +6 -3
- package/src/index.js +165 -36
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@justai/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The framework-free heart of the persona shell
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "The framework-free heart of the persona shell — the load-bearing behaviour (auth capture, reveal, ready) as plain strings, so Astro and Next inject the SAME logic instead of each re-writing it.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
package/src/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare const JS_CLASS_JS: string;
|
|
|
7
7
|
/** Hold paint until a page sets `body.ready`. A page with no kernel script must carry READY_JS. */
|
|
8
8
|
export declare const REVEAL_CSS: string;
|
|
9
9
|
|
|
10
|
-
/** Self-reveal for a page with no kernel script (a 404, a
|
|
10
|
+
/** Self-reveal for a page with no kernel script (a 404, a child persona like feed). */
|
|
11
11
|
export declare const READY_JS: string;
|
|
12
12
|
|
|
13
13
|
/** Which GA4 measurement id a host reports into. The ecosystem's stream layout, declared once. */
|
|
@@ -49,8 +49,11 @@ export interface PersonaScrollOptions {
|
|
|
49
49
|
titleFor?: (id: string) => string;
|
|
50
50
|
/** Whether this owns `body.ready`. Pass `false` on a page that renders asynchronously. */
|
|
51
51
|
reveal?: boolean;
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
// `line` was here — a pixel threshold a section had to cross to count as current, defaulting to 84.
|
|
53
|
+
// It is gone rather than deprecated, because it was the bug: a constant that had to be kept in step
|
|
54
|
+
// by hand with the `scroll-margin-top` twelve personas declared separately, and the two-pixel gap
|
|
55
|
+
// between them is where the hash stopped guaranteeing the cut. A cut now begins where the page
|
|
56
|
+
// comes to rest when you go to it — one definition, read from the DOM, nothing to tune.
|
|
54
57
|
}
|
|
55
58
|
|
|
56
59
|
/**
|
package/src/index.js
CHANGED
|
@@ -147,7 +147,7 @@ try{var __c=localStorage.getItem("consent");
|
|
|
147
147
|
if(__c==="granted+ads")gtag("consent","update",{analytics_storage:"granted",ad_storage:"granted",ad_user_data:"granted"});
|
|
148
148
|
else if(__c==="granted")gtag("consent","update",{analytics_storage:"granted"});
|
|
149
149
|
else if(__c==="denied")gtag("consent","update",{analytics_storage:"denied",ad_storage:"denied",ad_user_data:"denied"})}catch(e){}
|
|
150
|
-
gtag("config",__g,{cookie_expires:60*60*24*60,page_title:document.title,allow_google_signals:false,allow_ad_personalization_signals:false});
|
|
150
|
+
gtag("config",__g,{cookie_expires:60*60*24*60,page_title:document.title,page_location:location.origin+location.pathname,allow_google_signals:false,allow_ad_personalization_signals:false});
|
|
151
151
|
addEventListener("load",function(){(window.requestIdleCallback||function(f){setTimeout(f,1200)})(function(){var s=document.createElement("script");s.async=true;s.src="https://www.googletagmanager.com/gtag/js?id="+__g;document.head.appendChild(s)})});
|
|
152
152
|
}})();`;
|
|
153
153
|
}
|
|
@@ -431,6 +431,51 @@ export function personaScroll(options) {
|
|
|
431
431
|
// a `?data=` link, a `?city=`, a `?mode=atrium`. A copy of this got that wrong.
|
|
432
432
|
const setHash = (id) => history.replaceState(null, "", location.pathname + location.search + "#" + id);
|
|
433
433
|
|
|
434
|
+
// queries: what is the 84px line · the scroll spy reports the wrong section · the hash flips back to
|
|
435
|
+
// #profile · clicking the avatar does not activate the kernel · the toolbar switches
|
|
436
|
+
// sections on its own · hash does not guarantee the cut · where does a cut begin
|
|
437
|
+
//
|
|
438
|
+
// WHERE A CUT BEGINS — ONE DEFINITION, USED BY THE NAVIGATOR AND BY THE SPY.
|
|
439
|
+
//
|
|
440
|
+
// This is the keystone of the whole behaviour, and getting it wrong is what made the hash a
|
|
441
|
+
// suggestion instead of a guarantee. There used to be TWO definitions: navigation scrolled to one
|
|
442
|
+
// place, and the spy tested against a different one — a hardcoded 84px line, paired by nothing but a
|
|
443
|
+
// comment with the `scroll-margin-top: 82px` that twelve personas each declared in their own CSS.
|
|
444
|
+
// Two magic numbers, two pixels apart, and the bug lived in the gap:
|
|
445
|
+
//
|
|
446
|
+
// click the avatar -> lands the kernel at 82 -> spy's last mid-flight sample read 85 -> profile
|
|
447
|
+
//
|
|
448
|
+
// A cut begins where the page COMES TO REST when you go to it. That is the only definition either
|
|
449
|
+
// side needs, and both now ask for it here — so the spy cannot contradict the navigator, because
|
|
450
|
+
// there is no second number for it to disagree with. The hash stops being a hint.
|
|
451
|
+
//
|
|
452
|
+
// Nothing is guessed. The position comes from the browser, and the landing inset comes from the
|
|
453
|
+
// CSS that already declares it (`scroll-margin-top`) rather than from a constant that has to be
|
|
454
|
+
// kept in sync by hand.
|
|
455
|
+
//
|
|
456
|
+
// THE KERNEL IS THE ONE EXCEPTION, and it is what kills an oscillation rather than papering over
|
|
457
|
+
// it. The kernel begins at the top of `#below` — above the sticky toolbar — not at its own box.
|
|
458
|
+
// The toolbar's title strip lives INSIDE `#below` and is in flow, so opening it pushes every
|
|
459
|
+
// section under it down by its own height (26px, measured). While the spy compared the kernel's own
|
|
460
|
+
// box against a fixed line, that 26px carried the kernel from 82 (inside the line) to 108 (outside
|
|
461
|
+
// it) — and the kernel being current is what opened the strip. Measured on app.zone by dispatching
|
|
462
|
+
// scroll events at a page that never moved:
|
|
463
|
+
//
|
|
464
|
+
// rest hash #profile kernelTop 82 strip 0 active profile
|
|
465
|
+
// spy run 1 hash #kernel kernelTop 108 strip 26 active kernel
|
|
466
|
+
// spy run 2 hash #profile kernelTop 82 strip 0 active profile
|
|
467
|
+
// spy run 3 hash #kernel kernelTop 108 strip 26 active kernel
|
|
468
|
+
//
|
|
469
|
+
// A bistable oscillator: the spy was measuring its own output. Anchoring the kernel to `#below`
|
|
470
|
+
// removes the coupling at the source, because the strip is inside that boundary and cannot move it.
|
|
471
|
+
// Widening the line would only have moved the tipping point.
|
|
472
|
+
const landingY = (sec) => {
|
|
473
|
+
if (sec === kernel) return below.getBoundingClientRect().top + window.scrollY;
|
|
474
|
+
const inset = parseFloat(getComputedStyle(sec).scrollMarginTop) || 0;
|
|
475
|
+
return sec.getBoundingClientRect().top + window.scrollY - inset;
|
|
476
|
+
};
|
|
477
|
+
const goTo = (sec, behavior) => window.scrollTo({ top: landingY(sec), behavior: behavior });
|
|
478
|
+
|
|
434
479
|
const land = () => {
|
|
435
480
|
// presentation mode owns the viewport — a page presenting itself does not scroll out from under
|
|
436
481
|
// whoever opened it
|
|
@@ -439,8 +484,7 @@ export function personaScroll(options) {
|
|
|
439
484
|
const target = cuts.find((c) => "#" + c[0].id === h);
|
|
440
485
|
// A section hash is honoured on load and refresh; anything else — including #kernel and no hash
|
|
441
486
|
// at all — lands on the kernel. `instant` overrides the page's own scroll-behavior: smooth.
|
|
442
|
-
|
|
443
|
-
else window.scrollTo({ top: below.getBoundingClientRect().top + window.scrollY, behavior: "instant" });
|
|
487
|
+
goTo(target ? target[0] : kernel, "instant");
|
|
444
488
|
};
|
|
445
489
|
// queries: the page did not land on the kernel · landing scroll ignored · scrollTo does nothing on
|
|
446
490
|
// first paint · the page opens at the top sometimes
|
|
@@ -452,11 +496,52 @@ export function personaScroll(options) {
|
|
|
452
496
|
// and the page opens at the top with nothing anywhere saying so. Measured in this behaviour's own
|
|
453
497
|
// fixture: `scrollTo({top: 548})` followed by a scroll event reading 0.
|
|
454
498
|
//
|
|
455
|
-
// So it is attempted again on the next frame and on `load
|
|
456
|
-
//
|
|
457
|
-
//
|
|
499
|
+
// So it is attempted again on the next frame and on `load`. It used to retry only while the page
|
|
500
|
+
// was still at the VERY TOP — a landing that did nothing — and that missed the case that actually
|
|
501
|
+
// bit: a landing that ARRIVED and then DRIFTED. Traced in the fixture:
|
|
502
|
+
//
|
|
503
|
+
// scrollTo(468) y = 468 the landing is exact
|
|
504
|
+
// DOMContentLoaded y = 494 docH +26 — the title strip opened
|
|
505
|
+
// settled y = 465 docH -26 — and the strip closed again
|
|
506
|
+
//
|
|
507
|
+
// The strip lives inside the sticky toolbar, ABOVE the kernel, so every time it opens or closes the
|
|
508
|
+
// browser moves the scroll position to keep the content under it steady. The page came to rest three
|
|
509
|
+
// pixels short of where the kernel begins — close enough to look landed, far enough that the spy
|
|
510
|
+
// correctly answered "profile", which closed the strip, which is what moved it there.
|
|
511
|
+
//
|
|
512
|
+
// A LANDING IS A PROMISE, AND IT IS KEPT UNTIL THE PERSON TAKES OVER. Not "until the page moves" —
|
|
513
|
+
// the page moves on its own here — but until somebody actually reaches for it. Once they have, the
|
|
514
|
+
// promise is void forever: nobody is ever yanked back to a section they scrolled away from.
|
|
515
|
+
let taken = false;
|
|
516
|
+
let landed = false;
|
|
517
|
+
const takeOver = () => { taken = true; };
|
|
518
|
+
// What "reaching for it" IS: a wheel, a finger, a key, a drag on the bar. A CLICK is deliberately
|
|
519
|
+
// not on this list — pressing a link to a cut is asking for a landing, not overriding one.
|
|
520
|
+
for (const ev of ["wheel", "touchmove", "keydown"]) window.addEventListener(ev, takeOver, { passive: true });
|
|
458
521
|
land();
|
|
459
|
-
const landIfUntouched = () => {
|
|
522
|
+
const landIfUntouched = () => {
|
|
523
|
+
// THE PROMISE BELONGS TO THE ARRIVAL, NOT TO THE PAGE'S WHOLE LIFE. It is released the first time
|
|
524
|
+
// the page is found resting where it was sent — after that this never runs again, so a persona's
|
|
525
|
+
// own scrolling, a deep link handled later, or anything else that moves the page is never undone.
|
|
526
|
+
// Kept open until then rather than for a fixed number of tries, because the drift that has to be
|
|
527
|
+
// repaired happens after load, once the title strip has opened and closed.
|
|
528
|
+
if (taken || landed) return;
|
|
529
|
+
const h = location.hash;
|
|
530
|
+
const t = cuts.find((c) => "#" + c[0].id === h);
|
|
531
|
+
const want = landingY(t ? t[0] : kernel);
|
|
532
|
+
// A pixel of slack: a browser is free to land a scroll a hair off the number it was handed, and
|
|
533
|
+
// re-asserting over a rounding error would fight the person for the rest of the page's life.
|
|
534
|
+
const off = Math.abs(window.scrollY - want);
|
|
535
|
+
// HOW FAR OFF IS STILL "THE LANDING DRIFTED", and how far is somebody else moving the page? The
|
|
536
|
+
// drift has a known size: it is caused by chrome above the kernel changing height, so it can never
|
|
537
|
+
// exceed that chrome. Anything larger is a deliberate scroll — a spec, a deep link handled later,
|
|
538
|
+
// a persona's own code — and is left alone. Derived from the toolbar rather than chosen, so it
|
|
539
|
+
// stays true if the bar ever changes shape.
|
|
540
|
+
const drift = toolbar ? toolbar.getBoundingClientRect().height : 0;
|
|
541
|
+
if (off > 1 && off <= drift) land();
|
|
542
|
+
else if (off <= 1) landed = true;
|
|
543
|
+
else landed = true; // moved somewhere it was never sent — the promise is over
|
|
544
|
+
};
|
|
460
545
|
requestAnimationFrame(landIfUntouched);
|
|
461
546
|
window.addEventListener("load", landIfUntouched);
|
|
462
547
|
|
|
@@ -476,26 +561,16 @@ export function personaScroll(options) {
|
|
|
476
561
|
o.titleFor ||
|
|
477
562
|
((id) => (id === "kernel" ? text(".pname") : id === "posts" ? text(".handle") : ""));
|
|
478
563
|
|
|
479
|
-
// queries: what is the 84px line · the scroll spy reports the wrong section · scroll-margin-top
|
|
480
|
-
//
|
|
481
|
-
// THE LINE IS TIED TO `scroll-margin-top`, NOT TO THE TOOLBAR'S HEIGHT.
|
|
482
|
-
//
|
|
483
|
-
// A section is current once its top crosses this line. 84 reads like a toolbar measurement and it
|
|
484
|
-
// is not: it is chosen to be >= the `scroll-margin-top` the persona pages give #kernel and #posts
|
|
485
|
-
// (82px), so that an anchor click — which lands a section AT its scroll-margin — puts that section
|
|
486
|
-
// at or above the line and the spy sees it.
|
|
487
|
-
//
|
|
488
|
-
// Deriving it from the toolbar was tried and is wrong for exactly that reason: the fixture's row
|
|
489
|
-
// measured 36, giving a line of 68, and a click on the kernel nav would then land the kernel at 82
|
|
490
|
-
// — below its own line — and the spy would answer "profile" for the section the person just asked
|
|
491
|
-
// for. Both numbers move together or neither does.
|
|
492
564
|
let lastCut = null;
|
|
493
565
|
let first = true;
|
|
494
|
-
|
|
566
|
+
// The cut you are on is the last one you have reached — asked in the one vocabulary `landingY`
|
|
567
|
+
// defines, so this can never answer something a click could not produce. A pixel of tolerance,
|
|
568
|
+
// because a browser is free to land a smooth scroll a hair short of the number it was given.
|
|
569
|
+
const measure = () => {
|
|
495
570
|
if (toolbar) toolbar.classList.toggle("stuck", window.scrollY >= below.offsetTop - 1);
|
|
496
|
-
const
|
|
571
|
+
const y = window.scrollY + 1;
|
|
497
572
|
let current = cuts[0];
|
|
498
|
-
for (const c of cuts) if (c[0]
|
|
573
|
+
for (const c of cuts) if (landingY(c[0]) <= y) current = c;
|
|
499
574
|
for (const c of cuts) c[1].classList.toggle("active", c === current);
|
|
500
575
|
if (current === lastCut) return;
|
|
501
576
|
lastCut = current;
|
|
@@ -517,20 +592,74 @@ export function personaScroll(options) {
|
|
|
517
592
|
}
|
|
518
593
|
}
|
|
519
594
|
};
|
|
520
|
-
|
|
521
|
-
|
|
595
|
+
// queries: scroll janks while the toolbar updates · scroll handler runs too often · the spy reads a
|
|
596
|
+
// stale position · the section is right but the toolbar is one behind · scrollend
|
|
597
|
+
//
|
|
598
|
+
// THE LISTENER SCHEDULES; THE FRAME MEASURES. AND THE LAST WORD IS ALWAYS THE RESTING POSITION.
|
|
599
|
+
//
|
|
600
|
+
// Two rules, and the second one is what actually fixed the reported bug.
|
|
601
|
+
//
|
|
602
|
+
// NEVER IN THE EVENT. `measure` reads geometry, and reading geometry inside a scroll handler forces
|
|
603
|
+
// the browser to lay the page out synchronously on a thread that is trying to move it. So the
|
|
604
|
+
// handler does nothing but ask for a frame; all reading and writing happens in that frame, at most
|
|
605
|
+
// once per frame however many events arrived. Intermediate positions are DISCARDED — only the
|
|
606
|
+
// newest is ever measured, which is the whole point of coalescing rather than throttling.
|
|
607
|
+
//
|
|
608
|
+
// AND THE PAGE MUST BE MEASURED WHERE IT STOPS. Scroll events stop arriving slightly before a
|
|
609
|
+
// smooth scroll finishes settling, so the last position the old code ever evaluated was in flight:
|
|
610
|
+
// it read 85, decided "profile", and the page then came to rest at 82 — inside the kernel — with
|
|
611
|
+
// nothing left to run and say so. That single unread pixel is the entire reported symptom.
|
|
612
|
+
//
|
|
613
|
+
// So a frame that finds the page still moving asks for another one, and the loop ends on the frame
|
|
614
|
+
// where nothing changed — which is, by construction, the resting position. No timer, no `scrollend`
|
|
615
|
+
// (still not everywhere), and nothing running once the page is still.
|
|
616
|
+
let queued = false;
|
|
617
|
+
let lastY = null;
|
|
618
|
+
const frame = () => {
|
|
619
|
+
queued = false;
|
|
620
|
+
const y = window.scrollY;
|
|
621
|
+
const moved = y !== lastY;
|
|
622
|
+
lastY = y;
|
|
623
|
+
measure();
|
|
624
|
+
if (moved) schedule();
|
|
625
|
+
// The page has stopped. If it stopped somewhere it was never sent, and nobody has taken over,
|
|
626
|
+
// finish the landing — this is where the three-pixel drift above is actually repaired, because
|
|
627
|
+
// the drift happens after every earlier attempt has already run.
|
|
628
|
+
else landIfUntouched();
|
|
629
|
+
};
|
|
630
|
+
const schedule = () => { if (!queued) { queued = true; requestAnimationFrame(frame); } };
|
|
631
|
+
window.addEventListener("scroll", schedule, { passive: true });
|
|
632
|
+
measure();
|
|
633
|
+
|
|
634
|
+
// queries: a link to #kernel does not activate the kernel · the avatar link does not land · in-page
|
|
635
|
+
// hash link · anchor click · hash from another page
|
|
636
|
+
//
|
|
637
|
+
// EVERY HASH LINK IS A CUT LINK — not only the three in the toolbar.
|
|
638
|
+
//
|
|
639
|
+
// The toolbar's own navs used to be the only anchors wired up, so any OTHER link to the same place
|
|
640
|
+
// fell through to the browser's native anchor jump and was then overruled by the spy. The profile
|
|
641
|
+
// avatar is exactly that: a link to `#kernel` that took you near the kernel and left the page
|
|
642
|
+
// saying `#profile`. Delegated, so a cut link works wherever a persona puts one — today the avatar,
|
|
643
|
+
// tomorrow something nobody has written yet — with no registration and no per-persona wiring.
|
|
644
|
+
//
|
|
645
|
+
// Programmatic rather than native, so the landing is the one `landingY` defines. A native jump uses
|
|
646
|
+
// `scroll-margin` alone and lands the kernel below the sticky bar it should sit under.
|
|
647
|
+
document.addEventListener("click", (e) => {
|
|
648
|
+
const a = e.target && e.target.closest ? e.target.closest('a[href^="#"]') : null;
|
|
649
|
+
if (!a) return;
|
|
650
|
+
const t = cuts.find((c) => "#" + c[0].id === a.getAttribute("href"));
|
|
651
|
+
if (!t) return;
|
|
652
|
+
e.preventDefault();
|
|
653
|
+
goTo(t[0], "smooth");
|
|
654
|
+
setHash(t[0].id);
|
|
655
|
+
});
|
|
522
656
|
|
|
523
|
-
//
|
|
524
|
-
//
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
if (sec === kernel) window.scrollTo({ top: below.getBoundingClientRect().top + window.scrollY, behavior: "smooth" });
|
|
530
|
-
else sec.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
531
|
-
setHash(sec.id);
|
|
532
|
-
}),
|
|
533
|
-
);
|
|
657
|
+
// A hash that arrives without a click — the back button, or a link from another page to this one.
|
|
658
|
+
// `setHash` uses replaceState and fires nothing, so this can only be somebody else's navigation.
|
|
659
|
+
window.addEventListener("hashchange", () => {
|
|
660
|
+
const t = cuts.find((c) => "#" + c[0].id === location.hash);
|
|
661
|
+
if (t) goTo(t[0], "smooth");
|
|
662
|
+
});
|
|
534
663
|
}
|
|
535
664
|
|
|
536
665
|
// queries: persona scroll for a surface that cannot bundle · inline scroll script · feed toolbar
|