Skip to content

Commit 7b261e1

Browse files
committed
fix: create selectable-overlay file
1 parent 235163e commit 7b261e1

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

lib/datatip-manager.ts

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import {
77
TextEditorElement,
88
CommandEvent,
99
CursorPositionChangedEvent,
10-
TextEditorComponent,
1110
} from "atom"
1211
import type { Datatip, DatatipProvider } from "atom-ide-base"
1312
import { ViewContainer } from "atom-ide-base/commons-ui/float-pane/ViewContainer"
1413
import { ProviderRegistry } from "atom-ide-base/commons-atom/ProviderRegistry"
14+
import { makeOverlaySelectable, copyListener } from "./selectable-overlay"
1515

1616
export class DataTipManager {
1717
/**
@@ -476,7 +476,7 @@ export class DataTipManager {
476476
invalidate: "never",
477477
})
478478

479-
//
479+
// makes overlay selectable
480480
makeOverlaySelectable(editor, element)
481481

482482
editor.decorateMarker(overlayMarker, {
@@ -487,7 +487,6 @@ export class DataTipManager {
487487
})
488488
disposables.add(new Disposable(() => overlayMarker.destroy()))
489489

490-
491490
element.addEventListener("mouseenter", () => {
492491
this.editorView?.removeEventListener("mousemove", this.onMouseMoveEvt)
493492
element.addEventListener("keydown", copyListener)
@@ -520,46 +519,3 @@ export class DataTipManager {
520519
this.dataTipMarkerDisposables = null
521520
}
522521
}
523-
524-
/** makes the text selectable with the help of user-select: text */
525-
function makeOverlaySelectable(editor: TextEditor, overlayElement: HTMLElement) {
526-
overlayElement.setAttribute("tabindex", "-1")
527-
overlayFocusFix(editor, overlayElement)
528-
}
529-
530-
function overlayFocusFix(editor: TextEditor, element: HTMLElement) {
531-
const editorComponent = atom.views.getView(editor).getComponent()
532-
/**
533-
- focus on the datatip once the text is selected (cursor gets disabled temporarily)
534-
- remove focus once mouse leaves
535-
*/
536-
element.addEventListener("mousedown", () => {
537-
blurEditor(editorComponent)
538-
element.addEventListener("mouseleave", () => {
539-
focusEditor(editorComponent)
540-
})
541-
})
542-
543-
}
544-
545-
// TODO we should not need this
546-
/** A manual copy listener */
547-
async function copyListener(event: KeyboardEvent) {
548-
event.preventDefault()
549-
if (event.ctrlKey && event.key === "c") {
550-
const text = document.getSelection()?.toString() ?? ""
551-
await navigator.clipboard.writeText(text)
552-
}
553-
}
554-
555-
function focusEditor(editorComponent: TextEditorComponent) {
556-
// @ts-ignore
557-
editorComponent?.didFocus()
558-
}
559-
560-
function blurEditor(editorComponent: TextEditorComponent) {
561-
// @ts-ignore
562-
editorComponent?.didBlurHiddenInput({
563-
relatedTarget: null,
564-
})
565-
}

lib/selectable-overlay.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { TextEditor, TextEditorComponent } from "atom"
2+
3+
/** makes the text selectable with the help of `user-select: text` and `pointer-events: all` in CSS */
4+
export function makeOverlaySelectable(editor: TextEditor, overlayElement: HTMLElement) {
5+
overlayElement.setAttribute("tabindex", "-1")
6+
overlayFocusFix(editor, overlayElement)
7+
}
8+
9+
/**
10+
- focus on the datatip once the text is selected (cursor gets disabled temporarily)
11+
- remove focus once mouse leaves
12+
*/
13+
function overlayFocusFix(editor: TextEditor, element: HTMLElement) {
14+
const editorComponent = atom.views.getView(editor).getComponent()
15+
element.addEventListener("mousedown", () => {
16+
blurEditor(editorComponent)
17+
element.addEventListener("mouseleave", () => {
18+
focusEditor(editorComponent)
19+
})
20+
})
21+
}
22+
23+
/** A manual copy listener
24+
* Usage. Add the listener to your mouse enter and mouseleave listeners
25+
```ts
26+
element.addEventListener("mouseenter", () => {element.addEventListener("keydown", copyListener)}`
27+
element.addEventListener("mouseleave", () => {element.removeEventListener("keydown", copyListener)}`
28+
```
29+
*/
30+
export async function copyListener(event: KeyboardEvent) {
31+
event.preventDefault()
32+
if (event.ctrlKey && event.key === "c") {
33+
const text = document.getSelection()?.toString() ?? ""
34+
await navigator.clipboard.writeText(text)
35+
}
36+
} // TODO we should not need to manually listen for copy paste
37+
38+
function focusEditor(editorComponent: TextEditorComponent) {
39+
// @ts-ignore
40+
editorComponent?.didFocus()
41+
}
42+
43+
function blurEditor(editorComponent: TextEditorComponent) {
44+
// @ts-ignore
45+
editorComponent?.didBlurHiddenInput({
46+
relatedTarget: null,
47+
})
48+
}

0 commit comments

Comments
 (0)