@tristan2828/ui-foundation 4.156.0 → 4.157.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.
@@ -26,26 +26,26 @@
26
26
  - [A variant: a pressed icon inside another value's cell](#a-variant-a-pressed-icon-inside-another-values-cell) · lines 636–715
27
27
  - [16. Title linking to the record's view](#16-title-linking-to-the-records-view) · lines 717–758
28
28
  - [17. Quick action on the view](#17-quick-action-on-the-view) · lines 760–832
29
- - [18. Editable value](#18-editable-value) · lines 834–1702
29
+ - [18. Editable value](#18-editable-value) · lines 834–1705
30
30
  - [Presets: one line per field](#presets-one-line-per-field) · lines 1240–1312
31
31
  - [Several fields edited as one](#several-fields-edited-as-one) · lines 1314–1350
32
32
  - [A link and its label](#a-link-and-its-label) · lines 1352–1376
33
33
  - [A list of sub-items](#a-list-of-sub-items) · lines 1378–1511
34
- - [On a page of the app's own: `EditInPlaceScope`](#on-a-page-of-the-apps-own-editinplacescope) · lines 1513–1619
35
- - [In a table's rows](#in-a-tables-rows) · lines 1621–1695
36
- - [A table of a few short columns: `width="content"`](#a-table-of-a-few-short-columns-widthcontent) · lines 1697–1702
37
- - [19. Stage circle for an ordered status](#19-stage-circle-for-an-ordered-status) · lines 1704–1770
38
- - [20. Count linking to the related records](#20-count-linking-to-the-related-records) · lines 1772–1826
39
- - [21. Rows grouped under headings, and a pill filter](#21-rows-grouped-under-headings-and-a-pill-filter) · lines 1828–1910
40
- - [22. A highlighted row, and rows that are done](#22-a-highlighted-row-and-rows-that-are-done) · lines 1912–1937
41
- - [28. A list's rows: hover, lines, rows per page](#28-a-lists-rows-hover-lines-rows-per-page) · lines 1939–1972
42
- - [23. A Group menu, and a record read beside the list](#23-a-group-menu-and-a-record-read-beside-the-list) · lines 1974–2025
43
- - [24. A stored colour beside its name](#24-a-stored-colour-beside-its-name) · lines 2027–2101
44
- - [Picking one on a form: `ColorField`](#picking-one-on-a-form-colorfield) · lines 2047–2101
45
- - [25. A list's tools: views, filters, columns, row actions, a phone's rows](#25-a-lists-tools-views-filters-columns-row-actions-a-phones-rows) · lines 2103–2315
46
- - [The framed look, and sibling lists beside the title](#the-framed-look-and-sibling-lists-beside-the-title) · lines 2264–2315
47
- - [26. A record's related list, capped](#26-a-records-related-list-capped) · lines 2317–2351
48
- - [27. A repeat and its next time](#27-a-repeat-and-its-next-time) · lines 2353–2400
34
+ - [On a page of the app's own: `EditInPlaceScope`](#on-a-page-of-the-apps-own-editinplacescope) · lines 1513–1622
35
+ - [In a table's rows](#in-a-tables-rows) · lines 1624–1698
36
+ - [A table of a few short columns: `width="content"`](#a-table-of-a-few-short-columns-widthcontent) · lines 1700–1705
37
+ - [19. Stage circle for an ordered status](#19-stage-circle-for-an-ordered-status) · lines 1707–1773
38
+ - [20. Count linking to the related records](#20-count-linking-to-the-related-records) · lines 1775–1829
39
+ - [21. Rows grouped under headings, and a pill filter](#21-rows-grouped-under-headings-and-a-pill-filter) · lines 1831–1913
40
+ - [22. A highlighted row, and rows that are done](#22-a-highlighted-row-and-rows-that-are-done) · lines 1915–1940
41
+ - [28. A list's rows: hover, lines, rows per page](#28-a-lists-rows-hover-lines-rows-per-page) · lines 1942–1975
42
+ - [23. A Group menu, and a record read beside the list](#23-a-group-menu-and-a-record-read-beside-the-list) · lines 1977–2028
43
+ - [24. A stored colour beside its name](#24-a-stored-colour-beside-its-name) · lines 2030–2104
44
+ - [Picking one on a form: `ColorField`](#picking-one-on-a-form-colorfield) · lines 2050–2104
45
+ - [25. A list's tools: views, filters, columns, row actions, a phone's rows](#25-a-lists-tools-views-filters-columns-row-actions-a-phones-rows) · lines 2106–2318
46
+ - [The framed look, and sibling lists beside the title](#the-framed-look-and-sibling-lists-beside-the-title) · lines 2267–2318
47
+ - [26. A record's related list, capped](#26-a-records-related-list-capped) · lines 2320–2354
48
+ - [27. A repeat and its next time](#27-a-repeat-and-its-next-time) · lines 2356–2403
49
49
 
50
50
  <!-- /index -->
51
51
 
@@ -1556,14 +1556,17 @@ const body = editInPlace<string>({
1556
1556
  router's updates to `flushSync`. With the other, the hidden tab is
1557
1557
  still in front for a moment, and a key typed right after the switch
1558
1558
  goes into it.
1559
- - **An outline beside a long note** is `NotesOutline`, given the note's
1560
- Markdown and the editor's `foldKey`: its headings as a list to jump
1561
- with (each opens its section and brings it into view), with Fold All
1562
- and Open All. Put it beside the editor (in a rail, or above the note
1563
- on a phone), fed the value the editor last reported. It parses the
1564
- whole note on every change, so a page with several mounted editors
1565
- (a notes app's tabs) renders one outline, for the note in front,
1566
- never one per pane.
1559
+ - **An outline beside a long note** is `NotesOutline`, given the
1560
+ editor's `foldKey`: its headings as a list to jump with (each opens
1561
+ its section and brings it into view), with Fold All and Open All. Put
1562
+ it beside the editor (in a rail, or above the note on a phone). It
1563
+ needs no `markdown`: it lists the headings of the mounted editor with
1564
+ that key, a frame after one is added, renamed or toggled, and parses
1565
+ nothing, so typing in a paragraph and saving never touch it. Beside
1566
+ `<Markdown>`, or for the moment before the editor is up, pass
1567
+ `markdown` and it reads that instead. An app that draws its own list
1568
+ reads the same headings with `useFoldHeadings(foldKey)` (`null` with
1569
+ no editor under the key).
1567
1570
  - **An editor that is the page** (a note the app opens on) takes
1568
1571
  `autoFocus="end"`, the caret after its last words, in view, and
1569
1572
  `tabStaysInText`: Tab on a plain line stays in the text (Esc, then
@@ -97,7 +97,7 @@ import { DEFAULT_HIGHLIGHT, highlightHandler, isHighlightColor, remarkHighlightN
97
97
  import { textTopHeading } from '../../lib/markdown-top-heading.js';
98
98
  import { MarkdownMergeError, markdownStyle, mergeMarkdown } from '../../lib/markdown-merge.js';
99
99
  import { holdPendingEdit } from '../../lib/pending-edits.js';
100
- import { foldStore, listenForOtherTabs } from '../../hooks/fold-store.js';
100
+ import { foldStore, headingStore, listenForOtherTabs } from '../../hooks/fold-store.js';
101
101
  import { toggleFold } from '../../lib/markdown-fold.js';
102
102
  import { DEFAULT_HEADING_LEVEL } from './prose-look.js';
103
103
  import { foldPlugin, setFolds, shownEnd } from './rich-text-fold.js';
@@ -564,10 +564,21 @@ export default function MilkdownEditor({ id, defaultValue, onChange, onProblem,
564
564
  };
565
565
  openLinkRef.current = openLink;
566
566
  // Folding by headings, with `foldKey` (read once, when it mounts):
567
- // the fold store is the state, the plugin draws it.
567
+ // the fold store is the state, the plugin draws it, and publishes the
568
+ // headings for NotesOutline under the same key.
569
+ const headingsOwner = {};
568
570
  const folds = foldKey === undefined
569
571
  ? []
570
- : [$prose(() => foldPlugin({ initial: foldStore.get(foldKey), onChange: (folded) => foldStore.set(foldKey, folded) }))];
572
+ : [
573
+ $prose(() => foldPlugin({
574
+ initial: foldStore.get(foldKey),
575
+ onChange: (folded) => foldStore.set(foldKey, folded),
576
+ onHeadings: (headings) => {
577
+ if (!cancelled)
578
+ headingStore.publish(foldKey, headingsOwner, headings);
579
+ },
580
+ })),
581
+ ];
571
582
  const onFold = foldKey === undefined ? undefined : (id) => foldStore.set(foldKey, toggleFold(foldStore.get(foldKey), id));
572
583
  let stopFolds = () => { };
573
584
  // Paste, copy, the editor's own keys and the placeholder.
@@ -993,6 +1004,8 @@ export default function MilkdownEditor({ id, defaultValue, onChange, onProblem,
993
1004
  emit();
994
1005
  cancelled = true;
995
1006
  stopFolds();
1007
+ if (foldKey !== undefined)
1008
+ headingStore.withdraw(foldKey, headingsOwner);
996
1009
  setEditorState(null);
997
1010
  setEditorView(null);
998
1011
  const editor = editorRef.current;
@@ -1,6 +1,11 @@
1
1
  export type NotesOutlineProps = {
2
- /** The text whose headings are listed. */
3
- markdown: string;
2
+ /**
3
+ * The text whose headings are listed, beside <Markdown>. Beside a
4
+ * RichTextEditor with the same `foldKey` it isn't needed: the editor's
5
+ * headings are listed, current as it's edited, and this text is read
6
+ * only while no editor with the key is mounted.
7
+ */
8
+ markdown?: string;
4
9
  /** The `foldKey` of the <Markdown> or RichTextEditor showing the text. */
5
10
  foldKey: string;
6
11
  /**
@@ -8,13 +8,15 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
8
8
  // (`depth`), so a long text's outline stays a list of its parts.
9
9
  //
10
10
  // The jump lands on the heading's fold button, so from the keyboard the
11
- // next Enter folds what was just opened. The outline reads the Markdown
12
- // it's given: beside an editor, the text as last saved.
11
+ // next Enter folds what was just opened. Beside a RichTextEditor with
12
+ // the same `foldKey`, the outline lists the editor's own headings, as it
13
+ // edits (src/hooks/fold-store.ts's heading store), and parses nothing;
14
+ // otherwise it reads the Markdown it's given.
13
15
  import { useMemo } from 'react';
14
16
  import { ChevronRightIcon } from 'lucide-react';
15
17
  import { cn } from 'cn';
16
18
  import { Button } from '../ui/button.js';
17
- import { useFolds } from '../../hooks/fold-store.js';
19
+ import { useFoldHeadings, useFolds } from '../../hooks/fold-store.js';
18
20
  import { usePrefersReducedMotion } from '../../hooks/use-prefers-reduced-motion.js';
19
21
  import { allFoldable, markdownFoldPlan, openTo } from '../../lib/markdown-fold.js';
20
22
  const INDENT = ['pl-2', 'pl-6', 'pl-10', 'pl-14', 'pl-18', 'pl-22'];
@@ -32,9 +34,15 @@ function headingElement(foldKey, id) {
32
34
  export function NotesOutline({ markdown, foldKey, depth = 2, minHeadings = 2, 'aria-label': ariaLabel = 'Outline', className, }) {
33
35
  const [folded, setFolded] = useFolds(foldKey);
34
36
  const reducedMotion = usePrefersReducedMotion();
35
- const plan = useMemo(() => markdownFoldPlan(markdown), [markdown]);
36
- const top = Math.min(...plan.headings.map((heading) => heading.level));
37
- const listed = plan.headings.filter((heading) => heading.level < top + depth);
37
+ const live = useFoldHeadings(foldKey);
38
+ // Parsed only with no editor publishing under the key.
39
+ const parsed = useMemo(() => (live || markdown === undefined ? null : markdownFoldPlan(markdown).headings), [live, markdown]);
40
+ const headings = live ?? parsed;
41
+ if (!headings)
42
+ return null;
43
+ const plan = { headings };
44
+ const top = Math.min(...headings.map((heading) => heading.level));
45
+ const listed = headings.filter((heading) => heading.level < top + depth);
38
46
  if (listed.length < minHeadings)
39
47
  return null;
40
48
  const foldable = allFoldable(plan);
@@ -1,7 +1,7 @@
1
1
  import type { Node as ProseNode } from '@milkdown/kit/prose/model';
2
2
  import { Plugin, PluginKey, type EditorState } from '@milkdown/kit/prose/state';
3
3
  import { Decoration, DecorationSet } from '@milkdown/kit/prose/view';
4
- import { type FoldPlan } from '../../lib/markdown-fold.js';
4
+ import { type FoldHeading, type FoldPlan } from '../../lib/markdown-fold.js';
5
5
  /** What a heading's node view hears of its fold, through its node decoration's spec. */
6
6
  export type FoldSpec = {
7
7
  id: string;
@@ -27,8 +27,11 @@ export declare function setFolds(state: EditorState, folded: ReadonlySet<string>
27
27
  * (a caret put past it would open the section).
28
28
  */
29
29
  export declare function shownEnd(state: EditorState): number;
30
+ /** The same headings: each with the same id, words, level, kind, place and parents. */
31
+ export declare function sameHeadings(a: readonly FoldHeading[], b: readonly FoldHeading[]): boolean;
30
32
  export declare function foldPlugin(options: {
31
33
  initial: ReadonlySet<string>;
32
34
  onChange: (folded: ReadonlySet<string>) => void;
35
+ onHeadings?: (headings: readonly FoldHeading[]) => void;
33
36
  }): Plugin<FoldState>;
34
37
  export {};
@@ -84,6 +84,25 @@ export function shownEnd(state) {
84
84
  });
85
85
  return end;
86
86
  }
87
+ /** The same headings: each with the same id, words, level, kind, place and parents. */
88
+ export function sameHeadings(a, b) {
89
+ if (a === b)
90
+ return true;
91
+ if (a.length !== b.length)
92
+ return false;
93
+ return a.every((one, at) => {
94
+ const other = b[at];
95
+ return (one.id === other.id &&
96
+ one.level === other.level &&
97
+ one.text === other.text &&
98
+ one.toggle === other.toggle &&
99
+ one.foldable === other.foldable &&
100
+ one.index === other.index &&
101
+ one.end === other.end &&
102
+ one.parents.length === other.parents.length &&
103
+ one.parents.every((id, i) => id === other.parents[i]));
104
+ });
105
+ }
87
106
  export function foldPlugin(options) {
88
107
  return new Plugin({
89
108
  key: foldPluginKey,
@@ -124,27 +143,46 @@ export function foldPlugin(options) {
124
143
  return true;
125
144
  },
126
145
  },
127
- view: () => ({
128
- update: (view, previous) => {
129
- const fold = foldPluginKey.getState(view.state);
130
- if (!fold)
131
- return;
132
- const index = view.state.selection.$from.index(0);
133
- if (!fold.plan.hidden.has(index))
146
+ view: (editorView) => {
147
+ // The headings last sent, and the frame that sends the next.
148
+ let sent = foldPluginKey.getState(editorView.state)?.plan.headings ?? [];
149
+ let frame = 0;
150
+ options.onHeadings?.(sent);
151
+ const sendHeadings = (view) => {
152
+ frame = 0;
153
+ const headings = foldPluginKey.getState(view.state)?.plan.headings;
154
+ if (!headings || sameHeadings(headings, sent))
134
155
  return;
135
- // Folded around the caret (a fold button, the outline): the caret
136
- // goes to the end of the heading that folded, which shows.
137
- if (fold.folded !== foldPluginKey.getState(previous)?.folded && view.state.selection.eq(previous.selection)) {
138
- const outer = fold.plan.headings.find((heading) => fold.folded.has(heading.id) && heading.foldable && heading.index < index && index < heading.end);
139
- if (outer)
140
- queueMicrotask(() => view.dispatch(caretToEnd(view.state, outer.index)));
141
- return;
142
- }
143
- // The caret put in a folded section: open it (after this update,
144
- // as the change comes back in a transaction of its own).
145
- const opened = openAt(fold.plan, fold.folded, index);
146
- queueMicrotask(() => options.onChange(opened));
147
- },
148
- }),
156
+ sent = headings;
157
+ options.onHeadings?.(headings);
158
+ };
159
+ return {
160
+ destroy: () => cancelAnimationFrame(frame),
161
+ update: (view, previous) => {
162
+ const fold = foldPluginKey.getState(view.state);
163
+ if (!fold)
164
+ return;
165
+ // A doc change rebuilds the plan: its headings go out a frame on, if they changed.
166
+ if (options.onHeadings && frame === 0 && fold.plan !== foldPluginKey.getState(previous)?.plan) {
167
+ frame = requestAnimationFrame(() => sendHeadings(view));
168
+ }
169
+ const index = view.state.selection.$from.index(0);
170
+ if (!fold.plan.hidden.has(index))
171
+ return;
172
+ // Folded around the caret (a fold button, the outline): the caret
173
+ // goes to the end of the heading that folded, which shows.
174
+ if (fold.folded !== foldPluginKey.getState(previous)?.folded && view.state.selection.eq(previous.selection)) {
175
+ const outer = fold.plan.headings.find((heading) => fold.folded.has(heading.id) && heading.foldable && heading.index < index && index < heading.end);
176
+ if (outer)
177
+ queueMicrotask(() => view.dispatch(caretToEnd(view.state, outer.index)));
178
+ return;
179
+ }
180
+ // The caret put in a folded section: open it (after this update,
181
+ // as the change comes back in a transaction of its own).
182
+ const opened = openAt(fold.plan, fold.folded, index);
183
+ queueMicrotask(() => options.onChange(opened));
184
+ },
185
+ };
186
+ },
149
187
  });
150
188
  }
@@ -1,3 +1,4 @@
1
+ import type { FoldHeading } from '../lib/markdown-fold.js';
1
2
  type StorageLike = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;
2
3
  export type FoldStore = {
3
4
  /** The ids folded under `key`: the same set until it changes. */
@@ -16,4 +17,22 @@ export declare function listenForOtherTabs(): void;
16
17
  * forgotten with the page.
17
18
  */
18
19
  export declare function useFolds(foldKey: string | undefined): readonly [ReadonlySet<string>, (folded: ReadonlySet<string>) => void];
20
+ export type HeadingStore = {
21
+ /** The headings published under `key`: the same array until they change, `null` with no editor there. */
22
+ get: (key: string) => readonly FoldHeading[] | null;
23
+ /** `owner` (an editor) has these headings under `key`. */
24
+ publish: (key: string, owner: object, headings: readonly FoldHeading[]) => void;
25
+ /** `owner` is gone: its headings go, if they are the ones published. */
26
+ withdraw: (key: string, owner: object) => void;
27
+ subscribe: (key: string, listener: () => void) => () => void;
28
+ };
29
+ export declare function createHeadingStore(): HeadingStore;
30
+ export declare const headingStore: HeadingStore;
31
+ /**
32
+ * The headings of the RichTextEditor showing the text under `foldKey`,
33
+ * as folding sees them, kept current as it's edited (a frame after a
34
+ * heading changes; typing in a paragraph changes nothing). `null` while no
35
+ * editor with that key is mounted.
36
+ */
37
+ export declare function useFoldHeadings(foldKey: string): readonly FoldHeading[] | null;
19
38
  export {};
@@ -91,3 +91,44 @@ export function useFolds(foldKey) {
91
91
  const set = useCallback((folded) => (foldKey === undefined ? setLocal(folded) : foldStore.set(foldKey, folded)), [foldKey]);
92
92
  return [foldKey === undefined ? local : stored, set];
93
93
  }
94
+ export function createHeadingStore() {
95
+ const current = new Map();
96
+ const listeners = new Map();
97
+ const notify = (key) => {
98
+ for (const listener of listeners.get(key) ?? [])
99
+ listener();
100
+ };
101
+ return {
102
+ get: (key) => current.get(key)?.headings ?? null,
103
+ publish(key, owner, headings) {
104
+ current.set(key, { owner, headings });
105
+ notify(key);
106
+ },
107
+ withdraw(key, owner) {
108
+ if (current.get(key)?.owner !== owner)
109
+ return;
110
+ current.delete(key);
111
+ notify(key);
112
+ },
113
+ subscribe(key, listener) {
114
+ let set = listeners.get(key);
115
+ if (!set)
116
+ listeners.set(key, (set = new Set()));
117
+ set.add(listener);
118
+ return () => set.delete(listener);
119
+ },
120
+ };
121
+ }
122
+ export const headingStore = createHeadingStore();
123
+ /**
124
+ * The headings of the RichTextEditor showing the text under `foldKey`,
125
+ * as folding sees them, kept current as it's edited (a frame after a
126
+ * heading changes; typing in a paragraph changes nothing). `null` while no
127
+ * editor with that key is mounted.
128
+ */
129
+ export function useFoldHeadings(foldKey) {
130
+ const subscribe = useCallback((listener) => headingStore.subscribe(foldKey, listener), [foldKey]);
131
+ const get = () => headingStore.get(foldKey);
132
+ // No editor publishes before hydration, so the server's is the same.
133
+ return useSyncExternalStore(subscribe, get, get);
134
+ }
package/dist/index.d.ts CHANGED
@@ -64,6 +64,8 @@ export { useRecentRecord } from './hooks/use-recent-record.js';
64
64
  export type { CreatePage, PageLinkInfo, PageLinkResolver } from './lib/page-link.js';
65
65
  export { countTasks, toggleTaskAt, type TaskCount } from './lib/markdown-tasks.js';
66
66
  export { NotesOutline, type NotesOutlineProps } from './components/app/notes-outline.js';
67
+ export { useFoldHeadings } from './hooks/fold-store.js';
68
+ export type { FoldHeading } from './lib/markdown-fold.js';
67
69
  export type { HeadingFrom, HeadingLevel, TableLook } from './components/app/prose-look.js';
68
70
  export { MockModeBanner } from './components/app/mock-mode-banner.js';
69
71
  export { MultiChoice, type MultiChoiceOption, type MultiChoiceProps } from './components/app/multi-choice.js';
package/dist/index.js CHANGED
@@ -74,6 +74,7 @@ export { usePageTrail } from './components/app/page-trail-context.js';
74
74
  export { useRecentRecord } from './hooks/use-recent-record.js';
75
75
  export { countTasks, toggleTaskAt } from './lib/markdown-tasks.js';
76
76
  export { NotesOutline } from './components/app/notes-outline.js';
77
+ export { useFoldHeadings } from './hooks/fold-store.js';
77
78
  export { MockModeBanner } from './components/app/mock-mode-banner.js';
78
79
  export { MultiChoice } from './components/app/multi-choice.js';
79
80
  export { MultiPick } from './components/app/multi-pick.js';
@@ -43,9 +43,13 @@ export declare function mdBlocks(root: MdFoldNode): (FoldHeadingInput | null)[];
43
43
  /** The fold plan for a Markdown text, parsed as <Markdown> and the editor parse it. */
44
44
  export declare function markdownFoldPlan(markdown: string, folded?: ReadonlySet<string>): FoldPlan;
45
45
  /** Every heading with something to fold: Fold all. */
46
- export declare function allFoldable(plan: FoldPlan): Set<string>;
46
+ export declare function allFoldable(plan: {
47
+ headings: readonly FoldHeading[];
48
+ }): Set<string>;
47
49
  /** `folded` with the heading `id` and every heading it sits under opened: a jump to it. */
48
- export declare function openTo(plan: FoldPlan, folded: ReadonlySet<string>, id: string): Set<string>;
50
+ export declare function openTo(plan: {
51
+ headings: readonly FoldHeading[];
52
+ }, folded: ReadonlySet<string>, id: string): Set<string>;
49
53
  /** `folded` with every heading whose section holds the top-level block at `index` opened: the caret moved into it. */
50
54
  export declare function openAt(plan: FoldPlan, folded: ReadonlySet<string>, index: number): Set<string>;
51
55
  /** `folded` with `id` turned the other way. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tristan2828/ui-foundation",
3
- "version": "4.156.0",
3
+ "version": "4.157.0",
4
4
  "description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
5
5
  "license": "MIT",
6
6
  "repository": {