Skip to content

Commit d798833

Browse files
fix: hide cursor when editor not focused
1 parent e206224 commit d798833

File tree

4 files changed

+25
-217
lines changed

4 files changed

+25
-217
lines changed

packages/editor/src/core/extensions/smooth-cursor/plugin-better.ts

-103
This file was deleted.

packages/editor/src/core/extensions/smooth-cursor/plugin-good.ts

-97
This file was deleted.

packages/editor/src/core/extensions/smooth-cursor/plugin.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ export const PROSEMIRROR_SMOOTH_CURSOR_CLASS = "prosemirror-smooth-cursor";
66
export function smoothCursorPlugin(): Plugin {
77
let smoothCursor: HTMLElement | null = typeof document === "undefined" ? null : document.createElement("div");
88
let rafId: number | undefined;
9+
let isEditorFocused = false;
910

1011
function updateCursor(view?: EditorView, cursor?: HTMLElement) {
1112
if (!view || !view.dom || view.isDestroyed || !cursor) return;
1213

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+
1321
const { state, dom } = view;
1422
const { selection } = state;
1523
if (!isTextSelection(selection)) return;
@@ -43,28 +51,40 @@ export function smoothCursorPlugin(): Plugin {
4351

4452
const update = () => {
4553
if (rafId !== undefined) {
46-
console.log("cleaning up 1: " + rafId);
4754
cancelAnimationFrame(rafId);
4855
}
4956
updateCursor(view, cursor);
5057
};
5158

59+
const handleFocus = () => {
60+
isEditorFocused = true;
61+
update();
62+
};
63+
64+
const handleBlur = () => {
65+
isEditorFocused = false;
66+
update();
67+
};
68+
5269
let observer: ResizeObserver | undefined;
5370
if (window.ResizeObserver) {
5471
observer = new window.ResizeObserver(update);
5572
observer?.observe(view.dom);
5673
}
5774

5875
doc.addEventListener("selectionchange", update);
76+
view.dom.addEventListener("focus", handleFocus);
77+
view.dom.addEventListener("blur", handleBlur);
5978

6079
return {
6180
update,
6281
destroy: () => {
6382
doc.removeEventListener("selectionchange", update);
83+
view.dom.removeEventListener("focus", handleFocus);
84+
view.dom.removeEventListener("blur", handleBlur);
6485
observer?.unobserve(view.dom);
6586
// Clean up any pending animation frame
6687
if (rafId !== undefined) {
67-
console.log("cleaning up 2: " + rafId);
6888
cancelAnimationFrame(rafId);
6989
}
7090
},
@@ -81,9 +101,9 @@ export function smoothCursorPlugin(): Plugin {
81101
]);
82102
},
83103

84-
attributes: {
85-
class: "smooth-cursor-enabled",
86-
},
104+
attributes: () => ({
105+
class: isEditorFocused ? "smooth-cursor-enabled" : "",
106+
}),
87107
},
88108
});
89109
}

packages/editor/src/styles/editor.css

-12
Original file line numberDiff line numberDiff line change
@@ -478,17 +478,6 @@ p + p {
478478
[data-background-color="purple"] {
479479
background-color: var(--editor-colors-purple-background);
480480
}
481-
/* end background colors */
482-
@keyframes caret-flash-smooth {
483-
0%,
484-
100% {
485-
opacity: 0;
486-
}
487-
488-
50% {
489-
opacity: 1;
490-
}
491-
}
492481

493482
.smooth-cursor-enabled {
494483
caret-color: transparent;
@@ -504,5 +493,4 @@ p + p {
504493
transition: transform 0.2s var(--ease-out-quart);
505494
will-change: transform;
506495
opacity: 0.8;
507-
animation: cursorPulse 0.8s ease-in-out infinite;
508496
}

0 commit comments

Comments
 (0)