@molecule/app-margin-notes-react 1.0.1 → 1.0.7
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 +111 -24
- package/README.md +263 -94
- package/dist/MarginNotes.d.ts.map +1 -1
- package/dist/MarginNotes.js +13 -4
- package/dist/index.d.ts +198 -41
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +198 -41
- package/dist/rows.d.ts +25 -5
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +66 -7
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +14 -12
package/dist/rows.js
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Group blocks into layout rows and place each note once.
|
|
3
3
|
*
|
|
4
|
-
* A row
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* A row runs for as long as its blocks are covered by a note it OPENED with: a
|
|
5
|
+
* section's summary (every block of the section refers to it) keeps the whole
|
|
6
|
+
* section in one row, and a note that first appears mid-row — a prompt behind
|
|
7
|
+
* one of the section's paragraphs — joins that row's gutter but does not
|
|
8
|
+
* extend the row. (A prompt that produced paragraphs in several sections once
|
|
9
|
+
* carried a row across the next heading, and that section's summary was drawn
|
|
10
|
+
* beside the previous section.) A sticky note is
|
|
11
|
+
* bounded by its row, so this is what keeps a summary pinned until its section
|
|
12
|
+
* ends; splitting the row wherever the note set changed released it after the
|
|
13
|
+
* first paragraph. A run of blocks with no notes is a row of its own. Each note
|
|
14
|
+
* is shown once, in the first row that refers to it.
|
|
9
15
|
*
|
|
10
16
|
* @param blocks - The text, in reading order.
|
|
11
17
|
* @param notes - Every note the blocks refer to; ids with no note are ignored.
|
|
12
18
|
* @returns The rows, in reading order.
|
|
13
19
|
*/
|
|
14
20
|
export function buildRows(blocks, notes = []) {
|
|
21
|
+
blocks = mergeRepeatedNotes(blocks, notes);
|
|
15
22
|
const byId = new Map(notes.map((n) => [n.id, n]));
|
|
16
23
|
const known = (ids) => [...new Set(ids ?? [])].filter((id) => byId.has(id));
|
|
17
24
|
const key = (ids) => [...ids].sort().join('\u0000');
|
|
@@ -19,15 +26,26 @@ export function buildRows(blocks, notes = []) {
|
|
|
19
26
|
const placed = new Set();
|
|
20
27
|
let current = null;
|
|
21
28
|
let currentKey = '';
|
|
29
|
+
let opening = [];
|
|
22
30
|
for (const block of blocks) {
|
|
23
31
|
const ids = known(block.noteIds);
|
|
24
32
|
const k = key(ids);
|
|
25
|
-
|
|
33
|
+
const stillCovered = current !== null && ids.some((id) => opening.includes(id));
|
|
34
|
+
if (current && (k === currentKey || stillCovered)) {
|
|
26
35
|
current.blocks.push(block);
|
|
36
|
+
for (const id of ids) {
|
|
37
|
+
if (!current.noteIds.includes(id))
|
|
38
|
+
current.noteIds.push(id);
|
|
39
|
+
if (placed.has(id))
|
|
40
|
+
continue;
|
|
41
|
+
placed.add(id);
|
|
42
|
+
current.notes.push(byId.get(id));
|
|
43
|
+
}
|
|
27
44
|
continue;
|
|
28
45
|
}
|
|
29
|
-
current = { id: block.id, blocks: [block], notes: [], noteIds: ids };
|
|
46
|
+
current = { id: block.id, blocks: [block], notes: [], noteIds: [...ids] };
|
|
30
47
|
currentKey = k;
|
|
48
|
+
opening = ids;
|
|
31
49
|
for (const id of ids) {
|
|
32
50
|
if (placed.has(id))
|
|
33
51
|
continue;
|
|
@@ -38,6 +56,47 @@ export function buildRows(blocks, notes = []) {
|
|
|
38
56
|
}
|
|
39
57
|
return rows;
|
|
40
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* One note per act of writing: when consecutive blocks each carry their own
|
|
61
|
+
* note with the same kind and the same plain-text content (one prompt that produced three
|
|
62
|
+
* paragraphs, recorded as three notes), every later block is pointed at the
|
|
63
|
+
* first block's note instead. {@link buildRows} places each note once, so the
|
|
64
|
+
* prompt is shown once, beside the first paragraph it produced, and hovering
|
|
65
|
+
* any of those paragraphs highlights that one note. The same text appearing
|
|
66
|
+
* again after a block without it is a new act, and keeps its own note.
|
|
67
|
+
*
|
|
68
|
+
* @param blocks - The text, in reading order.
|
|
69
|
+
* @param notes - Every note the blocks refer to.
|
|
70
|
+
* @returns The blocks, with repeated notes pointed at the first copy.
|
|
71
|
+
*/
|
|
72
|
+
export function mergeRepeatedNotes(blocks, notes = []) {
|
|
73
|
+
const byId = new Map(notes.map((n) => [n.id, n]));
|
|
74
|
+
// Only plain-text content can be compared; a rendered element is never merged.
|
|
75
|
+
const content = (n) => typeof n.content === 'string' || typeof n.content === 'number'
|
|
76
|
+
? `${n.kind}\u0000${String(n.content).trim()}`
|
|
77
|
+
: `\u0001${n.id}`;
|
|
78
|
+
// Content → id of the note the PREVIOUS block showed for it.
|
|
79
|
+
let previous = new Map();
|
|
80
|
+
return blocks.map((block) => {
|
|
81
|
+
const current = new Map();
|
|
82
|
+
let changed = false;
|
|
83
|
+
const noteIds = (block.noteIds ?? []).map((id) => {
|
|
84
|
+
const note = byId.get(id);
|
|
85
|
+
if (!note)
|
|
86
|
+
return id;
|
|
87
|
+
const key = content(note);
|
|
88
|
+
const first = previous.get(key);
|
|
89
|
+
const use = first ?? id;
|
|
90
|
+
if (use !== id)
|
|
91
|
+
changed = true;
|
|
92
|
+
if (!current.has(key))
|
|
93
|
+
current.set(key, use);
|
|
94
|
+
return use;
|
|
95
|
+
});
|
|
96
|
+
previous = current;
|
|
97
|
+
return changed ? { ...block, noteIds: [...new Set(noteIds)] } : block;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
41
100
|
/**
|
|
42
101
|
* The kinds shown before the reader touches a switch.
|
|
43
102
|
*
|
package/dist/types.d.ts
CHANGED
|
@@ -37,7 +37,8 @@ export interface MarginNoteKind {
|
|
|
37
37
|
* How this kind reaches the phone panel. `'follow'` (default): the panel
|
|
38
38
|
* shows it for the section being read, following the reader. `'tap'`: only
|
|
39
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.
|
|
40
|
+
* for notes about one paragraph rather than the section. A tap shows the
|
|
41
|
+
* note even while its switch is off: on a phone the tap is how it is found.
|
|
41
42
|
*/
|
|
42
43
|
panel?: 'follow' | 'tap';
|
|
43
44
|
}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +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
|
|
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;I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A;IAC1B,qFAAqF;IACrF,KAAK,EAAE,UAAU,EAAE,CAAA;IACnB,2EAA2E;IAC3E,OAAO,EAAE,MAAM,EAAE,CAAA;CAClB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/app-margin-notes-react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
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
5
|
"homepage": "https://www.molecule.dev/packages/app-margin-notes-react",
|
|
6
6
|
"type": "module",
|
|
@@ -42,17 +42,19 @@
|
|
|
42
42
|
"react": "^18.0.0 || ^19.0.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@molecule/app-i18n": "1.0.
|
|
46
|
-
"@molecule/app-
|
|
47
|
-
"@molecule/app-
|
|
48
|
-
"@
|
|
49
|
-
"@
|
|
50
|
-
"@
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"react
|
|
45
|
+
"@molecule/app-i18n": "1.0.4",
|
|
46
|
+
"@molecule/app-markdown": "1.0.3",
|
|
47
|
+
"@molecule/app-markdown-marked": "1.0.3",
|
|
48
|
+
"@molecule/app-react": "1.10.1",
|
|
49
|
+
"@molecule/app-ui": "1.3.8",
|
|
50
|
+
"@molecule/app-ui-react": "1.3.6",
|
|
51
|
+
"@testing-library/react": "16.3.3",
|
|
52
|
+
"@types/node": "26.6.5",
|
|
53
|
+
"@types/react": "19.3.0",
|
|
54
|
+
"jsdom": "30.1.3",
|
|
55
|
+
"react": "19.3.1",
|
|
56
|
+
"react-dom": "19.3.1",
|
|
54
57
|
"typescript": "6.0.3",
|
|
55
|
-
"vitest": "4.1.
|
|
56
|
-
"@molecule/app-ui-react": "1.0.2"
|
|
58
|
+
"vitest": "4.1.12"
|
|
57
59
|
}
|
|
58
60
|
}
|