vultus-core 0.5.6 → 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.
Files changed (95) hide show
  1. package/README.md +232 -9
  2. package/dist/Components.d.ts +41 -0
  3. package/dist/Components.d.ts.map +1 -1
  4. package/dist/Components.js.map +1 -1
  5. package/dist/argStore.d.ts +9 -0
  6. package/dist/argStore.d.ts.map +1 -1
  7. package/dist/argStore.js.map +1 -1
  8. package/dist/changes/ChangeMark.d.ts +85 -0
  9. package/dist/changes/ChangeMark.d.ts.map +1 -0
  10. package/dist/changes/ChangeMark.js +113 -0
  11. package/dist/changes/ChangeMark.js.map +1 -0
  12. package/dist/changes/ChangingText.d.ts +20 -0
  13. package/dist/changes/ChangingText.d.ts.map +1 -0
  14. package/dist/changes/ChangingText.js +31 -0
  15. package/dist/changes/ChangingText.js.map +1 -0
  16. package/dist/changes/FallbackMark.d.ts +20 -0
  17. package/dist/changes/FallbackMark.d.ts.map +1 -0
  18. package/dist/changes/FallbackMark.js +53 -0
  19. package/dist/changes/FallbackMark.js.map +1 -0
  20. package/dist/changes/blocks.d.ts +15 -0
  21. package/dist/changes/blocks.d.ts.map +1 -0
  22. package/dist/changes/blocks.js +24 -0
  23. package/dist/changes/blocks.js.map +1 -0
  24. package/dist/changes/changeDurations.d.ts +21 -0
  25. package/dist/changes/changeDurations.d.ts.map +1 -0
  26. package/dist/changes/changeDurations.js +18 -0
  27. package/dist/changes/changeDurations.js.map +1 -0
  28. package/dist/changes/changesIn.d.ts +88 -0
  29. package/dist/changes/changesIn.d.ts.map +1 -0
  30. package/dist/changes/changesIn.js +470 -0
  31. package/dist/changes/changesIn.js.map +1 -0
  32. package/dist/changes/readText.d.ts +45 -0
  33. package/dist/changes/readText.d.ts.map +1 -0
  34. package/dist/changes/readText.js +99 -0
  35. package/dist/changes/readText.js.map +1 -0
  36. package/dist/changes/rehypeChanges.d.ts +81 -0
  37. package/dist/changes/rehypeChanges.d.ts.map +1 -0
  38. package/dist/changes/rehypeChanges.js +268 -0
  39. package/dist/changes/rehypeChanges.js.map +1 -0
  40. package/dist/changes/useTextChanges.d.ts +93 -0
  41. package/dist/changes/useTextChanges.d.ts.map +1 -0
  42. package/dist/changes/useTextChanges.js +154 -0
  43. package/dist/changes/useTextChanges.js.map +1 -0
  44. package/dist/changes/visibleBlocks.d.ts +42 -0
  45. package/dist/changes/visibleBlocks.d.ts.map +1 -0
  46. package/dist/changes/visibleBlocks.js +104 -0
  47. package/dist/changes/visibleBlocks.js.map +1 -0
  48. package/dist/changes.d.ts +9 -0
  49. package/dist/changes.d.ts.map +1 -0
  50. package/dist/changes.js +10 -0
  51. package/dist/changes.js.map +1 -0
  52. package/dist/field.d.ts +15 -2
  53. package/dist/field.d.ts.map +1 -1
  54. package/dist/field.js +8 -1
  55. package/dist/field.js.map +1 -1
  56. package/dist/index.d.ts +3 -1
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js.map +1 -1
  59. package/dist/kindHooks.d.ts +1 -1
  60. package/dist/kinds.d.ts +12 -5
  61. package/dist/kinds.d.ts.map +1 -1
  62. package/dist/kinds.js.map +1 -1
  63. package/dist/useArg.d.ts +9 -2
  64. package/dist/useArg.d.ts.map +1 -1
  65. package/dist/useArg.js +4 -2
  66. package/dist/useArg.js.map +1 -1
  67. package/dist/useFieldGroup.d.ts +2 -1
  68. package/dist/useFieldGroup.d.ts.map +1 -1
  69. package/dist/useFieldGroup.js +5 -2
  70. package/dist/useFieldGroup.js.map +1 -1
  71. package/dist/useStoredField.d.ts +14 -5
  72. package/dist/useStoredField.d.ts.map +1 -1
  73. package/dist/useStoredField.js +20 -7
  74. package/dist/useStoredField.js.map +1 -1
  75. package/package.json +13 -1
  76. package/src/Components.tsx +45 -0
  77. package/src/argStore.ts +7 -0
  78. package/src/changes/ChangeMark.tsx +165 -0
  79. package/src/changes/ChangingText.tsx +46 -0
  80. package/src/changes/FallbackMark.tsx +59 -0
  81. package/src/changes/blocks.ts +25 -0
  82. package/src/changes/changeDurations.ts +31 -0
  83. package/src/changes/changesIn.ts +479 -0
  84. package/src/changes/readText.ts +123 -0
  85. package/src/changes/rehypeChanges.ts +302 -0
  86. package/src/changes/useTextChanges.ts +239 -0
  87. package/src/changes/visibleBlocks.ts +121 -0
  88. package/src/changes.ts +14 -0
  89. package/src/field.ts +23 -3
  90. package/src/index.ts +4 -0
  91. package/src/kindHooks.ts +1 -1
  92. package/src/kinds.ts +17 -7
  93. package/src/useArg.ts +11 -3
  94. package/src/useFieldGroup.ts +5 -2
  95. package/src/useStoredField.ts +21 -8
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/field.ts CHANGED
@@ -63,6 +63,16 @@ export interface FieldBaseOptions<TValue, Ctx = any> {
63
63
  /** Changeable? Default true, optionally with a reason. Use `enabled` or `disabled`, not contradicting each other. */
64
64
  enabled?: ValueOrFn<Decision, Ctx>;
65
65
  disabled?: ValueOrFn<Decision, Ctx>;
66
+ /**
67
+ * Busy for a reason of the app's: while this says so, the field is pending exactly as during a
68
+ * commit (a request is refused with a warning; those widgets with a spinner show it), e.g.
69
+ * while the page has not yet shown what the field's last change asked for. Default false; in
70
+ * every storage mode, and with `commit`, a running commit makes the field pending too. Widgets
71
+ * lock a pending field: it keeps the keyboard focus and is said to be unavailable, but a change
72
+ * (a click, a key, typing, a drag) does nothing. An input being typed into stays focused and
73
+ * keeps a draft not yet requested; it is read-only until pending ends.
74
+ */
75
+ pending?: ValueOrFn<boolean, Ctx>;
66
76
  /**
67
77
  * What each value means right now, keyed by value (for booleans `true` / `false`, and `mixed`
68
78
  * where the field can show it). Shown with the current value, next to `description`, which
@@ -107,9 +117,12 @@ export interface FieldControls<TValue, TRequest = TValue> {
107
117
  enabled: boolean;
108
118
  whyDisabled: string | undefined;
109
119
  value: TValue;
110
- /** Request a new value. Ignored (with a warning) when disabled or while a commit is pending. */
120
+ /** Request a new value. Ignored (with a warning) when disabled or while the field is pending. */
111
121
  setValue(next: TRequest): void;
112
- /** A commit is running. */
122
+ /**
123
+ * A commit is running, or the field's `pending` option says the app is busy with it. Widgets
124
+ * lock the field meanwhile: focusable, said to be unavailable, changes ignored.
125
+ */
113
126
  pending: boolean;
114
127
  /** The field stores through `commit` — true exactly in commit storage mode, so a widget can hold back requests while typing. */
115
128
  commits: boolean;
@@ -196,6 +209,7 @@ export function useFieldCore<TValue, TRequest extends TValue = TValue, RouteId e
196
209
  );
