grayling 0.0.0-stage → 0.1.1

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 (55) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +268 -2
  3. package/dist/index.d.ts +290 -0
  4. package/dist/index.js +50 -0
  5. package/dist/node/archive.d.ts +4 -0
  6. package/dist/node/archive.js +115 -0
  7. package/dist/node/client.d.ts +188 -0
  8. package/dist/node/client.js +669 -0
  9. package/dist/node/connection.d.ts +56 -0
  10. package/dist/node/connection.js +240 -0
  11. package/dist/node/errors.d.ts +14 -0
  12. package/dist/node/errors.js +13 -0
  13. package/dist/node/handler.d.ts +56 -0
  14. package/dist/node/handler.js +208 -0
  15. package/dist/node/index.d.ts +15 -0
  16. package/dist/node/index.js +14 -0
  17. package/dist/node/paths.d.ts +51 -0
  18. package/dist/node/paths.js +115 -0
  19. package/dist/node/program.d.ts +74 -0
  20. package/dist/node/program.js +385 -0
  21. package/dist/node/state.d.ts +7 -0
  22. package/dist/node/state.js +12 -0
  23. package/dist/node/suggest.d.ts +13 -0
  24. package/dist/node/suggest.js +87 -0
  25. package/dist/node/types.d.ts +142 -0
  26. package/dist/node/types.js +13 -0
  27. package/dist/node/version.d.ts +10 -0
  28. package/dist/node/version.js +50 -0
  29. package/dist/node/wire.d.ts +32 -0
  30. package/dist/node/wire.js +44 -0
  31. package/dist/react/index.d.ts +121 -0
  32. package/dist/react/index.js +205 -0
  33. package/dist/web/announce.d.ts +51 -0
  34. package/dist/web/announce.js +141 -0
  35. package/dist/web/attach.d.ts +77 -0
  36. package/dist/web/attach.js +89 -0
  37. package/dist/web/box.d.ts +57 -0
  38. package/dist/web/box.js +150 -0
  39. package/dist/web/dom.d.ts +80 -0
  40. package/dist/web/dom.js +265 -0
  41. package/dist/web/index.d.ts +8 -0
  42. package/dist/web/index.js +8 -0
  43. package/dist/web/logic.d.ts +308 -0
  44. package/dist/web/logic.js +467 -0
  45. package/dist/web/overlay.d.ts +56 -0
  46. package/dist/web/overlay.js +211 -0
  47. package/dist/web/style.css +193 -0
  48. package/dist/web/suggester.d.ts +189 -0
  49. package/dist/web/suggester.js +696 -0
  50. package/dist/web/ui.d.ts +65 -0
  51. package/dist/web/ui.js +32 -0
  52. package/dist/web/usage.d.ts +39 -0
  53. package/dist/web/usage.js +84 -0
  54. package/package.json +89 -4
  55. package/program.json +35 -0
