Skip to content

Commit 62762a8

Browse files
authored
Merge pull request #181 from nperez0111/fix/selections
fix: use TextSelection.between for restoring the selection
2 parents b730c1a + 2298733 commit 62762a8

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/plugins/sync-plugin.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { createMutex } from 'lib0/mutex'
66
import * as PModel from 'prosemirror-model'
7-
import { AllSelection, Plugin, TextSelection } from "prosemirror-state"; // eslint-disable-line
7+
import { AllSelection, Plugin, TextSelection, NodeSelection } from "prosemirror-state"; // eslint-disable-line
88
import * as math from 'lib0/math'
99
import * as object from 'lib0/object'
1010
import * as set from 'lib0/set'
@@ -242,14 +242,22 @@ export const ySyncPlugin = (yXmlFragment, {
242242
}
243243

244244
/**
245-
* @param {any} tr
246-
* @param {any} relSel
245+
* @param {import('prosemirror-state').Transaction} tr
246+
* @param {ReturnType<typeof getRelativeSelection>} relSel
247247
* @param {ProsemirrorBinding} binding
248248
*/
249249
const restoreRelativeSelection = (tr, relSel, binding) => {
250250
if (relSel !== null && relSel.anchor !== null && relSel.head !== null) {
251251
if (relSel.type === 'all') {
252252
tr.setSelection(new AllSelection(tr.doc))
253+
} else if (relSel.type === 'node') {
254+
const anchor = relativePositionToAbsolutePosition(
255+
binding.doc,
256+
binding.type,
257+
relSel.anchor,
258+
binding.mapping
259+
)
260+
tr.setSelection(NodeSelection.create(tr.doc, anchor))
253261
} else {
254262
const anchor = relativePositionToAbsolutePosition(
255263
binding.doc,
@@ -264,7 +272,7 @@ const restoreRelativeSelection = (tr, relSel, binding) => {
264272
binding.mapping
265273
)
266274
if (anchor !== null && head !== null) {
267-
tr = tr.setSelection(TextSelection.create(tr.doc, anchor, head))
275+
tr.setSelection(TextSelection.between(tr.doc.resolve(anchor), tr.doc.resolve(head)))
268276
}
269277
}
270278
}

0 commit comments

Comments
 (0)