197
210
  const enabled = getVerdict(enabledDecision, true);
198
211
  const whyDisabled = enabled ? undefined : getReason(enabledDecision);
212
+ const busy = resolveIn<boolean>(opts.pending, false, ctx, where('pending'));
199
213
 
200
214
  // Hooks run unconditionally; the setter action is only fired in commit mode.
201
215
  const setter = useAction<TRequest, void, RouteId>(
@@ -211,7 +225,7 @@ export function useFieldCore<TValue, TRequest extends TValue = TValue, RouteId e
211
225
  );
212
226
 
213
227
  const value = mode === 'inside' ? inner : (opts.value as TValue);
214
- const pending = mode === 'commit' && setter.pending;
228
+ const pending = busy || (mode === 'commit' && setter.pending);
215
229
  const valueDescription = (opts.valueDescriptions as Record<string, string | undefined> | undefined)?.[String(value)];
216
230
 
217
231
  const setValue = (next: TRequest) => {
@@ -219,6 +233,12 @@ export function useFieldCore<TValue, TRequest extends TValue = TValue, RouteId e
219
233
  console.warn(`Field '${opts.id}': disabled — ${whyDisabled ?? '(no reason)'}`);
220
234
  return;
221
235
  }
236
+ // A running commit refuses a second request itself (its action is pending); the app's own
237
+ // pending is refused here, in every storage mode.
238
+ if (busy) {
239
+ console.warn(`Field '${opts.id}': pending — request ignored`);
240
+ return;
241
+ }
222
242
  if (mode === 'inside') setInner(next);
