@justai/core 0.6.0 → 0.7.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 +1 -1
- package/src/index.d.ts +25 -0
- package/src/index.js +155 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@justai/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "The framework-free heart of the persona shell \u2014 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": {
|
package/src/index.d.ts
CHANGED
|
@@ -34,3 +34,28 @@ export declare const PRESENTATION_EMBEDDED: string;
|
|
|
34
34
|
* agrees we are in a frame. Presentation only — never a permission. See the note in index.js.
|
|
35
35
|
*/
|
|
36
36
|
export declare function presentation(loc?: Location, win?: Window): "standalone" | "embedded";
|
|
37
|
+
|
|
38
|
+
export interface PersonaScrollOptions {
|
|
39
|
+
/** Section ids in document order. Default `["profile", "kernel", "posts"]`. */
|
|
40
|
+
cuts?: string[];
|
|
41
|
+
/** Id of the element the toolbar sits at the top of. Default `"below"`. */
|
|
42
|
+
below?: string | HTMLElement;
|
|
43
|
+
toolbar?: string | HTMLElement;
|
|
44
|
+
title?: string | HTMLElement;
|
|
45
|
+
titleText?: string | HTMLElement;
|
|
46
|
+
/** Maps a section id to its nav's id. Default `profile` → `navProfile`. */
|
|
47
|
+
navFor?: (id: string) => string;
|
|
48
|
+
/** What the title strip says for a section. Default: kernel → `.pname`, posts → `.handle`. */
|
|
49
|
+
titleFor?: (id: string) => string;
|
|
50
|
+
/** Whether this owns `body.ready`. Pass `false` on a page that renders asynchronously. */
|
|
51
|
+
reveal?: boolean;
|
|
52
|
+
/** The toolbar line a section must cross to be current. Default 84. */
|
|
53
|
+
line?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The persona page's scroll behaviour: land on the kernel, the toolbar hairline, the scroll-spy,
|
|
58
|
+
* the title strip and the hash. Framework-free, so the Astro cut, the Next face and hand-written
|
|
59
|
+
* surfaces (`/me`, `/files`, the feed) run one implementation rather than four copies.
|
|
60
|
+
*/
|
|
61
|
+
export declare function personaScroll(options?: PersonaScrollOptions): void;
|
package/src/index.js
CHANGED
|
@@ -377,3 +377,158 @@ export function presentation(loc, win) {
|
|
|
377
377
|
const framed = w.parent !== w;
|
|
378
378
|
return declared === PRESENTATION_EMBEDDED && framed ? "embedded" : "standalone";
|
|
379
379
|
}
|
|
380
|
+
|
|
381
|
+
// queries: persona page scroll behaviour · land on the kernel · scroll spy · toolbar title strip ·
|
|
382
|
+
// the page opens on the profile instead of the kernel · toolbar hairline · hash sync ·
|
|
383
|
+
// why is this in core rather than in the Toolbar cut
|
|
384
|
+
//
|
|
385
|
+
// THE PERSONA PAGE'S SCROLL BEHAVIOUR, ONCE, FOR EVERY SURFACE THAT HAS ONE.
|
|
386
|
+
//
|
|
387
|
+
// A persona page lands on its KERNEL. The profile is the face — it is there when you go looking for
|
|
388
|
+
// it, and scrolling up is how you look; what a person came for is the thing the persona does. Once
|
|
389
|
+
// the profile has scrolled away, the name it carried reappears in the toolbar's second row, or the
|
|
390
|
+
// page loses its own title the moment you start using it.
|
|
391
|
+
//
|
|
392
|
+
// WHY HERE AND NOT IN THE TOOLBAR CUT. `@justai/cuts/Toolbar` owned this and still calls it, but the
|
|
393
|
+
// cut can only serve pages that adopt its MARKUP — and three surfaces cannot: `/me` and `/files`
|
|
394
|
+
// carry extra navigation of their own, and the feed is a different build. So all three hand-wrote
|
|
395
|
+
// the same eighty lines, and by the third copy they had already drifted: `/files` opened on the
|
|
396
|
+
// profile with an empty title strip for a week, and its hash writer clobbered the query string
|
|
397
|
+
// (`?mode=atrium` disappeared on the first scroll) where the cut's preserves it.
|
|
398
|
+
//
|
|
399
|
+
// That is the shape core exists for — the load-bearing behaviour, framework-free, so an Astro cut, a
|
|
400
|
+
// Next face and a hand-written page all run the SAME logic instead of three versions of it. What
|
|
401
|
+
// stays in the cut is the markup and the CSS; what moves here is the part that was being copied.
|
|
402
|
+
//
|
|
403
|
+
// Every element is found by id so a surface can bring its own markup: only the ids are the contract.
|
|
404
|
+
export function personaScroll(options) {
|
|
405
|
+
const o = options || {};
|
|
406
|
+
const cutIds = o.cuts || ["profile", "kernel", "posts"];
|
|
407
|
+
const el = (id) => (typeof id === "string" ? document.getElementById(id) : id);
|
|
408
|
+
|
|
409
|
+
const below = el(o.below || "below");
|
|
410
|
+
const kernel = el("kernel");
|
|
411
|
+
if (!below || !kernel) return;
|
|
412
|
+
|
|
413
|
+
const toolbar = typeof o.toolbar === "string" ? document.querySelector(o.toolbar) : o.toolbar || document.querySelector(".toolbar");
|
|
414
|
+
const tbTitle = el(o.title || "tbTitle");
|
|
415
|
+
const tbTitleText =
|
|
416
|
+
(typeof o.titleText === "string" ? document.getElementById(o.titleText) : o.titleText) ||
|
|
417
|
+
document.getElementById("tbTitleText") ||
|
|
418
|
+
(tbTitle && tbTitle.querySelector(".tb-title-text"));
|
|
419
|
+
|
|
420
|
+
// A cut is a section AND the nav that points at it; anything missing either half is simply not one.
|
|
421
|
+
// That is how a page with extra toolbar links — /me's vault, /files' host link — needs no
|
|
422
|
+
// configuration: they are links, not cuts, and they fall out of this list on their own.
|
|
423
|
+
const cuts = cutIds
|
|
424
|
+
.map((id) => [el(id), el(o.navFor ? o.navFor(id) : "nav" + id.charAt(0).toUpperCase() + id.slice(1))])
|
|
425
|
+
.filter((c) => c[0] && c[1]);
|
|
426
|
+
if (!cuts.length) return;
|
|
427
|
+
|
|
428
|
+
if ("scrollRestoration" in history) history.scrollRestoration = "manual";
|
|
429
|
+
|
|
430
|
+
// Only the FRAGMENT. `"#" + id` would clobber the path and the query, and both carry meaning here:
|
|
431
|
+
// a `?data=` link, a `?city=`, a `?mode=atrium`. A copy of this got that wrong.
|
|
432
|
+
const setHash = (id) => history.replaceState(null, "", location.pathname + location.search + "#" + id);
|
|
433
|
+
|
|
434
|
+
const land = () => {
|
|
435
|
+
// presentation mode owns the viewport — a page presenting itself does not scroll out from under
|
|
436
|
+
// whoever opened it
|
|
437
|
+
if (document.body.dataset.present) return;
|
|
438
|
+
const h = location.hash;
|
|
439
|
+
const target = cuts.find((c) => "#" + c[0].id === h);
|
|
440
|
+
// A section hash is honoured on load and refresh; anything else — including #kernel and no hash
|
|
441
|
+
// at all — lands on the kernel. `instant` overrides the page's own scroll-behavior: smooth.
|
|
442
|
+
if (target && target[0] !== kernel) target[0].scrollIntoView({ behavior: "instant", block: "start" });
|
|
443
|
+
else window.scrollTo({ top: below.getBoundingClientRect().top + window.scrollY, behavior: "instant" });
|
|
444
|
+
};
|
|
445
|
+
// queries: the page did not land on the kernel · landing scroll ignored · scrollTo does nothing on
|
|
446
|
+
// first paint · the page opens at the top sometimes
|
|
447
|
+
//
|
|
448
|
+
// LANDED, OR TRIED AGAIN — because a scroll can be silently refused.
|
|
449
|
+
//
|
|
450
|
+
// `scrollTo` past the current document height is a no-op, and it does not report one: on a page
|
|
451
|
+
// whose sections size themselves a frame later, the landing is CALLED, the browser declines it,
|
|
452
|
+
// and the page opens at the top with nothing anywhere saying so. Measured in this behaviour's own
|
|
453
|
+
// fixture: `scrollTo({top: 548})` followed by a scroll event reading 0.
|
|
454
|
+
//
|
|
455
|
+
// So it is attempted again on the next frame and on `load` — but only while the page is still at
|
|
456
|
+
// the very top, which is the only state that can mean "the landing did not take". Somebody who has
|
|
457
|
+
// started reading is never yanked back.
|
|
458
|
+
land();
|
|
459
|
+
const landIfUntouched = () => { if (window.scrollY === 0) land(); };
|
|
460
|
+
requestAnimationFrame(landIfUntouched);
|
|
461
|
+
window.addEventListener("load", landIfUntouched);
|
|
462
|
+
|
|
463
|
+
// The reveal rides with this on pages whose content is ready at first paint. A page that renders
|
|
464
|
+
// asynchronously — the vault, the file kernel — holds paint until its own data has arrived and
|
|
465
|
+
// passes `reveal: false`, because revealing before then shows an empty room.
|
|
466
|
+
if (o.reveal !== false) {
|
|
467
|
+
const reveal = () => document.body.classList.add("ready");
|
|
468
|
+
requestAnimationFrame(reveal);
|
|
469
|
+
setTimeout(reveal, 500);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const text = (sel) => (document.querySelector(sel)?.textContent || "").trim();
|
|
473
|
+
// Kernel names the persona, Posts names its handle, Profile stays collapsed — its big header IS
|
|
474
|
+
// the title, and repeating it in the bar above would say the same thing twice.
|
|
475
|
+
const titleFor =
|
|
476
|
+
o.titleFor ||
|
|
477
|
+
((id) => (id === "kernel" ? text(".pname") : id === "posts" ? text(".handle") : ""));
|
|
478
|
+
|
|
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
|
+
let lastCut = null;
|
|
493
|
+
let first = true;
|
|
494
|
+
const onScroll = () => {
|
|
495
|
+
if (toolbar) toolbar.classList.toggle("stuck", window.scrollY >= below.offsetTop - 1);
|
|
496
|
+
const line = o.line == null ? 84 : o.line;
|
|
497
|
+
let current = cuts[0];
|
|
498
|
+
for (const c of cuts) if (c[0].getBoundingClientRect().top <= line) current = c;
|
|
499
|
+
for (const c of cuts) c[1].classList.toggle("active", c === current);
|
|
500
|
+
if (current === lastCut) return;
|
|
501
|
+
lastCut = current;
|
|
502
|
+
if (current[0].id && "#" + current[0].id !== location.hash) setHash(current[0].id);
|
|
503
|
+
|
|
504
|
+
const t = titleFor(current[0].id);
|
|
505
|
+
if (first) {
|
|
506
|
+
// First paint is not a transition: the page opens at the kernel, so the title is already true
|
|
507
|
+
// when it arrives. Animating it in tells a person something changed when nothing did.
|
|
508
|
+
first = false;
|
|
509
|
+
if (tbTitle) { tbTitle.style.transition = "none"; tbTitle.classList.toggle("show", !!t); }
|
|
510
|
+
if (t && tbTitleText) tbTitleText.textContent = t;
|
|
511
|
+
if (tbTitle) { void tbTitle.offsetHeight; tbTitle.style.transition = ""; }
|
|
512
|
+
} else {
|
|
513
|
+
if (tbTitle) tbTitle.classList.toggle("show", !!t);
|
|
514
|
+
if (t && tbTitleText && tbTitleText.textContent !== t) {
|
|
515
|
+
tbTitleText.classList.add("fade");
|
|
516
|
+
setTimeout(() => { tbTitleText.textContent = t; tbTitleText.classList.remove("fade"); }, 150);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
window.addEventListener("scroll", onScroll, { passive: true });
|
|
521
|
+
onScroll();
|
|
522
|
+
|
|
523
|
+
// Nav clicks scroll PROGRAMMATICALLY so the sticky toolbar always pins flush. A native anchor plus
|
|
524
|
+
// scroll-margin left a gap above the bar when jumping to the kernel from the profile: the title
|
|
525
|
+
// strip is still collapsed there, so the bar is shorter than the margin it was given.
|
|
526
|
+
cuts.forEach(([sec, nav]) =>
|
|
527
|
+
nav.addEventListener("click", (e) => {
|
|
528
|
+
e.preventDefault();
|
|
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
|
+
);
|
|
534
|
+
}
|