44
55import { createMutex } from 'lib0/mutex'
66import * 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
88import * as math from 'lib0/math'
99import * as object from 'lib0/object'
1010import * 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 */
249249const 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