@lexion-rte/wproofreader 0.0.0-stage → 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # @lexion-rte/wproofreader
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 50ee830: New package `@lexion-rte/wproofreader`: WebSpellChecker's WProofreader (spelling and grammar checking) as one extension, `WProofreader.configure({ serviceId, lang })`. It loads the bundle once per page in the browser only, starts an instance on every mounted view and destroys it with the view (so every adapter works, including when a framework re-creates the view), and disables WProofreader while the view is read-only. Commands `toggleWProofreader`, `openWProofreaderDialog` and `openWProofreaderSettings`; `createWProofreaderToolbarItems()` for the `@lexion-rte/ui` toolbar; `getWProofreader(view)`, `isWProofreaderEnabled(state)` and `loadWebSpellChecker()`. Options for self-hosted servers, CSP nonces and custom loading. Bring your own WebSpellChecker license. `@lexion-rte/ui` adds the `spellCheck` and `proofread` icons.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [70bbcd2]
12
+ - Updated dependencies [e0b92c6]
13
+ - Updated dependencies [81e3c49]
14
+ - Updated dependencies [db26cdf]
15
+ - @lexion-rte/core@0.5.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dario Espina
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,96 @@
1
- # Temporary Holding Version
1
+ ![Lexion Logo](../../assets/images/logo.png)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This package is part of the Lexion framework-agnostic rich text editor.
4
+
5
+ Lexion is a framework-agnostic, headless rich text editor platform built on ProseMirror, designed to provide a shared core, reusable extensions, and framework-specific adapters.
6
+
7
+ # @lexion-rte/wproofreader
8
+
9
+ Spelling and grammar checking with WebSpellChecker's [WProofreader](https://webspellchecker.com/wsc-proofreader/), as one Lexion extension. WProofreader is a commercial service: you need your own WebSpellChecker license and service ID (or a self-hosted server). This package is MIT.
10
+
11
+ The extension:
12
+
13
+ - loads WebSpellChecker's bundle once per page, in the browser only (server rendering is safe);
14
+ - starts a WProofreader instance on every view of the editor when it is mounted, and destroys it with the view, so React, Vue, Vue 2, Angular, Svelte, Solid, Astro, Next, Nuxt and plain `@lexion-rte/web` need no extra code, including when a framework re-creates the view;
15
+ - disables WProofreader while the view is read-only (WebSpellChecker does not follow `contenteditable` itself);
16
+ - adds the `toggleWProofreader`, `openWProofreaderDialog` and `openWProofreaderSettings` commands, and toolbar buttons for `@lexion-rte/ui`.
17
+
18
+ WProofreader never changes the editor's content to show errors, and accepted suggestions become ordinary transactions (marks kept, one undo step each). See [WProofreader with Lexion](../../docs/WPROOFREADER.md) for the test results.
19
+
20
+ ## Install
21
+
22
+ ```bash
23
+ pnpm add @lexion-rte/wproofreader
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ```ts
29
+ import { LexionEditor } from "@lexion-rte/core";
30
+ import { StarterKit } from "@lexion-rte/starter-kit";
31
+ import { WProofreader } from "@lexion-rte/wproofreader";
32
+
33
+ const editor = new LexionEditor({
34
+ extensions: [
35
+ StarterKit,
36
+ WProofreader.configure({ serviceId: "your-service-id", lang: "en_US", autocorrect: false })
37
+ ]
38
+ });
39
+ ```
40
+
41
+ Mount the editor with any adapter, as usual. That is all.
42
+
43
+ ## Options
44
+
45
+ | Option | Default | |
46
+ | --- | --- | --- |
47
+ | `serviceId` | | Your WebSpellChecker service ID (cloud service) |
48
+ | `serviceProtocol`, `serviceHost`, `servicePort`, `servicePath` | | Self-hosted server |
49
+ | `bundleUrl` | the cloud bundle | The bundle's URL; for a self-hosted server, its own `…/wscbundle/wscbundle.js` |
50
+ | `lang` | the service's | `"en_US"`, `"en_GB"`, `"de_DE"`, `"es_ES"`… |
51
+ | `autocorrect` | `true` (WebSpellChecker's) | Fix common typos while typing |
52
+ | `autocomplete`, `enableGrammar`, `enableBadgeButton`, `theme` | WebSpellChecker's | |
53
+ | `initOptions` | | Any other option of `WEBSPELLCHECKER.init()`, passed as is |
54
+ | `config` | | Any other key of `window.WEBSPELLCHECKER_CONFIG` (`autoSearch` is always `false`) |
55
+ | `enabled` | `true` | Start with checking on |
56
+ | `disableWhenReadOnly` | `true` | Disable WProofreader while the view is read-only |
57
+ | `nonce` | | Nonce for the `<script>` element (Content Security Policy) |
58
+ | `load` | adds a `<script>` | Load WebSpellChecker yourself; must resolve to `WEBSPELLCHECKER` |
59
+ | `onReady(instance, view)` | | Each instance, once ready |
60
+ | `onError(error)` | `console.error` | The bundle could not be loaded, or WProofreader failed to start |
61
+
62
+ The service settings (`serviceId`, `serviceHost`…, `config`) are read by the bundle once, when it loads: every editor on the page shares them. `lang`, `autocorrect` and the other instance options can differ per editor.
63
+
64
+ ## Commands
65
+
66
+ ```ts
67
+ editor.commands.toggleWProofreader(); // off and on
68
+ editor.commands.toggleWProofreader(false); // off
69
+ editor.commands.openWProofreaderDialog(); // "Proofread in dialog"
70
+ editor.commands.openWProofreaderSettings();
71
+ ```
72
+
73
+ The dialog and settings commands return `false` (and `editor.can()` is `false`) until WProofreader is ready, and while checking is off or the view is read-only. Turning checking off and on is not added to the undo history.
74
+
75
+ `getWProofreader(editor.view)` returns the instance for a view (for its other methods, such as `applyAllSuggestions()` or `setLang()`), and `isWProofreaderEnabled(editor.state)` tells whether checking is on.
76
+
77
+ ## Toolbar
78
+
79
+ ```ts
80
+ import { createLexionToolbar, createStarterKitToolbarItems } from "@lexion-rte/ui";
81
+ import { createWProofreaderToolbarItems } from "@lexion-rte/wproofreader";
82
+
83
+ createLexionToolbar({
84
+ element,
85
+ editor,
86
+ items: [...createStarterKitToolbarItems(), ...createWProofreaderToolbarItems()]
87
+ });
88
+ ```
89
+
90
+ Three buttons: spell checking on and off (pressed while on), "Proofread in dialog" and settings. Choose them with `items: ["toggle", "dialog"]`, rename them with `labels`, and show labels next to the icons with `withLabels: true`.
91
+
92
+ ## Before going live
93
+
94
+ - Content Security Policy: allow scripts from and connections to `svc.webspellchecker.net` (or your own server).
95
+ - Privacy: with the cloud service, the text being checked is sent to WebSpellChecker. For sensitive content, check that this is allowed, or use the self-hosted server.
96
+ - Mobile browsers and IME input were not part of the tests.
@@ -0,0 +1,36 @@
1
+ import { PluginKey, type EditorState } from "prosemirror-state";
2
+ import type { EditorView } from "prosemirror-view";
3
+ import type { WProofreaderInstance, WProofreaderOptions } from "./types.js";
4
+ export interface WProofreaderPluginState {
5
+ /** Checking is on (the `toggleWProofreader` command turns it off and on). */
6
+ readonly enabled: boolean;
7
+ }
8
+ export declare const wproofreaderPluginKey: PluginKey<WProofreaderPluginState>;
9
+ declare module "@lexion-rte/core" {
10
+ interface LexionCommands {
11
+ /** Turns checking off and on, or sets it with `enabled`. */
12
+ toggleWProofreader: [enabled?: boolean];
13
+ /** Opens WProofreader's "Proofread in dialog" window for the editor's view. */
14
+ openWProofreaderDialog: [];
15
+ /** Opens WProofreader's settings window for the editor's view. */
16
+ openWProofreaderSettings: [];
17
+ }
18
+ }
19
+ /** The WProofreader instance checking `view` (`editor.view`), once it is ready. */
20
+ export declare const getWProofreader: (view: EditorView | null | undefined) => WProofreaderInstance | null;
21
+ /** Whether checking is on in `state` (`false` without the extension). */
22
+ export declare const isWProofreaderEnabled: (state: EditorState) => boolean;
23
+ /**
24
+ * WebSpellChecker's WProofreader (spelling and grammar checking) on every view of the editor.
25
+ * It loads the bundle once, starts an instance when a view is mounted (in the browser only),
26
+ * destroys it with the view, and disables it while the view is read-only. You need your own
27
+ * WebSpellChecker license.
28
+ *
29
+ * ```ts
30
+ * new LexionEditor({
31
+ * extensions: [StarterKit, WProofreader.configure({ serviceId: "…", lang: "en_US" })]
32
+ * });
33
+ * ```
34
+ */
35
+ export declare const WProofreader: import("@lexion-rte/core").LexionExtensionDefinition<WProofreaderOptions, undefined>;
36
+ //# sourceMappingURL=extension.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extension.d.ts","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAInD,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAyB,MAAM,YAAY,CAAC;AAEnG,MAAM,WAAW,uBAAuB;IACtC,6EAA6E;IAC7E,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B;AAID,eAAO,MAAM,qBAAqB,oCAAgE,CAAC;AAEnG,OAAO,QAAQ,kBAAkB,CAAC;IAChC,UAAU,cAAc;QACtB,4DAA4D;QAC5D,kBAAkB,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC;QACxC,+EAA+E;QAC/E,sBAAsB,EAAE,EAAE,CAAC;QAC3B,kEAAkE;QAClE,wBAAwB,EAAE,EAAE,CAAC;KAC9B;CACF;AAID,mFAAmF;AACnF,eAAO,MAAM,eAAe,GAC1B,MAAM,UAAU,GAAG,IAAI,GAAG,SAAS,KAClC,oBAAoB,GAAG,IAAqD,CAAC;AAEhF,yEAAyE;AACzE,eAAO,MAAM,qBAAqB,GAAI,OAAO,WAAW,KAAG,OACF,CAAC;AAwJ1D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY,sFAiCvB,CAAC"}
@@ -0,0 +1,174 @@
1
+ import { Plugin, PluginKey } from "prosemirror-state";
2
+ import { Extension } from "@lexion-rte/core";
3
+ import { loadWebSpellChecker } from "./loader.js";
4
+ export const wproofreaderPluginKey = new PluginKey("lexion-wproofreader");
5
+ const instances = new WeakMap();
6
+ /** The WProofreader instance checking `view` (`editor.view`), once it is ready. */
7
+ export const getWProofreader = (view) => (view ? (instances.get(view) ?? null) : null);
8
+ /** Whether checking is on in `state` (`false` without the extension). */
9
+ export const isWProofreaderEnabled = (state) => wproofreaderPluginKey.getState(state)?.enabled ?? false;
10
+ const definedEntries = (source, keys) => Object.fromEntries(keys
11
+ .map((key) => [key, source[key]])
12
+ .filter(([, value]) => value !== undefined));
13
+ const serviceKeys = ["serviceId", "serviceProtocol", "serviceHost", "servicePort", "servicePath"];
14
+ const instanceKeys = [
15
+ "lang",
16
+ "autocorrect",
17
+ "autocomplete",
18
+ "enableGrammar",
19
+ "enableBadgeButton",
20
+ "theme"
21
+ ];
22
+ const reportError = (options, error) => {
23
+ if (options.onError) {
24
+ options.onError(error);
25
+ }
26
+ else {
27
+ console.error(error);
28
+ }
29
+ };
30
+ /** Runs one WProofreader instance for one mounted view, from mount to destroy. */
31
+ class WProofreaderView {
32
+ view;
33
+ options;
34
+ instance = null;
35
+ destroyed = false;
36
+ // A new instance starts enabled.
37
+ active = true;
38
+ constructor(view, options) {
39
+ this.view = view;
40
+ this.options = options;
41
+ const load = options.load ??
42
+ (() => loadWebSpellChecker({
43
+ ...(options.bundleUrl ? { bundleUrl: options.bundleUrl } : {}),
44
+ ...(options.nonce ? { nonce: options.nonce } : {}),
45
+ config: { ...definedEntries(options, serviceKeys), ...(options.config ?? {}) }
46
+ }));
47
+ load().then((wsc) => this.start(wsc), (error) => this.fail(error));
48
+ }
49
+ start(wsc) {
50
+ if (this.destroyed) {
51
+ return;
52
+ }
53
+ try {
54
+ wsc.init({
55
+ ...definedEntries(this.options, instanceKeys),
56
+ ...(this.options.initOptions ?? {}),
57
+ container: this.view.dom
58
+ }, (instance) => this.ready(instance), (error) => this.fail(error));
59
+ }
60
+ catch (error) {
61
+ this.fail(error);
62
+ }
63
+ }
64
+ ready(instance) {
65
+ if (this.destroyed) {
66
+ instance.destroy();
67
+ return;
68
+ }
69
+ this.instance = instance;
70
+ this.active = !instance.isDisabled();
71
+ instances.set(this.view, instance);
72
+ this.sync();
73
+ this.options.onReady?.(instance, this.view);
74
+ // Lets toolbars re-check the commands that need an instance.
75
+ const meta = { ready: true };
76
+ this.view.dispatch(this.view.state.tr.setMeta(wproofreaderPluginKey, meta).setMeta("addToHistory", false));
77
+ }
78
+ fail(error) {
79
+ if (!this.destroyed) {
80
+ reportError(this.options, error);
81
+ }
82
+ }
83
+ /** Enables or disables the instance to match the toggle and the view's read-only mode. */
84
+ sync() {
85
+ const instance = this.instance;
86
+ if (!instance) {
87
+ return;
88
+ }
89
+ const wanted = isWProofreaderEnabled(this.view.state) &&
90
+ (this.options.disableWhenReadOnly === false || this.view.editable);
91
+ if (wanted === this.active) {
92
+ return;
93
+ }
94
+ this.active = wanted;
95
+ if (wanted) {
96
+ instance.enable();
97
+ }
98
+ else {
99
+ instance.disable();
100
+ }
101
+ }
102
+ update() {
103
+ this.sync();
104
+ }
105
+ destroy() {
106
+ this.destroyed = true;
107
+ instances.delete(this.view);
108
+ const instance = this.instance;
109
+ this.instance = null;
110
+ try {
111
+ instance?.destroy();
112
+ }
113
+ catch (error) {
114
+ reportError(this.options, error);
115
+ }
116
+ }
117
+ }
118
+ const runOnInstance = (method) => ({ editor, state, dispatch }) => {
119
+ const instance = getWProofreader(editor.view);
120
+ if (!instance || !isWProofreaderEnabled(state) || instance.isDisabled()) {
121
+ return false;
122
+ }
123
+ if (dispatch) {
124
+ instance[method]();
125
+ }
126
+ return true;
127
+ };
128
+ /**
129
+ * WebSpellChecker's WProofreader (spelling and grammar checking) on every view of the editor.
130
+ * It loads the bundle once, starts an instance when a view is mounted (in the browser only),
131
+ * destroys it with the view, and disables it while the view is read-only. You need your own
132
+ * WebSpellChecker license.
133
+ *
134
+ * ```ts
135
+ * new LexionEditor({
136
+ * extensions: [StarterKit, WProofreader.configure({ serviceId: "…", lang: "en_US" })]
137
+ * });
138
+ * ```
139
+ */
140
+ export const WProofreader = Extension.create({
141
+ name: "wproofreader",
142
+ addOptions: () => ({ enabled: true, disableWhenReadOnly: true }),
143
+ addCommands: () => ({
144
+ toggleWProofreader: ({ state, dispatch }, enabled) => {
145
+ const current = wproofreaderPluginKey.getState(state);
146
+ if (!current) {
147
+ return false;
148
+ }
149
+ const meta = {
150
+ enabled: typeof enabled === "boolean" ? enabled : !current.enabled
151
+ };
152
+ dispatch?.(state.tr.setMeta(wproofreaderPluginKey, meta).setMeta("addToHistory", false));
153
+ return true;
154
+ },
155
+ openWProofreaderDialog: runOnInstance("openDialog"),
156
+ openWProofreaderSettings: runOnInstance("openSettings")
157
+ }),
158
+ addEditorPlugins: ({ options }) => [
159
+ new Plugin({
160
+ key: wproofreaderPluginKey,
161
+ state: {
162
+ init: () => ({ enabled: options.enabled !== false }),
163
+ apply: (tr, value) => {
164
+ const meta = tr.getMeta(wproofreaderPluginKey);
165
+ return meta && "enabled" in meta && meta.enabled !== value.enabled
166
+ ? { enabled: meta.enabled }
167
+ : value;
168
+ }
169
+ },
170
+ view: (view) => new WProofreaderView(view, options)
171
+ })
172
+ ]
173
+ });
174
+ //# sourceMappingURL=extension.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],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CAAC;QACnD,wBAAwB,EAAE,aAAa,CAAC,cAAc,CAAC;KACxD,CAAC;IACF,gBAAgB,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;QACjC,IAAI,MAAM,CAA0B;YAClC,GAAG,EAAE,qBAAqB;YAC1B,KAAK,EAAE;gBACL,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,KAAK,KAAK,EAAE,CAAC;gBACpD,KAAK,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE;oBACnB,MAAM,IAAI,GAAG,EAAE,CAAC,OAAO,CAAC,qBAAqB,CAAiC,CAAC;oBAC/E,OAAO,IAAI,IAAI,SAAS,IAAI,IAAI,IAAI,IAAI,CAAC,OAAO,KAAK,KAAK,CAAC,OAAO;wBAChE,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE;wBAC3B,CAAC,CAAC,KAAK,CAAC;gBACZ,CAAC;aACF;YACD,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC;SACpD,CAAC;KACH;CACF,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ export { WProofreader, getWProofreader, isWProofreaderEnabled, wproofreaderPluginKey } from "./extension.js";
2
+ export type { WProofreaderPluginState } from "./extension.js";
3
+ export { WPROOFREADER_BUNDLE_URL, loadWebSpellChecker } from "./loader.js";
4
+ export type { LoadWebSpellCheckerOptions } from "./loader.js";
5
+ export { createWProofreaderToolbarItems, defaultWProofreaderToolbarLabels } from "./toolbar.js";
6
+ export type { WProofreaderToolbarItem, WProofreaderToolbarItemName, WProofreaderToolbarItemsOptions, WProofreaderToolbarLabels } from "./toolbar.js";
7
+ export type { WProofreaderInstance, WProofreaderOptions, WProofreaderServiceOptions, WebSpellCheckerGlobal } from "./types.js";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,eAAe,EACf,qBAAqB,EACrB,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAC3E,YAAY,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,8BAA8B,EAAE,gCAAgC,EAAE,MAAM,cAAc,CAAC;AAChG,YAAY,EACV,uBAAuB,EACvB,2BAA2B,EAC3B,+BAA+B,EAC/B,yBAAyB,EAC1B,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,oBAAoB,EACpB,mBAAmB,EACnB,0BAA0B,EAC1B,qBAAqB,EACtB,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { WProofreader, getWProofreader, isWProofreaderEnabled, wproofreaderPluginKey } from "./extension.js";
2
+ export { WPROOFREADER_BUNDLE_URL, loadWebSpellChecker } from "./loader.js";
3
+ export { createWProofreaderToolbarItems, defaultWProofreaderToolbarLabels } from "./toolbar.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,eAAe,EACf,qBAAqB,EACrB,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAE3E,OAAO,EAAE,8BAA8B,EAAE,gCAAgC,EAAE,MAAM,cAAc,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { WebSpellCheckerGlobal } from "./types.js";
2
+ /** The cloud service's bundle. */
3
+ export declare const WPROOFREADER_BUNDLE_URL = "https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js";
4
+ export interface LoadWebSpellCheckerOptions {
5
+ readonly bundleUrl?: string;
6
+ /** Merged into `window.WEBSPELLCHECKER_CONFIG` (`autoSearch` is forced to `false`). */
7
+ readonly config?: Readonly<Record<string, unknown>>;
8
+ readonly nonce?: string;
9
+ }
10
+ /**
11
+ * Loads WebSpellChecker's bundle once per URL and resolves to `window.WEBSPELLCHECKER`.
12
+ * Browser only: it rejects during server rendering. A failed load is retried on the next call.
13
+ */
14
+ export declare const loadWebSpellChecker: (options?: LoadWebSpellCheckerOptions) => Promise<WebSpellCheckerGlobal>;
15
+ //# sourceMappingURL=loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAExD,kCAAkC;AAClC,eAAO,MAAM,uBAAuB,wEACmC,CAAC;AAcxE,MAAM,WAAW,0BAA0B;IACzC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,uFAAuF;IACvF,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACpD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,GAC9B,UAAS,0BAA+B,KACvC,OAAO,CAAC,qBAAqB,CA2C/B,CAAC"}
package/dist/loader.js ADDED
@@ -0,0 +1,56 @@
1
+ /** The cloud service's bundle. */
2
+ export const WPROOFREADER_BUNDLE_URL = "https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js";
3
+ const loading = new Map();
4
+ const browserWindow = () => typeof window === "undefined" || typeof document === "undefined"
5
+ ? null
6
+ : window;
7
+ /**
8
+ * Loads WebSpellChecker's bundle once per URL and resolves to `window.WEBSPELLCHECKER`.
9
+ * Browser only: it rejects during server rendering. A failed load is retried on the next call.
10
+ */
11
+ export const loadWebSpellChecker = (options = {}) => {
12
+ const win = browserWindow();
13
+ if (!win) {
14
+ return Promise.reject(new Error("WProofreader needs a browser"));
15
+ }
16
+ if (win.WEBSPELLCHECKER) {
17
+ return Promise.resolve(win.WEBSPELLCHECKER);
18
+ }
19
+ const url = options.bundleUrl ?? WPROOFREADER_BUNDLE_URL;
20
+ const pending = loading.get(url);
21
+ if (pending) {
22
+ return pending;
23
+ }
24
+ // The bundle reads its configuration once, when it runs; without `autoSearch: false` it
25
+ // would check every editable element on the page.
26
+ win.WEBSPELLCHECKER_CONFIG = {
27
+ ...(win.WEBSPELLCHECKER_CONFIG ?? {}),
28
+ ...(options.config ?? {}),
29
+ autoSearch: false
30
+ };
31
+ const promise = new Promise((resolve, reject) => {
32
+ const script = document.createElement("script");
33
+ script.src = url;
34
+ script.async = true;
35
+ if (options.nonce) {
36
+ script.nonce = options.nonce;
37
+ }
38
+ script.addEventListener("load", () => {
39
+ if (win.WEBSPELLCHECKER) {
40
+ resolve(win.WEBSPELLCHECKER);
41
+ }
42
+ else {
43
+ reject(new Error(`WProofreader: ${url} did not define WEBSPELLCHECKER`));
44
+ }
45
+ });
46
+ script.addEventListener("error", () => {
47
+ script.remove();
48
+ reject(new Error(`WProofreader: could not load ${url}`));
49
+ });
50
+ document.head.appendChild(script);
51
+ });
52
+ loading.set(url, promise);
53
+ promise.catch(() => loading.delete(url));
54
+ return promise;
55
+ };
56
+ //# sourceMappingURL=loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAEA,kCAAkC;AAClC,MAAM,CAAC,MAAM,uBAAuB,GAClC,qEAAqE,CAAC;AAOxE,MAAM,OAAO,GAAG,IAAI,GAAG,EAA0C,CAAC;AAElE,MAAM,aAAa,GAAG,GAA4C,EAAE,CAClE,OAAO,MAAM,KAAK,WAAW,I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MAAM,EAAE,GAAG,EAAE;YACnC,IAAI,GAAG,CAAC,eAAe,EAAE,CAAC;gBACxB,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,IAAI,KAAK,CAAC,iBAAiB,GAAG,iCAAiC,CAAC,CAAC,CAAC;YAC3E,CAAC;QACH,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE;YACpC,MAAM,CAAC,MAAM,EAAE,CAAC;YAChB,MAAM,CAAC,IAAI,KAAK,CAAC,gCAAgC,GAAG,EAAE,CAAC,CAAC,CAAC;QAC3D,CAAC,CAAC,CAAC;QACH,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAC1B,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IACzC,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC"}
@@ -0,0 +1,31 @@
1
+ import type { EditorState } from "prosemirror-state";
2
+ export type WProofreaderToolbarItemName = "toggle" | "dialog" | "settings";
3
+ export interface WProofreaderToolbarLabels {
4
+ readonly toggle: string;
5
+ readonly dialog: string;
6
+ readonly settings: string;
7
+ }
8
+ export declare const defaultWProofreaderToolbarLabels: WProofreaderToolbarLabels;
9
+ export interface WProofreaderToolbarItemsOptions {
10
+ /** Which buttons, in order. Default: all three. */
11
+ readonly items?: readonly WProofreaderToolbarItemName[];
12
+ readonly labels?: Partial<WProofreaderToolbarLabels>;
13
+ /** Show the label next to the icon. Default `false` (icon and tooltip only). */
14
+ readonly withLabels?: boolean;
15
+ }
16
+ /** A `@lexion-rte/ui` toolbar item (`LexionToolbarItemInput`), without depending on it. */
17
+ export interface WProofreaderToolbarItem {
18
+ readonly id: string;
19
+ readonly iconClass: string;
20
+ readonly title: string;
21
+ readonly label?: string;
22
+ readonly command: string;
23
+ readonly isActive?: (state: EditorState) => boolean;
24
+ }
25
+ /**
26
+ * Toolbar buttons for `createLexionToolbar` (`@lexion-rte/ui`): spell checking on and off
27
+ * (pressed while on), "Proofread in dialog" and settings. The last two are disabled until
28
+ * WProofreader is ready, and while checking is off or the editor is read-only.
29
+ */
30
+ export declare const createWProofreaderToolbarItems: (options?: WProofreaderToolbarItemsOptions) => readonly WProofreaderToolbarItem[];
31
+ //# sourceMappingURL=toolbar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolbar.d.ts","sourceRoot":"","sources":["../src/toolbar.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIrD,MAAM,MAAM,2BAA2B,GAAG,QAAQ,GAAG,QAAQ,GAAG,UAAU,CAAC;AAE3E,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED,eAAO,MAAM,gCAAgC,EAAE,yBAI9C,CAAC;AAEF,MAAM,WAAW,+BAA+B;IAC9C,mDAAmD;IACnD,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,2BAA2B,EAAE,CAAC;IACxD,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,CAAC;IACrD,gFAAgF;IAChF,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;CAC/B;AAED,2FAA2F;AAC3F,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,OAAO,CAAC;CACrD;AAcD;;;;GAIG;AACH,eAAO,MAAM,8BAA8B,GACzC,UAAS,+BAAoC,KAC5C,SAAS,uBAAuB,EAWlC,CAAC"}
@@ -0,0 +1,34 @@
1
+ import { isWProofreaderEnabled } from "./extension.js";
2
+ export const defaultWProofreaderToolbarLabels = {
3
+ toggle: "Spell checking",
4
+ dialog: "Proofread in dialog",
5
+ settings: "Spell checker settings"
6
+ };
7
+ const icons = {
8
+ toggle: "ri-check-double-line",
9
+ dialog: "ri-file-search-line",
10
+ settings: "ri-settings-3-line"
11
+ };
12
+ const commands = {
13
+ toggle: "toggleWProofreader",
14
+ dialog: "openWProofreaderDialog",
15
+ settings: "openWProofreaderSettings"
16
+ };
17
+ /**
18
+ * Toolbar buttons for `createLexionToolbar` (`@lexion-rte/ui`): spell checking on and off
19
+ * (pressed while on), "Proofread in dialog" and settings. The last two are disabled until
20
+ * WProofreader is ready, and while checking is off or the editor is read-only.
21
+ */
22
+ export const createWProofreaderToolbarItems = (options = {}) => {
23
+ const labels = { ...defaultWProofreaderToolbarLabels, ...(options.labels ?? {}) };
24
+ const names = options.items ?? ["toggle", "dialog", "settings"];
25
+ return names.map((name) => ({
26
+ id: `wproofreader-${name}`,
27
+ iconClass: icons[name],
28
+ title: labels[name],
29
+ ...(options.withLabels ? { label: labels[name] } : {}),
30
+ command: commands[name],
31
+ ...(name === "toggle" ? { isActive: isWProofreaderEnabled } : {})
32
+ }));
33
+ };
34
+ //# sourceMappingURL=toolbar.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolbar.js","sourceRoot":"","sources":["../src/toolbar.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAUvD,MAA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"prosemirror-view";
2
+ /**
3
+ * The parts of WebSpellChecker's browser API this package uses. The bundle defines
4
+ * `window.WEBSPELLCHECKER`; see https://docs.webspellchecker.com for the full API.
5
+ */
6
+ export interface WProofreaderInstance {
7
+ destroy(): void;
8
+ disable(): void;
9
+ enable(): void;
10
+ isDisabled(): boolean;
11
+ /** Opens the "Proofread in dialog" window. */
12
+ openDialog(): void;
13
+ /** Opens WProofreader's settings window. */
14
+ openSettings(): void;
15
+ /** Applies the first suggestion of every problem found. */
16
+ applyAllSuggestions?(): void;
17
+ setLang?(lang: string): void;
18
+ getLang?(): string;
19
+ getVersion?(): string;
20
+ }
21
+ export interface WebSpellCheckerGlobal {
22
+ init(options: Readonly<Record<string, unknown>> & {
23
+ readonly container: HTMLElement;
24
+ }, onSuccess?: (instance: WProofreaderInstance) => void, onError?: (error: unknown) => void): unknown;
25
+ }
26
+ /**
27
+ * Settings of the WebSpellChecker service, shared by every editor on the page: the bundle reads
28
+ * them from `window.WEBSPELLCHECKER_CONFIG` once, when it loads.
29
+ */
30
+ export interface WProofreaderServiceOptions {
31
+ /** Your WebSpellChecker service ID (cloud service). */
32
+ readonly serviceId?: string;
33
+ /** Self-hosted server: `"https"`, `"example.com"`, `443`, `"wscservice/api"`. */
34
+ readonly serviceProtocol?: string;
35
+ readonly serviceHost?: string;
36
+ readonly servicePort?: string | number;
37
+ readonly servicePath?: string;
38
+ }
39
+ export interface WProofreaderOptions extends WProofreaderServiceOptions {
40
+ /** Language, such as `"en_US"`, `"en_GB"`, `"de_DE"` or `"es_ES"`. Default: the service's. */
41
+ readonly lang?: string;
42
+ /** Fix common typos while typing (`Teh` → `The`). WebSpellChecker's default is `true`. */
43
+ readonly autocorrect?: boolean;
44
+ /** Suggest word completions while typing. */
45
+ readonly autocomplete?: boolean;
46
+ /** Check grammar as well as spelling. */
47
+ readonly enableGrammar?: boolean;
48
+ /** Show WProofreader's badge in the corner of the editor. */
49
+ readonly enableBadgeButton?: boolean;
50
+ /** `"default"`, `"gray"` or `"dark"`. */
51
+ readonly theme?: string;
52
+ /**
53
+ * Any other option of `WEBSPELLCHECKER.init()`, passed through as is (for example
54
+ * `{ actionItems: ["addWord", "ignoreAll", "settings"], suggestionsCount: 3 }`).
55
+ */
56
+ readonly initOptions?: Readonly<Record<string, unknown>>;
57
+ /**
58
+ * Any other key of `window.WEBSPELLCHECKER_CONFIG`, set before the bundle loads. `autoSearch`
59
+ * is always `false`: only editors with this extension are checked.
60
+ */
61
+ readonly config?: Readonly<Record<string, unknown>>;
62
+ /**
63
+ * URL of the bundle. Default: the cloud service's. For a self-hosted server, use its own
64
+ * (`https://example.com/wscservice/wscbundle/wscbundle.js`).
65
+ */
66
+ readonly bundleUrl?: string;
67
+ /** Nonce for the `<script>` element, under a Content Security Policy with nonces. */
68
+ readonly nonce?: string;
69
+ /**
70
+ * Loads WebSpellChecker yourself instead of adding a `<script>` (for example from an npm
71
+ * package). It must resolve to the `WEBSPELLCHECKER` object.
72
+ */
73
+ readonly load?: () => Promise<WebSpellCheckerGlobal>;
74
+ /** Start with checking on. Toggle it with the `toggleWProofreader` command. Default `true`. */
75
+ readonly enabled?: boolean;
76
+ /**
77
+ * Disable WProofreader while the view is read-only (its underlines and menus would otherwise
78
+ * stay, since WebSpellChecker does not follow `contenteditable`). Default `true`.
79
+ */
80
+ readonly disableWhenReadOnly?: boolean;
81
+ /** Called with each instance once it is ready, and the view it checks. */
82
+ readonly onReady?: (instance: WProofreaderInstance, view: EditorView) => void;
83
+ /** Called when the bundle cannot be loaded or WProofreader fails to start. Default: `console.error`. */
84
+ readonly onError?: (error: unknown) => void;
85
+ }
86
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,OAAO,IAAI,IAAI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CAAC,YAAY,CAAC,EAAE,OAAO,CAAC;IAChC,yCAAyC;IACzC,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;IACjC,6DAA6D;IAC7D,QAAQ,CAAC,iBAAiB,CAAC,EAAE,OAAO,CAAC;IACrC,yCAAyC;IACzC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACzD;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACpD;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,qFAAqF;IACrF,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,OAAO,CAAC,qBAAqB,CAAC,CAAC;IACrD,+FAA+F;IAC/F,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IACvC,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,oBAAoB,EAAE,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;IAC9E,wGAAwG;IACxG,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CAC7C"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
2
  "name": "@lexion-rte/wproofreader",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "WebSpellChecker WProofreader (spelling and grammar checking) for the Lexion editor: one extension that follows the editor's views and read-only mode.",
