vultus-core 0.5.7 → 0.5.8
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/README.md +191 -5
- package/dist/Components.d.ts +41 -0
- package/dist/Components.d.ts.map +1 -1
- package/dist/Components.js.map +1 -1
- package/dist/changes/ChangeMark.d.ts +85 -0
- package/dist/changes/ChangeMark.d.ts.map +1 -0
- package/dist/changes/ChangeMark.js +113 -0
- package/dist/changes/ChangeMark.js.map +1 -0
- package/dist/changes/ChangingText.d.ts +20 -0
- package/dist/changes/ChangingText.d.ts.map +1 -0
- package/dist/changes/ChangingText.js +31 -0
- package/dist/changes/ChangingText.js.map +1 -0
- package/dist/changes/FallbackMark.d.ts +20 -0
- package/dist/changes/FallbackMark.d.ts.map +1 -0
- package/dist/changes/FallbackMark.js +53 -0
- package/dist/changes/FallbackMark.js.map +1 -0
- package/dist/changes/blocks.d.ts +15 -0
- package/dist/changes/blocks.d.ts.map +1 -0
- package/dist/changes/blocks.js +24 -0
- package/dist/changes/blocks.js.map +1 -0
- package/dist/changes/changeDurations.d.ts +21 -0
- package/dist/changes/changeDurations.d.ts.map +1 -0
- package/dist/changes/changeDurations.js +18 -0
- package/dist/changes/changeDurations.js.map +1 -0
- package/dist/changes/changesIn.d.ts +88 -0
- package/dist/changes/changesIn.d.ts.map +1 -0
- package/dist/changes/changesIn.js +470 -0
- package/dist/changes/changesIn.js.map +1 -0
- package/dist/changes/readText.d.ts +45 -0
- package/dist/changes/readText.d.ts.map +1 -0
- package/dist/changes/readText.js +99 -0
- package/dist/changes/readText.js.map +1 -0
- package/dist/changes/rehypeChanges.d.ts +81 -0
- package/dist/changes/rehypeChanges.d.ts.map +1 -0
- package/dist/changes/rehypeChanges.js +268 -0
- package/dist/changes/rehypeChanges.js.map +1 -0
- package/dist/changes/useTextChanges.d.ts +93 -0
- package/dist/changes/useTextChanges.d.ts.map +1 -0
- package/dist/changes/useTextChanges.js +154 -0
- package/dist/changes/useTextChanges.js.map +1 -0
- package/dist/changes/visibleBlocks.d.ts +42 -0
- package/dist/changes/visibleBlocks.d.ts.map +1 -0
- package/dist/changes/visibleBlocks.js +104 -0
- package/dist/changes/visibleBlocks.js.map +1 -0
- package/dist/changes.d.ts +9 -0
- package/dist/changes.d.ts.map +1 -0
- package/dist/changes.js +10 -0
- package/dist/changes.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +13 -1
- package/src/Components.tsx +45 -0
- package/src/changes/ChangeMark.tsx +165 -0
- package/src/changes/ChangingText.tsx +46 -0
- package/src/changes/FallbackMark.tsx +59 -0
- package/src/changes/blocks.ts +25 -0
- package/src/changes/changeDurations.ts +31 -0
- package/src/changes/changesIn.ts +479 -0
- package/src/changes/readText.ts +123 -0
- package/src/changes/rehypeChanges.ts +302 -0
- package/src/changes/useTextChanges.ts +239 -0
- package/src/changes/visibleBlocks.ts +121 -0
- package/src/changes.ts +14 -0
- package/src/index.ts +4 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readText.js","sourceRoot":"","sources":["../../src/changes/readText.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AA+B7C;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAoB,EAAE,OAAyB;IACtE,MAAM,MAAM,GAAgB,EAAE,CAAC;IAC/B,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,gFAAgF;IAChF,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,MAAM,IAAI,GAAG,GAAG,EAAE;QAChB,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClB,MAAM,IAAI,CAAC,CAAC;IACd,CAAC,CAAC;IACF,MAAM,KAA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QAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC1D,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC1B,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE5B,gGAAgG;IAChG,mDAAmD;IACnD,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACrB,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACrB,OAAO,CAAC,KAAK,EAAE,CAAC;IAClB,CAAC;IACD,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACrB,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACzB,OAAO,CAAC,GAAG,EAAE,CAAC;IAChB,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC;AAC/C,CAAC;AAED;8FAC8F;AAC9F,MAAM,UAAU,SAAS,CAAC,OAAgB,EAAE,OAAyB;IACnE,MAAM,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC,oBAAoB,CAAC;IACrD,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC;IACvE,OAAO,OAAO,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { Element, Root } from 'hast';
|
|
2
|
+
/** The attribute the plugin numbers the blocks with (`NUMBERED_BLOCKS`), unless it is given an
|
|
3
|
+
* existing numbering's (`blockAttribute`). */
|
|
4
|
+
export declare const BLOCK_NUMBER_ATTRIBUTE = "data-vultus-block";
|
|
5
|
+
/** A data attribute holding a block's number. */
|
|
6
|
+
export type BlockAttribute = `data-${string}`;
|
|
7
|
+
/** 'data-block' → 'dataBlock', 'data-vultus-block' → 'dataVultusBlock': hast's property for the
|
|
8
|
+
* attribute (property-information's rule: each dash before a lowercase letter goes, the letter
|
|
9
|
+
* capitalised). */
|
|
10
|
+
export declare function blockProperty(attribute: BlockAttribute): string;
|
|
11
|
+
/** What one run of the plugin read (`onRead`): the part's reading (`readText`'s text), taken
|
|
12
|
+
* before any mark; and whether it cut in a removed mark (`ins[data-change=removed]`). */
|
|
13
|
+
export interface ReadReport {
|
|
14
|
+
reading: string;
|
|
15
|
+
removals: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface ChangesPluginOptions {
|
|
18
|
+
/** The part's previous reading; none: read and report only. */
|
|
19
|
+
from?: string;
|
|
20
|
+
/** The frozen near set, by block number; none: all marked. */
|
|
21
|
+
near?: (block: number) => boolean;
|
|
22
|
+
/** An existing numbering's attribute; none: the plugin numbers as `data-vultus-block`. */
|
|
23
|
+
blockAttribute?: BlockAttribute;
|
|
24
|
+
/** What the reading leaves out besides `data-vultus-change-skip` (`readText`). */
|
|
25
|
+
skip?: (element: Element) => boolean;
|
|
26
|
+
/** Called once per run, with what it read. */
|
|
27
|
+
onRead?: (report: ReadReport) => void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A rehype plugin marking in a hast tree what changed from its part's previous reading,
|
|
31
|
+
* `from`. Each run of its transformer:
|
|
32
|
+
* - numbers the blocks (`NUMBERED_BLOCKS`, pre-order from 1, as `data-vultus-block`), a skipped
|
|
33
|
+
* element and all it holds left out, unless `blockAttribute` names an existing numbering;
|
|
34
|
+
* - reads the tree (`readText`, with `skip`) before any mark is cut in, so the reading never holds
|
|
35
|
+
* removed text, on a re-parse during the light-up too, and the same tree gives the same reading;
|
|
36
|
+
* - without `from`, reports it (`onRead`) and is done;
|
|
37
|
+
* - with it, compares the two (`changesIn`), maps the spans back to the text nodes through the
|
|
38
|
+
* reading's `places`, cuts the marks in, and reports the reading and whether it cut in a removed
|
|
39
|
+
* mark (for the switch's timeline: removals anywhere put every part on `removals`).
|
|
40
|
+
*
|
|
41
|
+
* Each piece's part of a changed word goes in an `<ins data-change="word">`, inside its own element
|
|
42
|
+
* (an emphasis, a link, a code token: a word split over elements is marked piece by piece, `*hí*vei`
|
|
43
|
+
* gives two), and its part of the word's changed letters in an `<ins data-change="letters">` inside
|
|
44
|
+
* that. Never a piece between blocks (remark-rehype's line ends, where a mark would draw an empty
|
|
45
|
+
* line: `between`). The reading's line feeds are hard boundaries for `changesIn`, so nothing is lit
|
|
46
|
+
* across a block's edge, a `br` or a code line's end, and a word removed at a paragraph's end stays
|
|
47
|
+
* in that paragraph.
|
|
48
|
+
*
|
|
49
|
+
* The old letters that went are put in at their place, as text in an `<ins data-change="removed">`
|
|
50
|
+
* inside their word's mark, before the letters there; at a place where two pieces of the word meet,
|
|
51
|
+
* in the earlier. A word removed altogether is a word mark of its own holding only them, in the
|
|
52
|
+
* piece where its place is (where two meet, at the end of the earlier: the text before it, not an
|
|
53
|
+
* emphasis starting after it), so removed text never gets a block of its own. A line feed in a
|
|
54
|
+
* `letters` or `removed` mark is put in as a space: the lent mark may keep the white space of their
|
|
55
|
+
* pieces, and would break the line there.
|
|
56
|
+
*
|
|
57
|
+
* A replacement, old letters and the new ones that took their place ("hivei" → "hívei": the "i"
|
|
58
|
+
* and the "í"; "t. i." → "tudniillik", whole), is one `<ins data-change="replaced">` around the
|
|
59
|
+
* two, inside their word's mark, holding nothing else (the lent mark's `change-replaced`). Only when
|
|
60
|
+
* both are in one piece and the new letters end there: one split over pieces (an emphasis between
|
|
61
|
+
* the old letters and the new, "*h*ívei", or among the new ones, "*tudni*illik") is left as two, as
|
|
62
|
+
* is a pure removal or insertion.
|
|
63
|
+
*
|
|
64
|
+
* `ins`, not `mark`: a tag of their own, which `changeComponents`' `ins` entry draws (as the lent
|
|
65
|
+
* mark, or vultus's fallback, a `<mark>`). react-markdown keys an element by its tag's count among
|
|
66
|
+
* its siblings (`mark-0`, `mark-1`), so a search hit put in before a change mark (a search typed
|
|
67
|
+
* during the light-up) would change its key, and React would draw it anew, its fade starting over;
|
|
68
|
+
* counted apart, it keeps its key and its element.
|
|
69
|
+
*
|
|
70
|
+
* With `near` (the switch's frozen near set), only the pieces whose nearest numbered block (by
|
|
71
|
+
* `blockAttribute`, or the own numbering: the innermost holding them) has a number it takes are
|
|
72
|
+
* marked; the others show the new text as it is. The whole text is still compared, so what is
|
|
73
|
+
* marked is what it would be without it. A piece in no numbered block is marked.
|
|
74
|
+
*
|
|
75
|
+
* **Plugin order (a contract, not enforced):** after any plugin that reads or marks text first (a
|
|
76
|
+
* passage's marks: a change is then marked inside the passage's mark), and before plugins that add
|
|
77
|
+
* search marks, which must skip `[data-change=removed]` (the removed letters are no text of the
|
|
78
|
+
* page's).
|
|
79
|
+
*/
|
|
80
|
+
export declare function changesPlugin(options: ChangesPluginOptions): () => (tree: Root) => void;
|
|
81
|
+
//# sourceMappingURL=rehypeChanges.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rehypeChanges.d.ts","sourceRoot":"","sources":["../../src/changes/rehypeChanges.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAkB,IAAI,EAAQ,MAAM,MAAM,CAAC;AAKhE;+CAC+C;AAC/C,eAAO,MAAM,sBAAsB,sBAAsB,CAAC;AAE1D,iDAAiD;AACjD,MAAM,MAAM,cAAc,GAAG,QAAQ,MAAM,EAAE,CAAC;AAE9C;;oBAEoB;AACpB,wBAAgB,aAAa,CAAC,SAAS,EAAE,cAAc,GAAG,MAAM,CAG/D;AAKD;0FAC0F;AAC1F,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,oBAAoB;IACnC,+DAA+D;IAC/D,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8DAA8D;IAC9D,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IAClC,0FAA0F;IAC1F,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC,kFAAkF;IAClF,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC;IACrC,8CAA8C;IAC9C,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,IAAI,CAAC;CACvC;AAsBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,MAAM,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAcvF"}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { BLOCK_ELEMENTS, HOLDS_BLOCKS, NUMBERED_BLOCKS } from './blocks.js';
|
|
2
|
+
import { changesIn } from './changesIn.js';
|
|
3
|
+
import { isSkipped, readText } from './readText.js';
|
|
4
|
+
/** The attribute the plugin numbers the blocks with (`NUMBERED_BLOCKS`), unless it is given an
|
|
5
|
+
* existing numbering's (`blockAttribute`). */
|
|
6
|
+
export const BLOCK_NUMBER_ATTRIBUTE = 'data-vultus-block';
|
|
7
|
+
/** 'data-block' → 'dataBlock', 'data-vultus-block' → 'dataVultusBlock': hast's property for the
|
|
8
|
+
* attribute (property-information's rule: each dash before a lowercase letter goes, the letter
|
|
9
|
+
* capitalised). */
|
|
10
|
+
export function blockProperty(attribute) {
|
|
11
|
+
const rest = attribute.slice(5).replace(/-[a-z]/g, (dash) => dash.charAt(1).toUpperCase());
|
|
12
|
+
return `data${rest.charAt(0).toUpperCase()}${rest.slice(1)}`;
|
|
13
|
+
}
|
|
14
|
+
/** The property the plugin's own numbering sets. */
|
|
15
|
+
const OWN_PROPERTY = blockProperty(BLOCK_NUMBER_ATTRIBUTE);
|
|
16
|
+
/**
|
|
17
|
+
* A rehype plugin marking in a hast tree what changed from its part's previous reading,
|
|
18
|
+
* `from`. Each run of its transformer:
|
|
19
|
+
* - numbers the blocks (`NUMBERED_BLOCKS`, pre-order from 1, as `data-vultus-block`), a skipped
|
|
20
|
+
* element and all it holds left out, unless `blockAttribute` names an existing numbering;
|
|
21
|
+
* - reads the tree (`readText`, with `skip`) before any mark is cut in, so the reading never holds
|
|
22
|
+
* removed text, on a re-parse during the light-up too, and the same tree gives the same reading;
|
|
23
|
+
* - without `from`, reports it (`onRead`) and is done;
|
|
24
|
+
* - with it, compares the two (`changesIn`), maps the spans back to the text nodes through the
|
|
25
|
+
* reading's `places`, cuts the marks in, and reports the reading and whether it cut in a removed
|
|
26
|
+
* mark (for the switch's timeline: removals anywhere put every part on `removals`).
|
|
27
|
+
*
|
|
28
|
+
* Each piece's part of a changed word goes in an `<ins data-change="word">`, inside its own element
|
|
29
|
+
* (an emphasis, a link, a code token: a word split over elements is marked piece by piece, `*hí*vei`
|
|
30
|
+
* gives two), and its part of the word's changed letters in an `<ins data-change="letters">` inside
|
|
31
|
+
* that. Never a piece between blocks (remark-rehype's line ends, where a mark would draw an empty
|
|
32
|
+
* line: `between`). The reading's line feeds are hard boundaries for `changesIn`, so nothing is lit
|
|
33
|
+
* across a block's edge, a `br` or a code line's end, and a word removed at a paragraph's end stays
|
|
34
|
+
* in that paragraph.
|
|
35
|
+
*
|
|
36
|
+
* The old letters that went are put in at their place, as text in an `<ins data-change="removed">`
|
|
37
|
+
* inside their word's mark, before the letters there; at a place where two pieces of the word meet,
|
|
38
|
+
* in the earlier. A word removed altogether is a word mark of its own holding only them, in the
|
|
39
|
+
* piece where its place is (where two meet, at the end of the earlier: the text before it, not an
|
|
40
|
+
* emphasis starting after it), so removed text never gets a block of its own. A line feed in a
|
|
41
|
+
* `letters` or `removed` mark is put in as a space: the lent mark may keep the white space of their
|
|
42
|
+
* pieces, and would break the line there.
|
|
43
|
+
*
|
|
44
|
+
* A replacement, old letters and the new ones that took their place ("hivei" → "hívei": the "i"
|
|
45
|
+
* and the "í"; "t. i." → "tudniillik", whole), is one `<ins data-change="replaced">` around the
|
|
46
|
+
* two, inside their word's mark, holding nothing else (the lent mark's `change-replaced`). Only when
|
|
47
|
+
* both are in one piece and the new letters end there: one split over pieces (an emphasis between
|
|
48
|
+
* the old letters and the new, "*h*ívei", or among the new ones, "*tudni*illik") is left as two, as
|
|
49
|
+
* is a pure removal or insertion.
|
|
50
|
+
*
|
|
51
|
+
* `ins`, not `mark`: a tag of their own, which `changeComponents`' `ins` entry draws (as the lent
|
|
52
|
+
* mark, or vultus's fallback, a `<mark>`). react-markdown keys an element by its tag's count among
|
|
53
|
+
* its siblings (`mark-0`, `mark-1`), so a search hit put in before a change mark (a search typed
|
|
54
|
+
* during the light-up) would change its key, and React would draw it anew, its fade starting over;
|
|
55
|
+
* counted apart, it keeps its key and its element.
|
|
56
|
+
*
|
|
57
|
+
* With `near` (the switch's frozen near set), only the pieces whose nearest numbered block (by
|
|
58
|
+
* `blockAttribute`, or the own numbering: the innermost holding them) has a number it takes are
|
|
59
|
+
* marked; the others show the new text as it is. The whole text is still compared, so what is
|
|
60
|
+
* marked is what it would be without it. A piece in no numbered block is marked.
|
|
61
|
+
*
|
|
62
|
+
* **Plugin order (a contract, not enforced):** after any plugin that reads or marks text first (a
|
|
63
|
+
* passage's marks: a change is then marked inside the passage's mark), and before plugins that add
|
|
64
|
+
* search marks, which must skip `[data-change=removed]` (the removed letters are no text of the
|
|
65
|
+
* page's).
|
|
66
|
+
*/
|
|
67
|
+
export function changesPlugin(options) {
|
|
68
|
+
return () => (tree) => {
|
|
69
|
+
const { from, near, blockAttribute, skip, onRead } = options;
|
|
70
|
+
const reads = { skip };
|
|
71
|
+
if (blockAttribute === undefined)
|
|
72
|
+
numbered(tree, reads);
|
|
73
|
+
const reading = readText(tree, reads);
|
|
74
|
+
if (from === undefined) {
|
|
75
|
+
onRead?.({ reading: reading.text, removals: false });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const far = near ? outside(tree, near, blockProperty(blockAttribute ?? BLOCK_NUMBER_ATTRIBUTE)) : new Set();
|
|
79
|
+
const removals = marked(reading, from, far);
|
|
80
|
+
onRead?.({ reading: reading.text, removals });
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** `tree`'s `NUMBERED_BLOCKS`, numbered in pre-order from 1 as `data-vultus-block`; a skipped element
|
|
84
|
+
* (`isSkipped`) and all it holds left out. */
|
|
85
|
+
function numbered(tree, options) {
|
|
86
|
+
let n = 0;
|
|
87
|
+
const visit = (parent) => {
|
|
88
|
+
for (const child of parent.children) {
|
|
89
|
+
if (child.type !== 'element' || isSkipped(child, options))
|
|
90
|
+
continue;
|
|
91
|
+
if (NUMBERED_BLOCKS.has(child.tagName))
|
|
92
|
+
child.properties[OWN_PROPERTY] = ++n;
|
|
93
|
+
visit(child);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
visit(tree);
|
|
97
|
+
}
|
|
98
|
+
/** The text of `tree` whose nearest numbered block (`property`, the innermost holding it) has a
|
|
99
|
+
* number `near` refuses; none of the text in no numbered block. */
|
|
100
|
+
function outside(tree, near, property) {
|
|
101
|
+
const found = new Set();
|
|
102
|
+
const visit = (parent, block) => {
|
|
103
|
+
for (const child of parent.children) {
|
|
104
|
+
if (child.type === 'text') {
|
|
105
|
+
if (block !== undefined && !near(block))
|
|
106
|
+
found.add(child);
|
|
107
|
+
}
|
|
108
|
+
else if (child.type === 'element') {
|
|
109
|
+
const own = child.properties[property];
|
|
110
|
+
visit(child, own === undefined || own === null || own === false ? block : Number(own));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
visit(tree, undefined);
|
|
115
|
+
return found;
|
|
116
|
+
}
|
|
117
|
+
/** The changes from `old` marked in the tree `reading` was read from: its `text`, and its `places`
|
|
118
|
+
* (the text nodes, `pieces`, and each character's source offset, `map`); `far`, the text not to
|
|
119
|
+
* mark (`outside`). Whether it cut in a removed mark. */
|
|
120
|
+
function marked({ text, places: { pieces, offsets: map } }, old, far) {
|
|
121
|
+
const changes = changesIn(old, text);
|
|
122
|
+
if (!changes.length)
|
|
123
|
+
return false;
|
|
124
|
+
// A span maps from its first character to its last: a space at its edge (a new word's), or all
|
|
125
|
+
// of it (a split's), is one of the source's, of a run the one beside the word; a space inside it
|
|
126
|
+
// (a split's word), the run. A place (no width) maps to the character after it.
|
|
127
|
+
const spans = (level) => changes
|
|
128
|
+
.filter((change) => change.level === level && change.start < change.end)
|
|
129
|
+
.map(({ start, end }) => ({ start: start + 1 < end ? map[start + 1] - 1 : map[start], end: map[end - 1] + 1 }));
|
|
130
|
+
const places = (level) => changes
|
|
131
|
+
.filter((change) => change.level === level && change.start === change.end)
|
|
132
|
+
.map(({ start, text: went }) => ({ at: map[start], text: went ?? '' }));
|
|
133
|
+
const words = cutter(spans('word'));
|
|
134
|
+
const letters = cutter(spans('letters'));
|
|
135
|
+
const lone = places('word');
|
|
136
|
+
const gone = places('removed');
|
|
137
|
+
let removals = false;
|
|
138
|
+
// The cutters and the places' pointers go on past a piece left out, to the next one's.
|
|
139
|
+
let w = 0;
|
|
140
|
+
let r = 0;
|
|
141
|
+
for (const { node, parent, start } of pieces) {
|
|
142
|
+
if (between(node, parent) || far.has(node))
|
|
143
|
+
continue;
|
|
144
|
+
const end = start + node.value.length;
|
|
145
|
+
const own = words(start, end);
|
|
146
|
+
// A word removed altogether in the first piece reaching its place.
|
|
147
|
+
for (; w < lone.length && lone[w].at <= end; w++) {
|
|
148
|
+
if (lone[w].at >= start)
|
|
149
|
+
own.push({ from: lone[w].at - start, to: lone[w].at - start, goesOn: false });
|
|
150
|
+
}
|
|
151
|
+
if (!own.length)
|
|
152
|
+
continue;
|
|
153
|
+
own.sort((a, b) => a.from - b.from);
|
|
154
|
+
// The old letters in each word's cut, at its edges too, in the first piece reaching them.
|
|
155
|
+
const removed = [];
|
|
156
|
+
for (const word of own) {
|
|
157
|
+
for (; r < gone.length && gone[r].at - start <= word.to; r++) {
|
|
158
|
+
if (gone[r].at - start >= word.from)
|
|
159
|
+
removed.push({ at: gone[r].at - start, text: gone[r].text });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const i = parent.children.indexOf(node);
|
|
163
|
+
if (i < 0)
|
|
164
|
+
continue;
|
|
165
|
+
parent.children.splice(i, 1, ...markedPiece(node.value, own, letters(start, end), removed));
|
|
166
|
+
if (removed.length)
|
|
167
|
+
removals = true;
|
|
168
|
+
}
|
|
169
|
+
return removals;
|
|
170
|
+
}
|
|
171
|
+
/** Whether the text `node`, in `parent`, is between blocks (`changesPlugin`): white space whose
|
|
172
|
+
* parent holds blocks (`HOLDS_BLOCKS`, or the root), or white space right before or after a
|
|
173
|
+
* block (a loose list item's or a note's line ends around its paragraph, a tight item's before
|
|
174
|
+
* its nested list). The white space between two inline elements is not, in an item holding a
|
|
175
|
+
* nested list too. Text that is more than white space never is, even directly in a block
|
|
176
|
+
* holder (raw HTML, an app's own tree): it is read, so it is marked. */
|
|
177
|
+
function between(node, parent) {
|
|
178
|
+
if (/\S/.test(node.value))
|
|
179
|
+
return false;
|
|
180
|
+
if (parent.type === 'root' || HOLDS_BLOCKS.has(parent.tagName))
|
|
181
|
+
return true;
|
|
182
|
+
const i = parent.children.indexOf(node);
|
|
183
|
+
return [parent.children[i - 1], parent.children[i + 1]]
|
|
184
|
+
.some((sibling) => sibling?.type === 'element' && BLOCK_ELEMENTS.has(sibling.tagName));
|
|
185
|
+
}
|
|
186
|
+
/** For the pieces in document order, each piece's cuts of `spans` (in order, none overlapping): a
|
|
187
|
+
* function of the piece's place in the source, `start`-`end`, called in that order. */
|
|
188
|
+
function cutter(spans) {
|
|
189
|
+
let first = 0;
|
|
190
|
+
return (start, end) => {
|
|
191
|
+
while (first < spans.length && spans[first].end <= start)
|
|
192
|
+
first++;
|
|
193
|
+
const cuts = [];
|
|
194
|
+
for (let i = first; i < spans.length && spans[i].start < end; i++) {
|
|
195
|
+
const from = Math.max(spans[i].start, start) - start;
|
|
196
|
+
const to = Math.min(spans[i].end, end) - start;
|
|
197
|
+
if (from < to)
|
|
198
|
+
cuts.push({ from, to, goesOn: spans[i].end > end });
|
|
199
|
+
}
|
|
200
|
+
return cuts;
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/** `value` as text, its `words` (in order) each in an `<ins data-change="word">`, and its
|
|
204
|
+
* `letters` (each inside one of them) in an `<ins data-change="letters">` inside it, and the old
|
|
205
|
+
* letters that went (`removed`, each inside one of them, at an edge too) in an `<ins
|
|
206
|
+
* data-change="removed">` at their place, before the letters there; a word of no width holds
|
|
207
|
+
* only those. A replacement, old letters and the letters right after them at their place (no
|
|
208
|
+
* other old letters there in between) whose span ends in this piece, is one `<ins
|
|
209
|
+
* data-change="replaced">` around the two (`changesPlugin`). */
|
|
210
|
+
function markedPiece(value, words, letters, removed) {
|
|
211
|
+
const out = [];
|
|
212
|
+
let pos = 0;
|
|
213
|
+
let l = 0;
|
|
214
|
+
let g = 0;
|
|
215
|
+
for (const word of words) {
|
|
216
|
+
if (word.from > pos)
|
|
217
|
+
out.push(textNode(value.slice(pos, word.from)));
|
|
218
|
+
const inside = [];
|
|
219
|
+
let at = word.from;
|
|
220
|
+
const textTo = (to) => {
|
|
221
|
+
if (to > at)
|
|
222
|
+
inside.push(textNode(value.slice(at, to)));
|
|
223
|
+
at = Math.max(at, to);
|
|
224
|
+
};
|
|
225
|
+
// The next letters' mark, the word's text and its letters going on past them.
|
|
226
|
+
const takeLetters = (letter) => {
|
|
227
|
+
at = letter.to;
|
|
228
|
+
l++;
|
|
229
|
+
return changeMark('letters', [textNode(spaced(value.slice(letter.from, letter.to)))]);
|
|
230
|
+
};
|
|
231
|
+
for (;;) {
|
|
232
|
+
const gone = g < removed.length && removed[g].at <= word.to ? removed[g] : undefined;
|
|
233
|
+
const letter = l < letters.length && letters[l].from < word.to ? letters[l] : undefined;
|
|
234
|
+
if (gone && (!letter || gone.at <= letter.from)) {
|
|
235
|
+
textTo(gone.at);
|
|
236
|
+
const old = changeMark('removed', [textNode(spaced(gone.text))]);
|
|
237
|
+
g++;
|
|
238
|
+
const replaced = letter?.from === gone.at && removed[g]?.at !== gone.at && !letter.goesOn;
|
|
239
|
+
inside.push(replaced ? changeMark('replaced', [old, takeLetters(letter)]) : old);
|
|
240
|
+
}
|
|
241
|
+
else if (letter) {
|
|
242
|
+
textTo(letter.from);
|
|
243
|
+
inside.push(takeLetters(letter));
|
|
244
|
+
}
|
|
245
|
+
else
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
textTo(word.to);
|
|
249
|
+
out.push(changeMark('word', inside));
|
|
250
|
+
pos = word.to;
|
|
251
|
+
}
|
|
252
|
+
if (pos < value.length)
|
|
253
|
+
out.push(textNode(value.slice(pos)));
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
/** A text node of `value`. */
|
|
257
|
+
function textNode(value) {
|
|
258
|
+
return { type: 'text', value };
|
|
259
|
+
}
|
|
260
|
+
/** `text` with each line feed in it a space (`changesPlugin`). */
|
|
261
|
+
function spaced(text) {
|
|
262
|
+
return text.replace(/\r\n?|\n/g, ' ');
|
|
263
|
+
}
|
|
264
|
+
/** A change's mark at `level`, around `children`: an `ins` (see `changesPlugin`). */
|
|
265
|
+
function changeMark(level, children) {
|
|
266
|
+
return { type: 'element', tagName: 'ins', properties: { dataChange: level }, children };
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=rehypeChanges.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rehypeChanges.js","sourceRoot":"","sources":["../../src/changes/rehypeChanges.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,SAAS,EAAoB,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAsC,MAAM,eAAe,CAAC;AAExF;+CAC+C;AAC/C,MAAM,CAAC,MAAM,sBAAsB,GAAG,mBAAmB,CAA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root } from 'hast';
|
|
2
|
+
import { type ChangeComponents } from './ChangeMark.js';
|
|
3
|
+
import { type ChangeSpeed } from './changeDurations.js';
|
|
4
|
+
import { type BlockAttribute } from './rehypeChanges.js';
|
|
5
|
+
import { type ContainerOption } from './visibleBlocks.js';
|
|
6
|
+
/** `near` as the blocks on screen: those crossing `container` (the scrolling pane) as a switch
|
|
7
|
+
* starts, `blocks` more on each side; `rootMargin`, the observer's (a sticky bar over the pane's
|
|
8
|
+
* top). A ref object is read once, after mount: a pane that mounts later or is replaced is given
|
|
9
|
+
* as the element (a callback ref kept in state), or `near` limits nothing. */
|
|
10
|
+
export interface NearBlocks {
|
|
11
|
+
container: ContainerOption;
|
|
12
|
+
blocks: number;
|
|
13
|
+
rootMargin?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface TextChangesOptions {
|
|
16
|
+
/** The content's identity: same key, new text animates; a new key swaps plainly. */
|
|
17
|
+
key: string;
|
|
18
|
+
/** `false`: swap plainly once, under the same key too, replacing the reading. */
|
|
19
|
+
animate?: boolean;
|
|
20
|
+
/** The blocks a switch marks, by number: a function, or the blocks on screen (`NearBlocks`);
|
|
21
|
+
* none, all. Frozen per switch. */
|
|
22
|
+
near?: ((block: number) => boolean) | NearBlocks;
|
|
23
|
+
/** An existing numbering's attribute; none: the plugin numbers as `data-vultus-block`. */
|
|
24
|
+
blockAttribute?: BlockAttribute;
|
|
25
|
+
/** What the reading leaves out besides `data-vultus-change-skip`. */
|
|
26
|
+
skip?: (element: Element) => boolean;
|
|
27
|
+
/** How fast a switch's marks run (the lent mark's `speed`): `normal` by default. Taken as a
|
|
28
|
+
* switch starts and held for it, marks and timer alike. */
|
|
29
|
+
speed?: ChangeSpeed;
|
|
30
|
+
}
|
|
31
|
+
/** One tree's plugin and drawing: `rehypePlugin` for its Markdown's `rehypePlugins`, `components`
|
|
32
|
+
* merged into its `components`. */
|
|
33
|
+
export interface TextChangesPart {
|
|
34
|
+
rehypePlugin: () => (tree: Root) => void;
|
|
35
|
+
components: ChangeComponents;
|
|
36
|
+
}
|
|
37
|
+
/** The default part's plugin and drawing, the other parts (`part`), and whether a switch's marks
|
|
38
|
+
* are shown (`active`). */
|
|
39
|
+
export interface TextChanges extends TextChangesPart {
|
|
40
|
+
part(name: string): TextChangesPart;
|
|
41
|
+
active: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** The part `rehypePlugin` and `components` on the result belong to. */
|
|
44
|
+
export declare const DEFAULT_PART = "default";
|
|
45
|
+
/**
|
|
46
|
+
* What changed between two versions of a text, lit up. `text` is the whole content, all
|
|
47
|
+
* its parts; `key` its identity, the app's decision of which changes are related:
|
|
48
|
+
* - same key, new text: a switch. Each part diffs its previous reading against this one, and the
|
|
49
|
+
* switch animates;
|
|
50
|
+
* - a new key: the text appears plainly, and a running switch stops at once (its marks go in that
|
|
51
|
+
* render). This text is the start for the next related change;
|
|
52
|
+
* - same key, same text (`===`): nothing;
|
|
53
|
+
* - `animate: false`: swaps plainly once, under the same key too, and replaces the reading.
|
|
54
|
+
* The first render has nothing read before it, so nothing animates.
|
|
55
|
+
*
|
|
56
|
+
* Parts: `part(name)` gives one tree's `{ rehypePlugin, components }`; `rehypePlugin` and
|
|
57
|
+
* `components` on the result are the default part's. Each part keeps its own previous reading (the
|
|
58
|
+
* one its plugin reported for the text shown before; none, a part new in this version, all new
|
|
59
|
+
* against `''`) and diffs on its own. One switch has one number, one timer and one timeline over
|
|
60
|
+
* all its parts.
|
|
61
|
+
*
|
|
62
|
+
* A switch's timeline is decided after its parts render, before the paint: in a layout effect,
|
|
63
|
+
* from the plugins' reports (a removed mark cut in anywhere: `removals`; else `plain`), set in the
|
|
64
|
+
* hook's store, from which the marks re-render alone: the trees are not parsed again. The timer
|
|
65
|
+
* starts there, for `changeDurations()` on that timeline: the lent mark's `durations` (the
|
|
66
|
+
* `ChangeMark` in `VultusComponents` as the switch starts), at `near`'s container (else none) and
|
|
67
|
+
* the switch's speed, or vultus's defaults; at its end, the text renders without marks. A second
|
|
68
|
+
* switch within one diffs the text shown against the next, in marks of its own, and its timer
|
|
69
|
+
* replaces the first's.
|
|
70
|
+
*
|
|
71
|
+
* `speed` (`normal` by default; the lent mark's, which gives each one's times) is taken as a switch
|
|
72
|
+
* starts and held for that switch, like the near set: every mark of it (through the store) and its
|
|
73
|
+
* timer run at that speed, so they always agree. A new speed counts from the next switch on; it
|
|
74
|
+
* changes no identity and parses nothing.
|
|
75
|
+
*
|
|
76
|
+
* Identities: `rehypePlugin`, `components` and each part's object are kept while nothing changes:
|
|
77
|
+
* an app's Markdown memoises on them, and a big section takes 0.1 s or more to parse. They change as
|
|
78
|
+
* a switch starts and as it ends, and with a plain swap (a new text shown plainly): every part shown
|
|
79
|
+
* then parses again and reports its reading of the new text. A memo'd part whose own text did not
|
|
80
|
+
* change would otherwise keep its report of an older text, and diff all of itself as new at the
|
|
81
|
+
* next switch. `components` never changes. Inline `skip` and `near` functions, `near` objects and a
|
|
82
|
+
* new `speed` change nothing: `skip` and `blockAttribute` are read as a plugin runs, `near` and
|
|
83
|
+
* `speed` as a switch starts.
|
|
84
|
+
*
|
|
85
|
+
* `near`: a function of the block number, as it is; or `{ container, blocks, rootMargin? }`, the
|
|
86
|
+
* innermost blocks crossing the container (`useVisibleBlocks`) as the switch starts, `blocks` more
|
|
87
|
+
* each way. Frozen for the switch: a re-parse during the light-up keeps it. With no range (no
|
|
88
|
+
* IntersectionObserver, nothing reported yet), none: all of it marked.
|
|
89
|
+
*
|
|
90
|
+
* `active`: whether a switch's marks are shown.
|
|
91
|
+
*/
|
|
92
|
+
export declare function useTextChanges(text: string, options: TextChangesOptions): TextChanges;
|
|
93
|
+
//# sourceMappingURL=useTextChanges.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTextChanges.d.ts","sourceRoot":"","sources":["../../src/changes/useTextChanges.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAG1C,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,MAAM,EAAE,OAAO,EAAE,kBAAkB,GAAG,WAAW,CAsDrF"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
import { useVultusComponents } from '../Components.js';
|
|
3
|
+
import { changeComponents, changeStore } from './ChangeMark.js';
|
|
4
|
+
import { changeDurations } from './changeDurations.js';
|
|
5
|
+
import { BLOCK_NUMBER_ATTRIBUTE, changesPlugin } from './rehypeChanges.js';
|
|
6
|
+
import { useVisibleBlocks } from './visibleBlocks.js';
|
|
7
|
+
/** The part `rehypePlugin` and `components` on the result belong to. */
|
|
8
|
+
export const DEFAULT_PART = 'default';
|
|
9
|
+
// `useLayoutEffect` warns when it does nothing on the server; fall back to a plain effect there.
|
|
10
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
11
|
+
/**
|
|
12
|
+
* What changed between two versions of a text, lit up. `text` is the whole content, all
|
|
13
|
+
* its parts; `key` its identity, the app's decision of which changes are related:
|
|
14
|
+
* - same key, new text: a switch. Each part diffs its previous reading against this one, and the
|
|
15
|
+
* switch animates;
|
|
16
|
+
* - a new key: the text appears plainly, and a running switch stops at once (its marks go in that
|
|
17
|
+
* render). This text is the start for the next related change;
|
|
18
|
+
* - same key, same text (`===`): nothing;
|
|
19
|
+
* - `animate: false`: swaps plainly once, under the same key too, and replaces the reading.
|
|
20
|
+
* The first render has nothing read before it, so nothing animates.
|
|
21
|
+
*
|
|
22
|
+
* Parts: `part(name)` gives one tree's `{ rehypePlugin, components }`; `rehypePlugin` and
|
|
23
|
+
* `components` on the result are the default part's. Each part keeps its own previous reading (the
|
|
24
|
+
* one its plugin reported for the text shown before; none, a part new in this version, all new
|
|
25
|
+
* against `''`) and diffs on its own. One switch has one number, one timer and one timeline over
|
|
26
|
+
* all its parts.
|
|
27
|
+
*
|
|
28
|
+
* A switch's timeline is decided after its parts render, before the paint: in a layout effect,
|
|
29
|
+
* from the plugins' reports (a removed mark cut in anywhere: `removals`; else `plain`), set in the
|
|
30
|
+
* hook's store, from which the marks re-render alone: the trees are not parsed again. The timer
|
|
31
|
+
* starts there, for `changeDurations()` on that timeline: the lent mark's `durations` (the
|
|
32
|
+
* `ChangeMark` in `VultusComponents` as the switch starts), at `near`'s container (else none) and
|
|
33
|
+
* the switch's speed, or vultus's defaults; at its end, the text renders without marks. A second
|
|
34
|
+
* switch within one diffs the text shown against the next, in marks of its own, and its timer
|
|
35
|
+
* replaces the first's.
|
|
36
|
+
*
|
|
37
|
+
* `speed` (`normal` by default; the lent mark's, which gives each one's times) is taken as a switch
|
|
38
|
+
* starts and held for that switch, like the near set: every mark of it (through the store) and its
|
|
39
|
+
* timer run at that speed, so they always agree. A new speed counts from the next switch on; it
|
|
40
|
+
* changes no identity and parses nothing.
|
|
41
|
+
*
|
|
42
|
+
* Identities: `rehypePlugin`, `components` and each part's object are kept while nothing changes:
|
|
43
|
+
* an app's Markdown memoises on them, and a big section takes 0.1 s or more to parse. They change as
|
|
44
|
+
* a switch starts and as it ends, and with a plain swap (a new text shown plainly): every part shown
|
|
45
|
+
* then parses again and reports its reading of the new text. A memo'd part whose own text did not
|
|
46
|
+
* change would otherwise keep its report of an older text, and diff all of itself as new at the
|
|
47
|
+
* next switch. `components` never changes. Inline `skip` and `near` functions, `near` objects and a
|
|
48
|
+
* new `speed` change nothing: `skip` and `blockAttribute` are read as a plugin runs, `near` and
|
|
49
|
+
* `speed` as a switch starts.
|
|
50
|
+
*
|
|
51
|
+
* `near`: a function of the block number, as it is; or `{ container, blocks, rootMargin? }`, the
|
|
52
|
+
* innermost blocks crossing the container (`useVisibleBlocks`) as the switch starts, `blocks` more
|
|
53
|
+
* each way. Frozen for the switch: a re-parse during the light-up keeps it. With no range (no
|
|
54
|
+
* IntersectionObserver, nothing reported yet), none: all of it marked.
|
|
55
|
+
*
|
|
56
|
+
* `active`: whether a switch's marks are shown.
|
|
57
|
+
*/
|
|
58
|
+
export function useTextChanges(text, options) {
|
|
59
|
+
const [kept] = useState(() => {
|
|
60
|
+
const store = changeStore();
|
|
61
|
+
return { store, components: changeComponents(store), reports: new Map(), holder: { text }, phase: undefined };
|
|
62
|
+
});
|
|
63
|
+
// The options as this render has them, for the plugins as they run and the switch's effect.
|
|
64
|
+
const latest = useRef(options);
|
|
65
|
+
latest.current = options;
|
|
66
|
+
const { key, animate, near, blockAttribute, speed = 'normal' } = options;
|
|
67
|
+
// The lent mark, whose `durations` time a switch; the switch's effect reads the one of the render
|
|
68
|
+
// that starts it.
|
|
69
|
+
const { ChangeMark: mark } = useVultusComponents();
|
|
70
|
+
const nearBlocks = typeof near === 'object' ? near : undefined;
|
|
71
|
+
const visible = useVisibleBlocks(nearBlocks?.container, blockAttribute ?? BLOCK_NUMBER_ATTRIBUTE, nearBlocks?.rootMargin);
|
|
72
|
+
// Worked out as the text changes, in the render that shows it, so it never shows unlit first.
|
|
73
|
+
const [stored, setShown] = useState(() => ({ key, text, count: 0, current: undefined }));
|
|
74
|
+
let shown = stored;
|
|
75
|
+
if (key !== shown.key || text !== shown.text) {
|
|
76
|
+
const number = shown.count + 1;
|
|
77
|
+
shown = key === shown.key && animate !== false
|
|
78
|
+
? { key, text, count: number, current: { number, from: readings(kept.reports, shown.text), near: frozen(near, visible), speed } }
|
|
79
|
+
: { key, text, count: shown.count, current: undefined };
|
|
80
|
+
setShown(shown);
|
|
81
|
+
}
|
|
82
|
+
kept.holder.text = shown.text;
|
|
83
|
+
const { current } = shown;
|
|
84
|
+
// The new switch's marks drawn with its number and speed, plain until its timeline is decided.
|
|
85
|
+
if (current && kept.store.getSnapshot().number !== current.number) {
|
|
86
|
+
kept.store.set({ number: current.number, timeline: undefined, speed: current.speed }, false);
|
|
87
|
+
}
|
|
88
|
+
const phaseKey = current ?? shown.text;
|
|
89
|
+
let made = kept.phase;
|
|
90
|
+
if (!made || made.key !== phaseKey) {
|
|
91
|
+
made = phase(phaseKey, current, kept, latest);
|
|
92
|
+
kept.phase = made;
|
|
93
|
+
}
|
|
94
|
+
useIsomorphicLayoutEffect(() => {
|
|
95
|
+
if (!current)
|
|
96
|
+
return undefined;
|
|
97
|
+
const { number, speed: switchSpeed } = current;
|
|
98
|
+
const removals = Array.from(kept.reports.values()).some((report) => report.number === number && report.removals);
|
|
99
|
+
const timeline = removals ? 'removals' : 'plain';
|
|
100
|
+
kept.store.set({ number, timeline, speed: switchSpeed }, true);
|
|
101
|
+
const now = latest.current.near;
|
|
102
|
+
const container = now && typeof now === 'object' ? now.container : undefined;
|
|
103
|
+
const element = container && 'current' in container ? container.current : container;
|
|
104
|
+
const timer = setTimeout(() => {
|
|
105
|
+
setShown((then) => (then.current?.number === number ? { ...then, current: undefined } : then));
|
|
106
|
+
}, changeDurations(element ?? undefined, switchSpeed, mark)[timeline]);
|
|
107
|
+
return () => clearTimeout(timer);
|
|
108
|
+
}, [current]);
|
|
109
|
+
return made.result;
|
|
110
|
+
}
|
|
111
|
+
/** Each part's reading of `text`: the reports read for it (a part absent from it has none). */
|
|
112
|
+
function readings(reports, text) {
|
|
113
|
+
const from = new Map();
|
|
114
|
+
for (const [name, report] of reports)
|
|
115
|
+
if (report.text === text)
|
|
116
|
+
from.set(name, report.reading);
|
|
117
|
+
return from;
|
|
118
|
+
}
|
|
119
|
+
/** The near set a switch takes now: a function as it is; the blocks on screen, `blocks` more each
|
|
120
|
+
* way; none without a range. */
|
|
121
|
+
function frozen(near, visible) {
|
|
122
|
+
if (typeof near !== 'object')
|
|
123
|
+
return near;
|
|
124
|
+
const range = visible.range();
|
|
125
|
+
if (!range)
|
|
126
|
+
return undefined;
|
|
127
|
+
const { blocks } = near;
|
|
128
|
+
return (block) => range.first - blocks <= block && block <= range.last + blocks;
|
|
129
|
+
}
|
|
130
|
+
/** The objects of a phase, `key` (a switch, `current`, or the idle text): each part's plugin, with
|
|
131
|
+
* the switch's previous reading, near set and number in it, reporting to `kept`; and the result. */
|
|
132
|
+
function phase(key, current, kept, latest) {
|
|
133
|
+
const parts = new Map();
|
|
134
|
+
const number = current?.number ?? 0;
|
|
135
|
+
const part = (name) => {
|
|
136
|
+
let made = parts.get(name);
|
|
137
|
+
if (!made) {
|
|
138
|
+
const from = current ? current.from.get(name) ?? '' : undefined;
|
|
139
|
+
const skip = (element) => latest.current.skip?.(element) ?? false;
|
|
140
|
+
const onRead = (report) => {
|
|
141
|
+
kept.reports.set(name, { ...report, text: kept.holder.text, number });
|
|
142
|
+
};
|
|
143
|
+
made = {
|
|
144
|
+
rehypePlugin: () => changesPlugin({ from, near: current?.near, blockAttribute: latest.current.blockAttribute, skip, onRead })(),
|
|
145
|
+
components: kept.components,
|
|
146
|
+
};
|
|
147
|
+
parts.set(name, made);
|
|
148
|
+
}
|
|
149
|
+
return made;
|
|
150
|
+
};
|
|
151
|
+
const { rehypePlugin, components } = part(DEFAULT_PART);
|
|
152
|
+
return { key, parts, result: { rehypePlugin, components, part, active: current !== undefined } };
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=useTextChanges.js.map
|