223
243
  else if (mode === 'local') opts.onChange!(next);
224
244
  else (setter.fire as (args: TRequest) => void)(next);
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/kindHooks.ts CHANGED
@@ -13,7 +13,7 @@ import { useTextField } from './useTextField.js';
13
13
  /**
14
14
  * A definition in its outside form, as its kind's hook takes it: without `initialValue` (whoever
15
15
  * keeps the value keeps its default too), with the value, its writer, and the link a value
16
- * amounts to where whoever keeps it has one.
16
+ * amounts to where whoever keeps it has one. `permanent` stays on it, and the hook does not read it.
17
17
  */
18
18
  export type KindFieldOptions = OutsideForm<FieldDefinition>;
19
19
 
package/src/kinds.ts CHANGED
@@ -17,17 +17,26 @@ import { manyOfTreeInitialValue, type ManyOfTreeOwnOptions, type useManyOfTreeFi
17
17
  import { booleanType, numberType, rangeType, stringSetType, stringType, type ValueType } from './valueTypes.js';
18
18
 
19
19
  /**
20
- * A definition of kind `K`: the kind's hook options in their inside form, with `kind` required.
21
- * It has no `value`, `onChange` or `commit`: where the value lives is chosen by whoever runs the
22
- * field, and a store holds the definition's initial value as its default.
20
+ * A definition of kind `K`: the kind's hook options in their inside form, with `kind` required,
21
+ * plus `permanent`, which is for the store: it reaches the hook with the rest, unread. It has no
22
+ * `value`, `onChange` or `commit`: where the value lives is chosen by whoever runs the field, and a
23
+ * store holds the definition's initial value as its default.
23
24
  */
24
25
  type Definition<K extends FieldKind, V, Own, Ctx> =
25
- { kind: K } & FieldBaseOptions<V, Ctx> & { initialValue?: ValueOrFn<V, Ctx> } & Own;
26
+ { kind: K } & FieldBaseOptions<V, Ctx> & {
27
+ initialValue?: ValueOrFn<V, Ctx>;
28
+ /**
29
+ * Whether the value, kept in a store, stays when the store goes somewhere else (ArgDefinition's
30
+ * `permanent`). Default false. No kind's hook reads it.
31
+ */
32
+ permanent?: boolean;
33
+ } & Own;
26
34
 
27
35
  /**
28
36
  * A field as plain data (a module's filters, a dialog's options), tagged by its kind. Narrowed
29
- * by `kind`, a definition is its kind's hook options as they are: `useOneOfField(definition)`.
30
- * Its functions receive the field context `Ctx` (see FieldContextProvider).
37
+ * by `kind`, a definition is its kind's hook options as they are, but for `permanent`, which no
38
+ * hook reads: `useOneOfField(definition)`. Its functions receive the field context `Ctx` (see
39
+ * FieldContextProvider).
31
40
  */
32
41
  export type FieldDefinition<Ctx = any> =
33
42
  | Definition<'boolean', boolean, {}, Ctx>
@@ -45,7 +54,8 @@ type HookOptions<H extends (...args: any[]) => unknown> = Parameters<H>[0];
45
54
 
46
55
  /**
47
56
  * A compile-time check, the gate for "a definition is its kind's hook options as they are": if a
48
- * hook's options and the definition drift apart, `tsc` fails here.
57
+ * hook's options and the definition drift apart, `tsc` fails here. It checks assignability, so a
58
+ * key no hook takes passes: `permanent` is the one there is.
49
59
  */
50
60
  type DefinitionsFitTheirHooks = [
51
61
  Fits<DefinitionOf<'boolean'>, HookOptions<typeof useBoolField>>,
package/src/useArg.ts CHANGED
@@ -6,11 +6,17 @@ import { resolveIn } from './resolve.js';
6
6
  import type { ValueOrFn } from './types.js';
7
7
  import type { ValueType } from './valueTypes.js';
8
8
 
9
- /** An argument's shape (moxb's url-arg definition, without a `permanent` flag — that is a router concern). */
9
+ /** An argument's shape (moxb's url-arg definition). */
10
10
  export interface ArgDefinition<T, Ctx = any> {
11
11
  key: string;
12
12
  valueType: ValueType<T>;
13
13
  default: ValueOrFn<T, Ctx>;
14
+ /**
15
+ * Whether the argument stays when its store goes somewhere else (a URL store, when a path token
16
+ * changes). Default false. Said only here: the hooks tell the store (ArgStore.declare), and a
17
+ * store that never drops a key does not ask.
18
+ */
19
+ permanent?: boolean;
14
20
  }
15
21
 
16
22
  export interface ArgControls<T> {
@@ -33,7 +39,8 @@ export function rawFor<T>(value: T, defaultValue: T, valueType: ValueType<T>): s
33
39
 
34
40
  /**
35
41
  * An argument read from and written to the nearest ArgStore. An absent key reads as the default;
36
- * writing the default removes the key.
42
+ * writing the default removes the key. It tells the store the argument's permanence as it renders
43
+ * (ArgStore.declare).
37
44
  *
38
45
  * `parse(raw)` is memoized on `[raw, key, valueType]` (not the default, which may be a context
39
46
  * function with its own, independently changing inputs): re-rendering with the same raw value
@@ -44,8 +51,9 @@ export function rawFor<T>(value: T, defaultValue: T, valueType: ValueType<T>): s
44
51
  * effects keyed on `value` do not re-run just because nothing meaningful changed.
45
52
  */
46
53
  export function useArg<T>(definition: ArgDefinition<T>): ArgControls<T> {
47
- const { key, valueType, default: defaultOrFn } = definition;
54
+ const { key, valueType, default: defaultOrFn, permanent = false } = definition;
48
55
  const store = useArgStore();
56
+ store.declare?.(key, { permanent });
49
57
  const ctx = useFieldContext();
50
58
  // Called on the store, not passed detached: a store written as a class uses `this`.
51
59
  const subscribe = useCallback((listener: () => void) => store.subscribe(listener), [store]);
@@ -7,7 +7,7 @@ import { createGroupValues, type GroupValues } from './fieldGroupValues.js';
7
7
  import { useKindField } from './kindHooks.js';
8
8
  import { valueTypeOf, type FieldDefinition } from './kinds.js';
9
9
  import { rawFor } from './useArg.js';
10
- import { sameValue } from './useStoredField.js';
10
+ import { declareFields, sameValue } from './useStoredField.js';
11
11
  import type { ValueType } from './valueTypes.js';
12
12
 
13
13
  /** A field group's fields as one component runs them (see useFieldGroup). */
@@ -165,6 +165,8 @@ interface Link {
165
165
  */
166
166
  function useStoredValues(definitions: readonly FieldDefinition[], appCtx: FieldCtx): Keeping {
167
167
  const store = useArgStore();
168
+ // The definitions as they are now, not the ids below: their contents may change across renders.
169
+ declareFields(store, definitions);
168
170
  // The first render's ids: the shape check keeps them the same.
169
171
  const [ids] = useState(() => definitions.map((definition) => definition.id));
170
172
  // Called on the store, not passed detached: a store written as a class uses `this`.
@@ -265,7 +267,8 @@ function useStoredValues(definitions: readonly FieldDefinition[], appCtx: FieldC
265
267
  * parse, or falls back, warns once, as useStoredField's does: an unparsable one reads as the
266
268
  * current default. A raw value the group itself wrote does not warn (a write chained after
267
269
  * another field's in one event may fall back at once). `resetAll(method = 'replace')` removes
268
- * every field's key in one write.
270
+ * every field's key in one write. The group tells the store every field's permanence as it renders
271
+ * (ArgStore.declare), a hidden field's too.
269
272
  *
270
273
  * Each field runs its kind's hook with the definition in its outside form (the group's value and
271
274
  * writer), with the group's context as its field context, so its controls are the kind's own,
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useMemo, useRef, useSyncExternalStore } from 'react';
2
- import type { UpdateMethod } from './argStore.js';
2
+ import type { ArgStore, UpdateMethod } from './argStore.js';
3
3
  import { useArgStore } from './ArgStoreContext.js';
4
4
  import { useFieldContext, type FieldCtx } from './FieldContext.js';
5
5
  import { initialValueOf, sanitizeStored, valueTypeOf, type FieldDefinition } from './kinds.js';
@@ -8,19 +8,30 @@ import type { ValueType } from './valueTypes.js';
8
8
 
9
9
  /**
10
10
  * The argument a field's value is stored as: the definition's `id` as key, its kind's value
11
- * type, and its initial value, resolved with the field context, as default — a value, so the
12
- * argument layer compares against the same default the field starts at. A mixed-boolean field
13
- * shows an aggregate of other data and cannot be stored: it throws, naming the field.
11
+ * type, its initial value, resolved with the field context, as default — a value, so the
12
+ * argument layer compares against the same default the field starts at — and its `permanent`, a
13
+ * boolean, so a definition saying `false` and one saying nothing make the same argument. A
14
+ * mixed-boolean field shows an aggregate of other data and cannot be stored: it throws, naming
15
+ * the field.
14
16
  */
15
17
  export function argOf(definition: FieldDefinition, ctx: FieldCtx): ArgDefinition<any> {
16
- const { id, kind } = definition;
18
+ const { id, kind, permanent = false } = definition;
17
19
  let valueType: ValueType<any>;
18
20
  try {
19
21
  valueType = valueTypeOf(kind);
20
22
  } catch (error) {
21
23
  throw new Error(`Field '${id}': ${(error as Error).message}`);
22
24
  }
23
- return { key: id, valueType, default: initialValueOf(definition, ctx) };
25
+ return { key: id, valueType, default: initialValueOf(definition, ctx), permanent };
26
+ }
27
+
28
+ /**
29
+ * Tells the store each definition's permanence as it renders (ArgStore.declare), a hidden field's
30
+ * too: for the hooks that read the store themselves, not through useArg. Exported for the field
31
+ * group (useFieldGroup); not part of the package's API.
32
+ */
33
+ export function declareFields(store: ArgStore, definitions: readonly FieldDefinition[]): void {
34
+ for (const { id, permanent = false } of definitions) store.declare?.(id, { permanent });
24
35
  }
25
36
 
26
37
  function silently(_message: string): void {}
@@ -95,7 +106,7 @@ export function useStoredField<D extends FieldDefinition>(definition: D): Stored
95
106
  const argRef = useRef<ArgDefinition<any> | undefined>(undefined);
96
107
  const previousArg = argRef.current;
97
108
  const arg = previousArg !== undefined && previousArg.key === fresh.key && previousArg.valueType === fresh.valueType
98
- && fresh.valueType.isEqual(previousArg.default, fresh.default)
109
+ && previousArg.permanent === fresh.permanent && fresh.valueType.isEqual(previousArg.default, fresh.default)
99
110
  ? previousArg
100
111
  : fresh;
101
112
  argRef.current = arg;
@@ -178,10 +189,12 @@ function readField(definition: FieldDefinition, raw: string | null, ctx: FieldCt
178
189
  * subscription for all of them: its snapshot is their raw values joined into one string, which
179
190
  * changes exactly when one of them does. It is a summary, so it never warns (the fields do).
180
191
  * Each field is re-read only when its raw value, its definition or the field context changes,
181
- * so typing into one field does not re-read its siblings.
192
+ * so typing into one field does not re-read its siblings. It tells the store every field's
193
+ * permanence as it renders (ArgStore.declare).
182
194
  */
183
195
  export function useStoredFields(definitions: readonly FieldDefinition[]): StoredFields {
184
196
  const store = useArgStore();
197
+ declareFields(store, definitions);
185
198
  const ctx = useFieldContext();
186
199
  // Called on the store, not passed detached: a store written as a class uses `this`.
187
200
  const subscribe = useCallback((listener: () => void) => store.subscribe(listener), [store]);