Skip to content

Commit 1554ffc

Browse files
authored
Merge pull request #110 from atom-community/select-text
2 parents c5d18d5 + ceff0e2 commit 1554ffc

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

lib/datatip-manager.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
TextEditorElement,
88
CommandEvent,
99
CursorPositionChangedEvent,
10+
TextEditorComponent,
1011
} from "atom"
1112
import type { Datatip, DatatipProvider } from "atom-ide-base"
1213
import { ViewContainer } from "atom-ide-base/commons-ui/float-pane/ViewContainer"
@@ -474,6 +475,10 @@ export class DataTipManager {
474475
const overlayMarker = editor.markBufferRange(new Range(position, position), {
475476
invalidate: "never",
476477
})
478+
479+
// makes the text selectable with the help of user-select: text
480+
element.setAttribute("tabindex", "-1")
481+
477482
editor.decorateMarker(overlayMarker, {
478483
type: "overlay",
479484
class: "datatip-overlay",
@@ -482,12 +487,27 @@ export class DataTipManager {
482487
})
483488
disposables.add(new Disposable(() => overlayMarker.destroy()))
484489

490+
const editorComponent = atom.views.getView(editor).getComponent()
491+
485492
element.addEventListener("mouseenter", () => {
486493
this.editorView?.removeEventListener("mousemove", this.onMouseMoveEvt)
494+
element.addEventListener("keydown", copyListener)
487495
})
488496

489497
element.addEventListener("mouseleave", () => {
490498
this.editorView?.addEventListener("mousemove", this.onMouseMoveEvt)
499+
element.removeEventListener("keydown", copyListener)
500+
})
501+
502+
/**
503+
- focus on the datatip once the text is selected (cursor gets disabled temporarily)
504+
- remove focus once mouse leaves
505+
*/
506+
element.addEventListener("mousedown", () => {
507+
blurEditor(editorComponent)
508+
element.addEventListener("mouseleave", () => {
509+
focusEditor(editorComponent)
510+
})
491511
})
492512

493513
// TODO move this code to atom-ide-base
@@ -512,3 +532,25 @@ export class DataTipManager {
512532
this.dataTipMarkerDisposables = null
513533
}
514534
}
535+
536+
// TODO we should not need this
537+
/** A manual copy listener */
538+
async function copyListener(event: KeyboardEvent) {
539+
event.preventDefault()
540+
if (event.ctrlKey && event.key === "c") {
541+
const text = document.getSelection()?.toString() ?? ""
542+
await navigator.clipboard.writeText(text)
543+
}
544+
}
545+
546+
function focusEditor(editorComponent: TextEditorComponent) {
547+
// @ts-ignore
548+
editorComponent?.didFocus()
549+
}
550+
551+
function blurEditor(editorComponent: TextEditorComponent) {
552+
// @ts-ignore
553+
editorComponent?.didBlurHiddenInput({
554+
relatedTarget: null,
555+
})
556+
}

styles/atom-ide-datatips.less

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
overflow: auto; // prevents the long text to come out of the datatip
66
color: @syntax-text-color;
77
white-space: nowrap;
8+
89
pointer-events: all; // hyperlinks will work
9-
// user-select: text; // allow selecting text // TODO does not work
10+
user-select: text; // allow selecting text
1011

1112
font-family: @font-family;
1213
font-size: var(--editor-font-size);
@@ -46,11 +47,6 @@
4647
}
4748

4849
.datatip-overlay {
49-
z-index: 12 !important; // HACK: exceed the z-index of
50-
// .atom-dock-resize-handle-resizable, so that
51-
// mouseleaves aren't triggered when the cursor enters
52-
// the resizable
53-
5450
// info border
5551
border-bottom: 5px solid @background-color-highlight;
5652
border-radius: 3px;
@@ -85,20 +81,17 @@
8581
.list-tree {
8682
cursor: inherit;
8783
}
88-
// user-select: text; // allow selecting text // TODO does not work
8984
}
9085

9186
/** Applied to MarkdownView */
9287

9388
.datatip-markdown > div:not(:last-child) {
9489
border-bottom: 1px solid fade(@syntax-cursor-color, 10%);
95-
// user-select: text; // allow selecting text // TODO does not work
9690
}
9791

9892
.datatip-markdown-container {
9993
color: @syntax-text-color;
10094
white-space: normal;
101-
// user-select: text;
10295

10396
// Avoid excess internal padding from markdown blocks.
10497
:first-child {
@@ -130,7 +123,6 @@
130123
.cursors {
131124
display: none;
132125
}
133-
// user-select: text; // allow selecting text // TODO does not work
134126
}
135127

136128
// Highlight the hovered words

0 commit comments

Comments
 (0)