@witty-works/editor 2.6.4 → 2.8.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 CHANGED
@@ -10,7 +10,7 @@ One self-contained script: no framework, no stylesheet to include. As a script t
10
10
 
11
11
  ```html
12
12
  <div id="editor"></div>
13
- <script src="https://cdn.jsdelivr.net/npm/@witty-works/editor@2.6.4/dist/witty-editor.js"></script>
13
+ <script src="https://cdn.jsdelivr.net/npm/@witty-works/editor@2.8.0/dist/witty-editor.js"></script>
14
14
  <script>
15
15
  const editor = WittyEditor.mount(document.getElementById('editor'), {
16
16
  endpoint: 'https://your-nlp-api.example/',
@@ -39,6 +39,24 @@ const editor = mount(document.getElementById('editor')!, {
39
39
  editor.setApiKey(keyFromYourBackend);
40
40
  ```
41
41
 
42
+ ### Signing in with an access token
43
+
44
+ A page whose users sign in, such as a dashboard that issues OAuth tokens the NLP API accepts, passes a token instead of a key. The editor keeps it in memory and sends it as a bearer token; the page refreshes it and hands the editor the new one:
45
+
46
+ ```ts
47
+ const editor = mount(element, {endpoint, accessToken: tokens.accessToken});
48
+
49
+ // Before it expires, and once when the API refused it
50
+ // (`{state: 'unauthorized', status: 401 | 403}`):
51
+ editor.setAccessToken((await refresh()).accessToken);
52
+ // If the new token is refused too, stop and ask the user to sign in again:
53
+ // another refresh would be refused the same way.
54
+ ```
55
+
56
+ From NLP API 4.0.0, `401` means the token is not valid (for example expired) and `403` that the API does not let this user in. Earlier versions answer `403` for an expired token too, so a page that refreshes on `401` only would stop checking there.
57
+
58
+ Set either a key or a token: when both are set, the key is sent.
59
+
42
60
  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
61
 
44
62
  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.
@@ -51,6 +69,7 @@ For the script tag in a TypeScript project, the same types describe the global:
51
69
  |---|---|---|
52
70
  | `endpoint` | page origin | NLP API base URL; a relative one resolves against the page, and a missing trailing slash is added. Anything but an `http(s)` URL throws a `TypeError` |
53
71
  | `apiKey` | none | Sent as `x-key`; prefer `setApiKey` |
72
+ | `accessToken` | none | An OAuth access token, sent as `Authorization: Bearer`; prefer `setAccessToken`. An `apiKey` wins when both are set |
54
73
  | `lang` | `'auto'` | Text language: `auto`, `en`, `de`, `fr` or a variant such as `de-CH` |
55
74
  | `config` | none | Check config (gender formats, disabled categories, …); only fields you set are sent |
56
75
  | `content` | empty | Initial content as HTML |
@@ -69,6 +88,7 @@ For the script tag in a TypeScript project, the same types describe the global:
69
88
  It returns a handle:
70
89
 
71
90
  - `setApiKey(key)`: replace the API key and check again.
91
+ - `setAccessToken(token)`: replace the access token and check again, for example after refreshing it.
72
92
  - `setConfig(config)`: replace the whole check config (no merging) and check again; `{}` sends none. The same config again changes nothing, so a host can pass its config on every render. The editor keeps a copy: changing the object afterwards has no effect.
73
93
  - `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.
74
94
  - `getSettings()`: the current settings, `{config, llmAlternatives, orthography}`, as a copy; the same object `onSettingsChange` receives.
@@ -84,7 +104,7 @@ It returns a handle:
84
104
  | Status | When | Alerts |
85
105
  |---|---|---|
86
106
  | `{state: 'idle', alerts, limitReached}` | A check finished; after a gender format switch it also has `genderFormatSwitch` | Shown |
87
- | `{state: 'unauthorized'}` | The API refused the key (401) or the token behind it (403) | Cleared |
107
+ | `{state: 'unauthorized', status}` | `status` 401: the API refused the key or token (an access token may have expired). 403: the API does not let this user in | Cleared |
88
108
  | `{state: 'outdated', message}` | The API no longer supports this editor version (400); `message` is the API's explanation. The page needs to load a newer version | Cleared |
89
109
  | `{state: 'unsupportedLanguage'}` | The API could not tell the text's language (422) | Cleared |
90
110
  | `{state: 'error', message}` | Anything else, e.g. the API is unreachable, answered 5xx, or gave no answer within 30 seconds | Kept until the next check |
@@ -97,9 +97,16 @@ export type EditorStatus =
97
97
  /** Set once, on the status right after a gender format switch. */
98
98
  genderFormatSwitch?: GenderFormatSwitchResult;
99
99
  }
100
- /** The API refused the key (401), or the token behind it (403). */
100
+ /**
101
+ * The API refused the request. From NLP API 4.0.0, `status` 401: the key
102
+ * or token is not valid (an access token may have expired); 403: the API
103
+ * does not let this user in. Earlier versions answer 403 for an expired
104
+ * token too. With an access token, refresh it once and call
105
+ * `setAccessToken`; if the new one is refused as well, stop.
106
+ */
101
107
  | {
102
108
  state: 'unauthorized';
109
+ status: 401 | 403;
103
110
  }
104
111
  /**
105
112
  * The API no longer supports this version of the editor (400): the host
@@ -123,6 +130,12 @@ export interface MountOptions {
123
130
  endpoint?: string;
124
131
  /** Sent as `x-key`. Set later with `setApiKey`; never read from the DOM. */
125
132
  apiKey?: string;
133
+ /**
134
+ * An OAuth access token, sent as `Authorization: Bearer`, for a page whose
135
+ * users sign in (the dashboard's tokens). Set later with `setAccessToken`.
136
+ * An `apiKey` wins when both are set.
137
+ */
138
+ accessToken?: string;
126
139
  /** Language of the text, or `auto` (the default) to let the API detect it. */
127
140
  lang?: CheckLang;
128
141
  /**
@@ -179,6 +192,12 @@ export interface WittyEditorHandle {
179
192
  editor: Editor;
180
193
  /** Replace the API key (empty string clears it) and check again. */
181
194
  setApiKey(key: string): void;
195
+ /**
196
+ * Replace the access token (empty string clears it) and check again:
197
+ * after a refresh, before the token expires or once after
198
+ * `{state: 'unauthorized'}`. If the new token is refused too, stop.
199
+ */
200
+ setAccessToken(token: string): void;
182
201
  /**
183
202
  * Replace the whole check config — this does not merge — and check again.
184
203
  * `{}` goes back to sending no `config`, i.e. the account's own settings.