vultus-core 0.5.7 → 0.5.9
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 +199 -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/action-core.d.ts +1 -0
- package/dist/action-core.d.ts.map +1 -1
- package/dist/action-core.js +3 -0
- package/dist/action-core.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/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/useAction.d.ts.map +1 -1
- package/dist/useAction.js +1 -0
- package/dist/useAction.js.map +1 -1
- package/package.json +13 -1
- package/src/Components.tsx +45 -0
- package/src/action-core.ts +4 -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
- package/src/types.ts +4 -0
- package/src/useAction.ts +1 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
/** Block numbers from `first` to `last`, both included. */
|
|
4
|
+
export interface BlockRange {
|
|
5
|
+
first: number;
|
|
6
|
+
last: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Where the blocks are: an element, a ref object holding one, or none yet. */
|
|
10
|
+
export type ContainerOption = HTMLElement | null | { readonly current: HTMLElement | null };
|
|
11
|
+
|
|
12
|
+
/** `element`'s number in `attribute`; none for a missing or non-numeric one. */
|
|
13
|
+
function numberOf(element: Element, attribute: string): number | undefined {
|
|
14
|
+
const value = element.getAttribute(attribute);
|
|
15
|
+
const number = value === null || value.trim() === '' ? NaN : Number(value);
|
|
16
|
+
return Number.isFinite(number) ? number : undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The range of the innermost of `elements` (numbered blocks crossing the band, in any order): each
|
|
21
|
+
* one containing another of them is dropped, so a quote or an outer list item spanning the band
|
|
22
|
+
* pulls nothing back (a quote of 200 paragraphs, 150 to 153 in the band, gives 150 to 153, not 1
|
|
23
|
+
* to 153); the first and last of the rest's numbers. A block holding blocks only outside the band
|
|
24
|
+
* counts: a tight list item whose first line is in it, its nested items below. None numbered (by
|
|
25
|
+
* `attribute`), none. It works on a set of elements (those the observer reports crossing the band),
|
|
26
|
+
* not on laid-out boxes.
|
|
27
|
+
*/
|
|
28
|
+
export function innermostRange(elements: Iterable<Element>, attribute: string): BlockRange | undefined {
|
|
29
|
+
const blocks = Array.from(elements).filter((element) => numberOf(element, attribute) !== undefined);
|
|
30
|
+
let range: BlockRange | undefined;
|
|
31
|
+
for (const block of blocks) {
|
|
32
|
+
if (blocks.some((other) => other !== block && block.contains(other))) continue;
|
|
33
|
+
const number = numberOf(block, attribute)!;
|
|
34
|
+
range = range ? { first: Math.min(range.first, number), last: Math.max(range.last, number) } : { first: number, last: number };
|
|
35
|
+
}
|
|
36
|
+
return range;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The visible blocks as `useVisibleBlocks` tracks them. */
|
|
40
|
+
export interface VisibleBlocks {
|
|
41
|
+
/** The range of the innermost blocks crossing the container now (`innermostRange`); none without
|
|
42
|
+
* an IntersectionObserver or a container, before its first report, or with none crossing. */
|
|
43
|
+
range(): BlockRange | undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** What one tracking sees: its container, the numbering's attribute, and the blocks reported
|
|
47
|
+
* intersecting. */
|
|
48
|
+
interface Seen {
|
|
49
|
+
root: HTMLElement | null;
|
|
50
|
+
attribute: string;
|
|
51
|
+
visible: Set<Element>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The numbered blocks (`attribute`) crossing `container`, tracked with an IntersectionObserver
|
|
56
|
+
* whose root is the container (`rootMargin`: a sticky bar over its top, say), for the near set a
|
|
57
|
+
* switch takes (`useTextChanges`' `near`). Every numbered block inside the container is observed,
|
|
58
|
+
* those rendered later too (a MutationObserver sees them come and go). Set up in a passive effect,
|
|
59
|
+
* when an ancestor's ref is attached; again when the container (the ref object or the element), the
|
|
60
|
+
* attribute or the margin change. One object for the hook's life: read `range()` when it is needed.
|
|
61
|
+
* Without an IntersectionObserver (an old browser, jsdom) or a container, no range.
|
|
62
|
+
*
|
|
63
|
+
* The container is the scrolling element (the pane), the observer's root: blocks scrolled out of it
|
|
64
|
+
* are clipped away. Given an element that does not scroll (the content inside the pane), every block
|
|
65
|
+
* in it counts as crossing, and the near limit does nothing. A ref object is read when the effect
|
|
66
|
+
* runs, so a container that appears or changes later is given as the element (from a callback ref,
|
|
67
|
+
* kept in state).
|
|
68
|
+
*/
|
|
69
|
+
export function useVisibleBlocks(container: ContainerOption | undefined, attribute: string, rootMargin?: string): VisibleBlocks {
|
|
70
|
+
const [{ seen, blocks }] = useState(() => {
|
|
71
|
+
const seen: Seen = { root: null, attribute, visible: new Set() };
|
|
72
|
+
const blocks: VisibleBlocks = {
|
|
73
|
+
range() {
|
|
74
|
+
const { root } = seen;
|
|
75
|
+
if (!root) return undefined;
|
|
76
|
+
return innermostRange(Array.from(seen.visible).filter((block) => block.isConnected && root.contains(block)), seen.attribute);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
return { seen, blocks };
|
|
80
|
+
});
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
const root = container && 'current' in container ? container.current : container ?? null;
|
|
83
|
+
if (!root || typeof IntersectionObserver === 'undefined') return undefined;
|
|
84
|
+
const selector = `[${attribute}]`;
|
|
85
|
+
const intersection = new IntersectionObserver((entries) => {
|
|
86
|
+
for (const entry of entries) {
|
|
87
|
+
if (entry.isIntersecting) seen.visible.add(entry.target);
|
|
88
|
+
else seen.visible.delete(entry.target);
|
|
89
|
+
}
|
|
90
|
+
}, { root, rootMargin });
|
|
91
|
+
// The numbered blocks at or under a node.
|
|
92
|
+
const blocksAt = (node: Node): Element[] => {
|
|
93
|
+
if (node.nodeType !== Node.ELEMENT_NODE) return [];
|
|
94
|
+
const element = node as Element;
|
|
95
|
+
return [...(element.matches(selector) ? [element] : []), ...element.querySelectorAll(selector)];
|
|
96
|
+
};
|
|
97
|
+
for (const block of root.querySelectorAll(selector)) intersection.observe(block);
|
|
98
|
+
// In order, so that a block moved (taken out, put back) stays observed.
|
|
99
|
+
const mutation = new MutationObserver((records) => {
|
|
100
|
+
for (const record of records) {
|
|
101
|
+
for (const node of record.removedNodes) {
|
|
102
|
+
for (const block of blocksAt(node)) {
|
|
103
|
+
intersection.unobserve(block);
|
|
104
|
+
seen.visible.delete(block);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for (const node of record.addedNodes) for (const block of blocksAt(node)) intersection.observe(block);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
mutation.observe(root, { childList: true, subtree: true });
|
|
111
|
+
seen.root = root;
|
|
112
|
+
seen.attribute = attribute;
|
|
113
|
+
return () => {
|
|
114
|
+
intersection.disconnect();
|
|
115
|
+
mutation.disconnect();
|
|
116
|
+
seen.visible.clear();
|
|
117
|
+
seen.root = null;
|
|
118
|
+
};
|
|
119
|
+
}, [container, attribute, rootMargin, seen]);
|
|
120
|
+
return blocks;
|
|
121
|
+
}
|
package/src/changes.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// The `/changes` subpath: `import { ... } from 'vultus-core/changes'`.
|
|
2
|
+
// Text changes: what changed between two versions of a text, found and lit up, drawn by the
|
|
3
|
+
// `ChangeMark` an app lends (`VultusComponents`), else by vultus's static fallback. Kept out of the
|
|
4
|
+
// main entry, which stays light: only an app that shows text changes loads diff-match-patch.
|
|
5
|
+
export { changesIn, type Change, type ChangeLevel } from './changes/changesIn.js';
|
|
6
|
+
export { readText, type Reading, type ReadTextOptions, type TextPiece, type TextPlaces } from './changes/readText.js';
|
|
7
|
+
export { changeDurations, type ChangeDurations, type ChangeSpeed } from './changes/changeDurations.js';
|
|
8
|
+
export {
|
|
9
|
+
useTextChanges, type NearBlocks, type TextChanges, type TextChangesOptions, type TextChangesPart,
|
|
10
|
+
} from './changes/useTextChanges.js';
|
|
11
|
+
export { ChangingText, type ChangingTextProps } from './changes/ChangingText.js';
|
|
12
|
+
export type { ChangeComponents } from './changes/ChangeMark.js';
|
|
13
|
+
export type { BlockAttribute } from './changes/rehypeChanges.js';
|
|
14
|
+
export type { ContainerOption } from './changes/visibleBlocks.js';
|
package/src/index.ts
CHANGED
|
@@ -78,4 +78,8 @@ export { MessageSinkContext, useMessageSink, type MessageSink } from './MessageS
|
|
|
78
78
|
export {
|
|
79
79
|
VultusComponentsContext, useVultusComponents, SpinnerScope, useSpinnerKey,
|
|
80
80
|
type VultusComponents, type SpinnerProps, type SpinnerComponent,
|
|
81
|
+
type ChangeMarkVariant, type ChangeMarkProps, type ChangeMarkComponent,
|
|
81
82
|
} from './Components.js';
|
|
83
|
+
// Types only, so a lender can name its mark's props here; the code stays behind `vultus-core/changes`.
|
|
84
|
+
export type { ChangeDurations, ChangeSpeed } from './changes/changeDurations.js';
|
|
85
|
+
export type { ChangeLevel } from './changes/changesIn.js';
|
package/src/types.ts
CHANGED
|
@@ -67,6 +67,8 @@ export interface ActionOptions<TArgs = void, TResult = void, RouteId extends str
|
|
|
67
67
|
variant?: Thunk<'default' | 'primary' | 'danger'>;
|
|
68
68
|
enabled?: Thunk<Decision>;
|
|
69
69
|
invisible?: Thunk<boolean>;
|
|
70
|
+
/** The expanded state of what the action's control shows or hides, rendered as `aria-expanded`; absent: not a disclosure control, so no attribute. */
|
|
71
|
+
ariaExpanded?: Thunk<boolean>;
|
|
70
72
|
confirmation?: Thunk<ConfirmationSpec | undefined>;
|
|
71
73
|
errorRoute?: RouteId;
|
|
72
74
|
fire: (args: TArgs, ctx: ExecutionContext) => Promise<TResult> | TResult;
|
|
@@ -99,6 +101,8 @@ export interface ActionStatus<TArgs = void> {
|
|
|
99
101
|
disabled: boolean;
|
|
100
102
|
reason?: string;
|
|
101
103
|
invisible: boolean;
|
|
104
|
+
/** The options' `ariaExpanded`, resolved. Optional so hand-built statuses stay valid; absent: no `aria-expanded`. */
|
|
105
|
+
ariaExpanded?: boolean;
|
|
102
106
|
confirmation?: ConfirmationSpec;
|
|
103
107
|
/**
|
|
104
108
|
* Messages of the last run: reported through the ExecutionContext, or the thrown error.
|