Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap doc.update to ensure doc.update code is executed only once #222

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions frontend/src/components/editor/YorkieIntelligenceFeature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ function YorkieIntelligenceFeature(props: YorkieIntelligenceFeatureProps) {
const selectionFrom = replace ? from : from + 1;
const selectionTo = from + insert.length;

editorStore.doc?.update((root, presence) => {
root.content.edit(from, to, insert);
presence.set({
selection: root.content.indexRangeToPosRange([selectionFrom, selectionTo]),
});
});
editorStore.cmView?.dispatch({
changes: { from, to, insert },
selection: {
anchor: selectionFrom,
head: selectionTo,
},
});
editorStore.doc?.update((root, presence) => {
root.content.edit(from, to, insert);
presence.set({
selection: root.content.indexRangeToPosRange([selectionFrom, selectionTo]),
});
});
onClose();
};

Expand Down
12 changes: 6 additions & 6 deletions frontend/src/utils/yorkie/yorkieSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ class YorkieSyncPluginValue implements cmView.PluginValue {
continue;
}
let adj = 0;
tr.changes.iterChanges((fromA, toA, _, __, inserted) => {
const insertText = inserted.toJSON().join("\n");
this._doc.update((root) => {
this._doc.update((root) => {
tr.changes.iterChanges((fromA, toA, _, __, inserted) => {
const insertText = inserted.toJSON().join("\n");
root.content.edit(fromA + adj, toA + adj, insertText);
adj += insertText.length - (toA - fromA);
});
adj += insertText.length - (toA - fromA);
});
}
const isSuccessful =
this.view.state.doc.toString() === this._doc.getRoot().content.toString();

if (!isSuccessful) {
const errMessage = `YorkieSyncPlugin: Failed to sync the document
CM: ${this.view.state.doc.toString()}
Yorkie: ${this._doc.getRoot().content.toString()}`;
CM: ${this.view.state.doc.toString()}
Yorkie: ${this._doc.getRoot().content.toString()}`;
devleejb marked this conversation as resolved.
Show resolved Hide resolved

console.error(errMessage);
Sentry.captureMessage(errMessage);
Expand Down