@@ -0,0 +1,65 @@
1
+ /** The class a text box carries while suggestions are on in it (and loses when they are off). Programs that read a page's accessibility tree see it and leave that box alone. */
2
+ export declare const BOX_CLASS = "grayling-box";
3
+ /** Every visible string and aria-label, as functions so that a word can go anywhere in a sentence. */
4
+ export interface Labels {
5
+ /** The fix chip's button: its accessible name. */
6
+ replace: (word: string) => string;
7
+ /** The fix chip's ×: its accessible name. */
8
+ keep: () => string;
9
+ /** The chip over a text: its button's accessible name. */
10
+ complete: (word: string) => string;
11
+ /** The key shown beside a word on a chip. */
12
+ tab: () => string;
13
+ /** What a screen reader hears for a suggestion that has stood for 1.2 s (a polite live region); `key` is `tab()`. `announce: false` turns it off. */
14
+ suggestion: (word: string, key: string) => string;
15
+ /** What it hears for a word typed wrong: the right word, what was typed, and the key. */
16
+ correction: (word: string, typed: string, key: string) => string;
17
+ }
18
+ export declare const DEFAULT_LABELS: Labels;
19
+ /** The class names of every element drawn (one string each, with spaces for several). The defaults are the ones of style.css. */
20
+ export interface Classes {
21
+ /** The layer laid over a box that holds the grey words and the marks. */
22
+ mirror: string;
23
+ /** The rest of the word being typed (grey). */
24
+ grey: string;
25
+ /** The next word (dimmer). */
26
+ greyNext: string;
27
+ /** The layer of marks (the box's text, unseen, with the words typed wrong marked). */
28
+ marks: string;
29
+ /** A word typed wrong. */
30
+ mark: string;
31
+ /** The wrapper fixed in the page's body that places a chip. */
32
+ layer: string;
33
+ /** Inside it: what fades in. */
34
+ chip: string;
35
+ /** The chip's box. */
36
+ chipBox: string;
37
+ /** The coloured bar on the fix chip's left edge. */
38
+ chipBar: string;
39
+ /** A chip's button. */
40
+ chipPart: string;
41
+ /** The letters of the right word that differ from the typed word. */
42
+ chipStrong: string;
43
+ /** The letters of the word already typed (on the chip over a text). */
44
+ chipTyped: string;
45
+ /** The rest of the word (on the chip over a text). */
46
+ chipRest: string;
47
+ /** A key shown dim (Tab is the grey word's, not this fix's). */
48
+ chipDim: string;
49
+ /** The key shown beside a word (when no `kbd` render function is given). */
50
+ kbd: string;
51
+ /** The × that keeps a word as typed. */
52
+ chipClose: string;
53
+ }
54
+ export declare const DEFAULT_CLASSES: Classes;
55
+ /** Extra attributes for some elements (an app's own hooks: a tip, an editing marker). */
56
+ export interface Attributes {
57
+ /** On the layer of grey words. */
58
+ grey?: Record<string, string>;
59
+ /** On the chip's wrapper in the page's body. */
60
+ chip?: Record<string, string>;
61
+ /** On the layer of marks. */
62
+ marks?: Record<string, string>;
63
+ }
64
+ /** The inline X of the fix chip (no icon library needed). */
65
+ export declare const CLOSE_ICON = "<svg width=\"13\" height=\"13\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.8\" stroke-linecap=\"round\" aria-hidden=\"true\"><path d=\"M4 4l8 8M12 4l-8 8\"/></svg>";
package/dist/web/ui.js ADDED
@@ -0,0 +1,32 @@
1
+ // What the text-box code shows: the strings (so that an app can translate them), the class names (so that an app can give
2
+ // its own), and the one class a box carries while suggestions are on in it.
3
+ /** The class a text box carries while suggestions are on in it (and loses when they are off). Programs that read a page's accessibility tree see it and leave that box alone. */
4
+ export const BOX_CLASS = 'grayling-box';
5
+ export const DEFAULT_LABELS = {
6
+ replace: (word) => `Replace with ${word}`,
7
+ keep: () => 'Keep as typed',
8
+ complete: (word) => `Complete with ${word}`,
9
+ tab: () => 'Tab',
10
+ suggestion: (word, key) => `Suggestion: ${word}. ${key} takes it.`,
11
+ correction: (word, typed, key) => `Correction: ${word} for ${typed}. ${key} takes it.`,
12
+ };
13
+ export const DEFAULT_CLASSES = {
14
+ mirror: 'grayling-mirror',
15
+ grey: 'grayling-grey',
16
+ greyNext: 'grayling-grey-next',
17
+ marks: 'grayling-marks',
18
+ mark: 'grayling-mark',
19
+ layer: 'grayling-chip-layer',
20
+ chip: 'grayling-chip',
21
+ chipBox: 'grayling-chip-box',
22
+ chipBar: 'grayling-chip-bar',
23
+ chipPart: 'grayling-chip-part',
24
+ chipStrong: 'grayling-chip-strong',
25
+ chipTyped: 'grayling-chip-typed',
26
+ chipRest: 'grayling-chip-rest',
27
+ chipDim: 'grayling-chip-dim',
28
+ kbd: 'grayling-kbd',
29
+ chipClose: 'grayling-chip-close',
30
+ };
31
+ /** The inline X of the fix chip (no icon library needed). */
32
+ export const CLOSE_ICON = '<svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" aria-hidden="true"><path d="M4 4l8 8M12 4l-8 8"/></svg>';
@@ -0,0 +1,39 @@
1
+ import type { SuggestCounts, SuggestJudged, SuggestPlan } from '../index.js';
2
+ /** How long counts wait before they are sent (ms). */
3
+ export declare const FLUSH_MS = 15000;
4
+ /** One report: counts no bigger than SUGGEST_USAGE_CAP each, and the guesses judged (they go with the first piece). */
5
+ export type UsagePiece = Partial<SuggestCounts> & {
6
+ judged?: SuggestJudged;
7
+ };
8
+ export interface UsageBatchOptions {
9
+ /**
10
+ * Send one piece (a POST to your server, a message to the engine…); `leaving` is true when the page is being hidden (use a
11
+ * request that outlives the page then, such as fetch with keepalive). Say whether to try again: true when it did not get
12
+ * there (the network lost it, the server could not answer), false when it was delivered or refused for good. A throw or a
13
+ * rejection counts as true. The piece goes back in the batch for the next time.
14
+ */
15
+ send: (piece: UsagePiece, leaving: boolean) => boolean | Promise<boolean>;
16
+ /** How long counts wait (ms; FLUSH_MS). */
17
+ flushMs?: number;
18
+ /** Send at once when the page is hidden or left (default true). */
19
+ flushOnHide?: boolean;
20
+ }
21
+ export declare class UsageBatch {
22
+ private o;
23
+ private pending;
24
+ private judged;
25
+ private timer;
26
+ private off;
27
+ constructor(o: UsageBatchOptions);
28
+ private later;
29
+ /** A suggester's counts join the batch (give this as the `count` of `Suggester`). */
30
+ record: (c: Partial<SuggestCounts>) => void;
31
+ /** A guess judged against what was then typed joins the batch (give this as the `judge` of `Suggester`). */
32
+ recordJudged: (plan: SuggestPlan, conf: number, right: boolean) => void;
33
+ /** Forget what has not been sent yet (the numbers were reset, so it would count from before). */
34
+ discard(): void;
35
+ /** Send what is in the batch. `leaving`: the page is being hidden (told to `send`). */
36
+ flush(leaving?: boolean): Promise<void>;
37
+ /** Stop listening to the page; what is not sent yet stays in the batch (flush it first to send it). */
38
+ dispose(): void;
39
+ }
@@ -0,0 +1,84 @@
1
+ import { addCounts, addVerdict, hasCounts, hasJudged, mergeJudged, noCounts, splitCounts } from './logic.js';
2
+ /** How long counts wait before they are sent (ms). */
3
+ export const FLUSH_MS = 15_000;
4
+ export class UsageBatch {
5
+ o;
6
+ pending = noCounts();
7
+ judged = {};
8
+ timer;
9
+ off;
10
+ constructor(o) {
11
+ this.o = o;
12
+ if (o.flushOnHide !== false && typeof document !== 'undefined' && typeof window !== 'undefined') {
13
+ const hidden = () => {
14
+ if (document.visibilityState === 'hidden')
15
+ void this.flush(true);
16
+ };
17
+ const left = () => void this.flush(true);
18
+ document.addEventListener('visibilitychange', hidden);
19
+ window.addEventListener('pagehide', left);
20
+ this.off = () => {
21
+ document.removeEventListener('visibilitychange', hidden);
22
+ window.removeEventListener('pagehide', left);
23
+ };
24
+ }
25
+ }
26
+ later() {
27
+ if (this.timer !== undefined)
28
+ return;
29
+ this.timer = setTimeout(() => void this.flush(), this.o.flushMs ?? FLUSH_MS);
30
+ // A batch must never keep a Node process alive.
31
+ this.timer.unref?.();
32
+ }
33
+ /** A suggester's counts join the batch (give this as the `count` of `Suggester`). */
34
+ record = (c) => {
35
+ this.pending = addCounts(this.pending, c);
36
+ if (hasCounts(this.pending))
37
+ this.later();
38
+ };
39
+ /** A guess judged against what was then typed joins the batch (give this as the `judge` of `Suggester`). */
40
+ recordJudged = (plan, conf, right) => {
41
+ addVerdict(this.judged, plan, conf, right);
42
+ this.later();
43
+ };
44
+ /** Forget what has not been sent yet (the numbers were reset, so it would count from before). */
45
+ discard() {
46
+ clearTimeout(this.timer);
47
+ this.timer = undefined;
48
+ this.pending = noCounts();
49
+ this.judged = {};
50
+ }
51
+ /** Send what is in the batch. `leaving`: the page is being hidden (told to `send`). */
52
+ async flush(leaving = false) {
53
+ clearTimeout(this.timer);
54
+ this.timer = undefined;
55
+ if (!hasCounts(this.pending) && !hasJudged(this.judged))
56
+ return;
57
+ // The judged guesses go with the first piece (with no counts at all, in a piece of their own).
58
+ const pieces = splitCounts(this.pending);
59
+ if (hasJudged(this.judged))
60
+ pieces[0] = { ...pieces[0], judged: this.judged };
61
+ this.pending = noCounts();
62
+ this.judged = {};
63
+ const sent = await Promise.allSettled(pieces.map(async (p) => this.o.send(p, leaving)));
64
+ let again = false;
65
+ sent.forEach((r, i) => {
66
+ if (r.status === 'fulfilled' && !r.value)
67
+ return;
68
+ const { judged: j, ...counts } = pieces[i];
69
+ this.pending = addCounts(this.pending, counts);
70
+ if (j)
71
+ mergeJudged(this.judged, j);
72
+ again = true;
73
+ });
74
+ if (again)
75
+ this.later();
76
+ }
77
+ /** Stop listening to the page; what is not sent yet stays in the batch (flush it first to send it). */
78
+ dispose() {
79
+ clearTimeout(this.timer);
80
+ this.timer = undefined;
81
+ this.off?.();
82
+ this.off = undefined;
83
+ }
84
+ }
package/package.json CHANGED
@@ -1,6 +1,91 @@
1
1
  {
2
2
  "name": "grayling",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Word suggestions for text boxes: the rest of the word you are typing, and the next one, in grey. Plain JavaScript, React, and a Node client of the Grayling engine.",
5
+ "license": "AGPL-3.0-only",
6
+ "type": "module",
7
+ "keywords": [
8
+ "autocomplete",
9
+ "word-suggestions",
10
+ "text-prediction",
11
+ "textarea",
12
+ "react",
13
+ "grayling"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/algoprog/grayling.git",
18
+ "directory": "packages/grayling"
19
+ },
20
+ "homepage": "https://github.com/algoprog/grayling",
21
+ "sideEffects": [
22
+ "**/*.css"
23
+ ],
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "main": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "./web": {
35
+ "types": "./dist/web/index.d.ts",
36
+ "default": "./dist/web/index.js"
37
+ },
38
+ "./react": {
39
+ "types": "./dist/react/index.d.ts",
40
+ "default": "./dist/react/index.js"
41
+ },
42
+ "./node": {
43
+ "types": "./dist/node/index.d.ts",
44
+ "default": "./dist/node/index.js"
45
+ },
46
+ "./web/style.css": "./dist/web/style.css",
47
+ "./package.json": "./package.json"
48
+ },
49
+ "files": [
50
+ "dist",
51
+ "program.json",
52
+ "README.md",
53
+ "LICENSE"
54
+ ],
55
+ "scripts": {
56
+ "build": "tsc -p tsconfig.build.json && node scripts/copy-css.mjs",
57
+ "typecheck": "tsc -p tsconfig.json --noEmit",
58
+ "test": "node scripts/test.mjs",
59
+ "prepublishOnly": "npm run build"
60
+ },
61
+ "peerDependencies": {
62
+ "react": ">=18",
63
+ "react-dom": ">=18"
64
+ },
65
+ "peerDependenciesMeta": {
66
+ "react": {
67
+ "optional": true
68
+ },
69
+ "react-dom": {
70
+ "optional": true
71
+ }
72
+ },
73
+ "devDependencies": {
74
+ "@types/jsdom": "^30.0.0",
75
+ "@types/node": "^20.19.43",
76
+ "@types/react": "^19.3.3",
77
+ "@types/react-dom": "^19.3.0",
78
+ "jsdom": "^31.0.1",
79
+ "react": "^19.3.1",
80
+ "react-dom": "^19.3.1",
81
+ "typescript": "^7.0.2"
82
+ },
83
+ "optionalDependencies": {
84
+ "@algoprog/grayling-darwin-arm64": "0.1.1",
85
+ "@algoprog/grayling-darwin-x64": "0.1.1",
86
+ "@algoprog/grayling-linux-arm64": "0.1.1",
87
+ "@algoprog/grayling-linux-x64": "0.1.1",
88
+ "@algoprog/grayling-win32-arm64": "0.1.1",
89
+ "@algoprog/grayling-win32-x64": "0.1.1"
90
+ }
91
+ }
package/program.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "version": "0.1.1",
3
+ "targets": {
4
+ "x86_64-unknown-linux-gnu": {
5
+ "file": "grayling-0.1.1-x86_64-unknown-linux-gnu.tar.gz",
6
+ "size": 4671654,
7
+ "sha256": "c7f4472044458786f28a49a9f6d355c8b9cd380a017020dbfcc8492833db851f"
8
+ },
9
+ "aarch64-unknown-linux-gnu": {
10
+ "file": "grayling-0.1.1-aarch64-unknown-linux-gnu.tar.gz",
11
+ "size": 4363525,
12
+ "sha256": "732aaa4faed372cee66e7d89054e091b4b43374216aaf7a044809066c49b166d"
13
+ },
14
+ "aarch64-apple-darwin": {
15
+ "file": "grayling-0.1.1-aarch64-apple-darwin.tar.gz",
16
+ "size": 4213936,
17
+ "sha256": "218a5206cd7ad6279f2cf8f680367846743b6a4911a49d1b6df2dd0456b9158e"
18
+ },
19
+ "x86_64-apple-darwin": {
20
+ "file": "grayling-0.1.1-x86_64-apple-darwin.tar.gz",
21
+ "size": 4599012,
22
+ "sha256": "3d84dc616633aba724cab3a8153bd34e146f6da42c90227c1081ae69f8372eee"
23
+ },
24
+ "x86_64-pc-windows-msvc": {
25
+ "file": "grayling-0.1.1-x86_64-pc-windows-msvc.zip",
26
+ "size": 4690401,
27
+ "sha256": "61f2a1c740427ac22780398782184496c85ef94131269d9becac108ec655286d"
28
+ },
29
+ "aarch64-pc-windows-msvc": {
30
+ "file": "grayling-0.1.1-aarch64-pc-windows-msvc.zip",
31
+ "size": 4349909,
32
+ "sha256": "78233dcb2f6b3e02b851790eb1096a6981d652e802816e8be6bbd46f9c1b8bb0"
33
+ }
34
+ }
35
+ }