Skip to content

Commit

Permalink
Fix an issue where selection layer is not redrawn (#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Jan 18, 2025
1 parent 2e7c553 commit d65c0fb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CoreEditor/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export function afterDomUpdate(callback: () => void) {
setTimeout(callback, 50);
}

export function forceRedrawElement(element: HTMLElement) {
const visibility = element.style.visibility;
element.style.visibility = 'hidden';
afterDomUpdate(() => element.style.visibility = visibility);
}

export function sleep(milliseconds: number) {
// eslint-disable-next-line compat/compat
return new Promise(resolve => setTimeout(resolve, milliseconds));
Expand Down
6 changes: 6 additions & 0 deletions CoreEditor/src/modules/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { tokenizePosition } from '../tokenizer';
import { scrollCaretToVisible, scrollToSelection, selectedLineColumn, updateActiveLine } from '../../modules/selection';

import hasSelection from '../selection/hasSelection';
import redrawSelectionLayer from '../selection/redrawSelectionLayer';
import wrapBlock from './wrapBlock';
import insertCodeBlock from './insertCodeBlock';

Expand Down Expand Up @@ -129,6 +130,11 @@ export function observeChanges() {
updateActiveLine(newHasSelection);
}

// Work around a WebKit bug where selection layer is not updated
if (!newHasSelection && selectionStateChanged) {
redrawSelectionLayer();
}

// Handle native updates.
//
// It would be great if we could also provide the updated text here,
Expand Down
10 changes: 10 additions & 0 deletions CoreEditor/src/modules/selection/redrawSelectionLayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { forceRedrawElement } from '../../common/utils';

export default function redrawSelectionLayer() {
const layer = document.querySelector('.cm-selectionLayer') as HTMLElement | null;
if (layer === null) {
return;
}

forceRedrawElement(layer);
}

0 comments on commit d65c0fb

Please sign in to comment.