@molecule/app-margin-notes-react 1.0.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/LICENSE +115 -0
- package/README.md +283 -0
- package/dist/MarginNotes.d.ts +18 -0
- package/dist/MarginNotes.d.ts.map +1 -0
- package/dist/MarginNotes.js +155 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +72 -0
- package/dist/rows.d.ts +26 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/rows.js +49 -0
- package/dist/types.d.ts +74 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-09-24T06:29:04.932Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/app-margin-notes-react
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
Margin notes: long-form text with notes beside each paragraph — annotated
|
|
16
|
+
essays, commentary, and posts that show which paragraphs an AI wrote and the
|
|
17
|
+
prompt behind each.
|
|
18
|
+
|
|
19
|
+
Desktop: a large-type prose column and a narrower notes column to its right;
|
|
20
|
+
each note lines up with the paragraph (or run of paragraphs) it belongs to
|
|
21
|
+
and appears once. Hovering or focusing a paragraph emphasises its notes;
|
|
22
|
+
hovering or focusing a note tints the paragraphs it covers. A paragraph can
|
|
23
|
+
carry a small mark in its left margin. Note kinds (say, "Summaries" and
|
|
24
|
+
"Prompts") each get a real switch. Phone: the notes column gives way to a bar
|
|
25
|
+
fixed to the bottom that shows the notes for the section being read and
|
|
26
|
+
follows the reader; tapping a paragraph pins its notes, tapping again lets
|
|
27
|
+
go; the switches live in the bar. A document with no notes renders centred
|
|
28
|
+
prose at full measure with no notes column.
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { MarginNotes } from '@molecule/app-margin-notes-react'
|
|
34
|
+
|
|
35
|
+
// blocks: your rendered paragraphs, in order; notes: what goes beside them.
|
|
36
|
+
;<MarginNotes
|
|
37
|
+
blocks={[
|
|
38
|
+
{ id: 'p1', content: <p>Every post carries a map of who wrote what.</p>, noteIds: ['s1'] },
|
|
39
|
+
{
|
|
40
|
+
id: 'p2',
|
|
41
|
+
content: <p>The map is built from the session export.</p>,
|
|
42
|
+
noteIds: ['s1', 'q1'],
|
|
43
|
+
marked: true,
|
|
44
|
+
},
|
|
45
|
+
]}
|
|
46
|
+
notes={[
|
|
47
|
+
{ id: 's1', kind: 'summary', content: <p>How the map is made.</p> },
|
|
48
|
+
{ id: 'q1', kind: 'prompt', content: <p>“explain the map” — claude-opus-4</p> },
|
|
49
|
+
]}
|
|
50
|
+
kinds={[
|
|
51
|
+
{
|
|
52
|
+
id: 'summary',
|
|
53
|
+
label: t('post.summaries', undefined, { defaultValue: 'Summaries' }),
|
|
54
|
+
defaultOn: true,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: 'prompt',
|
|
58
|
+
label: t('post.prompts', undefined, { defaultValue: 'Prompts' }),
|
|
59
|
+
defaultOn: false,
|
|
60
|
+
panel: 'tap',
|
|
61
|
+
},
|
|
62
|
+
]}
|
|
63
|
+
markLabel={t('post.aiWritten', undefined, { defaultValue: 'Written with AI' })}
|
|
64
|
+
/>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Type
|
|
68
|
+
|
|
69
|
+
`feature`
|
|
70
|
+
|
|
71
|
+
## Installation
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
npm install @molecule/app-margin-notes-react @molecule/app-react @molecule/app-ui @molecule/app-ui-react react
|
|
75
|
+
npm install -D @types/react
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## API
|
|
79
|
+
|
|
80
|
+
### Interfaces
|
|
81
|
+
|
|
82
|
+
#### `MarginNote`
|
|
83
|
+
|
|
84
|
+
A note shown beside the block(s) it belongs to.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
interface MarginNote {
|
|
88
|
+
/** Stable id, referenced from {@link MarginNotesBlock.noteIds}. */
|
|
89
|
+
id: string
|
|
90
|
+
/** The kind this note belongs to ({@link MarginNoteKind.id}), for its switch. */
|
|
91
|
+
kind: string
|
|
92
|
+
/** The rendered note. */
|
|
93
|
+
content: ReactNode
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### `MarginNoteKind`
|
|
98
|
+
|
|
99
|
+
A kind of note the reader can show or hide with a switch.
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
interface MarginNoteKind {
|
|
103
|
+
/** Stable id, referenced from {@link MarginNote.kind}. */
|
|
104
|
+
id: string
|
|
105
|
+
/** The switch's visible label, already translated (e.g. "Summaries"). */
|
|
106
|
+
label: string
|
|
107
|
+
/** Whether this kind shows before the reader touches its switch. Default `true`. */
|
|
108
|
+
defaultOn?: boolean
|
|
109
|
+
/**
|
|
110
|
+
* How this kind reaches the phone panel. `'follow'` (default): the panel
|
|
111
|
+
* shows it for the section being read, following the reader. `'tap'`: only
|
|
112
|
+
* when the reader taps a block it belongs to (and gone on the second tap) —
|
|
113
|
+
* for notes about one paragraph rather than the section.
|
|
114
|
+
*/
|
|
115
|
+
panel?: 'follow' | 'tap'
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### `MarginNotesBlock`
|
|
120
|
+
|
|
121
|
+
One block of the text — usually a paragraph or heading from your markdown
|
|
122
|
+
renderer. Blocks render in order, in the prose column.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
interface MarginNotesBlock {
|
|
126
|
+
/** Stable id, unique within the document (used as the DOM anchor). */
|
|
127
|
+
id: string
|
|
128
|
+
/** The rendered block (e.g. `<p>…</p>`). */
|
|
129
|
+
content: ReactNode
|
|
130
|
+
/** Ids of the notes that belong to this block (see {@link MarginNote}). */
|
|
131
|
+
noteIds?: string[]
|
|
132
|
+
/**
|
|
133
|
+
* Draw a small mark in this block's left margin — the one permanent sign on
|
|
134
|
+
* the prose itself (e.g. "written with AI"). Unmarked blocks carry none.
|
|
135
|
+
*/
|
|
136
|
+
marked?: boolean
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
#### `MarginNotesProps`
|
|
141
|
+
|
|
142
|
+
`<MarginNotes>` props.
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
interface MarginNotesProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
|
|
146
|
+
/** The text, in reading order. */
|
|
147
|
+
blocks: MarginNotesBlock[]
|
|
148
|
+
/** Every note the blocks refer to. A document with none renders centred prose with no gutter. */
|
|
149
|
+
notes?: MarginNote[]
|
|
150
|
+
/** The note kinds, in the order their switches appear. Kinds absent here are always shown. */
|
|
151
|
+
kinds?: MarginNoteKind[]
|
|
152
|
+
/** Accessible name of the margin mark, already translated (e.g. "Written with AI"). */
|
|
153
|
+
markLabel?: string
|
|
154
|
+
/** Width of the prose column (a CSS length). Default `38rem`. */
|
|
155
|
+
measure?: string
|
|
156
|
+
/** Width of the notes column (a CSS length). Default `18rem`. */
|
|
157
|
+
gutterWidth?: string
|
|
158
|
+
/** Controlled: the ids of the kinds currently shown. Omit to let the component own it. */
|
|
159
|
+
shownKinds?: string[]
|
|
160
|
+
/** Called with the new list of shown kind ids whenever the reader flips a switch. */
|
|
161
|
+
onShownKindsChange?: (shown: string[]) => void
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### `MarginNotesRow`
|
|
166
|
+
|
|
167
|
+
One row of the layout: a run of blocks and the notes shown beside it.
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
interface MarginNotesRow {
|
|
171
|
+
/** The row's key: the id of its first block. */
|
|
172
|
+
id: string
|
|
173
|
+
/** The blocks in this row, in order. */
|
|
174
|
+
blocks: MarginNotesBlock[]
|
|
175
|
+
/** Notes shown beside this row — each note appears in exactly one row, its first. */
|
|
176
|
+
notes: MarginNote[]
|
|
177
|
+
/** Every note id any block in this row refers to (for the phone panel). */
|
|
178
|
+
noteIds: string[]
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Functions
|
|
183
|
+
|
|
184
|
+
#### `buildRows(blocks, notes)`
|
|
185
|
+
|
|
186
|
+
Group blocks into layout rows and place each note once.
|
|
187
|
+
|
|
188
|
+
A row is a run of consecutive blocks that refer to exactly the same set of
|
|
189
|
+
notes (a paragraph run sharing one note), or a run of consecutive blocks
|
|
190
|
+
with no notes. Each note is shown beside the FIRST row that refers to it and
|
|
191
|
+
never again, so a note covering several paragraphs — even ones that are not
|
|
192
|
+
next to each other — appears once on the page.
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
function buildRows(blocks: MarginNotesBlock[], notes?: MarginNote[]): MarginNotesRow[]
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
- `blocks` — The text, in reading order.
|
|
199
|
+
- `notes` — Every note the blocks refer to; ids with no note are ignored.
|
|
200
|
+
|
|
201
|
+
**Returns:** The rows, in reading order.
|
|
202
|
+
|
|
203
|
+
#### `defaultShownKinds(kinds)`
|
|
204
|
+
|
|
205
|
+
The kinds shown before the reader touches a switch.
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
function defaultShownKinds(kinds?: { id: string; defaultOn?: boolean }[]): string[]
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
- `kinds` — The switchable kinds.
|
|
212
|
+
|
|
213
|
+
**Returns:** The ids of the kinds that default on.
|
|
214
|
+
|
|
215
|
+
#### `MarginNotes(props)`
|
|
216
|
+
|
|
217
|
+
Long-form text with notes beside each paragraph.
|
|
218
|
+
|
|
219
|
+
Desktop: a prose column and, to its right, a narrower notes column; each
|
|
220
|
+
note lines up with the block(s) it belongs to and appears once. Phone: the
|
|
221
|
+
notes column disappears and a bar fixed to the bottom shows the notes for
|
|
222
|
+
the section being read (tap a block to pin its notes, tap again to let go),
|
|
223
|
+
with the kind switches in the bar. Every default — which kinds show, which
|
|
224
|
+
surface a width gets — is decided in the first render and by CSS, so
|
|
225
|
+
server-rendered or prerendered HTML is right before any JavaScript runs.
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
function MarginNotes(props: MarginNotesProps): JSX.Element
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
- `props` — See {@link MarginNotesProps}.
|
|
232
|
+
|
|
233
|
+
**Returns:** The layout.
|
|
234
|
+
|
|
235
|
+
## Injection Notes
|
|
236
|
+
|
|
237
|
+
### Requirements
|
|
238
|
+
|
|
239
|
+
Peer dependencies:
|
|
240
|
+
|
|
241
|
+
- `@molecule/app-react` ^1.0.1
|
|
242
|
+
- `@molecule/app-ui` ^1.1.1
|
|
243
|
+
- `@molecule/app-ui-react` ^1.0.1
|
|
244
|
+
- `react` ^18.0.0 || ^19.0.0
|
|
245
|
+
|
|
246
|
+
### Runtime Dependencies
|
|
247
|
+
|
|
248
|
+
- `@molecule/app-react`
|
|
249
|
+
- `@molecule/app-ui`
|
|
250
|
+
- `@molecule/app-ui-react`
|
|
251
|
+
- `react`
|
|
252
|
+
|
|
253
|
+
- **Defaults are right before JavaScript runs.** Which kinds show comes from
|
|
254
|
+
`kinds[].defaultOn` in the FIRST render (a hidden note carries the HTML
|
|
255
|
+
`hidden` attribute), and the desktop/phone split is CSS
|
|
256
|
+
(`cm.hiddenBelow('md')` / `cm.hiddenFrom('md')`). Prerender the page with
|
|
257
|
+
`renderToString` and a kind that defaults off is already absent; do NOT
|
|
258
|
+
toggle defaults in a `useEffect` of your own.
|
|
259
|
+
- **One note, one place.** A note shows beside the FIRST row that refers to
|
|
260
|
+
it. Give consecutive paragraphs that share a note the same `noteIds` and
|
|
261
|
+
they form one row with the note beside them once. A paragraph whose
|
|
262
|
+
`noteIds` differ from its neighbour's starts a new row.
|
|
263
|
+
- **The mark is drawn on the block**, as an absolutely-placed dot inside the
|
|
264
|
+
block's own wrapper (hidden on phones, where there is no margin). At rest a
|
|
265
|
+
marked block is typeset exactly like an unmarked one.
|
|
266
|
+
- **Pass rendered blocks, not markdown.** Split your renderer's output into
|
|
267
|
+
top-level blocks (one per paragraph/heading/list) and give each a stable
|
|
268
|
+
`id`; the component adds no markup inside them.
|
|
269
|
+
- **Section notes follow the reader; paragraph notes wait for a tap.** On a
|
|
270
|
+
phone the panel shows the kinds with `panel: 'follow'` (the default) for
|
|
271
|
+
the section in view. Give a kind that is about ONE paragraph (a prompt, a
|
|
272
|
+
citation) `panel: 'tap'`: it shows when that paragraph is tapped and goes
|
|
273
|
+
on the second tap.
|
|
274
|
+
- **Controlled or not.** Pass `shownKinds` + `onShownKindsChange` to own the
|
|
275
|
+
switches' state (e.g. to remember it); otherwise the component does.
|
|
276
|
+
- The phone bar reserves space at the end of the text so it never covers the
|
|
277
|
+
last paragraph. UI strings come from the companion locale bond
|
|
278
|
+
`@molecule/app-locales-margin-notes`; kind labels and `markLabel` are yours
|
|
279
|
+
to translate.
|
|
280
|
+
|
|
281
|
+
## Translations
|
|
282
|
+
|
|
283
|
+
Translation strings are provided by `@molecule/app-locales-margin-notes`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { JSX } from 'react';
|
|
2
|
+
import type { MarginNotesProps } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Long-form text with notes beside each paragraph.
|
|
5
|
+
*
|
|
6
|
+
* Desktop: a prose column and, to its right, a narrower notes column; each
|
|
7
|
+
* note lines up with the block(s) it belongs to and appears once. Phone: the
|
|
8
|
+
* notes column disappears and a bar fixed to the bottom shows the notes for
|
|
9
|
+
* the section being read (tap a block to pin its notes, tap again to let go),
|
|
10
|
+
* with the kind switches in the bar. Every default — which kinds show, which
|
|
11
|
+
* surface a width gets — is decided in the first render and by CSS, so
|
|
12
|
+
* server-rendered or prerendered HTML is right before any JavaScript runs.
|
|
13
|
+
*
|
|
14
|
+
* @param props - See {@link MarginNotesProps}.
|
|
15
|
+
* @returns The layout.
|
|
16
|
+
*/
|
|
17
|
+
export declare function MarginNotes(props: MarginNotesProps): JSX.Element;
|
|
18
|
+
//# sourceMappingURL=MarginNotes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MarginNotes.d.ts","sourceRoot":"","sources":["../src/MarginNotes.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,GAAG,EAAE,MAAM,OAAO,CAAA;AAQ/C,OAAO,KAAK,EAAc,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAQ9D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,GAAG,CAAC,OAAO,CAsShE"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { useTranslation } from '@molecule/app-react';
|
|
4
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
5
|
+
import { Switch } from '@molecule/app-ui-react';
|
|
6
|
+
import { buildRows, defaultShownKinds } from './rows.js';
|
|
7
|
+
/** Layout effect in the browser, plain effect on the server (where it never runs). */
|
|
8
|
+
const useBrowserLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
9
|
+
/** Where a phone reader's "current section" is read: this fraction down the viewport. */
|
|
10
|
+
const READING_LINE = 0.35;
|
|
11
|
+
/**
|
|
12
|
+
* Long-form text with notes beside each paragraph.
|
|
13
|
+
*
|
|
14
|
+
* Desktop: a prose column and, to its right, a narrower notes column; each
|
|
15
|
+
* note lines up with the block(s) it belongs to and appears once. Phone: the
|
|
16
|
+
* notes column disappears and a bar fixed to the bottom shows the notes for
|
|
17
|
+
* the section being read (tap a block to pin its notes, tap again to let go),
|
|
18
|
+
* with the kind switches in the bar. Every default — which kinds show, which
|
|
19
|
+
* surface a width gets — is decided in the first render and by CSS, so
|
|
20
|
+
* server-rendered or prerendered HTML is right before any JavaScript runs.
|
|
21
|
+
*
|
|
22
|
+
* @param props - See {@link MarginNotesProps}.
|
|
23
|
+
* @returns The layout.
|
|
24
|
+
*/
|
|
25
|
+
export function MarginNotes(props) {
|
|
26
|
+
const { blocks, notes = [], kinds = [], markLabel, measure = '38rem', gutterWidth = '18rem', shownKinds: shownKindsProp, onShownKindsChange, className, style, ...rest } = props;
|
|
27
|
+
const cm = getClassMap();
|
|
28
|
+
const { t } = useTranslation();
|
|
29
|
+
const rows = useMemo(() => buildRows(blocks, notes), [blocks, notes]);
|
|
30
|
+
const notesById = useMemo(() => new Map(notes.map((n) => [n.id, n])), [notes]);
|
|
31
|
+
const hasNotes = rows.some((r) => r.notes.length > 0);
|
|
32
|
+
// Which kinds show. From props in the FIRST render (never an effect), so the
|
|
33
|
+
// prerendered page already has the reader's defaults.
|
|
34
|
+
const [ownShown, setOwnShown] = useState(() => defaultShownKinds(kinds));
|
|
35
|
+
const shown = shownKindsProp ?? ownShown;
|
|
36
|
+
const switchable = useMemo(() => new Set(kinds.map((k) => k.id)), [kinds]);
|
|
37
|
+
const isShown = useCallback((note) => !switchable.has(note.kind) || shown.includes(note.kind), [shown, switchable]);
|
|
38
|
+
const setShown = (kind, on) => {
|
|
39
|
+
const next = on ? [...new Set([...shown, kind])] : shown.filter((k) => k !== kind);
|
|
40
|
+
if (shownKindsProp === undefined)
|
|
41
|
+
setOwnShown(next);
|
|
42
|
+
onShownKindsChange?.(next);
|
|
43
|
+
};
|
|
44
|
+
// The two-way relationship: a hovered/focused block emphasises its notes; a
|
|
45
|
+
// hovered/focused note tints the blocks it covers.
|
|
46
|
+
const [activeBlock, setActiveBlock] = useState(null);
|
|
47
|
+
const [activeNote, setActiveNote] = useState(null);
|
|
48
|
+
const activeBlockNotes = useMemo(() => {
|
|
49
|
+
const b = activeBlock ? blocks.find((x) => x.id === activeBlock) : undefined;
|
|
50
|
+
return new Set(b?.noteIds ?? []);
|
|
51
|
+
}, [activeBlock, blocks]);
|
|
52
|
+
// Phone: the section being read, and a block the reader tapped.
|
|
53
|
+
const firstWithNotes = rows.find((r) => r.noteIds.length > 0)?.id ?? null;
|
|
54
|
+
const [readingRow, setReadingRow] = useState(firstWithNotes);
|
|
55
|
+
const [pinnedBlock, setPinnedBlock] = useState(null);
|
|
56
|
+
const rowRefs = useRef(new Map());
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (!hasNotes || typeof window === 'undefined')
|
|
59
|
+
return;
|
|
60
|
+
let frame = 0;
|
|
61
|
+
const read = () => {
|
|
62
|
+
frame = 0;
|
|
63
|
+
const line = window.innerHeight * READING_LINE;
|
|
64
|
+
let current = null;
|
|
65
|
+
for (const row of rows) {
|
|
66
|
+
const el = rowRefs.current.get(row.id);
|
|
67
|
+
if (!el)
|
|
68
|
+
continue;
|
|
69
|
+
if (el.getBoundingClientRect().top > line)
|
|
70
|
+
break;
|
|
71
|
+
if (row.noteIds.length > 0)
|
|
72
|
+
current = row.id;
|
|
73
|
+
}
|
|
74
|
+
setReadingRow(current ?? firstWithNotes);
|
|
75
|
+
};
|
|
76
|
+
const onScroll = () => {
|
|
77
|
+
if (!frame)
|
|
78
|
+
frame = window.requestAnimationFrame(read);
|
|
79
|
+
};
|
|
80
|
+
read();
|
|
81
|
+
window.addEventListener('scroll', onScroll, { passive: true });
|
|
82
|
+
window.addEventListener('resize', onScroll);
|
|
83
|
+
return () => {
|
|
84
|
+
window.removeEventListener('scroll', onScroll);
|
|
85
|
+
window.removeEventListener('resize', onScroll);
|
|
86
|
+
if (frame)
|
|
87
|
+
window.cancelAnimationFrame(frame);
|
|
88
|
+
};
|
|
89
|
+
}, [hasNotes, rows, firstWithNotes]);
|
|
90
|
+
// A row that opens on a heading (or any block with a top margin) starts its
|
|
91
|
+
// text below the row's top; push the notes down by the same distance so a
|
|
92
|
+
// note lines up with the first line it covers, not the margin above it.
|
|
93
|
+
const [noteOffsets, setNoteOffsets] = useState({});
|
|
94
|
+
useBrowserLayoutEffect(() => {
|
|
95
|
+
if (!hasNotes)
|
|
96
|
+
return;
|
|
97
|
+
const measureOffsets = () => {
|
|
98
|
+
const next = {};
|
|
99
|
+
for (const row of rows) {
|
|
100
|
+
if (row.notes.length === 0)
|
|
101
|
+
continue;
|
|
102
|
+
const rowEl = rowRefs.current.get(row.id);
|
|
103
|
+
const blockEl = document.getElementById(row.blocks[0].id);
|
|
104
|
+
const first = blockEl?.firstElementChild ?? blockEl;
|
|
105
|
+
if (!rowEl || !first)
|
|
106
|
+
continue;
|
|
107
|
+
const top = first.getBoundingClientRect().top - rowEl.getBoundingClientRect().top;
|
|
108
|
+
const offset = Math.max(0, Math.round(top));
|
|
109
|
+
if (offset)
|
|
110
|
+
next[row.id] = offset;
|
|
111
|
+
}
|
|
112
|
+
setNoteOffsets((prev) => (JSON.stringify(prev) === JSON.stringify(next) ? prev : next));
|
|
113
|
+
};
|
|
114
|
+
measureOffsets();
|
|
115
|
+
window.addEventListener('resize', measureOffsets);
|
|
116
|
+
return () => window.removeEventListener('resize', measureOffsets);
|
|
117
|
+
}, [hasNotes, rows]);
|
|
118
|
+
// Kinds that reach the panel only on a tap (notes about one paragraph).
|
|
119
|
+
const tapOnly = useMemo(() => new Set(kinds.filter((k) => k.panel === 'tap').map((k) => k.id)), [kinds]);
|
|
120
|
+
const panelNoteIds = useMemo(() => {
|
|
121
|
+
if (pinnedBlock)
|
|
122
|
+
return blocks.find((b) => b.id === pinnedBlock)?.noteIds ?? [];
|
|
123
|
+
const ids = rows.find((r) => r.id === readingRow)?.noteIds ?? [];
|
|
124
|
+
return ids.filter((id) => !tapOnly.has(notesById.get(id)?.kind ?? ''));
|
|
125
|
+
}, [pinnedBlock, readingRow, blocks, rows, tapOnly, notesById]);
|
|
126
|
+
const panelNotes = panelNoteIds
|
|
127
|
+
.map((id) => notesById.get(id))
|
|
128
|
+
.filter((n) => !!n && isShown(n));
|
|
129
|
+
const switches = (placement) => kinds.length === 0 ? null : (_jsx("div", { role: "group", "aria-label": t('marginNotes.aria.toggles', undefined, {
|
|
130
|
+
defaultValue: 'Show or hide notes',
|
|
131
|
+
}), className: cm.flex({ direction: 'row', wrap: 'wrap', gap: 'md', align: 'center' }), "data-mol-id": `margin-notes-switches-${placement}`, children: kinds.map((kind) => (_jsx(Switch, { label: kind.label, checked: shown.includes(kind.id), onChange: (event) => setShown(kind.id, event.target.checked), className: placement === 'bar' ? cm.touchTarget : cm.touchTargetCompact, "data-mol-id": `margin-notes-switch-${placement}-${kind.id}` }, kind.id))) }));
|
|
132
|
+
const noteView = (note, where) => {
|
|
133
|
+
const emphasised = activeBlockNotes.has(note.id) || activeNote === note.id;
|
|
134
|
+
return (_jsx("aside", { hidden: !isShown(note), tabIndex: where === 'gutter' ? 0 : -1, "data-note-kind": note.kind, "data-note-emphasis": emphasised ? 'true' : undefined, "data-mol-id": `margin-note-${where}-${note.id}`, onMouseEnter: where === 'gutter' ? () => setActiveNote(note.id) : undefined, onMouseLeave: where === 'gutter' ? () => setActiveNote(null) : undefined, onFocus: where === 'gutter' ? () => setActiveNote(note.id) : undefined, onBlur: where === 'gutter' ? () => setActiveNote(null) : undefined, className: cm.cn(cm.sp('p', 3), cm.textSize('sm'), cm.textMuted, emphasised && cm.bgPrimarySubtle, emphasised && cm.shadowLifted), children: note.content }, note.id));
|
|
135
|
+
};
|
|
136
|
+
const containerStyle = {
|
|
137
|
+
...style,
|
|
138
|
+
maxWidth: hasNotes ? `calc(${measure} + ${gutterWidth} + 4rem)` : measure,
|
|
139
|
+
};
|
|
140
|
+
return (_jsxs("div", { ...rest, className: cm.cn(cm.mxAuto, cm.w('full'), className), style: containerStyle, "data-mol-id": "margin-notes", children: [hasNotes && kinds.length > 0 && (_jsx("div", { className: cm.cn(cm.hiddenBelow('md'), cm.sp('mb', 6)), children: switches('side') })), _jsx("div", { className: cm.cn(cm.textSize('xl'), cm.stack(0)), children: rows.map((row) => (_jsxs("div", { ref: (el) => {
|
|
141
|
+
if (el)
|
|
142
|
+
rowRefs.current.set(row.id, el);
|
|
143
|
+
else
|
|
144
|
+
rowRefs.current.delete(row.id);
|
|
145
|
+
}, className: cm.flex({ direction: 'row', align: 'start', gap: 'xl' }), "data-mol-id": `margin-notes-row-${row.id}`, children: [_jsx("div", { className: cm.flex1, style: { maxWidth: measure, minWidth: 0 }, children: row.blocks.map((block) => {
|
|
146
|
+
const ids = block.noteIds ?? [];
|
|
147
|
+
const tinted = !!activeNote && ids.includes(activeNote);
|
|
148
|
+
const interactive = ids.length > 0;
|
|
149
|
+
return (_jsxs("div", { id: block.id, tabIndex: interactive ? 0 : undefined, "data-mol-id": `margin-notes-block-${block.id}`, "data-block-marked": block.marked ? 'true' : undefined, "data-block-pinned": pinnedBlock === block.id ? 'true' : undefined, onMouseEnter: interactive ? () => setActiveBlock(block.id) : undefined, onMouseLeave: interactive ? () => setActiveBlock(null) : undefined, onFocus: interactive ? () => setActiveBlock(block.id) : undefined, onBlur: interactive ? () => setActiveBlock(null) : undefined, onClick: interactive
|
|
150
|
+
? () => setPinnedBlock((p) => (p === block.id ? null : block.id))
|
|
151
|
+
: undefined, className: cm.cn(cm.position('relative'), tinted && cm.bgPrimarySubtle), children: [block.marked && (_jsx("span", { role: "img", "aria-label": markLabel, "data-mol-id": `margin-notes-mark-${block.id}`, className: cm.cn(cm.position('absolute'), cm.roundedFull, cm.bgPrimary, cm.hiddenBelow('md')), style: { left: '-0.9rem', top: '0.75em', width: '6px', height: '6px' } })), block.content] }, block.id));
|
|
152
|
+
}) }), hasNotes && (_jsx("div", { className: cm.cn(cm.hiddenBelow('md'), cm.stack(3)), style: { width: gutterWidth, flexShrink: 0, paddingTop: noteOffsets[row.id] }, "aria-label": t('marginNotes.aria.notes', undefined, { defaultValue: 'Notes' }), role: "complementary", children: row.notes.map((note) => noteView(note, 'gutter')) }))] }, row.id))) }), hasNotes && (_jsxs(_Fragment, { children: [_jsx("div", { className: cm.hiddenFrom('md'), style: { height: '14rem' }, "aria-hidden": "true" }), _jsxs("div", { className: cm.cn(cm.hiddenFrom('md'), cm.position('fixed'), cm.surface, cm.borderT, cm.shadowLifted, cm.sp('p', 3), cm.stack(2)), style: { left: 0, right: 0, bottom: 0, zIndex: 20 }, "data-mol-id": "margin-notes-bar", children: [_jsxs("div", { role: "region", "aria-live": "polite", "aria-label": t('marginNotes.aria.panel', undefined, {
|
|
153
|
+
defaultValue: 'Notes for this section',
|
|
154
|
+
}), style: { maxHeight: '40vh', overflowY: 'auto' }, "data-mol-id": "margin-notes-panel", children: [panelNotes.map((note) => noteView(note, 'panel')), pinnedBlock && (_jsx("button", { type: "button", onClick: () => setPinnedBlock(null), className: cm.cn(cm.button({ variant: 'ghost', size: 'sm' }), cm.touchTarget), "data-mol-id": "margin-notes-dismiss", children: t('marginNotes.dismiss', undefined, { defaultValue: 'Hide notes' }) }))] }), switches('bar')] })] }))] }));
|
|
155
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Margin notes: long-form text with notes beside each paragraph — annotated
|
|
3
|
+
* essays, commentary, and posts that show which paragraphs an AI wrote and the
|
|
4
|
+
* prompt behind each.
|
|
5
|
+
*
|
|
6
|
+
* Desktop: a large-type prose column and a narrower notes column to its right;
|
|
7
|
+
* each note lines up with the paragraph (or run of paragraphs) it belongs to
|
|
8
|
+
* and appears once. Hovering or focusing a paragraph emphasises its notes;
|
|
9
|
+
* hovering or focusing a note tints the paragraphs it covers. A paragraph can
|
|
10
|
+
* carry a small mark in its left margin. Note kinds (say, "Summaries" and
|
|
11
|
+
* "Prompts") each get a real switch. Phone: the notes column gives way to a bar
|
|
12
|
+
* fixed to the bottom that shows the notes for the section being read and
|
|
13
|
+
* follows the reader; tapping a paragraph pins its notes, tapping again lets
|
|
14
|
+
* go; the switches live in the bar. A document with no notes renders centred
|
|
15
|
+
* prose at full measure with no notes column.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* import { MarginNotes } from '@molecule/app-margin-notes-react'
|
|
20
|
+
*
|
|
21
|
+
* // blocks: your rendered paragraphs, in order; notes: what goes beside them.
|
|
22
|
+
* <MarginNotes
|
|
23
|
+
* blocks={[
|
|
24
|
+
* { id: 'p1', content: <p>Every post carries a map of who wrote what.</p>, noteIds: ['s1'] },
|
|
25
|
+
* { id: 'p2', content: <p>The map is built from the session export.</p>, noteIds: ['s1', 'q1'], marked: true },
|
|
26
|
+
* ]}
|
|
27
|
+
* notes={[
|
|
28
|
+
* { id: 's1', kind: 'summary', content: <p>How the map is made.</p> },
|
|
29
|
+
* { id: 'q1', kind: 'prompt', content: <p>“explain the map” — claude-opus-4</p> },
|
|
30
|
+
* ]}
|
|
31
|
+
* kinds={[
|
|
32
|
+
* { id: 'summary', label: t('post.summaries', undefined, { defaultValue: 'Summaries' }), defaultOn: true },
|
|
33
|
+
* { id: 'prompt', label: t('post.prompts', undefined, { defaultValue: 'Prompts' }), defaultOn: false, panel: 'tap' },
|
|
34
|
+
* ]}
|
|
35
|
+
* markLabel={t('post.aiWritten', undefined, { defaultValue: 'Written with AI' })}
|
|
36
|
+
* />
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* - **Defaults are right before JavaScript runs.** Which kinds show comes from
|
|
41
|
+
* `kinds[].defaultOn` in the FIRST render (a hidden note carries the HTML
|
|
42
|
+
* `hidden` attribute), and the desktop/phone split is CSS
|
|
43
|
+
* (`cm.hiddenBelow('md')` / `cm.hiddenFrom('md')`). Prerender the page with
|
|
44
|
+
* `renderToString` and a kind that defaults off is already absent; do NOT
|
|
45
|
+
* toggle defaults in a `useEffect` of your own.
|
|
46
|
+
* - **One note, one place.** A note shows beside the FIRST row that refers to
|
|
47
|
+
* it. Give consecutive paragraphs that share a note the same `noteIds` and
|
|
48
|
+
* they form one row with the note beside them once. A paragraph whose
|
|
49
|
+
* `noteIds` differ from its neighbour's starts a new row.
|
|
50
|
+
* - **The mark is drawn on the block**, as an absolutely-placed dot inside the
|
|
51
|
+
* block's own wrapper (hidden on phones, where there is no margin). At rest a
|
|
52
|
+
* marked block is typeset exactly like an unmarked one.
|
|
53
|
+
* - **Pass rendered blocks, not markdown.** Split your renderer's output into
|
|
54
|
+
* top-level blocks (one per paragraph/heading/list) and give each a stable
|
|
55
|
+
* `id`; the component adds no markup inside them.
|
|
56
|
+
* - **Section notes follow the reader; paragraph notes wait for a tap.** On a
|
|
57
|
+
* phone the panel shows the kinds with `panel: 'follow'` (the default) for
|
|
58
|
+
* the section in view. Give a kind that is about ONE paragraph (a prompt, a
|
|
59
|
+
* citation) `panel: 'tap'`: it shows when that paragraph is tapped and goes
|
|
60
|
+
* on the second tap.
|
|
61
|
+
* - **Controlled or not.** Pass `shownKinds` + `onShownKindsChange` to own the
|
|
62
|
+
* switches' state (e.g. to remember it); otherwise the component does.
|
|
63
|
+
* - The phone bar reserves space at the end of the text so it never covers the
|
|
64
|
+
* last paragraph. UI strings come from the companion locale bond
|
|
65
|
+
* `@molecule/app-locales-margin-notes`; kind labels and `markLabel` are yours
|
|
66
|
+
* to translate.
|
|
67
|
+
*
|
|
68
|
+
* @module
|
|
69
|
+
*/
|
|
70
|
+
export * from './MarginNotes.js';
|
|
71
|
+
export * from './rows.js';
|
|
72
|
+
export * from './types.js';
|
|
73
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AAEH,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Margin notes: long-form text with notes beside each paragraph — annotated
|
|
3
|
+
* essays, commentary, and posts that show which paragraphs an AI wrote and the
|
|
4
|
+
* prompt behind each.
|
|
5
|
+
*
|
|
6
|
+
* Desktop: a large-type prose column and a narrower notes column to its right;
|
|
7
|
+
* each note lines up with the paragraph (or run of paragraphs) it belongs to
|
|
8
|
+
* and appears once. Hovering or focusing a paragraph emphasises its notes;
|
|
9
|
+
* hovering or focusing a note tints the paragraphs it covers. A paragraph can
|
|
10
|
+
* carry a small mark in its left margin. Note kinds (say, "Summaries" and
|
|
11
|
+
* "Prompts") each get a real switch. Phone: the notes column gives way to a bar
|
|
12
|
+
* fixed to the bottom that shows the notes for the section being read and
|
|
13
|
+
* follows the reader; tapping a paragraph pins its notes, tapping again lets
|
|
14
|
+
* go; the switches live in the bar. A document with no notes renders centred
|
|
15
|
+
* prose at full measure with no notes column.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* import { MarginNotes } from '@molecule/app-margin-notes-react'
|
|
20
|
+
*
|
|
21
|
+
* // blocks: your rendered paragraphs, in order; notes: what goes beside them.
|
|
22
|
+
* <MarginNotes
|
|
23
|
+
* blocks={[
|
|
24
|
+
* { id: 'p1', content: <p>Every post carries a map of who wrote what.</p>, noteIds: ['s1'] },
|
|
25
|
+
* { id: 'p2', content: <p>The map is built from the session export.</p>, noteIds: ['s1', 'q1'], marked: true },
|
|
26
|
+
* ]}
|
|
27
|
+
* notes={[
|
|
28
|
+
* { id: 's1', kind: 'summary', content: <p>How the map is made.</p> },
|
|
29
|
+
* { id: 'q1', kind: 'prompt', content: <p>“explain the map” — claude-opus-4</p> },
|
|
30
|
+
* ]}
|
|
31
|
+
* kinds={[
|
|
32
|
+
* { id: 'summary', label: t('post.summaries', undefined, { defaultValue: 'Summaries' }), defaultOn: true },
|
|
33
|
+
* { id: 'prompt', label: t('post.prompts', undefined, { defaultValue: 'Prompts' }), defaultOn: false, panel: 'tap' },
|
|
34
|
+
* ]}
|
|
35
|
+
* markLabel={t('post.aiWritten', undefined, { defaultValue: 'Written with AI' })}
|
|
36
|
+
* />
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* - **Defaults are right before JavaScript runs.** Which kinds show comes from
|
|
41
|
+
* `kinds[].defaultOn` in the FIRST render (a hidden note carries the HTML
|
|
42
|
+
* `hidden` attribute), and the desktop/phone split is CSS
|
|
43
|
+
* (`cm.hiddenBelow('md')` / `cm.hiddenFrom('md')`). Prerender the page with
|
|
44
|
+
* `renderToString` and a kind that defaults off is already absent; do NOT
|
|
45
|
+
* toggle defaults in a `useEffect` of your own.
|
|
46
|
+
* - **One note, one place.** A note shows beside the FIRST row that refers to
|
|
47
|
+
* it. Give consecutive paragraphs that share a note the same `noteIds` and
|
|
48
|
+
* they form one row with the note beside them once. A paragraph whose
|
|
49
|
+
* `noteIds` differ from its neighbour's starts a new row.
|
|
50
|
+
* - **The mark is drawn on the block**, as an absolutely-placed dot inside the
|
|
51
|
+
* block's own wrapper (hidden on phones, where there is no margin). At rest a
|
|
52
|
+
* marked block is typeset exactly like an unmarked one.
|
|
53
|
+
* - **Pass rendered blocks, not markdown.** Split your renderer's output into
|
|
54
|
+
* top-level blocks (one per paragraph/heading/list) and give each a stable
|
|
55
|
+
* `id`; the component adds no markup inside them.
|
|
56
|
+
* - **Section notes follow the reader; paragraph notes wait for a tap.** On a
|
|
57
|
+
* phone the panel shows the kinds with `panel: 'follow'` (the default) for
|
|
58
|
+
* the section in view. Give a kind that is about ONE paragraph (a prompt, a
|
|
59
|
+
* citation) `panel: 'tap'`: it shows when that paragraph is tapped and goes
|
|
60
|
+
* on the second tap.
|
|
61
|
+
* - **Controlled or not.** Pass `shownKinds` + `onShownKindsChange` to own the
|
|
62
|
+
* switches' state (e.g. to remember it); otherwise the component does.
|
|
63
|
+
* - The phone bar reserves space at the end of the text so it never covers the
|
|
64
|
+
* last paragraph. UI strings come from the companion locale bond
|
|
65
|
+
* `@molecule/app-locales-margin-notes`; kind labels and `markLabel` are yours
|
|
66
|
+
* to translate.
|
|
67
|
+
*
|
|
68
|
+
* @module
|
|
69
|
+
*/
|
|
70
|
+
export * from './MarginNotes.js';
|
|
71
|
+
export * from './rows.js';
|
|
72
|
+
export * from './types.js';
|
package/dist/rows.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { MarginNote, MarginNotesBlock, MarginNotesRow } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Group blocks into layout rows and place each note once.
|
|
4
|
+
*
|
|
5
|
+
* A row is a run of consecutive blocks that refer to exactly the same set of
|
|
6
|
+
* notes (a paragraph run sharing one note), or a run of consecutive blocks
|
|
7
|
+
* with no notes. Each note is shown beside the FIRST row that refers to it and
|
|
8
|
+
* never again, so a note covering several paragraphs — even ones that are not
|
|
9
|
+
* next to each other — appears once on the page.
|
|
10
|
+
*
|
|
11
|
+
* @param blocks - The text, in reading order.
|
|
12
|
+
* @param notes - Every note the blocks refer to; ids with no note are ignored.
|
|
13
|
+
* @returns The rows, in reading order.
|
|
14
|
+
*/
|
|
15
|
+
export declare function buildRows(blocks: MarginNotesBlock[], notes?: MarginNote[]): MarginNotesRow[];
|
|
16
|
+
/**
|
|
17
|
+
* The kinds shown before the reader touches a switch.
|
|
18
|
+
*
|
|
19
|
+
* @param kinds - The switchable kinds.
|
|
20
|
+
* @returns The ids of the kinds that default on.
|
|
21
|
+
*/
|
|
22
|
+
export declare function defaultShownKinds(kinds?: {
|
|
23
|
+
id: string;
|
|
24
|
+
defaultOn?: boolean;
|
|
25
|
+
}[]): string[];
|
|
26
|
+
//# sourceMappingURL=rows.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rows.d.ts","sourceRoot":"","sources":["../src/rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAE9E;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,gBAAgB,EAAE,EAAE,KAAK,GAAE,UAAU,EAAO,GAAG,cAAc,EAAE,CA0BhG;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,GAAE;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,EAAO,GAAG,MAAM,EAAE,CAE7F"}
|
package/dist/rows.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Group blocks into layout rows and place each note once.
|
|
3
|
+
*
|
|
4
|
+
* A row is a run of consecutive blocks that refer to exactly the same set of
|
|
5
|
+
* notes (a paragraph run sharing one note), or a run of consecutive blocks
|
|
6
|
+
* with no notes. Each note is shown beside the FIRST row that refers to it and
|
|
7
|
+
* never again, so a note covering several paragraphs — even ones that are not
|
|
8
|
+
* next to each other — appears once on the page.
|
|
9
|
+
*
|
|
10
|
+
* @param blocks - The text, in reading order.
|
|
11
|
+
* @param notes - Every note the blocks refer to; ids with no note are ignored.
|
|
12
|
+
* @returns The rows, in reading order.
|
|
13
|
+
*/
|
|
14
|
+
export function buildRows(blocks, notes = []) {
|
|
15
|
+
const byId = new Map(notes.map((n) => [n.id, n]));
|
|
16
|
+
const known = (ids) => [...new Set(ids ?? [])].filter((id) => byId.has(id));
|
|
17
|
+
const key = (ids) => [...ids].sort().join('\u0000');
|
|
18
|
+
const rows = [];
|
|
19
|
+
const placed = new Set();
|
|
20
|
+
let current = null;
|
|
21
|
+
let currentKey = '';
|
|
22
|
+
for (const block of blocks) {
|
|
23
|
+
const ids = known(block.noteIds);
|
|
24
|
+
const k = key(ids);
|
|
25
|
+
if (current && k === currentKey) {
|
|
26
|
+
current.blocks.push(block);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
current = { id: block.id, blocks: [block], notes: [], noteIds: ids };
|
|
30
|
+
currentKey = k;
|
|
31
|
+
for (const id of ids) {
|
|
32
|
+
if (placed.has(id))
|
|
33
|
+
continue;
|
|
34
|
+
placed.add(id);
|
|
35
|
+
current.notes.push(byId.get(id));
|
|
36
|
+
}
|
|
37
|
+
rows.push(current);
|
|
38
|
+
}
|
|
39
|
+
return rows;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The kinds shown before the reader touches a switch.
|
|
43
|
+
*
|
|
44
|
+
* @param kinds - The switchable kinds.
|
|
45
|
+
* @returns The ids of the kinds that default on.
|
|
46
|
+
*/
|
|
47
|
+
export function defaultShownKinds(kinds = []) {
|
|
48
|
+
return kinds.filter((k) => k.defaultOn !== false).map((k) => k.id);
|
|
49
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { HTMLAttributes, ReactNode } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* One block of the text — usually a paragraph or heading from your markdown
|
|
4
|
+
* renderer. Blocks render in order, in the prose column.
|
|
5
|
+
*/
|
|
6
|
+
export interface MarginNotesBlock {
|
|
7
|
+
/** Stable id, unique within the document (used as the DOM anchor). */
|
|
8
|
+
id: string;
|
|
9
|
+
/** The rendered block (e.g. `<p>…</p>`). */
|
|
10
|
+
content: ReactNode;
|
|
11
|
+
/** Ids of the notes that belong to this block (see {@link MarginNote}). */
|
|
12
|
+
noteIds?: string[];
|
|
13
|
+
/**
|
|
14
|
+
* Draw a small mark in this block's left margin — the one permanent sign on
|
|
15
|
+
* the prose itself (e.g. "written with AI"). Unmarked blocks carry none.
|
|
16
|
+
*/
|
|
17
|
+
marked?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** A note shown beside the block(s) it belongs to. */
|
|
20
|
+
export interface MarginNote {
|
|
21
|
+
/** Stable id, referenced from {@link MarginNotesBlock.noteIds}. */
|
|
22
|
+
id: string;
|
|
23
|
+
/** The kind this note belongs to ({@link MarginNoteKind.id}), for its switch. */
|
|
24
|
+
kind: string;
|
|
25
|
+
/** The rendered note. */
|
|
26
|
+
content: ReactNode;
|
|
27
|
+
}
|
|
28
|
+
/** A kind of note the reader can show or hide with a switch. */
|
|
29
|
+
export interface MarginNoteKind {
|
|
30
|
+
/** Stable id, referenced from {@link MarginNote.kind}. */
|
|
31
|
+
id: string;
|
|
32
|
+
/** The switch's visible label, already translated (e.g. "Summaries"). */
|
|
33
|
+
label: string;
|
|
34
|
+
/** Whether this kind shows before the reader touches its switch. Default `true`. */
|
|
35
|
+
defaultOn?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* How this kind reaches the phone panel. `'follow'` (default): the panel
|
|
38
|
+
* shows it for the section being read, following the reader. `'tap'`: only
|
|
39
|
+
* when the reader taps a block it belongs to (and gone on the second tap) —
|
|
40
|
+
* for notes about one paragraph rather than the section.
|
|
41
|
+
*/
|
|
42
|
+
panel?: 'follow' | 'tap';
|
|
43
|
+
}
|
|
44
|
+
/** `<MarginNotes>` props. */
|
|
45
|
+
export interface MarginNotesProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
|
|
46
|
+
/** The text, in reading order. */
|
|
47
|
+
blocks: MarginNotesBlock[];
|
|
48
|
+
/** Every note the blocks refer to. A document with none renders centred prose with no gutter. */
|
|
49
|
+
notes?: MarginNote[];
|
|
50
|
+
/** The note kinds, in the order their switches appear. Kinds absent here are always shown. */
|
|
51
|
+
kinds?: MarginNoteKind[];
|
|
52
|
+
/** Accessible name of the margin mark, already translated (e.g. "Written with AI"). */
|
|
53
|
+
markLabel?: string;
|
|
54
|
+
/** Width of the prose column (a CSS length). Default `38rem`. */
|
|
55
|
+
measure?: string;
|
|
56
|
+
/** Width of the notes column (a CSS length). Default `18rem`. */
|
|
57
|
+
gutterWidth?: string;
|
|
58
|
+
/** Controlled: the ids of the kinds currently shown. Omit to let the component own it. */
|
|
59
|
+
shownKinds?: string[];
|
|
60
|
+
/** Called with the new list of shown kind ids whenever the reader flips a switch. */
|
|
61
|
+
onShownKindsChange?: (shown: string[]) => void;
|
|
62
|
+
}
|
|
63
|
+
/** One row of the layout: a run of blocks and the notes shown beside it. */
|
|
64
|
+
export interface MarginNotesRow {
|
|
65
|
+
/** The row's key: the id of its first block. */
|
|
66
|
+
id: string;
|
|
67
|
+
/** The blocks in this row, in order. */
|
|
68
|
+
blocks: MarginNotesBlock[];
|
|
69
|
+
/** Notes shown beside this row — each note appears in exactly one row, its first. */
|
|
70
|
+
notes: MarginNote[];
|
|
71
|
+
/** Every note id any block in this row refers to (for the phone panel). */
|
|
72
|
+
noteIds: string[];
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEtD;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,sEAAsE;IACtE,EAAE,EAAE,MAAM,CAAA;IACV,4CAA4C;IAC5C,OAAO,EAAE,SAAS,CAAA;IAClB,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAED,sDAAsD;AACtD,MAAM,WAAW,UAAU;IACzB,mEAAmE;IACnE,EAAE,EAAE,MAAM,CAAA;IACV,iFAAiF;IACjF,IAAI,EAAE,MAAM,CAAA;IACZ,yBAAyB;IACzB,OAAO,EAAE,SAAS,CAAA;CACnB;AAED,gEAAgE;AAChE,MAAM,WAAW,cAAc;IAC7B,0DAA0D;IAC1D,EAAE,EAAE,MAAM,CAAA;IACV,yEAAyE;IACzE,KAAK,EAAE,MAAM,CAAA;IACb,oFAAoF;IACpF,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,QAAQ,GAAG,KAAK,CAAA;CACzB;AAED,6BAA6B;AAC7B,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,cAAc,CAAC,cAAc,CAAC,EAAE,UAAU,CAAC;IACxF,kCAAkC;IAClC,MAAM,EAAE,gBAAgB,EAAE,CAAA;IAC1B,iGAAiG;IACjG,KAAK,CAAC,EAAE,UAAU,EAAE,CAAA;IACpB,8FAA8F;IAC9F,KAAK,CAAC,EAAE,cAAc,EAAE,CAAA;IACxB,uFAAuF;IACvF,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,iEAAiE;IACjE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,iEAAiE;IACjE,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,0FAA0F;IAC1F,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,qFAAqF;IACrF,kBAAkB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,IAAI,CAAA;CAC/C;AAED,4EAA4E;AAC5E,MAAM,WAAW,cAAc;IAC7B,gDAAgD;IAChD,EAAE,EAAE,MAAM,CAAA;IACV,wCAAwC;IACxC,MAAM,EAAE,gBAAgB,EAAE,CAAA;IAC1B,qFAAqF;IACrF,KAAK,EAAE,UAAU,EAAE,CAAA;IACnB,2EAA2E;IAC3E,OAAO,EAAE,MAAM,EAAE,CAAA;CAClB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-margin-notes-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Long-form reading layout with notes beside each paragraph: aligned notes column, margin marks, two-way hover, switchable note kinds, and a phone bar that follows the reader",
|
|
5
|
+
"homepage": "https://www.molecule.dev/packages/app-margin-notes-react",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"test": "vitest run",
|
|
12
|
+
"test:watch": "vitest"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"molecule",
|
|
26
|
+
"margin notes",
|
|
27
|
+
"sidenotes",
|
|
28
|
+
"annotations",
|
|
29
|
+
"reading layout",
|
|
30
|
+
"react"
|
|
31
|
+
],
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
36
|
+
"directory": "packages/app/features/margin-notes-react"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@molecule/app-react": "^1.0.1",
|
|
40
|
+
"@molecule/app-ui": "^1.1.1",
|
|
41
|
+
"@molecule/app-ui-react": "^1.0.1",
|
|
42
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@molecule/app-i18n": "1.0.2",
|
|
46
|
+
"@molecule/app-react": "1.6.0",
|
|
47
|
+
"@molecule/app-ui": "1.1.1",
|
|
48
|
+
"@testing-library/react": "16.3.2",
|
|
49
|
+
"@types/node": "26.1.2",
|
|
50
|
+
"@types/react": "19.2.17",
|
|
51
|
+
"jsdom": "30.0.1",
|
|
52
|
+
"react": "19.2.8",
|
|
53
|
+
"react-dom": "19.2.8",
|
|
54
|
+
"typescript": "6.0.3",
|
|
55
|
+
"vitest": "4.1.11",
|
|
56
|
+
"@molecule/app-ui-react": "1.0.2"
|
|
57
|
+
}
|
|
58
|
+
}
|