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 76
/
string.ts
181 lines (158 loc) · 3.88 KB
/
string.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Signal, ISignal
} from '@phosphor/signaling';
import {
IObservableString, ObservableString
} from '@jupyterlab/coreutils';
import {
GoogleRealtimeObject
} from './googlerealtime';
export
class GoogleString implements IObservableString, GoogleRealtimeObject {
constructor (str: gapi.drive.realtime.CollaborativeString) {
this.googleObject = str;
}
type: 'String';
/**
* Set the value of the string.
*/
set text( value: string ) {
if(this._str.length === value.length && this._str.getText() === value) {
return;
}
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();
}
/**
* Get the underlying `gapi.drive.realtime.CollaborativeString`
* for this string.
*/
get googleObject(): gapi.drive.realtime.CollaborativeString {
return this._str;
}
/**
* Set the underlying `gapi.drive.realtime.CollaborativeString`
* for this string.
*/
set googleObject(str: gapi.drive.realtime.CollaborativeString) {
let prevText = '';
if(this._str) {
prevText = this._str.getText();
this._str.removeAllEventListeners();
}
// Set the new string.
this._str = 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
});
}
});
//Trigger text set event if necessary
if (prevText !== this._str.getText()) {
this._changed.emit({
type: 'set',
start: 0,
end: this._str.length,
value: this._str.getText()
});
}
}
/**
* A signal emitted when the string has changed.
*/
get changed(): ISignal<IObservableString, ObservableString.IChangedArgs> {
return this._changed;
}
/**
* 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 = '';
}
/**
* 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();
Signal.clearData(this);
this._isDisposed = true;
}
private _changed = new Signal<IObservableString, ObservableString.IChangedArgs>(this);
private _str: gapi.drive.realtime.CollaborativeString = null;
private _isDisposed: boolean = false;
}