forked from FirebaseExtended/firepad
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcursor-widget-controller.ts
109 lines (96 loc) · 3.05 KB
/
cursor-widget-controller.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
import * as monaco from "monaco-editor";
import { CursorWidget, ICursorWidget } from "./cursor-widget";
import { ClientIDType } from "./editor-adapter";
import { IDisposable } from "./utils";
export interface ICursorWidgetController extends IDisposable {
/**
* Add Cursor Widget for the first time for new User.
* @param clientId - Unique Identifier for remote User.
* @param range - Position of the Widget.
* @param userColor - Border Color of the Widget.
* @param userName - User's name to show up in Widget. (optional, default is set to User ID).
*/
addCursor(
clientId: ClientIDType,
range: monaco.Range,
userColor: string,
userName?: string
): void;
/**
* Update Cursor Widget for existing User, or add a new Cursor Widget if new User.
* @param clientId - Unique Identifier for remote User.
* @param range - Position of the Widget.
* @param userColor - Border Color of the Widget.
* @param userName - User's name to show up in Widget. (optional, default is set to User ID).
*/
updateCursor(
clientId: ClientIDType,
range: monaco.Range,
userColor: string,
userName?: string
): void;
/**
* Dispose Cursor Widget for existing User.
* @param clientId - Unique Identifier for remote User.
*/
removeCursor(clientId: ClientIDType): void;
}
export class CursorWidgetController implements ICursorWidgetController {
protected readonly _cursors: Map<ClientIDType, ICursorWidget>;
protected readonly _editor: monaco.editor.IStandaloneCodeEditor;
protected readonly _tooltipDuration: number;
constructor(editor: monaco.editor.IStandaloneCodeEditor) {
this._editor = editor;
this._tooltipDuration = 1000;
this._cursors = new Map<ClientIDType, ICursorWidget>();
}
addCursor(
clientId: ClientIDType,
range: monaco.Range,
userColor: string,
userName?: string
): void {
const cursorWidget = new CursorWidget({
codeEditor: this._editor,
widgetId: clientId,
color: userColor,
range,
label: userName || clientId.toString(),
tooltipDuration: this._tooltipDuration,
onDisposed: () => {
this.removeCursor(clientId);
},
});
this._cursors.set(clientId, cursorWidget);
}
removeCursor(clientId: ClientIDType): void {
const cursorWidget = this._cursors.get(clientId);
if (!cursorWidget) {
/** Already disposed, nothing to do here. */
return;
}
cursorWidget.dispose();
this._cursors.delete(clientId);
}
updateCursor(
clientId: ClientIDType,
range: monaco.Range,
userColor: string,
userName?: string
): void {
const cursorWidget = this._cursors.get(clientId);
if (cursorWidget) {
/** Widget already present, simple update should suffice. */
cursorWidget.updatePosition(range);
cursorWidget.updateContent(userName);
return;
}
this.addCursor(clientId, range, userColor, userName);
}
dispose(): void {
this._cursors.forEach((cursorWidget: ICursorWidget) =>
cursorWidget.dispose()
);
this._cursors.clear();
}
}