@tristan2828/ui-foundation 4.65.0 → 4.67.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/add-an-entity.md +32 -19
- package/dist/components/app/markdown.d.ts +4 -3
- package/dist/components/app/markdown.js +15 -8
- package/dist/components/app/milkdown-editor.js +39 -10
- package/dist/components/app/notes-outline.js +3 -2
- package/dist/components/app/rich-text-editor.d.ts +4 -3
- package/dist/components/app/rich-text-editor.js +5 -2
- package/dist/components/app/rich-text-fold.js +1 -1
- package/dist/components/app/rich-text-format-items.d.ts +6 -0
- package/dist/components/app/rich-text-format-items.js +13 -1
- package/dist/components/app/rich-text-formats.d.ts +6 -1
- package/dist/components/app/rich-text-formats.js +22 -5
- package/dist/components/app/rich-text-slash-menu.js +1 -1
- package/dist/components/app/rich-text-slash.js +9 -5
- package/dist/components/app/rich-text-toggle-heading.d.ts +8 -0
- package/dist/components/app/rich-text-toggle-heading.js +55 -0
- package/dist/lib/markdown-fold.d.ts +5 -2
- package/dist/lib/markdown-fold.js +24 -10
- package/dist/lib/markdown-toggle.d.ts +29 -0
- package/dist/lib/markdown-toggle.js +69 -0
- package/package.json +1 -1
|
@@ -3,24 +3,25 @@
|
|
|
3
3
|
<!-- index: written by `npm run docs:index` in the package from the headings below; never edit it by hand -->
|
|
4
4
|
## Index
|
|
5
5
|
|
|
6
|
-
- [The reference files](#the-reference-files) · lines
|
|
7
|
-
- [Before anything: the entity plan](#before-anything-the-entity-plan) · lines
|
|
8
|
-
- Step 1: Add `<Entity>` to `openapi.yaml` · lines
|
|
9
|
-
- Step 2: `npm run gen:api` · lines
|
|
10
|
-
- Step 3: Add gateway tests in `tests/gateway/<entity>.test.ts`, derived from the spec · lines
|
|
11
|
-
- Step 4: Add MSW handlers in `src/mocks/<entity>.ts` · lines
|
|
12
|
-
- Step 5: Add a gateway module in `src/api/gateway/<entity>.ts` · lines
|
|
13
|
-
- Step 6: Copy the reference files, one for one, not just the two screens · lines
|
|
14
|
-
- Step 7: Register routes · lines
|
|
15
|
-
- Step 8: Add Playwright specs for all three screens · lines
|
|
16
|
-
- Step 9: Register the nav entry and route names · lines
|
|
17
|
-
- Step 10: `npm run verify`. · lines
|
|
18
|
-
- [How a list opens a record](#how-a-list-opens-a-record) · lines
|
|
19
|
-
- [The record view](#the-record-view) · lines
|
|
20
|
-
- [What not to copy](#what-not-to-copy) · lines
|
|
21
|
-
- [After a commit, in the backend](#after-a-commit-in-the-backend) · lines
|
|
22
|
-
- [Deleting](#deleting) · lines
|
|
23
|
-
- [Fields only a script writes](#fields-only-a-script-writes) · lines
|
|
6
|
+
- [The reference files](#the-reference-files) · lines 40–62
|
|
7
|
+
- [Before anything: the entity plan](#before-anything-the-entity-plan) · lines 64–749
|
|
8
|
+
- Step 1: Add `<Entity>` to `openapi.yaml` · lines 106–113
|
|
9
|
+
- Step 2: `npm run gen:api` · lines 114–115
|
|
10
|
+
- Step 3: Add gateway tests in `tests/gateway/<entity>.test.ts`, derived from the spec · lines 116–127
|
|
11
|
+
- Step 4: Add MSW handlers in `src/mocks/<entity>.ts` · lines 128–137
|
|
12
|
+
- Step 5: Add a gateway module in `src/api/gateway/<entity>.ts` · lines 138–142
|
|
13
|
+
- Step 6: Copy the reference files, one for one, not just the two screens · lines 143–638
|
|
14
|
+
- Step 7: Register routes · lines 639–652
|
|
15
|
+
- Step 8: Add Playwright specs for all three screens · lines 653–731
|
|
16
|
+
- Step 9: Register the nav entry and route names · lines 732–746
|
|
17
|
+
- Step 10: `npm run verify`. · lines 747–749
|
|
18
|
+
- [How a list opens a record](#how-a-list-opens-a-record) · lines 751–854
|
|
19
|
+
- [The record view](#the-record-view) · lines 829–854
|
|
20
|
+
- [What not to copy](#what-not-to-copy) · lines 856–882
|
|
21
|
+
- [After a commit, in the backend](#after-a-commit-in-the-backend) · lines 884–901
|
|
22
|
+
- [Deleting](#deleting) · lines 903–932
|
|
23
|
+
- [Fields only a script writes](#fields-only-a-script-writes) · lines 934–955
|
|
24
|
+
- [Searching one part of the app](#searching-one-part-of-the-app) · lines 957–966
|
|
24
25
|
|
|
25
26
|
<!-- /index -->
|
|
26
27
|
|
|
@@ -841,7 +842,8 @@ view reaches them all. A record uses the parts it has, in this order:
|
|
|
841
842
|
`KeyStats`), each edited in place where it's shown.
|
|
842
843
|
- **Sections**: with long text (notes, a body) `layout="rail"` and
|
|
843
844
|
`mobileOrder="main-first"`: the label/value facts in the rail, the long
|
|
844
|
-
text in the main column, folded by its headings (`foldKey
|
|
845
|
+
text in the main column, folded by its toggle headings (`foldKey`;
|
|
846
|
+
`## Safety {toggle="true"}`, the only headings that fold) with an
|
|
845
847
|
Outline for a long one and Print notes (`print`). Without long text,
|
|
846
848
|
one column. Every value edited in place (`edit`), a sub-item list
|
|
847
849
|
included (`editInPlace({ kind: 'list' })`).
|
|
@@ -951,3 +953,14 @@ the next run would create the record a second time.
|
|
|
951
953
|
- Don't use it for a field the server works out itself (`computed`) or
|
|
952
954
|
one that's never on the request: those aren't in the create or update
|
|
953
955
|
schema to begin with.
|
|
956
|
+
|
|
957
|
+
## Searching one part of the app
|
|
958
|
+
|
|
959
|
+
An app built of parts (modules, areas) gives each entity's `McpEntity` a
|
|
960
|
+
`group`, the part's name in lowercase words joined by -
|
|
961
|
+
(`group="health"`). `search` then takes an optional `group` and looks only
|
|
962
|
+
in that group's entities, so a chat that searches "band" for a health
|
|
963
|
+
question isn't handed purchases and comics too. The tool's description
|
|
964
|
+
lists each group's entities, and `group` is offered only when some entity
|
|
965
|
+
has one; an unknown group is a refusal naming the ones there are. An
|
|
966
|
+
entity with no group is searched only when no group is given.
|
|
@@ -18,9 +18,10 @@ export type MarkdownProps = {
|
|
|
18
18
|
*/
|
|
19
19
|
headingFrom?: HeadingFrom;
|
|
20
20
|
/**
|
|
21
|
-
* Folds the text by its headings: each heading
|
|
22
|
-
*
|
|
23
|
-
*
|
|
21
|
+
* Folds the text by its toggle headings: each toggle heading
|
|
22
|
+
* (`## Safety {toggle="true"}`) at its top level gets a button that
|
|
23
|
+
* folds what's under it (to the next heading of its level or higher).
|
|
24
|
+
* A plain heading never folds. What's folded is remembered on this device under this key
|
|
24
25
|
* (name it for the app and the record: `"my-app:task:84:notes"`), and
|
|
25
26
|
* shared with a RichTextEditor or NotesOutline given the same key. The
|
|
26
27
|
* text is never changed.
|
|
@@ -45,10 +45,12 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
45
45
|
// `==words==` is highlighted words (amber, or `=={green}words==`: yellow,
|
|
46
46
|
// green, red): a <mark> on the colour's wash (src/lib/markdown-highlight.ts).
|
|
47
47
|
//
|
|
48
|
-
// With `foldKey`, each heading
|
|
48
|
+
// With `foldKey`, each toggle heading (`## Safety {toggle="true"}`,
|
|
49
|
+
// src/lib/markdown-toggle.ts) at the text's top level has a button before
|
|
49
50
|
// it that folds its section away (src/lib/markdown-fold.ts), remembered on
|
|
50
51
|
// this device under the key and shared with an editor or NotesOutline
|
|
51
|
-
// given the same one. The text itself is
|
|
52
|
+
// given the same one. A plain heading never folds. The text itself is
|
|
53
|
+
// never changed; a toggle heading's marker is never shown.
|
|
52
54
|
//
|
|
53
55
|
// Printed (EntityView's Print notes, PrintingContext), it's the whole
|
|
54
56
|
// text: nothing folded, no fold buttons, task boxes as glyphs.
|
|
@@ -70,12 +72,13 @@ import { CALLOUT_KINDS, remarkCallouts } from '../../lib/markdown-callout.js';
|
|
|
70
72
|
import { rehypeFold, toggleFold } from '../../lib/markdown-fold.js';
|
|
71
73
|
import { isHighlightColor, remarkHighlights } from '../../lib/markdown-highlight.js';
|
|
72
74
|
import { rehypeTables } from '../../lib/markdown-table.js';
|
|
75
|
+
import { remarkToggleHeadings } from '../../lib/markdown-toggle.js';
|
|
73
76
|
import { toggleTaskAt } from '../../lib/markdown-tasks.js';
|
|
74
77
|
import { textTopHeading } from '../../lib/markdown-top-heading.js';
|
|
75
78
|
import { CALLOUT_BOX, CALLOUT_LOOK, DEFAULT_HEADING_LEVEL, FOLD_BUTTON, FOLD_HEADING, headingBand, headingClass, headingTag, PROSE, PROSE_INSET, } from './prose-look.js';
|
|
76
79
|
import { usePrinting } from './print-context.js';
|
|
77
80
|
import { headerTexts, TABLE_SCROLL_FOCUS, watchTableScroll } from './prose-table-scroll.js';
|
|
78
|
-
const REMARK_PLUGINS = [remarkGfm, remarkCallouts, remarkHighlights];
|
|
81
|
+
const REMARK_PLUGINS = [remarkGfm, remarkCallouts, remarkHighlights, remarkToggleHeadings];
|
|
79
82
|
const HIGHLIGHT_WASH = {
|
|
80
83
|
amber: 'bg-highlight-amber',
|
|
81
84
|
yellow: 'bg-highlight-yellow',
|
|
@@ -139,19 +142,23 @@ function TaskBox({ checked }) {
|
|
|
139
142
|
const FoldContext = createContext(null);
|
|
140
143
|
// A heading by its Markdown level: the element the page's outline gives
|
|
141
144
|
// it (headingTag), the look its level has. The text's colour first, so a
|
|
142
|
-
// level's own (`###`'s muted label) wins. With `foldKey`, a top-level
|
|
143
|
-
// (marked by rehypeFold) sits beside its fold button, the
|
|
144
|
-
// together; one with nothing under it keeps the button's
|
|
145
|
-
// words line up with the others'.
|
|
145
|
+
// level's own (`###`'s muted label) wins. With `foldKey`, a top-level
|
|
146
|
+
// toggle heading (marked by rehypeFold) sits beside its fold button, the
|
|
147
|
+
// look on the two together; one with nothing under it keeps the button's
|
|
148
|
+
// room, so its words line up with the others'. A plain heading is drawn
|
|
149
|
+
// plain, with no button and no room for one.
|
|
146
150
|
function heading(level, top, textTop) {
|
|
147
151
|
const Tag = headingTag(level, top, textTop);
|
|
148
152
|
return function Heading(props) {
|
|
149
153
|
const toggle = useContext(FoldContext);
|
|
150
|
-
const { 'data-fold-id': foldId, 'data-foldable': foldable, 'data-folded': folded, 'data-fold-label': label, hidden, ...rest } = omitNode(props);
|
|
154
|
+
const { 'data-fold-id': foldId, 'data-foldable': foldable, 'data-folded': folded, 'data-fold-label': label, 'data-toggle': isToggle, hidden, ...rest } = omitNode(props);
|
|
151
155
|
const look = cn('text-foreground', headingClass(level));
|
|
152
156
|
const band = headingBand(level) || undefined;
|
|
153
157
|
if (!toggle || foldId === undefined)
|
|
154
158
|
return _jsx(Tag, { ...rest, hidden: hidden, "data-band": band, className: look });
|
|
159
|
+
// Plain: still found by its fold id (NotesOutline's jump), never folded.
|
|
160
|
+
if (isToggle !== 'true')
|
|
161
|
+
return _jsx(Tag, { ...rest, "data-fold-id": foldId, hidden: hidden, "data-band": band, className: look });
|
|
155
162
|
const open = folded !== 'true';
|
|
156
163
|
return (_jsxs("div", { "data-fold-id": foldId, hidden: hidden, "data-band": band, className: cn(FOLD_HEADING, look), children: [foldable === 'true' ? (_jsx("button", { type: "button", "aria-expanded": open, "aria-label": label || 'Section', "data-slot": "fold-button", className: FOLD_BUTTON, onClick: () => toggle(foldId), children: _jsx(ChevronDownIcon, { "aria-hidden": "true", className: cn('size-4 transition-transform', !open && '-rotate-90') }) })) : (_jsx("span", { "aria-hidden": "true", className: "-ml-1 w-5 shrink-0" })), _jsx(Tag, { ...rest, className: "min-w-0 flex-1" })] }));
|
|
157
164
|
};
|
|
@@ -54,14 +54,17 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
54
54
|
// With `upload`, an image pasted, dropped or picked with the toolbar's
|
|
55
55
|
// Image button is uploaded through it and goes in as ``
|
|
56
56
|
// (rich-text-image-upload.ts); a refusal or a failed upload is a toast.
|
|
57
|
-
// -
|
|
57
|
+
// - Toggle headings (`## Safety {toggle="true"}`, rich-text-toggle-heading.ts):
|
|
58
|
+
// a heading's `toggle` attribute, typed as `>## `, picked from the slash
|
|
59
|
+
// menu or the toolbar. A plain heading is the default.
|
|
60
|
+
// - `foldKey`: the text folds by its toggle headings, as <Markdown> does
|
|
58
61
|
// (rich-text-fold.ts), remembered on this device and shared with
|
|
59
62
|
// NotesOutline; the document never changes.
|
|
60
63
|
import { useEffect, useId, useRef, useState } from 'react';
|
|
61
64
|
import { createPortal } from 'react-dom';
|
|
62
65
|
import { defaultValueCtx, Editor, editorViewCtx, editorViewOptionsCtx, parserCtx, remarkStringifyOptionsCtx, rootCtx, schemaCtx, serializerCtx, } from '@milkdown/kit/core';
|
|
63
66
|
import { history } from '@milkdown/kit/plugin/history';
|
|
64
|
-
import { commands, inputRules, keymap, markInputRules, plugins, remarkPreserveEmptyLinePlugin, schema, } from '@milkdown/kit/preset/commonmark';
|
|
67
|
+
import { commands, headingSchema, inputRules, keymap, markInputRules, plugins, remarkPreserveEmptyLinePlugin, schema, } from '@milkdown/kit/preset/commonmark';
|
|
65
68
|
import { gfm } from '@milkdown/kit/preset/gfm';
|
|
66
69
|
import { Fragment, Slice } from '@milkdown/kit/prose/model';
|
|
67
70
|
import { NodeSelection, Plugin, TextSelection } from '@milkdown/kit/prose/state';
|
|
@@ -91,6 +94,7 @@ import { calloutKind, calloutView, tableView } from './rich-text-node-views.js';
|
|
|
91
94
|
import { droppedFiles, imageUploadPlugin, pastedImages, uploadImages, uploadPlaceholders } from './rich-text-image-upload.js';
|
|
92
95
|
import { handleShortcut } from './rich-text-shortcuts.js';
|
|
93
96
|
import { splitTaskItem, taskInputRule } from './rich-text-tasks.js';
|
|
97
|
+
import { toggleHeadingInputRule, toggleHeadingRemark, withToggle } from './rich-text-toggle-heading.js';
|
|
94
98
|
import { addRowAtEnd, colorTableCells, handleTableKey, runTableAction, tableLook as tableLookPlugin } from './rich-text-table.js';
|
|
95
99
|
import { TABLE_MENU_EVENT, TableGrips } from './rich-text-table-grips.js';
|
|
96
100
|
import { applySlash, closeSlash, slashItems, slashOptionId, slashPlugin, slashQuery, } from './rich-text-slash.js';
|
|
@@ -195,22 +199,43 @@ function htmlToText(fragment) {
|
|
|
195
199
|
// A heading: the element `headingLevel` gives its level, the look its
|
|
196
200
|
// level has. Marked as a band for PROSE_INSET (`#` and `##`).
|
|
197
201
|
//
|
|
198
|
-
// With `foldKey` (`onFold`),
|
|
199
|
-
// <Markdown>: the look on the two together, the button out of the
|
|
200
|
-
// order (Ctrl/Cmd+Alt+[ and ] and NotesOutline are the keyboard's
|
|
201
|
-
// kept invisible where there's nothing under the heading to fold,
|
|
202
|
-
// heading isn't at the top level.
|
|
202
|
+
// With `foldKey` (`onFold`), a toggle heading sits beside its fold button,
|
|
203
|
+
// as in <Markdown>: the look on the two together, the button out of the
|
|
204
|
+
// tab order (Ctrl/Cmd+Alt+[ and ] and NotesOutline are the keyboard's
|
|
205
|
+
// way), kept invisible where there's nothing under the heading to fold,
|
|
206
|
+
// or the heading isn't at the top level. A plain heading has no button
|
|
207
|
+
// and no room for one; its fold id still marks it, for NotesOutline.
|
|
203
208
|
function headingView(node, top, textTop, decorations, onFold) {
|
|
204
209
|
const level = Number(node.attrs.level);
|
|
205
210
|
const heading = document.createElement(headingTag(level, top, textTop));
|
|
206
|
-
const sameLevel = (next) => next.type === node.type && next.attrs.level === node.attrs.level;
|
|
207
|
-
if (
|
|
211
|
+
const sameLevel = (next) => next.type === node.type && next.attrs.level === node.attrs.level && Boolean(next.attrs.toggle) === Boolean(node.attrs.toggle);
|
|
212
|
+
if (node.attrs.toggle)
|
|
213
|
+
heading.dataset.toggle = '';
|
|
214
|
+
if (!onFold || !node.attrs.toggle) {
|
|
208
215
|
heading.className = headingClass(level);
|
|
209
216
|
if (headingBand(level))
|
|
210
217
|
heading.dataset.band = '';
|
|
211
|
-
|
|
218
|
+
const mark = (decorations) => {
|
|
219
|
+
const spec = onFold ? foldSpecOf(decorations) : null;
|
|
220
|
+
if (spec)
|
|
221
|
+
heading.dataset.foldId = spec.id;
|
|
222
|
+
else
|
|
223
|
+
delete heading.dataset.foldId;
|
|
224
|
+
};
|
|
225
|
+
mark(decorations);
|
|
226
|
+
return {
|
|
227
|
+
dom: heading,
|
|
228
|
+
contentDOM: heading,
|
|
229
|
+
update: (next, decorations) => {
|
|
230
|
+
if (!sameLevel(next))
|
|
231
|
+
return false;
|
|
232
|
+
mark(decorations);
|
|
233
|
+
return true;
|
|
234
|
+
},
|
|
235
|
+
};
|
|
212
236
|
}
|
|
213
237
|
const dom = document.createElement('div');
|
|
238
|
+
dom.dataset.toggle = '';
|
|
214
239
|
dom.className = cn(FOLD_HEADING, headingClass(level));
|
|
215
240
|
if (headingBand(level))
|
|
216
241
|
dom.dataset.band = '';
|
|
@@ -725,6 +750,8 @@ export default function MilkdownEditor({ id, defaultValue, onChange, onProblem,
|
|
|
725
750
|
...markdownStyle(original),
|
|
726
751
|
handlers: { ...options.handlers, callout: calloutHandler, highlight: highlightHandler },
|
|
727
752
|
}));
|
|
753
|
+
// Headings carry `toggle` (rich-text-toggle-heading.ts).
|
|
754
|
+
ctx.update(headingSchema.key, withToggle);
|
|
728
755
|
ctx.update(editorViewOptionsCtx, (options) => ({
|
|
729
756
|
...options,
|
|
730
757
|
editable: () => !latest.current.readOnly,
|
|
@@ -793,6 +820,8 @@ export default function MilkdownEditor({ id, defaultValue, onChange, onProblem,
|
|
|
793
820
|
.use(COMMONMARK)
|
|
794
821
|
.use(gfm)
|
|
795
822
|
.use(taskInputRule)
|
|
823
|
+
.use(toggleHeadingRemark)
|
|
824
|
+
.use(toggleHeadingInputRule)
|
|
796
825
|
.use(calloutRemark)
|
|
797
826
|
.use(calloutSchema)
|
|
798
827
|
.use(highlightRemark)
|
|
@@ -2,7 +2,8 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
2
2
|
// A long text's headings as a list to jump with, beside <Markdown> or the
|
|
3
3
|
// rich-text editor given the same `foldKey`: each heading a button that
|
|
4
4
|
// opens its section (and any it sits under) and brings it into view, with
|
|
5
|
-
// Fold all and Open all above
|
|
5
|
+
// Fold all and Open all above (when a toggle heading has something to
|
|
6
|
+
// fold). Every top-level heading is listed, plain or toggle, as folding
|
|
6
7
|
// sees them (src/lib/markdown-fold.ts); the deepest levels are left out
|
|
7
8
|
// (`depth`), so a long text's outline stays a list of its parts.
|
|
8
9
|
//
|
|
@@ -53,7 +54,7 @@ export function NotesOutline({ markdown, foldKey, depth = 2, minHeadings = 2, 'a
|
|
|
53
54
|
button.focus({ preventScroll: true });
|
|
54
55
|
});
|
|
55
56
|
};
|
|
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
|
+
return (_jsxs("nav", { "aria-label": ariaLabel, "data-slot": "notes-outline", className: cn('flex min-w-0 flex-col gap-2', className), children: [foldable.size > 0 && (_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
58
|
const isFolded = heading.foldable && folded.has(heading.id);
|
|
58
59
|
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
|
}) })] }));
|
|
@@ -74,9 +74,10 @@ export type RichTextEditorProps = {
|
|
|
74
74
|
*/
|
|
75
75
|
headingFrom?: HeadingFrom;
|
|
76
76
|
/**
|
|
77
|
-
* Folds the text by its headings, as `<Markdown>`'s `foldKey`
|
|
78
|
-
*
|
|
79
|
-
*
|
|
77
|
+
* Folds the text by its toggle headings, as `<Markdown>`'s `foldKey`
|
|
78
|
+
* does: each toggle heading (`## Safety {toggle="true"}`) at the text's
|
|
79
|
+
* top level has a button that folds what's under it (a plain heading
|
|
80
|
+
* never folds), Ctrl/Cmd+Alt+[ folds the section the caret is in and
|
|
80
81
|
* Ctrl/Cmd+Alt+] opens the heading it's on, and the caret moving into a
|
|
81
82
|
* folded section opens it. Remembered on this device under this key,
|
|
82
83
|
* and shared with `<Markdown>` and NotesOutline given the same one.
|
|
@@ -29,8 +29,11 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
29
29
|
// kept as Markdown already writes them (src/lib/markdown-table.ts).
|
|
30
30
|
// `tableLook` picks compact (the default) or boxed, as on <Markdown>.
|
|
31
31
|
//
|
|
32
|
-
// With `foldKey`, the text folds by its headings (NotesOutline
|
|
33
|
-
// beside it), the Markdown untouched.
|
|
32
|
+
// With `foldKey`, the text folds by its toggle headings (NotesOutline
|
|
33
|
+
// lists every heading beside it), the Markdown untouched. A toggle heading
|
|
34
|
+
// is `## Safety {toggle="true"}` in the Markdown: typed as `>## `, picked
|
|
35
|
+
// from the slash menu (Toggle heading 1 to 3) or made from the heading the
|
|
36
|
+
// caret is in with the toolbar's Toggle heading. Plain is the default.
|
|
34
37
|
//
|
|
35
38
|
// `defaultValue` is read when it mounts, then followed: a new value the
|
|
36
39
|
// editor didn't report itself (a form's loaded record arriving after the
|
|
@@ -5,7 +5,7 @@ export const foldPluginKey = new PluginKey('fold');
|
|
|
5
5
|
/** The fold plan of a document's top-level blocks. */
|
|
6
6
|
export function docFoldPlan(doc, folded) {
|
|
7
7
|
const blocks = [];
|
|
8
|
-
doc.forEach((node) => blocks.push(node.type.name === 'heading' ? { level: Number(node.attrs.level), text: node.textContent } : null));
|
|
8
|
+
doc.forEach((node) => blocks.push(node.type.name === 'heading' ? { level: Number(node.attrs.level), text: node.textContent, toggle: Boolean(node.attrs.toggle) } : null));
|
|
9
9
|
return foldPlan(blocks, folded);
|
|
10
10
|
}
|
|
11
11
|
function build(doc, folded) {
|
|
@@ -19,6 +19,12 @@ export type FormatItem = {
|
|
|
19
19
|
export declare const FORMAT_GROUPS: FormatItem[][];
|
|
20
20
|
/** Every format by its id. */
|
|
21
21
|
export declare const FORMAT_ITEMS: Map<FormatId, FormatItem>;
|
|
22
|
+
/**
|
|
23
|
+
* A toggle heading of each level, for the slash menu, beside the plain
|
|
24
|
+
* headings: "Toggle heading 2". On the toolbar, Toggle heading turns the
|
|
25
|
+
* caret's heading into one.
|
|
26
|
+
*/
|
|
27
|
+
export declare const TOGGLE_HEADING_VARIANTS: FormatItem[];
|
|
22
28
|
/**
|
|
23
29
|
* A callout of each kind but the default, for the slash menu: "Warning
|
|
24
30
|
* callout". Not on the toolbar (its Callout makes a note).
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// blocks (Cmd+Option on a Mac, where Cmd+Shift+3 to 5 take screenshots),
|
|
8
8
|
// written `Block+…` here; Mod+Shift+S strikes through. Milkdown's own
|
|
9
9
|
// (Ctrl+Alt+1, Mod+Alt+X, Mod+Shift+B, …) still work.
|
|
10
|
-
import { BoldIcon, CodeIcon, Heading1Icon, Heading2Icon, Heading3Icon, HighlighterIcon, IndentDecreaseIcon, IndentIncreaseIcon, ItalicIcon, LinkIcon, ListChecksIcon, ListIcon, ListOrderedIcon, MinusIcon, SquareCodeIcon, StrikethroughIcon, TableIcon, TextQuoteIcon, } from 'lucide-react';
|
|
10
|
+
import { BoldIcon, CodeIcon, Heading1Icon, Heading2Icon, Heading3Icon, HighlighterIcon, IndentDecreaseIcon, IndentIncreaseIcon, ItalicIcon, LinkIcon, ListChecksIcon, ListCollapseIcon, ListIcon, ListOrderedIcon, MinusIcon, SquareCodeIcon, StrikethroughIcon, TableIcon, TextQuoteIcon, } from 'lucide-react';
|
|
11
11
|
import { CALLOUT_KINDS } from '../../lib/markdown-callout.js';
|
|
12
12
|
import { CALLOUT_LOOK } from './prose-look.js';
|
|
13
13
|
import { HIGHLIGHT_COLORS } from '../../lib/markdown-highlight.js';
|
|
@@ -23,6 +23,7 @@ export const FORMAT_GROUPS = [
|
|
|
23
23
|
{ id: 'heading1', label: 'Heading 1', icon: Heading1Icon, keys: 'Block+1' },
|
|
24
24
|
{ id: 'heading2', label: 'Heading 2', icon: Heading2Icon, keys: 'Block+2' },
|
|
25
25
|
{ id: 'heading3', label: 'Heading 3', icon: Heading3Icon, keys: 'Block+3' },
|
|
26
|
+
{ id: 'toggleHeading', label: 'Toggle heading', icon: ListCollapseIcon, hint: 'folds what is under it; or type >## ' },
|
|
26
27
|
],
|
|
27
28
|
[
|
|
28
29
|
{ id: 'tasks', label: 'Task list', icon: ListChecksIcon, keys: 'Block+4', hint: 'tick an item: Mod+Shift+Enter' },
|
|
@@ -42,6 +43,17 @@ export const FORMAT_GROUPS = [
|
|
|
42
43
|
];
|
|
43
44
|
/** Every format by its id. */
|
|
44
45
|
export const FORMAT_ITEMS = new Map(FORMAT_GROUPS.flat().map((item) => [item.id, item]));
|
|
46
|
+
/**
|
|
47
|
+
* A toggle heading of each level, for the slash menu, beside the plain
|
|
48
|
+
* headings: "Toggle heading 2". On the toolbar, Toggle heading turns the
|
|
49
|
+
* caret's heading into one.
|
|
50
|
+
*/
|
|
51
|
+
export const TOGGLE_HEADING_VARIANTS = [1, 2, 3].map((level) => ({
|
|
52
|
+
id: `toggleHeading${level}`,
|
|
53
|
+
label: `Toggle heading ${level}`,
|
|
54
|
+
icon: [Heading1Icon, Heading2Icon, Heading3Icon][level - 1],
|
|
55
|
+
hint: `or type >${'#'.repeat(level)} `,
|
|
56
|
+
}));
|
|
45
57
|
/**
|
|
46
58
|
* A callout of each kind but the default, for the slash menu: "Warning
|
|
47
59
|
* callout". Not on the toolbar (its Callout makes a note).
|
|
@@ -2,7 +2,12 @@ import { type Node as ProseNode, type Schema } from '@milkdown/kit/prose/model';
|
|
|
2
2
|
import { type EditorState, type Transaction } from '@milkdown/kit/prose/state';
|
|
3
3
|
import type { CalloutKind } from '../../lib/markdown-callout.js';
|
|
4
4
|
import { type HighlightColor } from '../../lib/markdown-highlight.js';
|
|
5
|
-
export type FormatId = 'bold' | 'italic' | 'strike' | 'code' | 'highlight' | HighlightFormat | 'highlight-off' | 'indent' | 'outdent' | 'text' | 'heading1' | 'heading2' | 'heading3' | 'bullets' | 'numbers' | 'tasks' | 'quote' | CalloutFormat | 'codeBlock' | 'divider' | 'table' | 'link';
|
|
5
|
+
export type FormatId = 'bold' | 'italic' | 'strike' | 'code' | 'highlight' | HighlightFormat | 'highlight-off' | 'indent' | 'outdent' | 'text' | 'heading1' | 'heading2' | 'heading3' | ToggleHeadingFormat | 'toggleHeading' | 'bullets' | 'numbers' | 'tasks' | 'quote' | CalloutFormat | 'codeBlock' | 'divider' | 'table' | 'link';
|
|
6
|
+
/**
|
|
7
|
+
* A toggle heading of each level (the slash menu's). `toggleHeading`, the
|
|
8
|
+
* toolbar's, turns the heading the caret is in into a toggle one or back.
|
|
9
|
+
*/
|
|
10
|
+
export type ToggleHeadingFormat = 'toggleHeading1' | 'toggleHeading2' | 'toggleHeading3';
|
|
6
11
|
/** Words highlighted in each colour. `highlight` is the toolbar's menu of them. */
|
|
7
12
|
export type HighlightFormat = `highlight-${HighlightColor}`;
|
|
8
13
|
/** The colour a highlight format puts on, or null when it isn't one. */
|
|
@@ -20,6 +20,12 @@ export function calloutKindOf(id) {
|
|
|
20
20
|
return id.startsWith('callout-') ? id.slice('callout-'.length) : null;
|
|
21
21
|
}
|
|
22
22
|
const HEADING_LEVEL = { heading1: 1, heading2: 2, heading3: 3 };
|
|
23
|
+
const TOGGLE_LEVEL = { toggleHeading1: 1, toggleHeading2: 2, toggleHeading3: 3 };
|
|
24
|
+
// The heading the caret's line is, if it's one, and whether it's a toggle.
|
|
25
|
+
function headingHere(state) {
|
|
26
|
+
const parent = state.selection.$from.parent;
|
|
27
|
+
return parent.type.name === 'heading' ? { level: Number(parent.attrs.level), toggle: Boolean(parent.attrs.toggle) } : null;
|
|
28
|
+
}
|
|
23
29
|
const MARKS = {
|
|
24
30
|
bold: 'strong',
|
|
25
31
|
italic: 'emphasis',
|
|
@@ -95,9 +101,11 @@ export function formatOn(state, id) {
|
|
|
95
101
|
if (mark)
|
|
96
102
|
return markOn(state, schema.marks[mark]);
|
|
97
103
|
const parent = state.selection.$from.parent;
|
|
98
|
-
const level = HEADING_LEVEL[id];
|
|
104
|
+
const level = HEADING_LEVEL[id] ?? TOGGLE_LEVEL[id];
|
|
99
105
|
if (level)
|
|
100
|
-
return parent.type.name === 'heading' && parent.attrs.level === level;
|
|
106
|
+
return parent.type.name === 'heading' && parent.attrs.level === level && Boolean(parent.attrs.toggle) === (id in TOGGLE_LEVEL);
|
|
107
|
+
if (id === 'toggleHeading')
|
|
108
|
+
return Boolean(headingHere(state)?.toggle);
|
|
101
109
|
if (id === 'highlight')
|
|
102
110
|
return highlightColor(state) !== null;
|
|
103
111
|
const color = highlightColorOf(id);
|
|
@@ -178,9 +186,18 @@ export function toggleFormat(state, dispatch, id) {
|
|
|
178
186
|
return liftListItem(schema.nodes.list_item)(state, dispatch);
|
|
179
187
|
const on = formatOn(state, id);
|
|
180
188
|
const paragraph = schema.nodes.paragraph;
|
|
181
|
-
const level = HEADING_LEVEL[id];
|
|
182
|
-
if (level)
|
|
183
|
-
|
|
189
|
+
const level = HEADING_LEVEL[id] ?? TOGGLE_LEVEL[id];
|
|
190
|
+
if (level) {
|
|
191
|
+
if (on)
|
|
192
|
+
return setBlockType(paragraph)(state, dispatch);
|
|
193
|
+
return setBlockType(schema.nodes.heading, { level, toggle: id in TOGGLE_LEVEL })(state, dispatch);
|
|
194
|
+
}
|
|
195
|
+
// The toolbar's Toggle heading: a heading turned toggle or plain, its
|
|
196
|
+
// level kept; any other line a toggle `##`.
|
|
197
|
+
if (id === 'toggleHeading') {
|
|
198
|
+
const here = headingHere(state);
|
|
199
|
+
return setBlockType(schema.nodes.heading, { level: here?.level ?? 2, toggle: !here?.toggle })(state, dispatch);
|
|
200
|
+
}
|
|
184
201
|
const kind = calloutKindOf(id);
|
|
185
202
|
if (kind)
|
|
186
203
|
return toggleCallout(state, dispatch, kind);
|
|
@@ -44,7 +44,7 @@ export function RichTextSlashMenu({ id, view, at, field, items, active, onHighli
|
|
|
44
44
|
return (_jsx("div", { ref: setMenu, id: id, role: "listbox", "aria-label": "Blocks", "data-slot": "rich-text-slash-menu", "data-side": placement?.side,
|
|
45
45
|
// Tall enough for every block, but a filtered list can still scroll:
|
|
46
46
|
// focusable for axe (scrollable-region-focusable), never by a press.
|
|
47
|
-
tabIndex: -1, onMouseDown: (event) => event.preventDefault(), style: placement ? { top: placement.top, left: placement.left } : undefined, className: cn('absolute z-50 flex max-h-
|
|
47
|
+
tabIndex: -1, onMouseDown: (event) => event.preventDefault(), style: placement ? { top: placement.top, left: placement.left } : undefined, className: cn('absolute z-50 flex max-h-128 w-56 flex-col gap-0.5 overflow-y-auto rounded-lg bg-popover p-1 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10', 'animate-in fade-in-0 zoom-in-95 duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=top]:slide-in-from-bottom-2',
|
|
48
48
|
// Measured before it's seen, so it never shows in the wrong place.
|
|
49
49
|
!placement && 'invisible'), children: items.map((item, index) => {
|
|
50
50
|
const Icon = item.icon;
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// `/usr/bin`) never does, and neither does a "/" mid-line or inside a
|
|
9
9
|
// list, a quote or a code block.
|
|
10
10
|
import { Plugin, PluginKey } from '@milkdown/kit/prose/state';
|
|
11
|
-
import { CALLOUT_VARIANTS, FORMAT_GROUPS, HIGHLIGHT_VARIANTS } from './rich-text-format-items.js';
|
|
11
|
+
import { CALLOUT_VARIANTS, FORMAT_GROUPS, HIGHLIGHT_VARIANTS, TOGGLE_HEADING_VARIANTS } from './rich-text-format-items.js';
|
|
12
12
|
import { toggleFormat } from './rich-text-formats.js';
|
|
13
13
|
const slashKey = new PluginKey('rich-text-slash');
|
|
14
14
|
// Longer than any item's name: past it, the person is writing, not picking.
|
|
@@ -82,6 +82,9 @@ const KEYWORDS = {
|
|
|
82
82
|
heading1: ['h1', 'title', 'heading'],
|
|
83
83
|
heading2: ['h2', 'subheading', 'heading'],
|
|
84
84
|
heading3: ['h3', 'heading'],
|
|
85
|
+
toggleHeading1: ['toggle', 'fold', 'collapse', 'heading'],
|
|
86
|
+
toggleHeading2: ['toggle', 'fold', 'collapse', 'heading'],
|
|
87
|
+
toggleHeading3: ['toggle', 'fold', 'collapse', 'heading'],
|
|
85
88
|
bullets: ['ul', 'unordered', 'bullet'],
|
|
86
89
|
numbers: ['ol', 'ordered', 'number'],
|
|
87
90
|
tasks: ['todo', 'checkbox', 'checklist'],
|
|
@@ -99,11 +102,12 @@ const KEYWORDS = {
|
|
|
99
102
|
'highlight-green': ['highlight', 'mark', 'marker', 'colour', 'color'],
|
|
100
103
|
'highlight-red': ['highlight', 'mark', 'marker', 'colour', 'color'],
|
|
101
104
|
};
|
|
102
|
-
// The menu's items: the blocks, in the toolbar's order
|
|
103
|
-
//
|
|
104
|
-
//
|
|
105
|
-
// the list "/" opens with stays short.
|
|
105
|
+
// The menu's items: the blocks, in the toolbar's order, the toggle
|
|
106
|
+
// headings after the plain ones (the toolbar's one Toggle heading isn't
|
|
107
|
+
// here). The kinds of callout past the default, and the highlights, only
|
|
108
|
+
// once something's typed ("/warn"), so the list "/" opens with stays short.
|
|
106
109
|
const BLOCKS = FORMAT_GROUPS.flat().filter((item) => KEYWORDS[item.id]);
|
|
110
|
+
BLOCKS.splice(BLOCKS.findIndex((item) => item.id === 'heading3') + 1, 0, ...TOGGLE_HEADING_VARIANTS);
|
|
107
111
|
/** The blocks matching what's typed after the "/": in the name, or the start of a keyword. */
|
|
108
112
|
export function slashItems(query) {
|
|
109
113
|
const typed = query.trim().toLowerCase();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { NodeSchema } from '@milkdown/kit/transformer';
|
|
2
|
+
export declare const toggleHeadingRemark: import("@milkdown/utils").$Remark<"remarkToggleHeadings", unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* Milkdown's heading schema with the `toggle` attribute, read from and
|
|
5
|
+
* written to the Markdown: for `ctx.update(headingSchema.key, …)`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withToggle<Ctx>(previous: (ctx: Ctx) => NodeSchema): (ctx: Ctx) => NodeSchema;
|
|
8
|
+
export declare const toggleHeadingInputRule: import("@milkdown/utils").$InputRule;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Toggle headings in the rich-text editor (src/lib/markdown-toggle.ts):
|
|
2
|
+
// Milkdown's heading node with a `toggle` attribute, false by default, so
|
|
3
|
+
// every heading the text already has stays plain.
|
|
4
|
+
// - Read: remarkToggleHeadings takes the `{toggle="true"}` marker off a
|
|
5
|
+
// heading's end and marks it `toggle`, which the schema reads.
|
|
6
|
+
// - Written: a toggle heading gets the marker back at its end; a plain one
|
|
7
|
+
// is written as Milkdown writes any heading.
|
|
8
|
+
// - Typed: `>## ` at a line's start makes a toggle `##` (`># ` and `>### `
|
|
9
|
+
// likewise), as Notion's `>` makes a toggle.
|
|
10
|
+
// - Picked: the slash menu's Toggle heading 1 to 3, and the toolbar's
|
|
11
|
+
// Toggle heading (rich-text-formats.ts).
|
|
12
|
+
// Only a toggle heading folds, and only it has a fold button
|
|
13
|
+
// (headingView in milkdown-editor.tsx, rich-text-fold.ts).
|
|
14
|
+
import { headingSchema } from '@milkdown/kit/preset/commonmark';
|
|
15
|
+
import { textblockTypeInputRule } from '@milkdown/kit/prose/inputrules';
|
|
16
|
+
import { $inputRule, $remark } from '@milkdown/kit/utils';
|
|
17
|
+
import { remarkToggleHeadings, TOGGLE_MARKER } from '../../lib/markdown-toggle.js';
|
|
18
|
+
export const toggleHeadingRemark = $remark('remarkToggleHeadings', () => remarkToggleHeadings);
|
|
19
|
+
/**
|
|
20
|
+
* Milkdown's heading schema with the `toggle` attribute, read from and
|
|
21
|
+
* written to the Markdown: for `ctx.update(headingSchema.key, …)`.
|
|
22
|
+
*/
|
|
23
|
+
export function withToggle(previous) {
|
|
24
|
+
return (ctx) => {
|
|
25
|
+
const base = previous(ctx);
|
|
26
|
+
return {
|
|
27
|
+
...base,
|
|
28
|
+
attrs: { ...base.attrs, toggle: { default: false, validate: 'boolean' } },
|
|
29
|
+
parseMarkdown: {
|
|
30
|
+
match: ({ type }) => type === 'heading',
|
|
31
|
+
runner: (state, node, type) => {
|
|
32
|
+
state.openNode(type, { level: node.depth, toggle: node.toggle === true });
|
|
33
|
+
state.next(node.children);
|
|
34
|
+
state.closeNode();
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
toMarkdown: {
|
|
38
|
+
match: (node) => node.type.name === 'heading',
|
|
39
|
+
runner: (state, node) => {
|
|
40
|
+
state.openNode('heading', undefined, { depth: node.attrs.level });
|
|
41
|
+
// A hard break at the end is left out, as Milkdown's own does.
|
|
42
|
+
const last = node.lastChild;
|
|
43
|
+
const content = last?.type.name === 'hardbreak' ? node.content.cut(0, node.content.size - last.nodeSize) : node.content;
|
|
44
|
+
state.next(content);
|
|
45
|
+
if (node.attrs.toggle)
|
|
46
|
+
state.addNode('text', undefined, content.size > 0 ? ` ${TOGGLE_MARKER}` : TOGGLE_MARKER);
|
|
47
|
+
state.closeNode();
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// `>`, then one to three `#`, then a space, at a line's start.
|
|
54
|
+
const TOGGLE_START = /^>(#{1,3})\s$/;
|
|
55
|
+
export const toggleHeadingInputRule = $inputRule((ctx) => textblockTypeInputRule(TOGGLE_START, headingSchema.type(ctx), (match) => ({ level: match[1].length, toggle: true })));
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
/** A top-level heading, by its level (1 for `#`) and
|
|
1
|
+
/** A top-level heading, by its level (1 for `#`), its words and whether it's a toggle heading (the only kind that folds). */
|
|
2
2
|
export type FoldHeadingInput = {
|
|
3
3
|
level: number;
|
|
4
4
|
text: string;
|
|
5
|
+
toggle?: boolean;
|
|
5
6
|
};
|
|
6
7
|
/** A top-level heading as folding sees it. */
|
|
7
8
|
export type FoldHeading = FoldHeadingInput & {
|
|
@@ -9,7 +10,9 @@ export type FoldHeading = FoldHeadingInput & {
|
|
|
9
10
|
id: string;
|
|
10
11
|
/** Its index among the text's top-level blocks. */
|
|
11
12
|
index: number;
|
|
12
|
-
/**
|
|
13
|
+
/** A toggle heading: the only kind with a fold button. */
|
|
14
|
+
toggle: boolean;
|
|
15
|
+
/** A toggle heading with something under it to fold. */
|
|
13
16
|
foldable: boolean;
|
|
14
17
|
/** The ids of the headings it sits under, outermost first. */
|
|
15
18
|
parents: string[];
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// Folding a text by its headings (`foldKey` on <Markdown> and the
|
|
2
|
-
// rich-text editor, and NotesOutline beside them). A heading
|
|
3
|
-
// level of the text folds everything after
|
|
4
|
-
// its level or higher: a `##` folds its
|
|
5
|
-
// stops at the next `##` or `#`.
|
|
6
|
-
//
|
|
2
|
+
// rich-text editor, and NotesOutline beside them). A toggle heading
|
|
3
|
+
// (markdown-toggle.ts) at the top level of the text folds everything after
|
|
4
|
+
// it up to the next heading of its level or higher: a `##` folds its
|
|
5
|
+
// paragraphs and its `###`s, and stops at the next `##` or `#`. A plain
|
|
6
|
+
// heading never folds, though it still ends the sections above it, and
|
|
7
|
+
// NotesOutline still lists it. Headings inside a quote or a callout don't
|
|
8
|
+
// fold.
|
|
7
9
|
//
|
|
8
10
|
// Folding is the reader's, never the text's: what's folded is a set of
|
|
9
11
|
// heading ids, kept on the device (src/hooks/fold-store.ts), and the
|
|
@@ -15,6 +17,7 @@
|
|
|
15
17
|
import remarkGfm from 'remark-gfm';
|
|
16
18
|
import remarkParse from 'remark-parse';
|
|
17
19
|
import { unified } from 'unified';
|
|
20
|
+
import { takeToggleMarker } from './markdown-toggle.js';
|
|
18
21
|
function words(text) {
|
|
19
22
|
return text.replace(/\s+/g, ' ').trim();
|
|
20
23
|
}
|
|
@@ -32,11 +35,13 @@ export function foldPlan(blocks, folded = new Set()) {
|
|
|
32
35
|
if (block)
|
|
33
36
|
while (open.length > 0 && open[open.length - 1].level >= block.level)
|
|
34
37
|
open.pop().end = index;
|
|
35
|
-
|
|
38
|
+
// A plain heading's id in `folded` (folded before it was plain) hides nothing.
|
|
39
|
+
if (open.some((heading) => heading.toggle && folded.has(heading.id)))
|
|
36
40
|
hidden.add(index);
|
|
37
41
|
// Anything after a heading, before one of its level or higher, is its section.
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
const last = open[open.length - 1];
|
|
43
|
+
if (last?.toggle)
|
|
44
|
+
last.foldable = true;
|
|
40
45
|
if (!block)
|
|
41
46
|
return;
|
|
42
47
|
const text = words(block.text);
|
|
@@ -48,6 +53,7 @@ export function foldPlan(blocks, folded = new Set()) {
|
|
|
48
53
|
text,
|
|
49
54
|
id: count === 1 ? base : `${base}#${count}`,
|
|
50
55
|
index,
|
|
56
|
+
toggle: block.toggle === true,
|
|
51
57
|
foldable: false,
|
|
52
58
|
parents: open.map((one) => one.id),
|
|
53
59
|
end: blocks.length,
|
|
@@ -67,7 +73,13 @@ export function mdText(node) {
|
|
|
67
73
|
}
|
|
68
74
|
/** The top-level blocks of an mdast root, as foldPlan takes them. */
|
|
69
75
|
export function mdBlocks(root) {
|
|
70
|
-
return (root.children ?? []).map((node) =>
|
|
76
|
+
return (root.children ?? []).map((node) => {
|
|
77
|
+
if (node.type !== 'heading' || !node.depth)
|
|
78
|
+
return null;
|
|
79
|
+
// Read here without remarkToggleHeadings: the marker is still in its words.
|
|
80
|
+
const { text, toggle } = takeToggleMarker(mdText(node));
|
|
81
|
+
return { level: node.depth, text, toggle };
|
|
82
|
+
});
|
|
71
83
|
}
|
|
72
84
|
const parser = unified().use(remarkParse).use(remarkGfm);
|
|
73
85
|
/** The fold plan for a Markdown text, parsed as <Markdown> and the editor parse it. */
|
|
@@ -135,7 +147,8 @@ export function rehypeFold(options) {
|
|
|
135
147
|
const blocks = at.map((index) => {
|
|
136
148
|
const node = children[index];
|
|
137
149
|
const level = node.type === 'element' ? HEADING.exec(node.tagName ?? '')?.[1] : undefined;
|
|
138
|
-
|
|
150
|
+
// remarkToggleHeadings marked it, its marker gone.
|
|
151
|
+
return level ? { level: Number(level), text: hastText(node), toggle: node.properties?.dataToggle === 'true' } : null;
|
|
139
152
|
});
|
|
140
153
|
const plan = foldPlan(blocks, options.folded);
|
|
141
154
|
for (const heading of plan.headings) {
|
|
@@ -144,6 +157,7 @@ export function rehypeFold(options) {
|
|
|
144
157
|
...node.properties,
|
|
145
158
|
dataFoldId: heading.id,
|
|
146
159
|
dataFoldLabel: heading.text,
|
|
160
|
+
dataToggle: String(heading.toggle),
|
|
147
161
|
dataFoldable: String(heading.foldable),
|
|
148
162
|
dataFolded: String(heading.foldable && options.folded.has(heading.id)),
|
|
149
163
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** The marker at a toggle heading's end, as it's written. */
|
|
2
|
+
export declare const TOGGLE_MARKER = "{toggle=\"true\"}";
|
|
3
|
+
/** `text` without a toggle marker at its end, and whether it had one. */
|
|
4
|
+
export declare function takeToggleMarker(text: string): {
|
|
5
|
+
text: string;
|
|
6
|
+
toggle: boolean;
|
|
7
|
+
};
|
|
8
|
+
type MdNode = {
|
|
9
|
+
type: string;
|
|
10
|
+
depth?: number;
|
|
11
|
+
value?: string;
|
|
12
|
+
children?: MdNode[];
|
|
13
|
+
data?: Record<string, unknown>;
|
|
14
|
+
toggle?: boolean;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A heading that's a toggle: true, with the marker taken out of its last
|
|
19
|
+
* text (and that text too, when the marker was all of it). False for any
|
|
20
|
+
* other node, untouched.
|
|
21
|
+
*/
|
|
22
|
+
export declare function takeToggle(node: MdNode): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Marks each toggle heading, its marker taken out: `toggle: true` on the
|
|
25
|
+
* mdast node (the editor's schema reads it) and a `data-toggle` property
|
|
26
|
+
* on its element (rehypeFold reads it).
|
|
27
|
+
*/
|
|
28
|
+
export declare function remarkToggleHeadings(): (tree: unknown) => void;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Toggle headings (Notion's): the only headings that fold. A plain
|
|
2
|
+
// heading never folds and never shows a fold button; a toggle heading
|
|
3
|
+
// folds its section, as every heading did before 4.66.0.
|
|
4
|
+
//
|
|
5
|
+
// In Markdown a toggle heading is a heading whose line ends in Notion's
|
|
6
|
+
// own attribute, so a chat that writes Notion's Markdown writes it, and a
|
|
7
|
+
// plain heading stays plain Markdown:
|
|
8
|
+
//
|
|
9
|
+
// ## Safety {toggle="true"}
|
|
10
|
+
//
|
|
11
|
+
// Anything else reads it as a heading with those words at its end. These
|
|
12
|
+
// plugins take the marker off and mark the heading instead:
|
|
13
|
+
// `remarkToggleHeadings` for <Markdown> (a `data-toggle` property
|
|
14
|
+
// rehypeFold reads) and for the rich-text editor (its heading node's
|
|
15
|
+
// `toggle` attribute, written back with the marker by its schema). The
|
|
16
|
+
// marker never counts in a heading's words, so its fold id
|
|
17
|
+
// (markdown-fold.ts) is the same plain or toggle.
|
|
18
|
+
/** The marker at a toggle heading's end, as it's written. */
|
|
19
|
+
export const TOGGLE_MARKER = '{toggle="true"}';
|
|
20
|
+
// The marker at the end of a heading's text, the space before it and any
|
|
21
|
+
// after it. Single quotes, no quotes and spaces inside the braces are read
|
|
22
|
+
// too; it's written back as TOGGLE_MARKER.
|
|
23
|
+
const AT_END = /[ \t]*\{\s*toggle\s*=\s*(?:"true"|'true'|true)\s*\}[ \t]*$/;
|
|
24
|
+
/** `text` without a toggle marker at its end, and whether it had one. */
|
|
25
|
+
export function takeToggleMarker(text) {
|
|
26
|
+
const found = AT_END.exec(text);
|
|
27
|
+
return found ? { text: text.slice(0, found.index), toggle: true } : { text, toggle: false };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A heading that's a toggle: true, with the marker taken out of its last
|
|
31
|
+
* text (and that text too, when the marker was all of it). False for any
|
|
32
|
+
* other node, untouched.
|
|
33
|
+
*/
|
|
34
|
+
export function takeToggle(node) {
|
|
35
|
+
if (node.type !== 'heading')
|
|
36
|
+
return false;
|
|
37
|
+
const last = node.children?.[node.children.length - 1];
|
|
38
|
+
if (last?.type !== 'text' || typeof last.value !== 'string')
|
|
39
|
+
return false;
|
|
40
|
+
const taken = takeToggleMarker(last.value);
|
|
41
|
+
if (!taken.toggle)
|
|
42
|
+
return false;
|
|
43
|
+
if (taken.text)
|
|
44
|
+
last.value = taken.text;
|
|
45
|
+
else
|
|
46
|
+
node.children.pop();
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
function visit(node, each) {
|
|
50
|
+
for (const child of node.children ?? [])
|
|
51
|
+
visit(child, each);
|
|
52
|
+
each(node);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Marks each toggle heading, its marker taken out: `toggle: true` on the
|
|
56
|
+
* mdast node (the editor's schema reads it) and a `data-toggle` property
|
|
57
|
+
* on its element (rehypeFold reads it).
|
|
58
|
+
*/
|
|
59
|
+
export function remarkToggleHeadings() {
|
|
60
|
+
return (tree) => {
|
|
61
|
+
visit(tree, (node) => {
|
|
62
|
+
if (!takeToggle(node))
|
|
63
|
+
return;
|
|
64
|
+
node.toggle = true;
|
|
65
|
+
const hProperties = node.data?.hProperties ?? {};
|
|
66
|
+
node.data = { ...node.data, hProperties: { ...hProperties, dataToggle: 'true' } };
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tristan2828/ui-foundation",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.67.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": {
|