jazz-tools 0.17.7 → 0.17.8

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/dist/index.js CHANGED
@@ -37,7 +37,7 @@ import {
37
37
  randomSessionProvider,
38
38
  subscribeToCoValue,
39
39
  zodReExport_exports
40
- } from "./chunk-SJHXI5AB.js";
40
+ } from "./chunk-L3GGWY3X.js";
41
41
 
42
42
  // src/tools/auth/clerk/index.ts
43
43
  import {
@@ -2086,28 +2086,52 @@ function proseMirrorToHtml(doc2) {
2086
2086
 
2087
2087
  // src/prosemirror/lib/sync.ts
2088
2088
  import { recreateTransform } from "@manuscripts/prosemirror-recreate-steps";
2089
+ import { EditorState } from "prosemirror-state";
2089
2090
  var META_KEY = "fromJazz";
2090
2091
  function createSyncHandlers(coRichText) {
2091
2092
  let view;
2093
+ let localChange = false;
2094
+ let remoteChange = false;
2092
2095
  function handleCoRichTextChange(newText) {
2093
- if (!view || !newText) return;
2094
- const pmDoc = htmlToProseMirror(
2095
- newText.toString(),
2096
- view.state.doc.type.schema
2097
- );
2098
- const transform = recreateTransform(view.state.doc, pmDoc);
2099
- const tr = view.state.tr;
2100
- transform.steps.forEach((step) => {
2101
- tr.step(step);
2096
+ if (!view || !newText || localChange || remoteChange) return;
2097
+ const currentView = view;
2098
+ remoteChange = true;
2099
+ queueMicrotask(() => {
2100
+ const pmDoc = htmlToProseMirror(
2101
+ newText.toString(),
2102
+ currentView.state.doc.type.schema
2103
+ );
2104
+ try {
2105
+ const transform = recreateTransform(currentView.state.doc, pmDoc);
2106
+ const tr = currentView.state.tr;
2107
+ transform.steps.forEach((step) => {
2108
+ tr.step(step);
2109
+ });
2110
+ tr.setMeta(META_KEY, true);
2111
+ currentView.dispatch(tr);
2112
+ } catch (err) {
2113
+ const newState = EditorState.create({
2114
+ schema: currentView.state.schema,
2115
+ doc: pmDoc,
2116
+ plugins: currentView.state.plugins,
2117
+ selection: currentView.state.selection
2118
+ });
2119
+ currentView.updateState(newState);
2120
+ } finally {
2121
+ remoteChange = false;
2122
+ }
2102
2123
  });
2103
- tr.setMeta(META_KEY, true);
2104
- view.dispatch(tr);
2105
2124
  }
2106
2125
  function handleProseMirrorChange(tr) {
2107
2126
  if (!coRichText || tr.getMeta(META_KEY)) return;
2108
2127
  if (tr.docChanged) {
2109
2128
  const str = proseMirrorToHtml(tr.doc);
2110
- coRichText.applyDiff(str);
2129
+ localChange = true;
2130
+ try {
2131
+ coRichText.applyDiff(str);
2132
+ } finally {
2133
+ localChange = false;
2134
+ }
2111
2135
  }
2112
2136
  }
2113
2137
  return {