@@ -6,10 +6,18 @@ export const PROSEMIRROR_SMOOTH_CURSOR_CLASS = "prosemirror-smooth-cursor";
6
6
export function smoothCursorPlugin ( ) : Plugin {
7
7
let smoothCursor : HTMLElement | null = typeof document === "undefined" ? null : document . createElement ( "div" ) ;
8
8
let rafId : number | undefined ;
9
+ let isEditorFocused = false ;
9
10
10
11
function updateCursor ( view ?: EditorView , cursor ?: HTMLElement ) {
11
12
if ( ! view || ! view . dom || view . isDestroyed || ! cursor ) return ;
12
13
14
+ // Hide cursor if editor is not focused
15
+ if ( ! isEditorFocused ) {
16
+ cursor . style . display = "none" ;
17
+ return ;
18
+ }
19
+ cursor . style . display = "block" ;
20
+
13
21
const { state, dom } = view ;
14
22
const { selection } = state ;
15
23
if ( ! isTextSelection ( selection ) ) return ;
@@ -43,28 +51,40 @@ export function smoothCursorPlugin(): Plugin {
43
51
44
52
const update = ( ) => {
45
53
if ( rafId !== undefined ) {
46
- console . log ( "cleaning up 1: " + rafId ) ;
47
54
cancelAnimationFrame ( rafId ) ;
48
55
}
49
56
updateCursor ( view , cursor ) ;
50
57
} ;
51
58
59
+ const handleFocus = ( ) => {
60
+ isEditorFocused = true ;
61
+ update ( ) ;
62
+ } ;
63
+
64
+ const handleBlur = ( ) => {
65
+ isEditorFocused = false ;
66
+ update ( ) ;
67
+ } ;
68
+
52
69
let observer : ResizeObserver | undefined ;
53
70
if ( window . ResizeObserver ) {
54
71
observer = new window . ResizeObserver ( update ) ;
55
72
observer ?. observe ( view . dom ) ;
56
73
}
57
74
58
75
doc . addEventListener ( "selectionchange" , update ) ;
76
+ view . dom . addEventListener ( "focus" , handleFocus ) ;
77
+ view . dom . addEventListener ( "blur" , handleBlur ) ;
59
78
60
79
return {
61
80
update,
62
81
destroy : ( ) => {
63
82
doc . removeEventListener ( "selectionchange" , update ) ;
83
+ view . dom . removeEventListener ( "focus" , handleFocus ) ;
84
+ view . dom . removeEventListener ( "blur" , handleBlur ) ;
64
85
observer ?. unobserve ( view . dom ) ;
65
86
// Clean up any pending animation frame
66
87
if ( rafId !== undefined ) {
67
- console . log ( "cleaning up 2: " + rafId ) ;
68
88
cancelAnimationFrame ( rafId ) ;
69
89
}
70
90
} ,
@@ -81,9 +101,9 @@ export function smoothCursorPlugin(): Plugin {
81
101
] ) ;
82
102
} ,
83
103
84
- attributes : {
85
- class : "smooth-cursor-enabled" ,
86
- } ,
104
+ attributes : ( ) => ( {
105
+ class : isEditorFocused ? "smooth-cursor-enabled" : " ",
106
+ } ) ,
87
107
} ,
88
108
} ) ;
89
109
}
0 commit comments