@tristan2828/ui-foundation 4.52.0 → 4.54.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/conventions/docs/design-language.md +18 -1
- package/dist/components/app/fill-in-field.d.ts +65 -0
- package/dist/components/app/fill-in-field.js +99 -0
- package/dist/components/app/fill-in-queue.d.ts +13 -1
- package/dist/components/app/fill-in-queue.js +15 -3
- package/dist/components/app/markdown.d.ts +10 -1
- package/dist/components/app/markdown.js +37 -11
- package/dist/components/app/milkdown-editor.d.ts +1 -1
- package/dist/components/app/milkdown-editor.js +82 -10
- package/dist/components/app/notes-outline.d.ts +17 -0
- package/dist/components/app/notes-outline.js +60 -0
- package/dist/components/app/prose-look.d.ts +8 -0
- package/dist/components/app/prose-look.js +8 -0
- package/dist/components/app/rich-text-editor.d.ts +11 -0
- package/dist/components/app/rich-text-editor.js +3 -0
- package/dist/components/app/rich-text-fold.d.ts +28 -0
- package/dist/components/app/rich-text-fold.js +130 -0
- package/dist/hooks/fold-store.d.ts +19 -0
- package/dist/hooks/fold-store.js +93 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -0
- package/dist/lib/markdown-fold.d.ts +59 -0
- package/dist/lib/markdown-fold.js +163 -0
- package/package.json +1 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
// A long text's headings as a list to jump with, beside <Markdown> or the
|
|
3
|
+
// rich-text editor given the same `foldKey`: each heading a button that
|
|
4
|
+
// opens its section (and any it sits under) and brings it into view, with
|
|
5
|
+
// Fold all and Open all above. The text's top-level headings, as folding
|
|
6
|
+
// sees them (src/lib/markdown-fold.ts); the deepest levels are left out
|
|
7
|
+
// (`depth`), so a long text's outline stays a list of its parts.
|
|
8
|
+
//
|
|
9
|
+
// The jump lands on the heading's fold button, so from the keyboard the
|
|
10
|
+
// next Enter folds what was just opened. The outline reads the Markdown
|
|
11
|
+
// it's given: beside an editor, the text as last saved.
|
|
12
|
+
import { useMemo } from 'react';
|
|
13
|
+
import { ChevronRightIcon } from 'lucide-react';
|
|
14
|
+
import { cn } from 'cn';
|
|
15
|
+
import { Button } from '../ui/button.js';
|
|
16
|
+
import { useFolds } from '../../hooks/fold-store.js';
|
|
17
|
+
import { usePrefersReducedMotion } from '../../hooks/use-prefers-reduced-motion.js';
|
|
18
|
+
import { allFoldable, markdownFoldPlan, openTo } from '../../lib/markdown-fold.js';
|
|
19
|
+
// Each level's indent, from the highest listed.
|
|
20
|
+
const INDENT = ['pl-2', 'pl-6', 'pl-10', 'pl-14', 'pl-18', 'pl-22'];
|
|
21
|
+
// The element showing the heading `id` in the text under `foldKey`.
|
|
22
|
+
function headingElement(foldKey, id) {
|
|
23
|
+
for (const scope of document.querySelectorAll('[data-fold-scope]')) {
|
|
24
|
+
if (scope.dataset.foldScope !== foldKey)
|
|
25
|
+
continue;
|
|
26
|
+
for (const heading of scope.querySelectorAll('[data-fold-id]'))
|
|
27
|
+
if (heading.dataset.foldId === id)
|
|
28
|
+
return heading;
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
export function NotesOutline({ markdown, foldKey, depth = 2, minHeadings = 2, 'aria-label': ariaLabel = 'Outline', className, }) {
|
|
33
|
+
const [folded, setFolded] = useFolds(foldKey);
|
|
34
|
+
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);
|
|
38
|
+
if (listed.length < minHeadings)
|
|
39
|
+
return null;
|
|
40
|
+
const foldable = allFoldable(plan);
|
|
41
|
+
const anyFolded = [...folded].some((id) => foldable.has(id));
|
|
42
|
+
const allFolded = [...foldable].every((id) => folded.has(id));
|
|
43
|
+
const jump = (id) => {
|
|
44
|
+
setFolded(openTo(plan, folded, id));
|
|
45
|
+
// Once the text has opened up to it.
|
|
46
|
+
requestAnimationFrame(() => {
|
|
47
|
+
const heading = headingElement(foldKey, id);
|
|
48
|
+
if (!heading)
|
|
49
|
+
return;
|
|
50
|
+
heading.scrollIntoView({ block: 'start', behavior: reducedMotion ? 'auto' : 'smooth' });
|
|
51
|
+
const button = heading.querySelector('[data-slot="fold-button"]');
|
|
52
|
+
if (button && getComputedStyle(button).visibility !== 'hidden')
|
|
53
|
+
button.focus({ preventScroll: true });
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
return (_jsxs("nav", { "aria-label": ariaLabel, "data-slot": "notes-outline", className: cn('flex min-w-0 flex-col gap-2', className), children: [_jsxs("div", { className: "flex flex-wrap gap-1", children: [_jsx(Button, { size: "xs", variant: "outline", disabled: allFolded, onClick: () => setFolded(foldable), children: "Fold all" }), _jsx(Button, { size: "xs", variant: "outline", disabled: !anyFolded, onClick: () => setFolded(new Set()), children: "Open all" })] }), _jsx("ul", { className: "flex flex-col", children: listed.map((heading) => {
|
|
57
|
+
const isFolded = heading.foldable && folded.has(heading.id);
|
|
58
|
+
return (_jsx("li", { children: _jsxs("button", { type: "button", onClick: () => jump(heading.id), className: cn('flex w-full min-w-0 items-center gap-1 rounded-md py-1 pr-2 text-left type-body text-foreground outline-none hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50', INDENT[Math.min(heading.level - top, INDENT.length - 1)], heading.level > top && 'text-muted-foreground hover:text-foreground'), children: [_jsx("span", { className: "min-w-0 flex-1 truncate", children: heading.text || 'Untitled section' }), isFolded && (_jsxs(_Fragment, { children: [_jsx(ChevronRightIcon, { "aria-hidden": "true", className: "size-3.5 shrink-0 text-muted-foreground" }), _jsx("span", { className: "sr-only", children: " (folded)" })] }))] }) }, heading.id));
|
|
59
|
+
}) })] }));
|
|
60
|
+
}
|
|
@@ -26,6 +26,14 @@ export declare const PROSE: {
|
|
|
26
26
|
/** `---`: a 2px line in the rule tone, with room around it. */
|
|
27
27
|
readonly rule: "my-2 h-0.5 rounded-full border-0 bg-rule";
|
|
28
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* A heading's fold button (`foldKey`), before its words, the line's height
|
|
31
|
+
* so it sits on the first line of a heading that wraps. Its chevron points
|
|
32
|
+
* down while the section shows and right while it's folded.
|
|
33
|
+
*/
|
|
34
|
+
export declare const FOLD_BUTTON = "-ml-1 flex h-[1lh] w-5 shrink-0 items-center justify-center rounded-sm text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50";
|
|
35
|
+
/** A heading with a fold button: the button and the words side by side, the look on both. */
|
|
36
|
+
export declare const FOLD_HEADING = "flex items-start gap-1";
|
|
29
37
|
/**
|
|
30
38
|
* On the text's container: when it has a band (a `#` or `##`, marked
|
|
31
39
|
* `data-band`, whatever element its `headingLevel` makes it), everything
|
|
@@ -32,6 +32,14 @@ export const PROSE = {
|
|
|
32
32
|
/** `---`: a 2px line in the rule tone, with room around it. */
|
|
33
33
|
rule: 'my-2 h-0.5 rounded-full border-0 bg-rule',
|
|
34
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* A heading's fold button (`foldKey`), before its words, the line's height
|
|
37
|
+
* so it sits on the first line of a heading that wraps. Its chevron points
|
|
38
|
+
* down while the section shows and right while it's folded.
|
|
39
|
+
*/
|
|
40
|
+
export const FOLD_BUTTON = '-ml-1 flex h-[1lh] w-5 shrink-0 items-center justify-center rounded-sm text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50';
|
|
41
|
+
/** A heading with a fold button: the button and the words side by side, the look on both. */
|
|
42
|
+
export const FOLD_HEADING = 'flex items-start gap-1';
|
|
35
43
|
/**
|
|
36
44
|
* On the text's container: when it has a band (a `#` or `##`, marked
|
|
37
45
|
* `data-band`, whatever element its `headingLevel` makes it), everything
|
|
@@ -69,6 +69,17 @@ export type RichTextEditorProps = {
|
|
|
69
69
|
* `headingLevel` too. The Markdown saved is never rewritten.
|
|
70
70
|
*/
|
|
71
71
|
headingFrom?: HeadingFrom;
|
|
72
|
+
/**
|
|
73
|
+
* Folds the text by its headings, as `<Markdown>`'s `foldKey` does: each
|
|
74
|
+
* heading at the text's top level has a button that folds what's under
|
|
75
|
+
* it, Ctrl/Cmd+Alt+[ folds the section the caret is in and
|
|
76
|
+
* Ctrl/Cmd+Alt+] opens the heading it's on, and the caret moving into a
|
|
77
|
+
* folded section opens it. Remembered on this device under this key,
|
|
78
|
+
* and shared with `<Markdown>` and NotesOutline given the same one.
|
|
79
|
+
* Only the view folds: the Markdown saved never changes. Read once,
|
|
80
|
+
* when it mounts.
|
|
81
|
+
*/
|
|
82
|
+
foldKey?: string;
|
|
72
83
|
className?: string;
|
|
73
84
|
/** The field's label. A rich-text area must have one: this, or `aria-labelledby`. */
|
|
74
85
|
'aria-label'?: string;
|
|
@@ -23,6 +23,9 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
23
23
|
// Image button is uploaded through the app's own storage and goes in as
|
|
24
24
|
// ``; while it uploads, a placeholder shows where it will go.
|
|
25
25
|
//
|
|
26
|
+
// With `foldKey`, the text folds by its headings (NotesOutline lists them
|
|
27
|
+
// beside it), the Markdown untouched.
|
|
28
|
+
//
|
|
26
29
|
// The editor (Milkdown) is loaded on first use, so a page that only reads
|
|
27
30
|
// Markdown never downloads it. Until it arrives, a placeholder the size of
|
|
28
31
|
// the field shows.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Node as ProseNode } from '@milkdown/kit/prose/model';
|
|
2
|
+
import { Plugin, PluginKey, type EditorState } from '@milkdown/kit/prose/state';
|
|
3
|
+
import { Decoration, DecorationSet } from '@milkdown/kit/prose/view';
|
|
4
|
+
import { type FoldPlan } from '../../lib/markdown-fold.js';
|
|
5
|
+
/** What a heading's node view hears of its fold, through its node decoration's spec. */
|
|
6
|
+
export type FoldSpec = {
|
|
7
|
+
id: string;
|
|
8
|
+
label: string;
|
|
9
|
+
foldable: boolean;
|
|
10
|
+
folded: boolean;
|
|
11
|
+
};
|
|
12
|
+
type FoldState = {
|
|
13
|
+
folded: ReadonlySet<string>;
|
|
14
|
+
plan: FoldPlan;
|
|
15
|
+
decorations: DecorationSet;
|
|
16
|
+
};
|
|
17
|
+
export declare const foldPluginKey: PluginKey<FoldState>;
|
|
18
|
+
/** The fold plan of a document's top-level blocks. */
|
|
19
|
+
export declare function docFoldPlan(doc: ProseNode, folded?: ReadonlySet<string>): FoldPlan;
|
|
20
|
+
/** A heading node view's fold, from the decorations ProseMirror gives it. */
|
|
21
|
+
export declare function foldSpecOf(decorations: readonly Decoration[]): FoldSpec | null;
|
|
22
|
+
/** A transaction that sets what's folded, from outside the editor. */
|
|
23
|
+
export declare function setFolds(state: EditorState, folded: ReadonlySet<string>): import("prosemirror-state").Transaction;
|
|
24
|
+
export declare function foldPlugin(options: {
|
|
25
|
+
initial: ReadonlySet<string>;
|
|
26
|
+
onChange: (folded: ReadonlySet<string>) => void;
|
|
27
|
+
}): Plugin<FoldState>;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { Plugin, PluginKey, TextSelection } from '@milkdown/kit/prose/state';
|
|
2
|
+
import { Decoration, DecorationSet } from '@milkdown/kit/prose/view';
|
|
3
|
+
import { foldPlan, openAt, toggleFold } from '../../lib/markdown-fold.js';
|
|
4
|
+
export const foldPluginKey = new PluginKey('fold');
|
|
5
|
+
/** The fold plan of a document's top-level blocks. */
|
|
6
|
+
export function docFoldPlan(doc, folded) {
|
|
7
|
+
const blocks = [];
|
|
8
|
+
doc.forEach((node) => blocks.push(node.type.name === 'heading' ? { level: Number(node.attrs.level), text: node.textContent } : null));
|
|
9
|
+
return foldPlan(blocks, folded);
|
|
10
|
+
}
|
|
11
|
+
function build(doc, folded) {
|
|
12
|
+
const plan = docFoldPlan(doc, folded);
|
|
13
|
+
const headings = new Map(plan.headings.map((heading) => [heading.index, heading]));
|
|
14
|
+
const decorations = [];
|
|
15
|
+
doc.forEach((node, offset, index) => {
|
|
16
|
+
const heading = headings.get(index);
|
|
17
|
+
const attrs = {};
|
|
18
|
+
if (plan.hidden.has(index))
|
|
19
|
+
attrs.hidden = '';
|
|
20
|
+
if (heading) {
|
|
21
|
+
const spec = {
|
|
22
|
+
id: heading.id,
|
|
23
|
+
label: heading.text,
|
|
24
|
+
foldable: heading.foldable,
|
|
25
|
+
folded: heading.foldable && folded.has(heading.id),
|
|
26
|
+
};
|
|
27
|
+
decorations.push(Decoration.node(offset, offset + node.nodeSize, attrs, { fold: spec }));
|
|
28
|
+
}
|
|
29
|
+
else if (attrs.hidden !== undefined) {
|
|
30
|
+
decorations.push(Decoration.node(offset, offset + node.nodeSize, attrs));
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return { folded, plan, decorations: DecorationSet.create(doc, decorations) };
|
|
34
|
+
}
|
|
35
|
+
/** A heading node view's fold, from the decorations ProseMirror gives it. */
|
|
36
|
+
export function foldSpecOf(decorations) {
|
|
37
|
+
for (const decoration of decorations) {
|
|
38
|
+
const spec = decoration.spec.fold;
|
|
39
|
+
if (spec)
|
|
40
|
+
return spec;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
/** A transaction that sets what's folded, from outside the editor. */
|
|
45
|
+
export function setFolds(state, folded) {
|
|
46
|
+
return state.tr.setMeta(foldPluginKey, folded).setMeta('addToHistory', false);
|
|
47
|
+
}
|
|
48
|
+
// The innermost foldable heading whose section (or heading) holds the
|
|
49
|
+
// top-level block at `index`.
|
|
50
|
+
function headingAround(plan, index) {
|
|
51
|
+
let found;
|
|
52
|
+
for (const heading of plan.headings) {
|
|
53
|
+
if (heading.foldable && heading.index <= index && index < heading.end)
|
|
54
|
+
found = heading;
|
|
55
|
+
}
|
|
56
|
+
return found;
|
|
57
|
+
}
|
|
58
|
+
// The caret at the end of the top-level block at `index` (a heading).
|
|
59
|
+
function caretToEnd(state, index) {
|
|
60
|
+
let end = 0;
|
|
61
|
+
state.doc.forEach((node, offset, at) => {
|
|
62
|
+
if (at === index)
|
|
63
|
+
end = offset + node.nodeSize - 1;
|
|
64
|
+
});
|
|
65
|
+
return state.tr.setSelection(TextSelection.create(state.doc, end)).scrollIntoView();
|
|
66
|
+
}
|
|
67
|
+
export function foldPlugin(options) {
|
|
68
|
+
return new Plugin({
|
|
69
|
+
key: foldPluginKey,
|
|
70
|
+
state: {
|
|
71
|
+
init: (_config, state) => build(state.doc, options.initial),
|
|
72
|
+
apply: (tr, value, _old, state) => {
|
|
73
|
+
const next = tr.getMeta(foldPluginKey);
|
|
74
|
+
if (!next && !tr.docChanged)
|
|
75
|
+
return value;
|
|
76
|
+
return build(state.doc, next ?? value.folded);
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
props: {
|
|
80
|
+
decorations: (state) => foldPluginKey.getState(state)?.decorations,
|
|
81
|
+
handleKeyDown: (view, event) => {
|
|
82
|
+
const mod = event.metaKey || event.ctrlKey;
|
|
83
|
+
if (!mod || !event.altKey || event.shiftKey)
|
|
84
|
+
return false;
|
|
85
|
+
if (event.code !== 'BracketLeft' && event.code !== 'BracketRight')
|
|
86
|
+
return false;
|
|
87
|
+
const fold = foldPluginKey.getState(view.state);
|
|
88
|
+
if (!fold)
|
|
89
|
+
return false;
|
|
90
|
+
const index = view.state.selection.$from.index(0);
|
|
91
|
+
event.preventDefault();
|
|
92
|
+
if (event.code === 'BracketLeft') {
|
|
93
|
+
const heading = headingAround(fold.plan, index);
|
|
94
|
+
if (!heading || fold.folded.has(heading.id))
|
|
95
|
+
return true;
|
|
96
|
+
// The caret to the end of the heading, which stays in sight.
|
|
97
|
+
view.dispatch(caretToEnd(view.state, heading.index));
|
|
98
|
+
options.onChange(toggleFold(fold.folded, heading.id));
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
const heading = fold.plan.headings.find((one) => one.index === index);
|
|
102
|
+
if (heading && fold.folded.has(heading.id))
|
|
103
|
+
options.onChange(toggleFold(fold.folded, heading.id));
|
|
104
|
+
return true;
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
view: () => ({
|
|
108
|
+
update: (view, previous) => {
|
|
109
|
+
const fold = foldPluginKey.getState(view.state);
|
|
110
|
+
if (!fold)
|
|
111
|
+
return;
|
|
112
|
+
const index = view.state.selection.$from.index(0);
|
|
113
|
+
if (!fold.plan.hidden.has(index))
|
|
114
|
+
return;
|
|
115
|
+
// Folded around the caret (a fold button, the outline): the caret
|
|
116
|
+
// goes to the end of the heading that folded, which shows.
|
|
117
|
+
if (fold.folded !== foldPluginKey.getState(previous)?.folded && view.state.selection.eq(previous.selection)) {
|
|
118
|
+
const outer = fold.plan.headings.find((heading) => fold.folded.has(heading.id) && heading.foldable && heading.index < index && index < heading.end);
|
|
119
|
+
if (outer)
|
|
120
|
+
queueMicrotask(() => view.dispatch(caretToEnd(view.state, outer.index)));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
// The caret put in a folded section: open it (after this update,
|
|
124
|
+
// as the change comes back in a transaction of its own).
|
|
125
|
+
const opened = openAt(fold.plan, fold.folded, index);
|
|
126
|
+
queueMicrotask(() => options.onChange(opened));
|
|
127
|
+
},
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type StorageLike = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;
|
|
2
|
+
export type FoldStore = {
|
|
3
|
+
/** The ids folded under `key`: the same set until it changes. */
|
|
4
|
+
get: (key: string) => ReadonlySet<string>;
|
|
5
|
+
set: (key: string, folded: ReadonlySet<string>) => void;
|
|
6
|
+
subscribe: (key: string, listener: () => void) => () => void;
|
|
7
|
+
/** Another tab changed `key` (a `storage` event): re-read it. */
|
|
8
|
+
changedElsewhere: (key: string) => void;
|
|
9
|
+
};
|
|
10
|
+
export declare function createFoldStore(getStorage: () => StorageLike | undefined): FoldStore;
|
|
11
|
+
export declare const foldStore: FoldStore;
|
|
12
|
+
export declare function listenForOtherTabs(): void;
|
|
13
|
+
/**
|
|
14
|
+
* `[folded, setFolded]`: the heading ids folded under `foldKey`, shared
|
|
15
|
+
* with everything else given that key. Without a key it's plain state,
|
|
16
|
+
* forgotten with the page.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useFolds(foldKey: string | undefined): readonly [ReadonlySet<string>, (folded: ReadonlySet<string>) => void];
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { useCallback, useState, useSyncExternalStore } from 'react';
|
|
2
|
+
const NONE = new Set();
|
|
3
|
+
function parse(stored) {
|
|
4
|
+
if (!stored)
|
|
5
|
+
return NONE;
|
|
6
|
+
try {
|
|
7
|
+
const ids = JSON.parse(stored);
|
|
8
|
+
return Array.isArray(ids) ? new Set(ids.filter((id) => typeof id === 'string')) : NONE;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return NONE;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function createFoldStore(getStorage) {
|
|
15
|
+
// Each key's set as last read or set: one object per state, so React
|
|
16
|
+
// sees a change only when there is one.
|
|
17
|
+
const current = new Map();
|
|
18
|
+
const listeners = new Map();
|
|
19
|
+
const notify = (key) => {
|
|
20
|
+
for (const listener of listeners.get(key) ?? [])
|
|
21
|
+
listener();
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
get(key) {
|
|
25
|
+
let folded = current.get(key);
|
|
26
|
+
if (!folded) {
|
|
27
|
+
try {
|
|
28
|
+
folded = parse(getStorage()?.getItem(key));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
folded = NONE;
|
|
32
|
+
}
|
|
33
|
+
current.set(key, folded);
|
|
34
|
+
}
|
|
35
|
+
return folded;
|
|
36
|
+
},
|
|
37
|
+
set(key, folded) {
|
|
38
|
+
const next = new Set(folded);
|
|
39
|
+
current.set(key, next);
|
|
40
|
+
try {
|
|
41
|
+
if (next.size === 0)
|
|
42
|
+
getStorage()?.removeItem(key);
|
|
43
|
+
else
|
|
44
|
+
getStorage()?.setItem(key, JSON.stringify([...next]));
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Storage blocked or full: remembered in memory for this page only.
|
|
48
|
+
}
|
|
49
|
+
notify(key);
|
|
50
|
+
},
|
|
51
|
+
subscribe(key, listener) {
|
|
52
|
+
let set = listeners.get(key);
|
|
53
|
+
if (!set)
|
|
54
|
+
listeners.set(key, (set = new Set()));
|
|
55
|
+
set.add(listener);
|
|
56
|
+
return () => set.delete(listener);
|
|
57
|
+
},
|
|
58
|
+
changedElsewhere(key) {
|
|
59
|
+
if (!current.has(key))
|
|
60
|
+
return;
|
|
61
|
+
current.delete(key);
|
|
62
|
+
notify(key);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export const foldStore = createFoldStore(() => (typeof window === 'undefined' ? undefined : window.localStorage));
|
|
67
|
+
let listeningForOtherTabs = false;
|
|
68
|
+
export function listenForOtherTabs() {
|
|
69
|
+
if (listeningForOtherTabs || typeof window === 'undefined')
|
|
70
|
+
return;
|
|
71
|
+
listeningForOtherTabs = true;
|
|
72
|
+
window.addEventListener('storage', (event) => {
|
|
73
|
+
if (event.key !== null)
|
|
74
|
+
foldStore.changedElsewhere(event.key);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `[folded, setFolded]`: the heading ids folded under `foldKey`, shared
|
|
79
|
+
* with everything else given that key. Without a key it's plain state,
|
|
80
|
+
* forgotten with the page.
|
|
81
|
+
*/
|
|
82
|
+
export function useFolds(foldKey) {
|
|
83
|
+
const [local, setLocal] = useState(NONE);
|
|
84
|
+
const subscribe = useCallback((listener) => {
|
|
85
|
+
if (foldKey === undefined)
|
|
86
|
+
return () => { };
|
|
87
|
+
listenForOtherTabs();
|
|
88
|
+
return foldStore.subscribe(foldKey, listener);
|
|
89
|
+
}, [foldKey]);
|
|
90
|
+
const stored = useSyncExternalStore(subscribe, () => (foldKey === undefined ? NONE : foldStore.get(foldKey)), () => NONE);
|
|
91
|
+
const set = useCallback((folded) => (foldKey === undefined ? setLocal(folded) : foldStore.set(foldKey, folded)), [foldKey]);
|
|
92
|
+
return [foldKey === undefined ? local : stored, set];
|
|
93
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ export type { IconComponent } from './components/app/icon-component.js';
|
|
|
3
3
|
export { Board, type BoardColumn, type BoardMoveTarget, type BoardProps } from './components/app/board.js';
|
|
4
4
|
export { SpinWheel, type SpinWheelProps } from './components/app/spin-wheel.js';
|
|
5
5
|
export { pickSpinWheelIndex, type SpinWheelItem } from './components/app/spin-wheel-pick.js';
|
|
6
|
-
export { FillInChoice, FillInQueue, type FillInChoiceProps, type FillInQueueProgress, type FillInQueueProps, } from './components/app/fill-in-queue.js';
|
|
6
|
+
export { FillInChoice, FillInQueue, type FillInChoiceProps, type FillInQueueGroup, type FillInQueueProgress, type FillInQueueProps, } from './components/app/fill-in-queue.js';
|
|
7
|
+
export { FillInField, FillInInput, FillInSelect, type FillInFieldKind, type FillInFieldProps, type FillInFieldSpec, type FillInInputKind, type FillInInputProps, type FillInSelectProps, type FillInSuggestion, type FillInValue, } from './components/app/fill-in-field.js';
|
|
7
8
|
export { Artwork, ArtworkTag, ArtworkToggle, type ArtworkAspect, type ArtworkProps, type ArtworkToggleProps, } from './components/app/artwork.js';
|
|
8
9
|
export { CountLink, type CountLinkProps } from './components/app/count-link.js';
|
|
9
10
|
export { DataTable, type DataTableCardLayout, type DataTableGrouping, type DataTableProps, type DataTableTotals, type DataTableWidth, type SortingState, } from './components/app/data-table.js';
|
|
@@ -19,6 +20,7 @@ export { ErrorState } from './components/app/error-state.js';
|
|
|
19
20
|
export { InlineCreate, type InlineCreateProps } from './components/app/inline-create.js';
|
|
20
21
|
export { FoundationProviders } from './components/app/foundation-providers.js';
|
|
21
22
|
export { Markdown, type MarkdownProps } from './components/app/markdown.js';
|
|
23
|
+
export { NotesOutline, type NotesOutlineProps } from './components/app/notes-outline.js';
|
|
22
24
|
export type { HeadingFrom, HeadingLevel } from './components/app/prose-look.js';
|
|
23
25
|
export { MockModeBanner } from './components/app/mock-mode-banner.js';
|
|
24
26
|
export { MultiChoice } from './components/app/multi-choice.js';
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { Board } from './components/app/board.js';
|
|
|
8
8
|
export { SpinWheel } from './components/app/spin-wheel.js';
|
|
9
9
|
export { pickSpinWheelIndex } from './components/app/spin-wheel-pick.js';
|
|
10
10
|
export { FillInChoice, FillInQueue, } from './components/app/fill-in-queue.js';
|
|
11
|
+
export { FillInField, FillInInput, FillInSelect, } from './components/app/fill-in-field.js';
|
|
11
12
|
export { Artwork, ArtworkTag, ArtworkToggle, } from './components/app/artwork.js';
|
|
12
13
|
export { CountLink } from './components/app/count-link.js';
|
|
13
14
|
export { DataTable, } from './components/app/data-table.js';
|
|
@@ -23,6 +24,7 @@ export { ErrorState } from './components/app/error-state.js';
|
|
|
23
24
|
export { InlineCreate } from './components/app/inline-create.js';
|
|
24
25
|
export { FoundationProviders } from './components/app/foundation-providers.js';
|
|
25
26
|
export { Markdown } from './components/app/markdown.js';
|
|
27
|
+
export { NotesOutline } from './components/app/notes-outline.js';
|
|
26
28
|
export { MockModeBanner } from './components/app/mock-mode-banner.js';
|
|
27
29
|
export { MultiChoice } from './components/app/multi-choice.js';
|
|
28
30
|
export { MultiPick } from './components/app/multi-pick.js';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** A top-level heading, by its level (1 for `#`) and its words. */
|
|
2
|
+
export type FoldHeadingInput = {
|
|
3
|
+
level: number;
|
|
4
|
+
text: string;
|
|
5
|
+
};
|
|
6
|
+
/** A top-level heading as folding sees it. */
|
|
7
|
+
export type FoldHeading = FoldHeadingInput & {
|
|
8
|
+
/** Its id in the fold state. */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Its index among the text's top-level blocks. */
|
|
11
|
+
index: number;
|
|
12
|
+
/** It has something under it to fold. */
|
|
13
|
+
foldable: boolean;
|
|
14
|
+
/** The ids of the headings it sits under, outermost first. */
|
|
15
|
+
parents: string[];
|
|
16
|
+
/** The index of the first top-level block after its section (the text's length at the end). */
|
|
17
|
+
end: number;
|
|
18
|
+
};
|
|
19
|
+
/** The plan for a text's top-level blocks: which are headings, which are folded away. */
|
|
20
|
+
export type FoldPlan = {
|
|
21
|
+
headings: FoldHeading[];
|
|
22
|
+
/** Indexes of the top-level blocks hidden by a folded heading. */
|
|
23
|
+
hidden: Set<number>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The plan for a text whose top-level blocks are `blocks`: each one a
|
|
27
|
+
* heading (`{ level, text }`) or `null`. `folded` is the ids folded now.
|
|
28
|
+
*/
|
|
29
|
+
export declare function foldPlan(blocks: readonly (FoldHeadingInput | null)[], folded?: ReadonlySet<string>): FoldPlan;
|
|
30
|
+
export type MdFoldNode = {
|
|
31
|
+
type: string;
|
|
32
|
+
depth?: number;
|
|
33
|
+
value?: string;
|
|
34
|
+
children?: MdFoldNode[];
|
|
35
|
+
};
|
|
36
|
+
/** A heading's words as the editor reads them: its text and inline code (not raw HTML, not image alt text). */
|
|
37
|
+
export declare function mdText(node: MdFoldNode): string;
|
|
38
|
+
/** The top-level blocks of an mdast root, as foldPlan takes them. */
|
|
39
|
+
export declare function mdBlocks(root: MdFoldNode): (FoldHeadingInput | null)[];
|
|
40
|
+
/** The fold plan for a Markdown text, parsed as <Markdown> and the editor parse it. */
|
|
41
|
+
export declare function markdownFoldPlan(markdown: string, folded?: ReadonlySet<string>): FoldPlan;
|
|
42
|
+
/** Every heading with something to fold: Fold all. */
|
|
43
|
+
export declare function allFoldable(plan: FoldPlan): Set<string>;
|
|
44
|
+
/** `folded` with the heading `id` and every heading it sits under opened: a jump to it. */
|
|
45
|
+
export declare function openTo(plan: FoldPlan, folded: ReadonlySet<string>, id: string): Set<string>;
|
|
46
|
+
/** `folded` with every heading whose section holds the top-level block at `index` opened: the caret moved into it. */
|
|
47
|
+
export declare function openAt(plan: FoldPlan, folded: ReadonlySet<string>, index: number): Set<string>;
|
|
48
|
+
/** `folded` with `id` turned the other way. */
|
|
49
|
+
export declare function toggleFold(folded: ReadonlySet<string>, id: string): Set<string>;
|
|
50
|
+
/**
|
|
51
|
+
* For <Markdown> (react-markdown's rehype plugins): marks each top-level
|
|
52
|
+
* heading with its fold id and state, and hides every top-level block a
|
|
53
|
+
* folded heading covers (`hidden`). Raw HTML at the top level, which
|
|
54
|
+
* <Markdown> shows as its characters, is hidden as a paragraph of the same
|
|
55
|
+
* characters. GFM's footnotes, gathered at the end, are never hidden.
|
|
56
|
+
*/
|
|
57
|
+
export declare function rehypeFold(options: {
|
|
58
|
+
folded: ReadonlySet<string>;
|
|
59
|
+
}): (tree: unknown) => void;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Folding a text by its headings (`foldKey` on <Markdown> and the
|
|
2
|
+
// rich-text editor, and NotesOutline beside them). A heading at the top
|
|
3
|
+
// level of the text folds everything after it up to the next heading of
|
|
4
|
+
// its level or higher: a `##` folds its paragraphs and its `###`s, and
|
|
5
|
+
// stops at the next `##` or `#`. Headings inside a quote or a callout
|
|
6
|
+
// don't fold.
|
|
7
|
+
//
|
|
8
|
+
// Folding is the reader's, never the text's: what's folded is a set of
|
|
9
|
+
// heading ids, kept on the device (src/hooks/fold-store.ts), and the
|
|
10
|
+
// Markdown is never touched. A heading's id is its level and its words
|
|
11
|
+
// (`2:Safety`), with `#2`, `#3` added for the second and third heading of
|
|
12
|
+
// the same level and words, so the same heading gets the same id in
|
|
13
|
+
// <Markdown>, in the editor and in the outline, and keeps it while the
|
|
14
|
+
// text around it changes. Renaming a heading opens it.
|
|
15
|
+
import remarkGfm from 'remark-gfm';
|
|
16
|
+
import remarkParse from 'remark-parse';
|
|
17
|
+
import { unified } from 'unified';
|
|
18
|
+
function words(text) {
|
|
19
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The plan for a text whose top-level blocks are `blocks`: each one a
|
|
23
|
+
* heading (`{ level, text }`) or `null`. `folded` is the ids folded now.
|
|
24
|
+
*/
|
|
25
|
+
export function foldPlan(blocks, folded = new Set()) {
|
|
26
|
+
const headings = [];
|
|
27
|
+
const hidden = new Set();
|
|
28
|
+
const seen = new Map();
|
|
29
|
+
// The headings the current block sits under, outermost first.
|
|
30
|
+
const open = [];
|
|
31
|
+
blocks.forEach((block, index) => {
|
|
32
|
+
if (block)
|
|
33
|
+
while (open.length > 0 && open[open.length - 1].level >= block.level)
|
|
34
|
+
open.pop().end = index;
|
|
35
|
+
if (open.some((heading) => folded.has(heading.id)))
|
|
36
|
+
hidden.add(index);
|
|
37
|
+
// Anything after a heading, before one of its level or higher, is its section.
|
|
38
|
+
if (open.length > 0)
|
|
39
|
+
open[open.length - 1].foldable = true;
|
|
40
|
+
if (!block)
|
|
41
|
+
return;
|
|
42
|
+
const text = words(block.text);
|
|
43
|
+
const base = `${block.level}:${text}`;
|
|
44
|
+
const count = (seen.get(base) ?? 0) + 1;
|
|
45
|
+
seen.set(base, count);
|
|
46
|
+
const heading = {
|
|
47
|
+
level: block.level,
|
|
48
|
+
text,
|
|
49
|
+
id: count === 1 ? base : `${base}#${count}`,
|
|
50
|
+
index,
|
|
51
|
+
foldable: false,
|
|
52
|
+
parents: open.map((one) => one.id),
|
|
53
|
+
end: blocks.length,
|
|
54
|
+
};
|
|
55
|
+
headings.push(heading);
|
|
56
|
+
open.push(heading);
|
|
57
|
+
});
|
|
58
|
+
return { headings, hidden };
|
|
59
|
+
}
|
|
60
|
+
/** A heading's words as the editor reads them: its text and inline code (not raw HTML, not image alt text). */
|
|
61
|
+
export function mdText(node) {
|
|
62
|
+
if (node.type === 'text' || node.type === 'inlineCode')
|
|
63
|
+
return node.value ?? '';
|
|
64
|
+
if (node.type === 'break')
|
|
65
|
+
return ' ';
|
|
66
|
+
return (node.children ?? []).map(mdText).join('');
|
|
67
|
+
}
|
|
68
|
+
/** The top-level blocks of an mdast root, as foldPlan takes them. */
|
|
69
|
+
export function mdBlocks(root) {
|
|
70
|
+
return (root.children ?? []).map((node) => node.type === 'heading' && node.depth ? { level: node.depth, text: mdText(node) } : null);
|
|
71
|
+
}
|
|
72
|
+
const parser = unified().use(remarkParse).use(remarkGfm);
|
|
73
|
+
/** The fold plan for a Markdown text, parsed as <Markdown> and the editor parse it. */
|
|
74
|
+
export function markdownFoldPlan(markdown, folded) {
|
|
75
|
+
return foldPlan(mdBlocks(parser.parse(markdown)), folded);
|
|
76
|
+
}
|
|
77
|
+
/** Every heading with something to fold: Fold all. */
|
|
78
|
+
export function allFoldable(plan) {
|
|
79
|
+
return new Set(plan.headings.filter((heading) => heading.foldable).map((heading) => heading.id));
|
|
80
|
+
}
|
|
81
|
+
/** `folded` with the heading `id` and every heading it sits under opened: a jump to it. */
|
|
82
|
+
export function openTo(plan, folded, id) {
|
|
83
|
+
const heading = plan.headings.find((one) => one.id === id);
|
|
84
|
+
const next = new Set(folded);
|
|
85
|
+
if (!heading)
|
|
86
|
+
return next;
|
|
87
|
+
for (const parent of heading.parents)
|
|
88
|
+
next.delete(parent);
|
|
89
|
+
return next;
|
|
90
|
+
}
|
|
91
|
+
/** `folded` with every heading whose section holds the top-level block at `index` opened: the caret moved into it. */
|
|
92
|
+
export function openAt(plan, folded, index) {
|
|
93
|
+
const next = new Set(folded);
|
|
94
|
+
for (const heading of plan.headings)
|
|
95
|
+
if (heading.index < index && index < heading.end)
|
|
96
|
+
next.delete(heading.id);
|
|
97
|
+
return next;
|
|
98
|
+
}
|
|
99
|
+
/** `folded` with `id` turned the other way. */
|
|
100
|
+
export function toggleFold(folded, id) {
|
|
101
|
+
const next = new Set(folded);
|
|
102
|
+
if (next.has(id))
|
|
103
|
+
next.delete(id);
|
|
104
|
+
else
|
|
105
|
+
next.add(id);
|
|
106
|
+
return next;
|
|
107
|
+
}
|
|
108
|
+
function hastText(node) {
|
|
109
|
+
if (node.type === 'text')
|
|
110
|
+
return node.value ?? '';
|
|
111
|
+
if (node.type === 'element' && node.tagName === 'br')
|
|
112
|
+
return ' ';
|
|
113
|
+
if (node.type !== 'element')
|
|
114
|
+
return '';
|
|
115
|
+
return (node.children ?? []).map(hastText).join('');
|
|
116
|
+
}
|
|
117
|
+
const HEADING = /^h([1-6])$/;
|
|
118
|
+
/**
|
|
119
|
+
* For <Markdown> (react-markdown's rehype plugins): marks each top-level
|
|
120
|
+
* heading with its fold id and state, and hides every top-level block a
|
|
121
|
+
* folded heading covers (`hidden`). Raw HTML at the top level, which
|
|
122
|
+
* <Markdown> shows as its characters, is hidden as a paragraph of the same
|
|
123
|
+
* characters. GFM's footnotes, gathered at the end, are never hidden.
|
|
124
|
+
*/
|
|
125
|
+
export function rehypeFold(options) {
|
|
126
|
+
return (tree) => {
|
|
127
|
+
const root = tree;
|
|
128
|
+
const children = root.children ?? [];
|
|
129
|
+
// The blocks: the root's elements and raw HTML; the line breaks between them aren't.
|
|
130
|
+
const at = [];
|
|
131
|
+
children.forEach((node, index) => {
|
|
132
|
+
if (node.type === 'element' || node.type === 'raw')
|
|
133
|
+
at.push(index);
|
|
134
|
+
});
|
|
135
|
+
const blocks = at.map((index) => {
|
|
136
|
+
const node = children[index];
|
|
137
|
+
const level = node.type === 'element' ? HEADING.exec(node.tagName ?? '')?.[1] : undefined;
|
|
138
|
+
return level ? { level: Number(level), text: hastText(node) } : null;
|
|
139
|
+
});
|
|
140
|
+
const plan = foldPlan(blocks, options.folded);
|
|
141
|
+
for (const heading of plan.headings) {
|
|
142
|
+
const node = children[at[heading.index]];
|
|
143
|
+
node.properties = {
|
|
144
|
+
...node.properties,
|
|
145
|
+
dataFoldId: heading.id,
|
|
146
|
+
dataFoldLabel: heading.text,
|
|
147
|
+
dataFoldable: String(heading.foldable),
|
|
148
|
+
dataFolded: String(heading.foldable && options.folded.has(heading.id)),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
for (const block of plan.hidden) {
|
|
152
|
+
const index = at[block];
|
|
153
|
+
let node = children[index];
|
|
154
|
+
if (node.properties?.dataFootnotes !== undefined)
|
|
155
|
+
continue;
|
|
156
|
+
if (node.type === 'raw') {
|
|
157
|
+
node = { type: 'element', tagName: 'p', properties: {}, children: [{ type: 'text', value: node.value ?? '' }] };
|
|
158
|
+
children[index] = node;
|
|
159
|
+
}
|
|
160
|
+
node.properties = { ...node.properties, hidden: true };
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|