Skip to content

Commit

Permalink
Synchronize cursor when the snapshot event is published
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Nov 3, 2024
1 parent 20a84cf commit 168e9b8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions frontend/src/utils/yorkie/yorkieSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,22 @@ class YorkieSyncPluginValue implements cmView.PluginValue {

// The text is replaced to snapshot and must be re-synced.
const text = this._doc.getRoot().content;
view.dispatch({
const selection = this._doc.getMyPresence().selection;
const transactionSpec: cmState.TransactionSpec = {
changes: { from: 0, to: view.state.doc.length, insert: text.toString() },
annotations: [cmState.Transaction.remote.of(true)],
});
};

if (selection) {
// Restore the cursor position when the text is replaced.
const cursor = text.posRangeToIndexRange(selection);
transactionSpec["selection"] = {
anchor: cursor[0],
head: cursor[1],
};
}

view.dispatch(transactionSpec);
});

this._doc.update((root) => {
Expand Down

0 comments on commit 168e9b8

Please sign in to comment.