77 */
88
99import type { Binding } from './Bindings' ;
10- import type { BaseSelection , NodeKey , NodeMap , Point } from 'lexical' ;
10+ import type {
11+ BaseSelection ,
12+ LexicalEditor ,
13+ NodeKey ,
14+ NodeMap ,
15+ Point ,
16+ } from 'lexical' ;
1117import type { AbsolutePosition , RelativePosition } from 'yjs' ;
1218
1319import { createDOMRange , createRectsFromDOMRange } from '@lexical/selection' ;
1420import {
21+ $createPoint ,
1522 $getNodeByKey ,
1623 $getSelection ,
1724 $isElementNode ,
@@ -34,16 +41,10 @@ import {CollabTextNode} from './CollabTextNode';
3441import { getPositionFromElementAndOffset } from './Utils' ;
3542
3643export type CursorSelection = {
37- anchor : {
38- key : NodeKey ;
39- offset : number ;
40- } ;
44+ anchor : Point ;
4145 caret : HTMLElement ;
4246 color : string ;
43- focus : {
44- key : NodeKey ;
45- offset : number ;
46- } ;
47+ focus : Point ;
4748 name : HTMLSpanElement ;
4849 selections : Array < HTMLElement > ;
4950} ;
@@ -153,7 +154,8 @@ function destroyCursor(binding: Binding, cursor: Cursor) {
153154 }
154155}
155156
156- function createCursorSelection (
157+ function $createCursorSelection (
158+ editor : LexicalEditor ,
157159 cursor : Cursor ,
158160 anchorKey : NodeKey ,
159161 anchorOffset : number ,
@@ -167,17 +169,24 @@ function createCursorSelection(
167169 name . textContent = cursor . name ;
168170 name . style . cssText = `position:absolute;left:-2px;top:-16px;background-color:${ color } ;color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;` ;
169171 caret . appendChild ( name ) ;
172+
173+ const editorState = editor . getEditorState ( ) ;
174+ const anchorNode = editorState . read ( ( ) => $getNodeByKey ( anchorKey ) ) ;
175+ const focusNode = editorState . read ( ( ) => $getNodeByKey ( focusKey ) ) ;
176+
170177 return {
171- anchor : {
172- key : anchorKey ,
173- offset : anchorOffset ,
174- } ,
178+ anchor : $createPoint (
179+ anchorKey ,
180+ anchorOffset ,
181+ $isElementNode ( anchorNode ) ? 'element' : 'text' ,
182+ ) ,
175183 caret,
176184 color,
177- focus : {
178- key : focusKey ,
179- offset : focusOffset ,
180- } ,
185+ focus : $createPoint (
186+ focusKey ,
187+ focusOffset ,
188+ $isElementNode ( focusNode ) ? 'element' : 'text' ,
189+ ) ,
181190 name,
182191 selections : [ ] ,
183192 } ;
@@ -410,7 +419,7 @@ function getCollabNodeAndOffset(
410419 return [ null , 0 ] ;
411420}
412421
413- export function syncCursorPositions (
422+ export function $ syncCursorPositions(
414423 binding : Binding ,
415424 provider : Provider ,
416425) : void {
@@ -447,20 +456,17 @@ export function syncCursorPositions(
447456 selection = cursor . selection ;
448457
449458 if ( selection === null ) {
450- selection = createCursorSelection (
459+ selection = $createCursorSelection (
460+ editor ,
451461 cursor ,
452462 anchorKey ,
453463 anchorOffset ,
454464 focusKey ,
455465 focusOffset ,
456466 ) ;
457467 } else {
458- const anchor = selection . anchor ;
459- const focus = selection . focus ;
460- anchor . key = anchorKey ;
461- anchor . offset = anchorOffset ;
462- focus . key = focusKey ;
463- focus . offset = focusOffset ;
468+ $setPoint ( selection . anchor , anchorKey , anchorOffset ) ;
469+ $setPoint ( selection . focus , focusKey , focusOffset ) ;
464470 }
465471 }
466472 }
@@ -484,6 +490,8 @@ export function syncCursorPositions(
484490 }
485491 }
486492}
493+ /** @deprecated renamed to {@link $syncCursorPositions} by @lexical/eslint-plugin rules-of-lexical */
494+ export const syncCursorPositions = $syncCursorPositions ;
487495
488496export function syncLexicalSelectionToYjs (
489497 binding : Binding ,
0 commit comments