forked from FirebaseExtended/firepad
-
Notifications
You must be signed in to change notification settings - Fork 20
/
cursor-widget.ts
248 lines (203 loc) · 6.7 KB
/
cursor-widget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright (c) 2019 Convergence Labs, Inc.
*
* This file is part of the Monaco Collaborative Extensions, which is
* released under the terms of the MIT license. A copy of the MIT license
* is usually provided as part of this source code package in the LICENCE
* file. If it was not, please see <https://opensource.org/licenses/MIT>
*/
import * as monaco from "monaco-editor";
import { ClientIDType } from "./editor-adapter";
import * as Utils from "./utils";
type OnDisposed = Utils.VoidFunctionType;
export interface ICursorWidgetConstructorOptions {
codeEditor: monaco.editor.ICodeEditor;
widgetId: ClientIDType;
color: string;
label: string;
range: monaco.Range;
tooltipDuration?: number;
opacity?: string;
onDisposed: OnDisposed;
}
export interface ICursorWidget
extends monaco.editor.IContentWidget,
Utils.IDisposable {
/**
* Update Widget position according to the Cursor position.
* @param range - Current Position of the Cursor.
*/
updatePosition(range: monaco.Range): void;
/**
* Update Widget content when Username changes.
* @param userName - New Username of the current User.
*/
updateContent(userName?: string): void;
/**
* Returns whether the widget was disposed or not.
*/
isDisposed(): boolean;
}
/**
* This class implements a Monaco Content Widget to render a remote user's
* name in a tooltip.
*/
export class CursorWidget implements ICursorWidget {
protected readonly _id: string;
protected readonly _editor: monaco.editor.ICodeEditor;
protected readonly _domNode: HTMLElement;
protected readonly _tooltipDuration: number;
protected readonly _scrollListener: monaco.IDisposable | null;
protected readonly _onDisposed: OnDisposed;
protected _tooltipNode: HTMLElement;
protected _color: string;
protected _content: string;
protected _opacity: string;
protected _position: monaco.editor.IContentWidgetPosition | null;
protected _hideTimer: any;
protected _disposed: boolean;
static readonly WIDGET_NODE_CLASSNAME = "firepad-remote-cursor";
static readonly TOOLTIP_NODE_CLASSNAME = "firepad-remote-cursor-message";
constructor({
codeEditor,
widgetId,
color,
label,
range,
tooltipDuration = 1000,
opacity = "1.0",
onDisposed,
}: ICursorWidgetConstructorOptions) {
this._editor = codeEditor;
this._tooltipDuration = tooltipDuration;
this._id = `monaco-remote-cursor-${widgetId}`;
this._onDisposed = onDisposed;
this._color = color;
this._content = label;
this._opacity = opacity;
this._domNode = this._createWidgetNode();
// we only need to listen to scroll positions to update the
// tooltip location on scrolling.
this._scrollListener = this._editor.onDidScrollChange(() => {
this._updateTooltipPosition();
});
this.updatePosition(range);
this._hideTimer = null;
this._editor.addContentWidget(this);
this._disposed = false;
}
getId(): string {
return this._id;
}
getDomNode(): HTMLElement {
return this._domNode;
}
getPosition(): monaco.editor.IContentWidgetPosition | null {
return this._position;
}
updatePosition(range: monaco.Range): void {
this._updatePosition(range);
setTimeout(() => this._showTooltip(), 0);
}
updateContent(userName?: string): void {
if (typeof userName !== "string" || userName === this._content) {
return;
}
this._tooltipNode.textContent = userName;
}
dispose(): void {
if (this._disposed) {
return;
}
this._editor.removeContentWidget(this);
if (this._scrollListener !== null) {
this._scrollListener.dispose();
}
this._disposed = true;
this._onDisposed();
}
isDisposed(): boolean {
return this._disposed;
}
protected _updatePosition(range: monaco.Range): void {
this._position = {
position: range.getEndPosition(),
preference: [
monaco.editor.ContentWidgetPositionPreference.ABOVE,
monaco.editor.ContentWidgetPositionPreference.BELOW,
],
};
this._editor.layoutContentWidget(this);
}
protected _showTooltip(): void {
this._updateTooltipPosition();
if (this._hideTimer !== null) {
clearTimeout(this._hideTimer);
} else {
this._setTooltipVisible(true);
}
this._hideTimer = setTimeout(() => {
this._setTooltipVisible(false);
this._hideTimer = null;
}, this._tooltipDuration);
}
protected _updateTooltipPosition(): void {
const distanceFromTop =
this._domNode.offsetTop - this._editor.getScrollTop();
if (distanceFromTop - this._tooltipNode.offsetHeight < 5) {
this._tooltipNode.style.top = `${this._tooltipNode.offsetHeight + 2}px`;
} else {
this._tooltipNode.style.top = `-${this._tooltipNode.offsetHeight}px`;
}
this._tooltipNode.style.left = "0";
}
protected _setTooltipVisible(visible: boolean): void {
if (visible) {
this._tooltipNode.style.display = "block";
} else {
this._tooltipNode.style.display = "none";
}
}
protected _colorWithCSSVars(property: string): string {
const varName = `--color-${property}-${CursorWidget.WIDGET_NODE_CLASSNAME}`;
return `var(${varName}, ${this._color})`;
}
protected _getTextColor(): string {
const rgb = Utils.hexToRgb(this._color);
const brightness = Math.round(
(rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000
);
return brightness >= 125 ? "#000000" : "#ffffff";
}
protected _createTooltipNode(): HTMLElement {
const tooltipNode = document.createElement("div");
tooltipNode.style.borderColor = this._colorWithCSSVars("border");
tooltipNode.style.backgroundColor = this._colorWithCSSVars("bg");
tooltipNode.style.color = this._getTextColor();
tooltipNode.style.opacity = this._opacity;
tooltipNode.style.borderRadius = "2px";
tooltipNode.style.fontSize = "12px";
tooltipNode.style.padding = "2px 8px";
tooltipNode.style.whiteSpace = "nowrap";
tooltipNode.textContent = this._content;
const className = `${
CursorWidget.TOOLTIP_NODE_CLASSNAME
}-${this._color.replace("#", "")}`;
tooltipNode.classList.add(className, CursorWidget.TOOLTIP_NODE_CLASSNAME);
return tooltipNode;
}
protected _createWidgetNode(): HTMLElement {
Utils.validateTruth(document != null, "This package must run on browser!");
const widgetNode = document.createElement("div");
widgetNode.style.height = "20px";
widgetNode.style.paddingBottom = "0px";
widgetNode.style.transition = "all 0.1s linear";
this._tooltipNode = this._createTooltipNode();
widgetNode.appendChild(this._tooltipNode);
widgetNode.classList.add(
"monaco-editor-overlaymessage",
CursorWidget.WIDGET_NODE_CLASSNAME
);
return widgetNode;
}
}