-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
270 lines (200 loc) · 7.28 KB
/
code.js
File metadata and controls
270 lines (200 loc) · 7.28 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// Text field accessors
//
function getEditorView() {
let cmEditorElement = document.querySelector(".cm-editor") // Or whatever query you need
let editorView = cmEditorElement.querySelector(".cm-content").cmView.view
return editorView
}
function getLength() {
editorView = getEditorView()
length = editorView.viewState.state.doc.length
return length
}
function clearTextField() {
editorView = getEditorView()
editorView.dispatch({
changes: {from: 0, to: getLength(editorView), insert: ""}
})
}
function setTextField(text) {
editorView = getEditorView()
clearTextField(editorView)
editorView.dispatch({
changes: {from: 0, insert: text}
})
}
function getText() {
editorView = getEditorView()
text = editorView.viewState.state.doc.toString()
cursorPos = getCursorPos()
return {
'text': text,
'cursorPos': cursorPos,
}
function getCursorPos() {
editorView.state.selection.ranges[0].from
}
function setCursorPos(pos) {
editorView.dispatch({selection: {anchor: pos, head: pos}})
}
//
// Slider accessors
//
function getSliderElement() {
sliderElement = document.querySelector('#__next > div.page-wrapper > div.content-wrapper > div.page-content.page-content--no-padding > div > div.container.container--fixed-height > div.container__item.container__item--scrollable.container__item--output > div.item__content > div.Preview_preview__pMaO6 > div > div:nth-child(1)')
return sliderElement
}
function getSliderWidth() {
sliderElement = getSliderElement()
return sliderElement.style.width
}
function setSliderWidth(newWidth) {
// newWidth = '25%'
sliderElement = getSliderElement()
sliderElement.style.width = newWidth
}
//
// Slider label accessors
//
function getSliderLabelElement() {
sliderLabelElement = document.querySelector('#__next > div.page-wrapper > div.content-wrapper > div.page-content.page-content--no-padding > div > div.container.container--fixed-height > div.container__item.container__item--scrollable.container__item--output > div.item__content > div.Preview_preview__pMaO6 > div > div.Preview_previewDistance__bcOmJ')
return sliderLabelElement
}
function getSliderLabelNumber() {
sliderLabelElement = getSliderLabelElement()
sliderLabelNumber = sliderLabelElement.style.left
return sliderLabelNumber
}
function setSliderLabelNumber(newNumber) {
// newNumber = 295
sliderLabelElement = getSliderLabelElement()
sliderLabelElement.style.left = `${newNumber}px`
sliderLabelElement.textContent = newNumber
}
function getSlider() {
sliderWidth = getSliderWidth()
sliderLabelNumber = getSliderLabelNumber()
return {
'width': sliderWidth,
'labelNumber': sliderLabelNumber,
}
}
function setSlider(sliderInfo) {
// sliderInfo = {width: '69%', labelNumber: '276px'}
setSliderWidth(sliderInfo.width)
setSliderLabelNumber(sliderInfo.labelNumber)
}
//
// WEBSOCKETS
//
async function initializeWebSocketConnection(serverURL) {
//
// Socket stuff
//
// Create a new script element
const script = document.createElement('script');
// Set the src attribute to the Socket.IO CDN URL
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js';
// Set integrity and crossorigin attributes for security (optional)
script.integrity = 'sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==';
script.crossOrigin = 'anonymous';
// Append the script element to the document body to load the library
document.body.appendChild(script);
// Sleep for a second for the JS script to load
await new Promise(resolve => setTimeout(resolve, 1000));
socket = io(serverURL);
console.log(socket)
// socket.send('Hello, server!');
return socket
}
//
// EVENT LISTENERS
//
function getTextFieldElement() {
textFieldElement = document.querySelector('#__next > div.page-wrapper > div.content-wrapper > div.page-content.page-content--no-padding > div > div.container.container--fixed-height > div.container__item.container__item--main.container__item--editor > div.Editor_editor__32JYa > div.cm-theme > div > div.cm-scroller > div.cm-content.cm-lineWrapping')
return textFieldElement
}
function initializeEventListeners(socket) {
//
// TEXT FIELD STUFF
//
var TEXT_FIELD_LOCK = false;
console.log(socket)
socket.on('text_changed', function (response) {
console.log('Received response from server:', response);
responseJson = JSON.parse(response)
text = responseJson.text
cursorPos = responseJson.cursorPos
TEXT_FIELD_LOCK = true;
console.log('SET TEXT_FIELD_LOCK', TEXT_FIELD_LOCK)
setTextField(text)
setCursorPos(cursorPos)
});
var textFieldChangedCallback = function(mutationsList, observer) {
console.log('IN CALLBACK TEXT_FIELD_LOCK', TEXT_FIELD_LOCK);
if (TEXT_FIELD_LOCK) {
TEXT_FIELD_LOCK = false;
return;
}
for (var mutation of mutationsList) {
if (mutation.type === 'characterData') {
console.log('Text content changed:', mutation);
console.log('Target DOM element:', mutation.target);
// Your code to handle the text content change here
text = getText()
request = JSON.stringify(text)
socket.send(request);
}
}
};
//
// Attach an event handler to text field to broadcast changes
//
var textFieldElement = getTextFieldElement();
var config = { characterData: true, subtree: true };
var observer = new MutationObserver(textFieldChangedCallback);
observer.observe(textFieldElement, config);
//
// SLIDER STUFF
//
var SLIDER_LOCK = false;
socket.on('slider_changed', function (response) {
console.log('Received response from server:', response);
SLIDER_LOCK = true;
console.log('SET SLIDER_LOCK', SLIDER_LOCK)
var slider = JSON.parse(response);
setSlider(slider)
});
var sliderChangedCallback = function(mutationsList, observer) {
console.log('IN sliderChangedCallback SLIDER_LOCK', SLIDER_LOCK);
if (SLIDER_LOCK) {
SLIDER_LOCK = false;
return;
}
for (var mutation of mutationsList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
// Style attribute has changed, execute your event handler here
console.log('Style changed:', mutation);
// Call your event handler function here
slider = getSlider()
sliderSerialized = JSON.stringify(slider)
socket.send(sliderSerialized);
}
}
};
//
// Attach an event handler to slider to broadcast changes
//
var sliderElement = getSliderElement();
var config = { attributes: true };
var observer = new MutationObserver(sliderChangedCallback);
observer.observe(sliderElement, config);
}
//
// Main
//
// SERVER_URL = 'https://0.0.0.0:5000'
SERVER_URL = 'https://collaborative-cssbattle.com'
socket = await initializeWebSocketConnection(SERVER_URL)
initializeEventListeners(socket)