@crossworks/share-ui 0.235.1 → 0.237.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossworks/share-ui",
3
- "version": "0.235.1",
3
+ "version": "0.237.1",
4
4
  "description": "The server-rendered share surface — the /s/<token> presenters, view-payload contract, mini-app sandbox, and the few primitives they need. Lives in MANTLE (the server renders these), published for jackdaw to consume; may depend only on @mantle/{client-types,content-core} (the jackdaw-repo-split boundary).",
5
5
  "exports": {
6
6
  "./app-presenter": "./src/app-presenter.tsx",
@@ -13,6 +13,7 @@
13
13
  "./table-presenter": "./src/table-presenter.tsx",
14
14
  "./task-presenter": "./src/task-presenter.tsx",
15
15
  "./view-payload": "./src/view-payload.ts",
16
+ "./heading-fold-dom": "./src/heading-fold-dom.ts",
16
17
  "./app-sandbox": "./src/app-sandbox.tsx",
17
18
  "./app-frame-html": "./src/app-frame-html.ts",
18
19
  "./app-bridge-protocol": "./src/app-bridge-protocol.ts",
@@ -38,8 +39,8 @@
38
39
  "@dicebear/core": "^10.7.0",
39
40
  "@dicebear/styles": "^10.6.0",
40
41
  "@firecms/neat": "1.0.2",
41
- "@mantle/client-types": "npm:@crossworks/client-types@0.235.1",
42
- "@mantle/content-core": "npm:@crossworks/content-core@0.235.1",
42
+ "@mantle/client-types": "npm:@crossworks/client-types@0.237.1",
43
+ "@mantle/content-core": "npm:@crossworks/content-core@0.237.1",
43
44
  "@radix-ui/react-label": "^2.1.15",
44
45
  "@radix-ui/react-slot": "^1.3.3",
45
46
  "class-variance-authority": "^0.7.1",
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Foldable headings on a STATIC render: the share reader (/s pages), and the
3
+ * client's read views (StaticDoc / PageView), which all draw a page as plain
4
+ * HTML with no editor behind it. The live editor does the same job with a
5
+ * ProseMirror plugin over the same rules (content-core heading-fold.ts).
6
+ *
7
+ * A foldable heading arrives as `<hN data-fold="open|closed">`. This adds the
8
+ * arrow button in front of its text and hides its section by setting
9
+ * `data-fold-hidden` on the blocks under it (app.css hides those on screen
10
+ * only, so print always shows the whole page). A click anywhere on the
11
+ * heading row folds or unfolds it, except a click on a link inside it. The
12
+ * reader's choice is remembered in localStorage by the heading's block id
13
+ * (`data-block-id` on the client render, `id` on the server render).
14
+ *
15
+ * Without script (the print surface, a reader with JS off) nothing here runs:
16
+ * the page shows every section open and no arrow, which reads fine.
17
+ *
18
+ * The arrows are added once per heading, so a second call on the same root
19
+ * adds none. Returns a cleanup that removes the click listener.
20
+ */
21
+ import {
22
+ foldSections,
23
+ isFolded,
24
+ readFoldChoices,
25
+ writeFoldChoice,
26
+ } from '@mantle/content-core/heading-fold';
27
+
28
+ const HEADING_SEL = 'h1[data-fold], h2[data-fold], h3[data-fold]';
29
+ const WIRED_ATTR = 'data-folds-wired';
30
+ export const FOLD_TOGGLE_CLASS = 'fold-toggle';
31
+
32
+ /** The chevron (lucide chevron-right). app.css turns it down when open. */
33
+ export const FOLD_CHEVRON_SVG =
34
+ '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" ' +
35
+ 'stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg>';
36
+
37
+ function headingLevel(el: Element): number | null {
38
+ const m = /^H([1-6])$/.exec(el.tagName);
39
+ return m ? Number(m[1]) : null;
40
+ }
41
+
42
+ function headingId(el: HTMLElement): string {
43
+ return el.getAttribute('data-block-id') || el.id || '';
44
+ }
45
+
46
+ /** Hide or show every section under the foldable headings in `root`. */
47
+ function apply(root: HTMLElement): void {
48
+ const choices = readFoldChoices();
49
+ const parents = new Set<Element>();
50
+ for (const h of root.querySelectorAll<HTMLElement>(HEADING_SEL)) {
51
+ if (h.parentElement) parents.add(h.parentElement);
52
+ const folded = isFolded(headingId(h), h.getAttribute('data-fold'), choices);
53
+ h.toggleAttribute('data-folded', folded);
54
+ const btn = h.querySelector(`:scope > .${FOLD_TOGGLE_CLASS}`);
55
+ btn?.setAttribute('aria-expanded', String(!folded));
56
+ btn?.setAttribute('aria-label', folded ? 'Unfold section' : 'Fold section');
57
+ }
58
+ for (const parent of parents) {
59
+ const kids = Array.from(parent.children) as HTMLElement[];
60
+ const hidden = new Array<boolean>(kids.length).fill(false);
61
+ const sections = foldSections(
62
+ kids.length,
63
+ (i) => headingLevel(kids[i]!),
64
+ (i) => kids[i]!.hasAttribute('data-fold'),
65
+ );
66
+ for (const [i, end] of sections) {
67
+ if (!kids[i]!.hasAttribute('data-folded')) continue;
68
+ for (let j = i + 1; j < end; j++) hidden[j] = true;
69
+ }
70
+ kids.forEach((k, i) => k.toggleAttribute('data-fold-hidden', hidden[i]!));
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Open every fold that hides `el` (an outline jump to a heading inside a
76
+ * folded section). The opening is remembered like a click. No-op when `el`
77
+ * is not in a wired render or is not hidden.
78
+ */
79
+ export function revealInFolds(el: Element | null | undefined): void {
80
+ const root = el?.closest<HTMLElement>(`[${WIRED_ATTR}]`);
81
+ if (!el || !root) return;
82
+ for (let guard = 0; guard < 32; guard++) {
83
+ const hid = el.closest('[data-fold-hidden]');
84
+ if (!hid || !root.contains(hid)) return;
85
+ // The nearest folded heading above it whose section still covers it.
86
+ let opened = false;
87
+ let minLevel = Infinity; // the highest heading met between it and `hid`
88
+ for (let p = hid.previousElementSibling; p; p = p.previousElementSibling) {
89
+ const lv = headingLevel(p);
90
+ if (lv == null) continue;
91
+ if (p.hasAttribute('data-folded') && lv < minLevel) {
92
+ writeFoldChoice(headingId(p as HTMLElement), false);
93
+ if (!headingId(p as HTMLElement)) p.setAttribute('data-fold', 'open');
94
+ apply(root);
95
+ opened = true;
96
+ break;
97
+ }
98
+ minLevel = Math.min(minLevel, lv);
99
+ }
100
+ if (!opened) return;
101
+ }
102
+ }
103
+
104
+ export function wireHeadingFolds(root: HTMLElement | null | undefined): () => void {
105
+ if (!root) return () => {};
106
+ const doc = root.ownerDocument;
107
+ for (const h of root.querySelectorAll<HTMLElement>(HEADING_SEL)) {
108
+ if (h.querySelector(`:scope > .${FOLD_TOGGLE_CLASS}`)) continue;
109
+ const btn = doc.createElement('button');
110
+ btn.type = 'button';
111
+ btn.className = FOLD_TOGGLE_CLASS;
112
+ btn.innerHTML = FOLD_CHEVRON_SVG;
113
+ h.prepend(btn);
114
+ }
115
+ root.setAttribute(WIRED_ATTR, '');
116
+ apply(root);
117
+
118
+ const onClick = (e: MouseEvent) => {
119
+ const target = e.target as Element | null;
120
+ const h = target?.closest<HTMLElement>(HEADING_SEL);
121
+ if (!h || !root.contains(h)) return;
122
+ // A link in the heading still goes where it points.
123
+ if (target?.closest('a') && !target.closest(`.${FOLD_TOGGLE_CLASS}`)) return;
124
+ // A drag to select the heading's words is not a click on it.
125
+ const sel = doc.getSelection();
126
+ if (sel && !sel.isCollapsed && h.contains(sel.anchorNode)) return;
127
+ e.preventDefault();
128
+ writeFoldChoice(headingId(h), !h.hasAttribute('data-folded'));
129
+ // A heading without an id cannot be remembered: flip it for this view.
130
+ if (!headingId(h))
131
+ h.setAttribute('data-fold', h.hasAttribute('data-folded') ? 'open' : 'closed');
132
+ apply(root);
133
+ };
134
+ root.addEventListener('click', onClick);
135
+ return () => root.removeEventListener('click', onClick);
136
+ }
@@ -4,6 +4,7 @@ import { useEffect, useState } from 'react';
4
4
  import { ChevronLeft, FileText, ListTree } from 'lucide-react';
5
5
  import { cn } from './lib/utils';
6
6
  import type { TocEntry } from '@mantle/content-core/page-toc';
7
+ import { revealInFolds } from './heading-fold-dom';
7
8
 
8
9
  const COLLAPSE_KEY = 'mantle:page-outline-collapsed';
9
10
 
@@ -77,7 +78,10 @@ export function PageOutline({
77
78
  return;
78
79
  }
79
80
  if (typeof document !== 'undefined') {
80
- document.getElementById(id)?.scrollIntoView({ behavior: 'smooth', block: 'start' });
81
+ const el = document.getElementById(id);
82
+ // A heading inside a folded section opens first, or there is nothing to see.
83
+ revealInFolds(el);
84
+ el?.scrollIntoView({ behavior: 'smooth', block: 'start' });
81
85
  }
82
86
  };
83
87
 
package/styles/app.css CHANGED
@@ -571,6 +571,66 @@
571
571
  border-radius: 0.2rem;
572
572
  }
573
573
 
574
+ /* Foldable headings (content-core heading-fold.ts). One look on every
575
+ surface: the editor draws the arrow as a widget, the static renders
576
+ (share reader, read views) add it with heading-fold-dom.ts; both use
577
+ `.fold-toggle` and mark a folded heading `data-folded`. The arrow sits in
578
+ front of the heading text, sideways when folded and down when open. Hidden
579
+ blocks carry `data-fold-hidden`, hidden on SCREEN only: print shows the
580
+ whole page unfolded and drops the arrows. */
581
+ .ProseMirror .fold-toggle {
582
+ display: inline-flex;
583
+ align-items: center;
584
+ justify-content: center;
585
+ width: 1em;
586
+ height: 1em;
587
+ margin-right: 0.3em;
588
+ padding: 0;
589
+ border: 0;
590
+ border-radius: 0.25rem;
591
+ background: transparent;
592
+ color: var(--muted-foreground);
593
+ vertical-align: -0.1em;
594
+ cursor: pointer;
595
+ user-select: none;
596
+ transition:
597
+ background-color 120ms,
598
+ color 120ms;
599
+ }
600
+ .ProseMirror .fold-toggle:hover {
601
+ background-color: var(--accent);
602
+ color: var(--accent-foreground);
603
+ }
604
+ .ProseMirror .fold-toggle:focus-visible {
605
+ outline: 2px solid var(--ring);
606
+ outline-offset: 1px;
607
+ }
608
+ .ProseMirror .fold-toggle svg {
609
+ width: 0.7em;
610
+ height: 0.7em;
611
+ transform: rotate(90deg);
612
+ transition: transform 150ms;
613
+ }
614
+ .ProseMirror [data-folded] > .fold-toggle svg {
615
+ transform: rotate(0deg);
616
+ }
617
+ /* A folded heading opens on a click anywhere on it; a static render folds
618
+ and opens on a click on the row too. */
619
+ .ProseMirror [data-fold][data-folded],
620
+ .ProseMirror[data-folds-wired] [data-fold] {
621
+ cursor: pointer;
622
+ }
623
+ @media screen {
624
+ .ProseMirror [data-fold-hidden] {
625
+ display: none !important;
626
+ }
627
+ }
628
+ @media print {
629
+ .ProseMirror .fold-toggle {
630
+ display: none !important;
631
+ }
632
+ }
633
+
574
634
  /* Callouts on the raw/public render. In-app callouts use a React NodeView; the
575
635
  public page surface (server-rendered HTML) emits raw [data-callout] divs, so
576
636
  these rules style only that path — a tinted panel + left accent, themed per