@witty-works/editor 2.5.0 → 2.6.0
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 +34 -2
- package/dist/witty-editor.d.ts +217 -0
- package/dist/witty-editor.js +3 -3
- package/dist/witty-editor.mjs +34345 -0
- package/dist/witty-editor.mjs.LICENSE.txt +1643 -0
- package/package.json +29 -4
package/README.md
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
[Witty](https://witty.works)'s inclusive-language checker as an embeddable rich-text editor. It checks as you type, underlines what Witty flags, and offers alternatives in the same popover as the Witty browser extension. It includes a formatting toolbar and Witty's settings (categories, gender formats, spelling, AI suggestions).
|
|
4
4
|
|
|
5
|
-
One self-contained script: no framework, no stylesheet to include.
|
|
5
|
+
One self-contained script: no framework, no stylesheet to include. As a script tag or as an ES module for bundlers, with TypeScript types.
|
|
6
6
|
|
|
7
7
|
## Usage
|
|
8
8
|
|
|
9
|
+
### Script tag
|
|
10
|
+
|
|
9
11
|
```html
|
|
10
12
|
<div id="editor"></div>
|
|
11
|
-
<script src="https://cdn.jsdelivr.net/npm/@witty-works/editor@2.
|
|
13
|
+
<script src="https://cdn.jsdelivr.net/npm/@witty-works/editor@2.6.0/dist/witty-editor.js"></script>
|
|
12
14
|
<script>
|
|
13
15
|
const editor = WittyEditor.mount(document.getElementById('editor'), {
|
|
14
16
|
endpoint: 'https://your-nlp-api.example/',
|
|
@@ -21,6 +23,28 @@ One self-contained script: no framework, no stylesheet to include.
|
|
|
21
23
|
|
|
22
24
|
Pin an exact version, as above, rather than a range: the script runs with access to the page. Or `npm install @witty-works/editor` and serve `node_modules/@witty-works/editor/dist/witty-editor.js` yourself. Releases are published from CI with npm provenance.
|
|
23
25
|
|
|
26
|
+
### With a bundler
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @witty-works/editor
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import {mount, type EditorStatus} from '@witty-works/editor';
|
|
34
|
+
|
|
35
|
+
const editor = mount(document.getElementById('editor')!, {
|
|
36
|
+
endpoint: 'https://your-nlp-api.example/',
|
|
37
|
+
onStatus: (status: EditorStatus) => console.log(status),
|
|
38
|
+
});
|
|
39
|
+
editor.setApiKey(keyFromYourBackend);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The package's main entry is an ES module (`dist/witty-editor.mjs`) exporting `mount`, with types (`dist/witty-editor.d.ts`) for the options, the handle, `onStatus` and the settings: `MountOptions`, `WittyEditorHandle`, `EditorStatus`, `EditorSettings`, `CheckConfig`, `CheckLang`, `GenderFormatSwitchResult` and the rest. It works like the script: the same editor, React, TipTap and styles included (injected when the module is imported), nothing to configure in the bundler. It is not minified; your bundler does that. It does not share your page's React, so a React app loads a second copy for the editor.
|
|
43
|
+
|
|
44
|
+
The type of the handle's `editor` property comes from TipTap: install `@tiptap/core` (version 3, an optional peer dependency) if you use it from TypeScript. Nothing else needs it.
|
|
45
|
+
|
|
46
|
+
For the script tag in a TypeScript project, the same types describe the global: `declare const WittyEditor: typeof import('@witty-works/editor');`.
|
|
47
|
+
|
|
24
48
|
## `WittyEditor.mount(element, options)`
|
|
25
49
|
|
|
26
50
|
| Option | Default | |
|
|
@@ -46,6 +70,7 @@ It returns a handle:
|
|
|
46
70
|
|
|
47
71
|
- `setApiKey(key)`: replace the API key and check again.
|
|
48
72
|
- `setConfig(config)`: replace the whole check config (no merging) and check again; `{}` sends none.
|
|
73
|
+
- `updateSettings(settings)`: change settings from the host, e.g. its own form, as the settings panel does (the panel follows). The fields given replace the current ones and the rest stay: `updateSettings({llmAlternatives: true})`, `updateSettings({orthography: false})`. `config`, when given, is replaced whole, as with `setConfig`; to change one field of it, start from `getSettings().config`. Calls `onSettingsChange` once if anything changed, and nothing happens if nothing did. A changed `config` checks the text again; popovers opened afterwards follow `llmAlternatives`, an open one keeps what it shows. A value of the wrong type (`llmAlternatives: 'false'`, a `config` that is not an object) throws a `TypeError` and changes nothing. Unlike `setConfig`, which is for the host's own changes, this reports to `onSettingsChange`, so a host that stores the settings there sees changes from its form and from the panel alike.
|
|
49
74
|
- `getSettings()`: the current settings, `{config, llmAlternatives, orthography}`, as a copy; the same object `onSettingsChange` receives.
|
|
50
75
|
- `getText()`: the document as plain text.
|
|
51
76
|
- `switchGenderFormat(target)`: rewrite the text into a German or French gender format, as the menu does (see below); `target` is a `german_gender_ending` or `french_gender_separator` value. Resolves with `{outcome, target, count, limitReached}`; `outcome` is `switched`, `nothing`, `fromInklusivum` (nothing switched, and the text was in the Inklusivum, which the API cannot convert out of yet), `disabled` (the account keeps these alerts off), `forced` (the account forces another format, given as `applied`; nothing is changed), `unsupported` (the API cannot switch to this target yet) or `unavailable` (not a gender format).
|
|
@@ -131,6 +156,13 @@ Part of a text can stay unchecked: anything past `maxTextLength`, or a single se
|
|
|
131
156
|
- `connect-src` for the API endpoint's origin, when it differs from the page's.
|
|
132
157
|
- **CORS:** the NLP API must allow the page's origin.
|
|
133
158
|
|
|
159
|
+
## Settings from the host: what to keep in mind
|
|
160
|
+
|
|
161
|
+
- **The switches are not access control.** Anything running in the page can call the handle, and a user can from the browser console. So `llmAlternatives: false` is a default, not a limit. Who may use the LLM at all is the NLP API's decision (`LLM_ACCESS` and its allow list, and whether clients may ask for it, `CLIENT_CONFIG_ENABLED`); how many requests they make is not limited by the API, so a deployment that has to ration them needs a rate limit in front of it. The same goes for everything in `config`: an organisation's forced settings are applied by the API, whatever the editor sends.
|
|
162
|
+
- **Keep the handle to yourself.** Don't put it on `window`; any script on the page could then change settings or read the text. The API key can only be set through it, never read back.
|
|
163
|
+
- **Settings hold no secrets.** `getSettings()` and `onSettingsChange` never include the API key, so hosts can store the settings, e.g. per user.
|
|
164
|
+
- **Spelling and the browser.** `orthography: false` lets the browser's own spellcheck underline the text again. Some browsers' enhanced spellcheck (Chrome, Edge) sends the text to the vendor's service; with `orthography: true` (the default) the editor turns the browser's spellcheck off.
|
|
165
|
+
|
|
134
166
|
## Storage
|
|
135
167
|
|
|
136
168
|
The API key is kept in memory only; it is never written to the page, to cookies or to storage. The only thing the editor stores is i18next's language cache, `localStorage["i18nextLng"]`.
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/core';
|
|
2
|
+
/**
|
|
3
|
+
* The editor's public API types: what `dist/witty-editor.d.ts` declares for
|
|
4
|
+
* the package (`npm run build:types`). The only source of these types; the
|
|
5
|
+
* modules that use them import them from here. Keep this module free of
|
|
6
|
+
* imports other than types from @tiptap/core, so the declarations stand on
|
|
7
|
+
* their own.
|
|
8
|
+
*/
|
|
9
|
+
/** Languages `POST /v2.4/check` accepts; `auto` detects. */
|
|
10
|
+
export type CheckLang = 'auto' | 'en' | 'de' | 'fr' | 'de-DE' | 'de-CH' | 'de-AT' | 'en-US' | 'en-GB' | 'fr-FR';
|
|
11
|
+
/** Regional variants, for `primary_language` and `preferred_variants`. */
|
|
12
|
+
export type CheckVariant = 'de-DE' | 'de-CH' | 'de-AT' | 'en-US' | 'en-GB' | 'fr-FR';
|
|
13
|
+
/**
|
|
14
|
+
* Per-request `config` for `POST /v2.4/check`.
|
|
15
|
+
*
|
|
16
|
+
* Every field is optional, and only the ones a caller actually set are sent:
|
|
17
|
+
* a field in the request overrides the user's stored config where that config
|
|
18
|
+
* marks it `suggestion`, an omitted field keeps the stored value or the server
|
|
19
|
+
* default, and a stored `force` wins either way. Filling in defaults here would
|
|
20
|
+
* silently override the account's own settings.
|
|
21
|
+
*
|
|
22
|
+
* `alternatives_max_count` is deliberately absent — the server overwrites it.
|
|
23
|
+
*/
|
|
24
|
+
export interface CheckConfig {
|
|
25
|
+
/** Gender ending, as offered by `GET /v2.0/config-options`. `de-e` is Inklusivum. */
|
|
26
|
+
german_gender_ending?: '/in' | '/-in' | '_in' | '*in' | ':in' | '(-)' | '()' | 'In' | 'de-e';
|
|
27
|
+
french_gender_separator?: '·' | '·s' | '.' | '.s' | '/' | '/s';
|
|
28
|
+
gendered_roles_format?: 'none' | 'both' | 'inclusive_gender' | 'binary_gender';
|
|
29
|
+
/**
|
|
30
|
+
* Category keys from `GET /v2.0/categories`. A category and its
|
|
31
|
+
* `advanced_key` have to be disabled together.
|
|
32
|
+
*/
|
|
33
|
+
disabled_categories?: string[];
|
|
34
|
+
show_inspiration_alternatives?: boolean;
|
|
35
|
+
primary_language?: CheckVariant;
|
|
36
|
+
preferred_languages?: ('en' | 'de' | 'fr')[];
|
|
37
|
+
preferred_variants?: CheckVariant[];
|
|
38
|
+
addons?: string[];
|
|
39
|
+
/** Ignored unless the deployment sets `CLIENT_CONFIG_ENABLED`. */
|
|
40
|
+
store_context?: boolean;
|
|
41
|
+
/** Same, and the operator's `LLM_ACCESS` policy can still turn it off. */
|
|
42
|
+
llm_alternatives?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* What the user can change in the editor's settings panel, and what the host
|
|
46
|
+
* can set through the handle. One store, so the panel, `setConfig` and the
|
|
47
|
+
* checker never disagree.
|
|
48
|
+
*/
|
|
49
|
+
export interface EditorSettings {
|
|
50
|
+
/** Sent with every check; only fields that were set. */
|
|
51
|
+
config: CheckConfig;
|
|
52
|
+
/** Offer the LLM's sentence rewrites in the popover. */
|
|
53
|
+
llmAlternatives: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Witty checks spelling, so the browser's own spellcheck is off. Off here,
|
|
56
|
+
* the browser underlines misspellings as well.
|
|
57
|
+
*/
|
|
58
|
+
orthography: boolean;
|
|
59
|
+
}
|
|
60
|
+
export type SwitchOutcome =
|
|
61
|
+
/** Forms were rewritten. */
|
|
62
|
+
'switched'
|
|
63
|
+
/** Nothing in the text needed rewriting. */
|
|
64
|
+
| 'nothing'
|
|
65
|
+
/** The account's configuration keeps these alerts off (a stored force). */
|
|
66
|
+
| 'disabled'
|
|
67
|
+
/** The API predates bulk alerts. */
|
|
68
|
+
| 'unsupported'
|
|
69
|
+
/** The account forces another format; nothing was changed. */
|
|
70
|
+
| 'forced'
|
|
71
|
+
/**
|
|
72
|
+
* Nothing to switch, and the text was in the Inklusivum: the API does not
|
|
73
|
+
* convert out of it yet, so nothing may well have been possible.
|
|
74
|
+
*/
|
|
75
|
+
| 'fromInklusivum'
|
|
76
|
+
/** Not a gender format of German or French. */
|
|
77
|
+
| 'unavailable';
|
|
78
|
+
export interface GenderFormatSwitchResult {
|
|
79
|
+
outcome: SwitchOutcome;
|
|
80
|
+
target: string;
|
|
81
|
+
/** Forms rewritten. */
|
|
82
|
+
count: number;
|
|
83
|
+
/** Part of the text was not checked, so it may hold unswitched forms. */
|
|
84
|
+
limitReached: boolean;
|
|
85
|
+
/** With `forced`: the format the account enforces. */
|
|
86
|
+
applied?: string;
|
|
87
|
+
}
|
|
88
|
+
export type EditorStatus =
|
|
89
|
+
/**
|
|
90
|
+
* `limitReached`: part of the text was not checked, because it is longer
|
|
91
|
+
* than `maxTextLength` or a sentence exceeds what the API checks at once.
|
|
92
|
+
*/
|
|
93
|
+
{
|
|
94
|
+
state: 'idle';
|
|
95
|
+
alerts: number;
|
|
96
|
+
limitReached: boolean;
|
|
97
|
+
/** Set once, on the status right after a gender format switch. */
|
|
98
|
+
genderFormatSwitch?: GenderFormatSwitchResult;
|
|
99
|
+
}
|
|
100
|
+
/** The API refused the key (401), or the token behind it (403). */
|
|
101
|
+
| {
|
|
102
|
+
state: 'unauthorized';
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The API no longer supports this version of the editor (400): the host
|
|
106
|
+
* page needs to load a newer one. `message` is the API's explanation.
|
|
107
|
+
*/
|
|
108
|
+
| {
|
|
109
|
+
state: 'outdated';
|
|
110
|
+
message: string;
|
|
111
|
+
}
|
|
112
|
+
/** The API could not tell the text's language (422). */
|
|
113
|
+
| {
|
|
114
|
+
state: 'unsupportedLanguage';
|
|
115
|
+
}
|
|
116
|
+
/** Anything else, e.g. the API is unreachable; the last alerts stay. */
|
|
117
|
+
| {
|
|
118
|
+
state: 'error';
|
|
119
|
+
message: string;
|
|
120
|
+
};
|
|
121
|
+
export interface MountOptions {
|
|
122
|
+
/** NLP API base URL with trailing slash; defaults to the page's origin. */
|
|
123
|
+
endpoint?: string;
|
|
124
|
+
/** Sent as `x-key`. Set later with `setApiKey`; never read from the DOM. */
|
|
125
|
+
apiKey?: string;
|
|
126
|
+
/** Language of the text, or `auto` (the default) to let the API detect it. */
|
|
127
|
+
lang?: CheckLang;
|
|
128
|
+
/**
|
|
129
|
+
* Per-request check config. Only the fields set here are sent, so the user's
|
|
130
|
+
* stored settings keep deciding the rest. Replace it later with `setConfig`.
|
|
131
|
+
*/
|
|
132
|
+
config?: CheckConfig;
|
|
133
|
+
/** Initial content (HTML). */
|
|
134
|
+
content?: string;
|
|
135
|
+
/**
|
|
136
|
+
* The formatting bar with the Witty settings button (categories, gender
|
|
137
|
+
* formats, spelling, AI suggestions). On by default.
|
|
138
|
+
*/
|
|
139
|
+
toolbar?: boolean;
|
|
140
|
+
/** Called when the user changes a setting in the settings panel. */
|
|
141
|
+
onSettingsChange?: (settings: EditorSettings) => void;
|
|
142
|
+
/** Accessible name of the editable area. */
|
|
143
|
+
label?: string;
|
|
144
|
+
/**
|
|
145
|
+
* Ids of elements that further describe the editable area, separated by
|
|
146
|
+
* spaces; added to its `aria-describedby` after the component's own hint.
|
|
147
|
+
*/
|
|
148
|
+
describedBy?: string;
|
|
149
|
+
/**
|
|
150
|
+
* Characters per check request; longer texts are checked sentence by
|
|
151
|
+
* sentence over several requests. Match the API's TEXT_MAX_LENGTH (1000 by
|
|
152
|
+
* default); a lower limit is detected and batches shrink.
|
|
153
|
+
*/
|
|
154
|
+
maxRequestLength?: number;
|
|
155
|
+
/** Characters of the text checked at all (default 20000). */
|
|
156
|
+
maxTextLength?: number;
|
|
157
|
+
/** Debounce after the last edit, in ms. */
|
|
158
|
+
delay?: number;
|
|
159
|
+
/**
|
|
160
|
+
* Opaque id sent with each check (`id`), as the extension sends its
|
|
161
|
+
* installation id; it only ends up in the API's request logs. Defaults to
|
|
162
|
+
* a random id per editor, kept in memory. Pass a stable one, never personal
|
|
163
|
+
* data, to tell installations apart across page loads.
|
|
164
|
+
*/
|
|
165
|
+
installationId?: string;
|
|
166
|
+
/**
|
|
167
|
+
* Offer the LLM's sentence rewrites in the popover (`/v1.0/rephrase`). The
|
|
168
|
+
* extension takes this from the organisation config.
|
|
169
|
+
*/
|
|
170
|
+
llmAlternatives?: boolean;
|
|
171
|
+
/**
|
|
172
|
+
* How long to wait for the rewrites, in ms. The extension's 3s suits a
|
|
173
|
+
* hosted model; a local one (Ollama) needs longer.
|
|
174
|
+
*/
|
|
175
|
+
llmTimeoutMs?: number;
|
|
176
|
+
onStatus?: (status: EditorStatus) => void;
|
|
177
|
+
}
|
|
178
|
+
export interface WittyEditorHandle {
|
|
179
|
+
editor: Editor;
|
|
180
|
+
/** Replace the API key (empty string clears it) and check again. */
|
|
181
|
+
setApiKey(key: string): void;
|
|
182
|
+
/**
|
|
183
|
+
* Replace the whole check config — this does not merge — and check again.
|
|
184
|
+
* `{}` goes back to sending no `config`, i.e. the account's own settings.
|
|
185
|
+
*/
|
|
186
|
+
setConfig(config: CheckConfig): void;
|
|
187
|
+
/**
|
|
188
|
+
* Change settings as the settings panel does, e.g. from the host's own form
|
|
189
|
+
* (the panel follows): the fields given replace the current ones, the rest
|
|
190
|
+
* stay. `config`, when given, is replaced whole, as with `setConfig`; start
|
|
191
|
+
* from a copy of `getSettings().config` to change one field of it. Calls
|
|
192
|
+
* `onSettingsChange` once if anything changed. A changed `config` checks
|
|
193
|
+
* again; popovers opened afterwards follow `llmAlternatives`, an open one
|
|
194
|
+
* keeps what it shows.
|
|
195
|
+
*/
|
|
196
|
+
updateSettings(settings: Partial<EditorSettings>): void;
|
|
197
|
+
/** Plain text of the document. */
|
|
198
|
+
getText(): string;
|
|
199
|
+
/**
|
|
200
|
+
* The current settings: what `onSettingsChange` receives, as a copy. For a
|
|
201
|
+
* host that sends the same `config` with its own API calls.
|
|
202
|
+
*/
|
|
203
|
+
getSettings(): EditorSettings;
|
|
204
|
+
/**
|
|
205
|
+
* Rewrite every gendered form in the text into `target` (a German gender
|
|
206
|
+
* ending such as ':in'), in one undoable step, and make it the configured
|
|
207
|
+
* format. Resolves once the text has been checked and switched. Needs an NLP
|
|
208
|
+
* API with bulk alerts; see the README.
|
|
209
|
+
*/
|
|
210
|
+
switchGenderFormat(target: string): Promise<GenderFormatSwitchResult>;
|
|
211
|
+
destroy(): void;
|
|
212
|
+
}
|
|
213
|
+
/** `WittyEditor.mount` / the module's `mount`. */
|
|
214
|
+
export type Mount = (element: HTMLElement, options?: MountOptions) => WittyEditorHandle;
|
|
215
|
+
|
|
216
|
+
/** Mount the editor into `element`; see the README. */
|
|
217
|
+
export declare const mount: Mount;
|