Skip to content

Commit fa3a5a1

Browse files
committed
fix(web): drop chip selection highlight when the editor blurs
Lexical retains the range selection on blur without emitting an update, so the mirrored highlight outlived the native one. Signed-off-by: Yordis Prieto <[email protected]>
1 parent 8d7be1f commit fa3a5a1

1 file changed

Lines changed: 49 additions & 10 deletions

File tree

‎apps/web/src/components/ComposerPromptEditor.tsx‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import {
2828
KEY_ENTER_COMMAND,
2929
KEY_TAB_COMMAND,
3030
COMMAND_PRIORITY_HIGH,
31+
COMMAND_PRIORITY_LOW,
3132
KEY_BACKSPACE_COMMAND,
33+
BLUR_COMMAND,
34+
FOCUS_COMMAND,
3235
$getRoot,
3336
HISTORY_MERGE_TAG,
3437
DecoratorNode,
@@ -1178,7 +1181,24 @@ function ComposerChipSelectionPlugin() {
11781181

11791182
useEffect(() => {
11801183
let selectedKeys = new Set<string>();
1181-
return editor.registerUpdateListener(() => {
1184+
// Lexical keeps the range selection on blur without emitting an update,
1185+
// so focus is tracked separately; while blurred the native highlight is
1186+
// gone and the mirrored one has to go with it.
1187+
let hasFocus = editor.getRootElement() === document.activeElement;
1188+
1189+
const applyKeys = (nextKeys: Set<string>) => {
1190+
for (const key of selectedKeys) {
1191+
if (!nextKeys.has(key)) {
1192+
editor.getElementByKey(key)?.removeAttribute("data-composer-chip-selected");
1193+
}
1194+
}
1195+
for (const key of nextKeys) {
1196+
editor.getElementByKey(key)?.setAttribute("data-composer-chip-selected", "true");
1197+
}
1198+
selectedKeys = nextKeys;
1199+
};
1200+
1201+
const readSelectedKeys = () => {
11821202
const nextKeys = new Set<string>();
11831203
editor.getEditorState().read(() => {
11841204
const selection = $getSelection();
@@ -1190,16 +1210,35 @@ function ComposerChipSelectionPlugin() {
11901210
}
11911211
}
11921212
});
1193-
for (const key of selectedKeys) {
1194-
if (!nextKeys.has(key)) {
1195-
editor.getElementByKey(key)?.removeAttribute("data-composer-chip-selected");
1196-
}
1197-
}
1198-
for (const key of nextKeys) {
1199-
editor.getElementByKey(key)?.setAttribute("data-composer-chip-selected", "true");
1200-
}
1201-
selectedKeys = nextKeys;
1213+
return nextKeys;
1214+
};
1215+
1216+
const unregisterUpdate = editor.registerUpdateListener(() => {
1217+
applyKeys(hasFocus ? readSelectedKeys() : new Set());
12021218
});
1219+
const unregisterFocus = editor.registerCommand(
1220+
FOCUS_COMMAND,
1221+
() => {
1222+
hasFocus = true;
1223+
applyKeys(readSelectedKeys());
1224+
return false;
1225+
},
1226+
COMMAND_PRIORITY_LOW,
1227+
);
1228+
const unregisterBlur = editor.registerCommand(
1229+
BLUR_COMMAND,
1230+
() => {
1231+
hasFocus = false;
1232+
applyKeys(new Set());
1233+
return false;
1234+
},
1235+
COMMAND_PRIORITY_LOW,
1236+
);
1237+
return () => {
1238+
unregisterUpdate();
1239+
unregisterFocus();
1240+
unregisterBlur();
1241+
};
12031242
}, [editor]);
12041243

12051244
return null;

0 commit comments

Comments
 (0)