Skip to content

Commit 17cc80a

Browse files
Merge pull request #202 from SwiftFiddle/balloon
Add editor balloon(tooltip)
2 parents 2b8e1ee + 030e3bd commit 17cc80a

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

Public/css/balloon.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.balloon-content {
2+
font-size: 70%;
3+
white-space: nowrap;
4+
border-radius: 5px;
5+
background-color: #555;
6+
color: #fff;
7+
padding: 2px 8px;
8+
position: absolute;
9+
transform: translateX(-30%);
10+
}
11+
12+
.balloon-content:before {
13+
content: "";
14+
position: absolute;
15+
top: 34px;
16+
left: calc(50% - 9px);
17+
border: 9px solid transparent;
18+
border-top: 9px solid #555;
19+
}
20+
21+
.balloon-content .title {
22+
color: #fff;
23+
font-weight: bolder;
24+
}
25+
26+
.balloon-content .range {
27+
color: #dcdcdc;
28+
font-family: Menlo, Consolas, "DejaVu Sans Mono", "Ubuntu Mono", monospace;
29+
}

Public/js/app.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Tooltip } from "bootstrap";
44
import { Editor } from "./editor.js";
5+
import { Balloon } from "./balloon.js";
56
import { StructureView } from "./structure_view.js";
67
import { SyntaxView } from "./syntax_view.js";
78
import { StatisticsView } from "./statistics_view.js";
@@ -18,6 +19,7 @@ export class App {
1819

1920
constructor() {
2021
this.editor = new Editor(document.getElementById("editor-container"));
22+
this.balloon = new Balloon();
2123

2224
this.structureView = new StructureView(
2325
document.getElementById("structure")
@@ -145,8 +147,19 @@ export class App {
145147

146148
updateStructure(structureData) {
147149
this.structureView.update(structureData);
150+
148151
this.structureView.onmouseover = (event, target, data) => {
149-
this.editor.setSelection(data.range);
152+
const title = data.token ? "Token" : `${data.text}`;
153+
const range = data.range;
154+
155+
this.balloon.content = `<div class="title">${title}</div><div class="range">${range.startRow}:${range.startColumn} - ${range.endRow}:${range.endColumn}</div>`;
156+
this.balloon.show(this.editor.charCoords(range));
157+
158+
this.editor.setSelection(range);
159+
};
160+
this.structureView.onmouseout = (event, target, data) => {
161+
this.balloon.hide();
162+
this.editor.clearSelection();
150163
};
151164
}
152165

Public/js/balloon.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
3+
import "../css/balloon.css";
4+
5+
export class Balloon {
6+
set content(value) {
7+
this.balloon.innerHTML = value;
8+
}
9+
10+
constructor() {
11+
this.balloon = document.createElement("div");
12+
13+
this.onmouseover = () => {};
14+
this.onmouseout = () => {};
15+
16+
this.init();
17+
}
18+
19+
init() {
20+
this.balloon.classList.add("d-none", "balloon-content");
21+
document.body.appendChild(this.balloon);
22+
23+
this.balloon.addEventListener("mouseenter", (event) => {
24+
event.preventDefault();
25+
event.stopPropagation();
26+
this.onmouseover(event);
27+
});
28+
this.balloon.addEventListener("mouseleave", (event) => {
29+
event.preventDefault();
30+
event.stopPropagation();
31+
this.onmouseout(event);
32+
});
33+
}
34+
35+
show(position) {
36+
this.balloon.classList.remove("d-none");
37+
const bounds = this.balloon.getBoundingClientRect();
38+
39+
this.balloon.style.top = `${position.top - bounds.height - 4}px`;
40+
this.balloon.style.left = `${position.left + 10}px`;
41+
}
42+
43+
hide() {
44+
this.balloon.classList.add("d-none");
45+
}
46+
}

Public/js/editor.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
"use strict";
22

33
import "codemirror/lib/codemirror.css";
4+
import "../css/editor.css";
45

56
import CodeMirror from "codemirror";
67
import "codemirror/mode/swift/swift";
78
import "codemirror/addon/edit/matchbrackets";
89
import "codemirror/addon/edit/closebrackets";
910

10-
import "../css/editor.css";
11-
1211
export class Editor {
1312
constructor(container) {
1413
this.container = container;
15-
1614
this.init();
1715
}
1816

@@ -82,6 +80,13 @@ export class Editor {
8280
});
8381
}
8482

83+
charCoords(range) {
84+
return this.editor.charCoords(
85+
{ ch: range.startColumn - 1, line: range.startRow - 1 },
86+
"page"
87+
);
88+
}
89+
8590
focus() {
8691
this.editor.focus();
8792
}

Public/js/structure_view.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class StructureView {
1313
this.popover = new Popover();
1414

1515
this.onmouseover = () => {};
16+
this.onmouseout = () => {};
1617

1718
this.init();
1819
}
@@ -51,6 +52,8 @@ export class StructureView {
5152
};
5253

5354
this.treeView.onmouseout = (event, target, data) => {
55+
this.onmouseout(event, target, data);
56+
5457
if (!event.relatedTarget) {
5558
return;
5659
}

0 commit comments

Comments
 (0)