This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
realtimestring.ts
167 lines (148 loc) · 3.58 KB
/
realtimestring.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
clearSignalData, defineSignal, ISignal
} from 'phosphor/lib/core/signaling';
import {
IObservableString, ObservableString
} from 'jupyterlab/lib/common/observablestring';
declare let gapi : any;
export
class GoogleRealtimeString implements IObservableString {
constructor(model : any, id : string, initialValue?: string) {
this._str = model.getRoot().get(id);
if(!this._str) {
this._str = model.createString(initialValue);
model.getRoot().set(id, this._str);
}
//Add event listeners to the collaborativeString
this._str.addEventListener(
gapi.drive.realtime.EventType.TEXT_INSERTED,
(evt : any) => {
if(!evt.isLocal) {
this.changed.emit({
type : 'insert',
start: evt.index,
end: evt.index + evt.text.length,
value: evt.text
});
}
});
this._str.addEventListener(
gapi.drive.realtime.EventType.TEXT_DELETED,
(evt : any) => {
if(!evt.isLocal) {
this.changed.emit({
type : 'remove',
start: evt.index,
end: evt.index + evt.text.length,
value: evt.text
});
}
});
}
/**
* A signal emitted when the string has changed.
*/
changed: ISignal<IObservableString, ObservableString.IChangedArgs>;
/**
* Whether this string is linkable.
*
* @returns `false'
*/
readonly isLinkable: boolean = false;
/**
* Set the value of the string.
*/
set text( value: string ) {
this._str.setText(value);
this.changed.emit({
type: 'set',
start: 0,
end: value.length,
value: value
});
}
/**
* Get the value of the string.
*/
get text(): string {
return this._str.getText();
}
/**
* Insert a substring.
*
* @param index - The starting index.
*
* @param text - The substring to insert.
*/
insert(index: number, text: string): void {
this._str.insertString(index, text);
this.changed.emit({
type: 'insert',
start: index,
end: index + text.length,
value: text
});
}
/**
* Remove a substring.
*
* @param start - The starting index.
*
* @param end - The ending index.
*/
remove(start: number, end: number): void {
let oldValue: string = this.text.slice(start, end);
this._str.removeRange(start, end);
this.changed.emit({
type: 'remove',
start: start,
end: end,
value: oldValue
});
}
/**
* Set the ObservableString to an empty string.
*/
clear(): void {
this.text = '';
}
/**
* Link the string to another string.
* Any changes to either are mirrored in the other.
*
* @param str: the parent string.
*/
link(str: IObservableString): void {
//no-op
}
/**
* Unlink the string from its parent string.
*/
unlink(): void {
//no-op
}
/**
* Test whether the string has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources held by the string.
*/
dispose(): void {
if(this._isDisposed) {
return;
}
this._str.removeAllEventListeners();
clearSignalData(this);
this._isDisposed = true;
}
private _model : gapi.drive.realtime.Model = null;
private _str : gapi.drive.realtime.CollaborativeString = null;
private _isDisposed : boolean = false;
}
// Define the signals for the Google realtime string.
defineSignal(GoogleRealtimeString.prototype, 'changed');