5
+ "private": false,
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "sideEffects": false,
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/dariusve/lexion.git",
20
+ "directory": "packages/wproofreader"
21
+ },
22
+ "homepage": "https://github.com/dariusve/lexion/tree/main/packages/wproofreader",
23
+ "bugs": {
24
+ "url": "https://github.com/dariusve/lexion/issues"
25
+ },
26
+ "keywords": [
27
+ "lexion",
28
+ "editor",
29
+ "rich-text",
30
+ "prosemirror",
31
+ "spellcheck",
32
+ "grammar",
33
+ "wproofreader",
34
+ "webspellchecker"
35
+ ],
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "dependencies": {
40
+ "prosemirror-state": "^1.4.3",
41
+ "prosemirror-view": "^1.37.2",
42
+ "@lexion-rte/core": "^0.5.0"
43
+ },
44
+ "scripts": {
45
+ "build": "tsc -p tsconfig.json",
46
+ "lint": "eslint src"
47
+ }
6
48
  }
@@ -0,0 +1,234 @@
1
+ import { Plugin, PluginKey, type EditorState } from "prosemirror-state";
2
+ import type { EditorView } from "prosemirror-view";
3
+ import { Extension } from "@lexion-rte/core";
4
+
5
+ import { loadWebSpellChecker } from "./loader.js";
6
+ import type { WProofreaderInstance, WProofreaderOptions, WebSpellCheckerGlobal } from "./types.js";
7
+
8
+ export interface WProofreaderPluginState {
9
+ /** Checking is on (the `toggleWProofreader` command turns it off and on). */
10
+ readonly enabled: boolean;
11
+ }
12
+
13
+ type WProofreaderMeta = { readonly enabled: boolean } | { readonly ready: true };
14
+
15
+ export const wproofreaderPluginKey = new PluginKey<WProofreaderPluginState>("lexion-wproofreader");
16
+
17
+ declare module "@lexion-rte/core" {
18
+ interface LexionCommands {
19
+ /** Turns checking off and on, or sets it with `enabled`. */
20
+ toggleWProofreader: [enabled?: boolean];
21
+ /** Opens WProofreader's "Proofread in dialog" window for the editor's view. */
22
+ openWProofreaderDialog: [];
23
+ /** Opens WProofreader's settings window for the editor's view. */
24
+ openWProofreaderSettings: [];
25
+ }
26
+ }
27
+
28
+ const instances = new WeakMap<EditorView, WProofreaderInstance>();
29
+
30
+ /** The WProofreader instance checking `view` (`editor.view`), once it is ready. */
31
+ export const getWProofreader = (
32
+ view: EditorView | null | undefined
33
+ ): WProofreaderInstance | null => (view ? (instances.get(view) ?? null) : null);
34
+
35
+ /** Whether checking is on in `state` (`false` without the extension). */
36
+ export const isWProofreaderEnabled = (state: EditorState): boolean =>
37
+ wproofreaderPluginKey.getState(state)?.enabled ?? false;
38
+
39
+ const definedEntries = (source: object, keys: readonly string[]): Record<string, unknown> =>
40
+ Object.fromEntries(
41
+ keys
42
+ .map((key) => [key, (source as Record<string, unknown>)[key]] as const)
43
+ .filter(([, value]) => value !== undefined)
44
+ );
45
+
46
+ const serviceKeys = ["serviceId", "serviceProtocol", "serviceHost", "servicePort", "servicePath"];
47
+ const instanceKeys = [
48
+ "lang",
49
+ "autocorrect",
50
+ "autocomplete",
51
+ "enableGrammar",
52
+ "enableBadgeButton",
53
+ "theme"
54
+ ];
55
+
56
+ const reportError = (options: WProofreaderOptions, error: unknown): void => {
57
+ if (options.onError) {
58
+ options.onError(error);
59
+ } else {
60
+ console.error(error);
61
+ }
62
+ };
63
+
64
+ /** Runs one WProofreader instance for one mounted view, from mount to destroy. */
65
+ class WProofreaderView {
66
+ private instance: WProofreaderInstance | null = null;
67
+ private destroyed = false;
68
+ // A new instance starts enabled.
69
+ private active = true;
70
+
71
+ public constructor(
72
+ private readonly view: EditorView,
73
+ private readonly options: WProofreaderOptions
74
+ ) {
75
+ const load =
76
+ options.load ??
77
+ (() =>
78
+ loadWebSpellChecker({
79
+ ...(options.bundleUrl ? { bundleUrl: options.bundleUrl } : {}),
80
+ ...(options.nonce ? { nonce: options.nonce } : {}),
81
+ config: { ...definedEntries(options, serviceKeys), ...(options.config ?? {}) }
82
+ }));
83
+ load().then(
84
+ (wsc) => this.start(wsc),
85
+ (error: unknown) => this.fail(error)
86
+ );
87
+ }
88
+
89
+ private start(wsc: WebSpellCheckerGlobal): void {
90
+ if (this.destroyed) {
91
+ return;
92
+ }
93
+ try {
94
+ wsc.init(
95
+ {
96
+ ...definedEntries(this.options, instanceKeys),
97
+ ...(this.options.initOptions ?? {}),
98
+ container: this.view.dom as HTMLElement
99
+ },
100
+ (instance) => this.ready(instance),
101
+ (error) => this.fail(error)
102
+ );
103
+ } catch (error) {
104
+ this.fail(error);
105
+ }
106
+ }
107
+
108
+ private ready(instance: WProofreaderInstance): void {
109
+ if (this.destroyed) {
110
+ instance.destroy();
111
+ return;
112
+ }
113
+ this.instance = instance;
114
+ this.active = !instance.isDisabled();
115
+ instances.set(this.view, instance);
116
+ this.sync();
117
+ this.options.onReady?.(instance, this.view);
118
+ // Lets toolbars re-check the commands that need an instance.
119
+ const meta: WProofreaderMeta = { ready: true };
120
+ this.view.dispatch(
121
+ this.view.state.tr.setMeta(wproofreaderPluginKey, meta).setMeta("addToHistory", false)
122
+ );
123
+ }
124
+
125
+ private fail(error: unknown): void {
126
+ if (!this.destroyed) {
127
+ reportError(this.options, error);
128
+ }
129
+ }
130
+
131
+ /** Enables or disables the instance to match the toggle and the view's read-only mode. */
132
+ private sync(): void {
133
+ const instance = this.instance;
134
+ if (!instance) {
135
+ return;
136
+ }
137
+ const wanted =
138
+ isWProofreaderEnabled(this.view.state) &&
139
+ (this.options.disableWhenReadOnly === false || this.view.editable);
140
+ if (wanted === this.active) {
141
+ return;
142
+ }
143
+ this.active = wanted;
144
+ if (wanted) {
145
+ instance.enable();
146
+ } else {
147
+ instance.disable();
148
+ }
149
+ }
150
+
151
+ public update(): void {
152
+ this.sync();
153
+ }
154
+
155
+ public destroy(): void {
156
+ this.destroyed = true;
157
+ instances.delete(this.view);
158
+ const instance = this.instance;
159
+ this.instance = null;
160
+ try {
161
+ instance?.destroy();
162
+ } catch (error) {
163
+ reportError(this.options, error);
164
+ }
165
+ }
166
+ }
167
+
168
+ const runOnInstance =
169
+ (method: "openDialog" | "openSettings") =>
170
+ ({
171
+ editor,
172
+ state,
173
+ dispatch
174
+ }: {
175
+ readonly editor: { readonly view: EditorView | null };
176
+ readonly state: EditorState;
177
+ readonly dispatch?: unknown;
178
+ }): boolean => {
179
+ const instance = getWProofreader(editor.view);
180
+ if (!instance || !isWProofreaderEnabled(state) || instance.isDisabled()) {
181
+ return false;
182
+ }
183
+ if (dispatch) {
184
+ instance[method]();
185
+ }
186
+ return true;
187
+ };
188
+
189
+ /**
190
+ * WebSpellChecker's WProofreader (spelling and grammar checking) on every view of the editor.
191
+ * It loads the bundle once, starts an instance when a view is mounted (in the browser only),
192
+ * destroys it with the view, and disables it while the view is read-only. You need your own
193
+ * WebSpellChecker license.
194
+ *
195
+ * ```ts
196
+ * new LexionEditor({
197
+ * extensions: [StarterKit, WProofreader.configure({ serviceId: "…", lang: "en_US" })]
198
+ * });
199
+ * ```
200
+ */
201
+ export const WProofreader = Extension.create<WProofreaderOptions>({
202
+ name: "wproofreader",
203
+ addOptions: () => ({ enabled: true, disableWhenReadOnly: true }),
204
+ addCommands: () => ({
205
+ toggleWProofreader: ({ state, dispatch }, enabled) => {
206
+ const current = wproofreaderPluginKey.getState(state);
207
+ if (!current) {
208
+ return false;
209
+ }
210
+ const meta: WProofreaderMeta = {
211
+ enabled: typeof enabled === "boolean" ? enabled : !current.enabled
212
+ };
213
+ dispatch?.(state.tr.setMeta(wproofreaderPluginKey, meta).setMeta("addToHistory", false));
214
+ return true;
215
+ },
216
+ openWProofreaderDialog: runOnInstance("openDialog"),
217
+ openWProofreaderSettings: runOnInstance("openSettings")
218
+ }),
219
+ addEditorPlugins: ({ options }) => [
220
+ new Plugin<WProofreaderPluginState>({
221
+ key: wproofreaderPluginKey,
222
+ state: {
223
+ init: () => ({ enabled: options.enabled !== false }),
224
+ apply: (tr, value) => {
225
+ const meta = tr.getMeta(wproofreaderPluginKey) as WProofreaderMeta | undefined;
226
+ return meta && "enabled" in meta && meta.enabled !== value.enabled
227
+ ? { enabled: meta.enabled }
228
+ : value;
229
+ }
230
+ },
231
+ view: (view) => new WProofreaderView(view, options)
232
+ })
233
+ ]
234
+ });
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ export {
2
+ WProofreader,
3
+ getWProofreader,
4
+ isWProofreaderEnabled,
5
+ wproofreaderPluginKey
6
+ } from "./extension.js";
7
+ export type { WProofreaderPluginState } from "./extension.js";
8
+ export { WPROOFREADER_BUNDLE_URL, loadWebSpellChecker } from "./loader.js";
9
+ export type { LoadWebSpellCheckerOptions } from "./loader.js";
10
+ export { createWProofreaderToolbarItems, defaultWProofreaderToolbarLabels } from "./toolbar.js";
11
+ export type {
12
+ WProofreaderToolbarItem,
13
+ WProofreaderToolbarItemName,
14
+ WProofreaderToolbarItemsOptions,
15
+ WProofreaderToolbarLabels
16
+ } from "./toolbar.js";
17
+ export type {
18
+ WProofreaderInstance,
19
+ WProofreaderOptions,
20
+ WProofreaderServiceOptions,
21
+ WebSpellCheckerGlobal
22
+ } from "./types.js";
package/src/loader.ts ADDED
@@ -0,0 +1,75 @@
1
+ import type { WebSpellCheckerGlobal } from "./types.js";
2
+
3
+ /** The cloud service's bundle. */
4
+ export const WPROOFREADER_BUNDLE_URL =
5
+ "https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js";
6
+
7
+ interface WebSpellCheckerWindow {
8
+ WEBSPELLCHECKER?: WebSpellCheckerGlobal;
9
+ WEBSPELLCHECKER_CONFIG?: Record<string, unknown>;
10
+ }
11
+
12
+ const loading = new Map<string, Promise<WebSpellCheckerGlobal>>();
13
+
14
+ const browserWindow = (): (Window & WebSpellCheckerWindow) | null =>
15
+ typeof window === "undefined" || typeof document === "undefined"
16
+ ? null
17
+ : (window as Window & WebSpellCheckerWindow);
18
+
19
+ export interface LoadWebSpellCheckerOptions {
20
+ readonly bundleUrl?: string;
21
+ /** Merged into `window.WEBSPELLCHECKER_CONFIG` (`autoSearch` is forced to `false`). */
22
+ readonly config?: Readonly<Record<string, unknown>>;
23
+ readonly nonce?: string;
24
+ }
25
+
26
+ /**
27
+ * Loads WebSpellChecker's bundle once per URL and resolves to `window.WEBSPELLCHECKER`.
28
+ * Browser only: it rejects during server rendering. A failed load is retried on the next call.
29
+ */
30
+ export const loadWebSpellChecker = (
31
+ options: LoadWebSpellCheckerOptions = {}
32
+ ): Promise<WebSpellCheckerGlobal> => {
33
+ const win = browserWindow();
34
+ if (!win) {
35
+ return Promise.reject(new Error("WProofreader needs a browser"));
36
+ }
37
+ if (win.WEBSPELLCHECKER) {
38
+ return Promise.resolve(win.WEBSPELLCHECKER);
39
+ }
40
+ const url = options.bundleUrl ?? WPROOFREADER_BUNDLE_URL;
41
+ const pending = loading.get(url);
42
+ if (pending) {
43
+ return pending;
44
+ }
45
+ // The bundle reads its configuration once, when it runs; without `autoSearch: false` it
46
+ // would check every editable element on the page.
47
+ win.WEBSPELLCHECKER_CONFIG = {
48
+ ...(win.WEBSPELLCHECKER_CONFIG ?? {}),
49
+ ...(options.config ?? {}),
50
+ autoSearch: false
51
+ };
52
+ const promise = new Promise<WebSpellCheckerGlobal>((resolve, reject) => {
53
+ const script = document.createElement("script");
54
+ script.src = url;
55
+ script.async = true;
56
+ if (options.nonce) {
57
+ script.nonce = options.nonce;
58
+ }
59
+ script.addEventListener("load", () => {
60
+ if (win.WEBSPELLCHECKER) {
61
+ resolve(win.WEBSPELLCHECKER);
62
+ } else {
63
+ reject(new Error(`WProofreader: ${url} did not define WEBSPELLCHECKER`));
64
+ }
65
+ });
66
+ script.addEventListener("error", () => {
67
+ script.remove();
68
+ reject(new Error(`WProofreader: could not load ${url}`));
69
+ });
70
+ document.head.appendChild(script);
71
+ });
72
+ loading.set(url, promise);
73
+ promise.catch(() => loading.delete(url));
74
+ return promise;
75
+ };
package/src/toolbar.ts ADDED
@@ -0,0 +1,67 @@
1
+ import type { EditorState } from "prosemirror-state";
2
+
3
+ import { isWProofreaderEnabled } from "./extension.js";
4
+
5
+ export type WProofreaderToolbarItemName = "toggle" | "dialog" | "settings";
6
+
7
+ export interface WProofreaderToolbarLabels {
8
+ readonly toggle: string;
9
+ readonly dialog: string;
10
+ readonly settings: string;
11
+ }
12
+
13
+ export const defaultWProofreaderToolbarLabels: WProofreaderToolbarLabels = {
14
+ toggle: "Spell checking",
15
+ dialog: "Proofread in dialog",
16
+ settings: "Spell checker settings"
17
+ };
18
+
19
+ export interface WProofreaderToolbarItemsOptions {
20
+ /** Which buttons, in order. Default: all three. */
21
+ readonly items?: readonly WProofreaderToolbarItemName[];
22
+ readonly labels?: Partial<WProofreaderToolbarLabels>;
23
+ /** Show the label next to the icon. Default `false` (icon and tooltip only). */
24
+ readonly withLabels?: boolean;
25
+ }
26
+
27
+ /** A `@lexion-rte/ui` toolbar item (`LexionToolbarItemInput`), without depending on it. */
28
+ export interface WProofreaderToolbarItem {
29
+ readonly id: string;
30
+ readonly iconClass: string;
31
+ readonly title: string;
32
+ readonly label?: string;
33
+ readonly command: string;
34
+ readonly isActive?: (state: EditorState) => boolean;
35
+ }
36
+
37
+ const icons: Readonly<Record<WProofreaderToolbarItemName, string>> = {
38
+ toggle: "ri-check-double-line",
39
+ dialog: "ri-file-search-line",
40
+ settings: "ri-settings-3-line"
41
+ };
42
+
43
+ const commands: Readonly<Record<WProofreaderToolbarItemName, string>> = {
44
+ toggle: "toggleWProofreader",
45
+ dialog: "openWProofreaderDialog",
46
+ settings: "openWProofreaderSettings"
47
+ };
48
+
49
+ /**
50
+ * Toolbar buttons for `createLexionToolbar` (`@lexion-rte/ui`): spell checking on and off
51
+ * (pressed while on), "Proofread in dialog" and settings. The last two are disabled until
52
+ * WProofreader is ready, and while checking is off or the editor is read-only.
53
+ */
54
+ export const createWProofreaderToolbarItems = (
55
+ options: WProofreaderToolbarItemsOptions = {}
56
+ ): readonly WProofreaderToolbarItem[] => {
57
+ const labels = { ...defaultWProofreaderToolbarLabels, ...(options.labels ?? {}) };
58
+ const names = options.items ?? (["toggle", "dialog", "settings"] as const);
59
+ return names.map((name) => ({
60
+ id: `wproofreader-${name}`,
61
+ iconClass: icons[name],
62
+ title: labels[name],
63
+ ...(options.withLabels ? { label: labels[name] } : {}),
64
+ command: commands[name],
65
+ ...(name === "toggle" ? { isActive: isWProofreaderEnabled } : {})
66
+ }));
67
+ };
package/src/types.ts ADDED
@@ -0,0 +1,91 @@
1
+ import type { EditorView } from "prosemirror-view";
2
+
3
+ /**
4
+ * The parts of WebSpellChecker's browser API this package uses. The bundle defines
5
+ * `window.WEBSPELLCHECKER`; see https://docs.webspellchecker.com for the full API.
6
+ */
7
+ export interface WProofreaderInstance {
8
+ destroy(): void;
9
+ disable(): void;
10
+ enable(): void;
11
+ isDisabled(): boolean;
12
+ /** Opens the "Proofread in dialog" window. */
13
+ openDialog(): void;
14
+ /** Opens WProofreader's settings window. */
15
+ openSettings(): void;
16
+ /** Applies the first suggestion of every problem found. */
17
+ applyAllSuggestions?(): void;
18
+ setLang?(lang: string): void;
19
+ getLang?(): string;
20
+ getVersion?(): string;
21
+ }
22
+
23
+ export interface WebSpellCheckerGlobal {
24
+ init(
25
+ options: Readonly<Record<string, unknown>> & { readonly container: HTMLElement },
26
+ onSuccess?: (instance: WProofreaderInstance) => void,
27
+ onError?: (error: unknown) => void
28
+ ): unknown;
29
+ }
30
+
31
+ /**
32
+ * Settings of the WebSpellChecker service, shared by every editor on the page: the bundle reads
33
+ * them from `window.WEBSPELLCHECKER_CONFIG` once, when it loads.
34
+ */
35
+ export interface WProofreaderServiceOptions {
36
+ /** Your WebSpellChecker service ID (cloud service). */
37
+ readonly serviceId?: string;
38
+ /** Self-hosted server: `"https"`, `"example.com"`, `443`, `"wscservice/api"`. */
39
+ readonly serviceProtocol?: string;
40
+ readonly serviceHost?: string;
41
+ readonly servicePort?: string | number;
42
+ readonly servicePath?: string;
43
+ }
44
+
45
+ export interface WProofreaderOptions extends WProofreaderServiceOptions {
46
+ /** Language, such as `"en_US"`, `"en_GB"`, `"de_DE"` or `"es_ES"`. Default: the service's. */
47
+ readonly lang?: string;
48
+ /** Fix common typos while typing (`Teh` → `The`). WebSpellChecker's default is `true`. */
49
+ readonly autocorrect?: boolean;
50
+ /** Suggest word completions while typing. */
51
+ readonly autocomplete?: boolean;
52
+ /** Check grammar as well as spelling. */
53
+ readonly enableGrammar?: boolean;
54
+ /** Show WProofreader's badge in the corner of the editor. */
55
+ readonly enableBadgeButton?: boolean;
56
+ /** `"default"`, `"gray"` or `"dark"`. */
57
+ readonly theme?: string;
58
+ /**
59
+ * Any other option of `WEBSPELLCHECKER.init()`, passed through as is (for example
60
+ * `{ actionItems: ["addWord", "ignoreAll", "settings"], suggestionsCount: 3 }`).
61
+ */
62
+ readonly initOptions?: Readonly<Record<string, unknown>>;
63
+ /**
64
+ * Any other key of `window.WEBSPELLCHECKER_CONFIG`, set before the bundle loads. `autoSearch`
65
+ * is always `false`: only editors with this extension are checked.
66
+ */
67
+ readonly config?: Readonly<Record<string, unknown>>;
68
+ /**
69
+ * URL of the bundle. Default: the cloud service's. For a self-hosted server, use its own
70
+ * (`https://example.com/wscservice/wscbundle/wscbundle.js`).
71
+ */
72
+ readonly bundleUrl?: string;
73
+ /** Nonce for the `<script>` element, under a Content Security Policy with nonces. */
74
+ readonly nonce?: string;
75
+ /**
76
+ * Loads WebSpellChecker yourself instead of adding a `<script>` (for example from an npm
77
+ * package). It must resolve to the `WEBSPELLCHECKER` object.
78
+ */
79
+ readonly load?: () => Promise<WebSpellCheckerGlobal>;
80
+ /** Start with checking on. Toggle it with the `toggleWProofreader` command. Default `true`. */
81
+ readonly enabled?: boolean;
82
+ /**
83
+ * Disable WProofreader while the view is read-only (its underlines and menus would otherwise
84
+ * stay, since WebSpellChecker does not follow `contenteditable`). Default `true`.
85
+ */
86
+ readonly disableWhenReadOnly?: boolean;
87
+ /** Called with each instance once it is ready, and the view it checks. */
88
+ readonly onReady?: (instance: WProofreaderInstance, view: EditorView) => void;
89
+ /** Called when the bundle cannot be loaded or WProofreader fails to start. Default: `console.error`. */
90
+ readonly onError?: (error: unknown) => void;
91
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "lib": ["ES2022", "DOM"],
5
+ "rootDir": "src",
6
+ "outDir": "dist"
7
+ },
8
+ "include": ["src/**/*.ts"]
9
+ }