@nocentraldb/tiptap 0.2.0-alpha.37 → 0.2.0-alpha.39

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.
@@ -5,7 +5,15 @@ export interface NcEditorOptions {
5
5
  element: HTMLElement;
6
6
  /** The editor's extensions, such as StarterKit, without Collaboration and its undo, which ncEditor adds. */
7
7
  extensions: AnyExtension[];
8
- /** Any other options of TipTap's editor; ncEditor sets its own content check, element, extensions and editable. */
8
+ /**
9
+ * Any other options of TipTap's editor; ncEditor sets its own content
10
+ * check, element, extensions and editable. In a rebuild, the new editor
11
+ * calls these callbacks, such as onTransaction, onUpdate,
12
+ * onSelectionUpdate and onFocus, before onEditor hands it over: inside a
13
+ * callback, take the editor from its `props.editor`, never from the app's
14
+ * state or a closure, which still hold the destroyed one. A callback that
15
+ * throws is reported with reportError and does not stop the rebuild.
16
+ */
9
17
  editorOptions?: Partial<EditorOptions>;
10
18
  /** Called with the first editor and with each one a rebuild makes; read the editor from here, never from a closure. */
11
19
  onEditor: (editor: Editor) => void;
package/dist/ncEditor.js CHANGED
@@ -17,6 +17,19 @@
17
17
  // starts, since TipTap takes the focus away before onContentError (NOC-555),
18
18
  // and hands each editor to `onEditor`. A composition in the editor reaches
19
19
  // the binding, which holds its writes and pulls meanwhile.
20
+ //
21
+ // The timing is the one the spike on NOC-555 measured in Chromium, Firefox
22
+ // and WebKit (NOC-626):
23
+ //
24
+ // - The rebuild runs in a microtask after the content error. y-tiptap
25
+ // refuses every local edit from the error on, so a key in a task between
26
+ // the two would be lost.
27
+ // - The end of a composition reaches the binding one task after
28
+ // compositionend. ProseMirror reads the composition's last DOM change in
29
+ // a microtask after compositionend, and the binding's deferred pull, with
30
+ // the rebuild it can cause, would otherwise overtake that read.
31
+ // - An app callback that throws, onEditor or one of editorOptions, is
32
+ // reported with reportError and does not stop the rebuild.
20
33
  import { Editor, Extension, getSchema, } from "@tiptap/core";
21
34
  import Collaboration from "@tiptap/extension-collaboration";
22
35
  import { Plugin, TextSelection } from "@tiptap/pm/state";
@@ -116,10 +129,39 @@ const placeholderNames = (base, widened) => {
116
129
  attributes,
117
130
  };
118
131
  };
132
+ /** Reports an app's error without stopping what ncEditor does. */
133
+ const report = (error) => {
134
+ if (typeof globalThis.reportError === "function") {
135
+ globalThis.reportError(error);
136
+ }
137
+ else {
138
+ setTimeout(() => {
139
+ throw error;
140
+ }, 0);
141
+ }
142
+ };
143
+ /** `callback`, with what it throws reported instead of thrown. */
144
+ const guarded = (callback) => (...args) => {
145
+ try {
146
+ callback(...args);
147
+ }
148
+ catch (error) {
149
+ report(error);
150
+ }
151
+ };
152
+ /** The app's editor options, each callback guarded. */
153
+ const guardedOptions = (options = {}) => Object.fromEntries(Object.entries(options).map(([key, value]) => [
154
+ key,
155
+ /^on[A-Z]/.test(key) && typeof value === "function"
156
+ ? guarded(value)
157
+ : value,
158
+ ]));
119
159
  /** Builds the field's editor and keeps rebuilding it; `destroy` ends it. */
120
160
  export const ncEditor = (body, options) => {
121
- const { element, extensions, editorOptions, onEditor } = options;
161
+ const { element, extensions } = options;
122
162
  checkVocabulary(extensions, body.vocabulary);
163
+ const editorOptions = guardedOptions(options.editorOptions);
164
+ const onEditor = guarded(options.onEditor);
123
165
  let editor;
124
166
  let composing = false;
125
167
  let pending = false;
@@ -151,7 +193,7 @@ export const ncEditor = (body, options) => {
151
193
  onContentError: () => {
152
194
  pending = true;
153
195
  if (!composing)
154
- setTimeout(rebuild, 0);
196
+ queueMicrotask(() => rebuild());
155
197
  },
156
198
  extensions: [
157
199
  ...extensions,
@@ -204,9 +246,11 @@ export const ncEditor = (body, options) => {
204
246
  };
205
247
  const compositionEnd = () => {
206
248
  composing = false;
207
- body.composing(false);
208
- if (pending)
209
- setTimeout(rebuild, 0);
249
+ setTimeout(() => {
250
+ body.composing(false);
251
+ if (pending)
252
+ rebuild();
253
+ }, 0);
210
254
  };
211
255
  element.addEventListener("compositionstart", compositionStart, true);
212
256
  element.addEventListener("compositionend", compositionEnd, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocentraldb/tiptap",
3
- "version": "0.2.0-alpha.37",
3
+ "version": "0.2.0-alpha.39",
4
4
  "license": "MIT OR Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,7 +31,7 @@
31
31
  "yjs": "13.6.33"
32
32
  },
33
33
  "peerDependencies": {
34
- "@nocentraldb/sdk": "0.2.0-alpha.37",
34
+ "@nocentraldb/sdk": "0.2.0-alpha.39",
35
35
  "@tiptap/core": "3.35.0",
36
36
  "@tiptap/extension-collaboration": "3.35.0",
37
37
  "@tiptap/pm": "3.35.0",