Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.

Commit 4114c72

Browse files
author
bhasher
committed
Fix some sockets frontend bugs
1 parent ced5678 commit 4114c72

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

src/publics/js/dev/page/editor/cursor.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* This module deals with cursor.
3+
* @author Brieuc Dubois
4+
* @date Created on 14/11/2020
5+
* @date Last modification on 19/11/2020
6+
* @version 1.0.0
7+
*/
18
import Config from "/js/dev/config.js";
29
import _, {getCurrentNode, getNodeFromAttribute} from "/js/dev/utils/element.js";
310
import EventManager from "/js/dev/utils/events.js";
@@ -18,7 +25,7 @@ export default class Cursor{
1825
this.request = {};
1926

2027
// Listen for others caret moves
21-
document.addEventListener('socket.receive.cursor-moves', e => {
28+
document.addEventListener('editor.cursor-moves', e => {
2229
this.update(e);
2330
});
2431

@@ -57,6 +64,10 @@ export default class Cursor{
5764
return {};
5865
}
5966

67+
/**
68+
* Send the cursor position if it change
69+
* @param {Cursor} cursor
70+
*/
6071
sendCursorPosition(cursor){
6172
const request = cursor.request;
6273
if(cursor && Object.keys(request).length > 0){
@@ -70,7 +81,7 @@ export default class Cursor{
7081
* @param {Object.<string, string | Array>} ev
7182
*/
7283
update(ev){
73-
const data = ev.detail.request.data;
84+
const data = ev.detail;
7485
if(this.current.has(data.userId)){
7586
this.current.get(data.userId)[0].remove();
7687
this.current.get(data.userId)[1].removeAttribute('contenteditable');

src/publics/js/dev/page/editor/editable.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
* @version 2.0.0
77
*/
88
import temporaryCardAlert from "/js/dev/component/text-alert.js";
9-
import Config from "/js/dev/config.js";
109
import Key from "/js/dev/page/editor/key.js";
11-
import Keys from "/js/dev/page/editor/key.js";
1210
import LinesManager from "/js/dev/page/editor/linesManager.js";
1311
import PrismCustom from "/js/dev/page/editor/prism/prismCustom.js";
1412
import Request from "/js/dev/page/editor/requests.js";
1513
import Tab, {TabType} from "/js/dev/page/editor/tab.js";
1614
import Caret from "/js/dev/utils/caret.js";
17-
import Debug from "/js/dev/utils/debug.js";
15+
import Debug from "/js/dev/utils/debugging/debug.js";
1816
import {getNodeFromAttribute, getParentFromSpecificTypes} from "/js/dev/utils/element.js";
1917
import EventManager from "/js/dev/utils/events.js";
2018
import Random from "/js/dev/utils/random.js";
@@ -29,6 +27,23 @@ export default class Editable{
2927

3028
this.keepSpace = false;
3129

30+
/**
31+
* Receive all update events
32+
*/
33+
document.addEventListener('socket.receive.update', e => {
34+
if(e && e.detail && e.detail && typeof e.detail[Symbol.iterator] === 'function'){
35+
for(const req of e.detail){
36+
if(req.type){
37+
EventManager.triggerCustom('editor.' + req.type, req.data);
38+
}else{
39+
Debug.warn('Trying to trigger a null received event.');
40+
}
41+
}
42+
}else{
43+
Debug.warn('Trying to iterate on non-iterable data.');
44+
}
45+
});
46+
3247
EventManager.triggerCustom('socket.preprocess', [this.coroutine, [this]]);
3348

3449
/**
@@ -157,29 +172,29 @@ export default class Editable{
157172
/**
158173
* Receive an update about the content of a line
159174
*/
160-
document.addEventListener('socket.receive.set-line', e => {
161-
const id = e.detail.request.data.id;
162-
const content = e.detail.request.data.content;
175+
document.addEventListener('editor.set-line', e => {
176+
const id = e.detail.id;
177+
const content = e.detail.content;
163178
this.last_request[id] = content;
164179
this.linesManager.update(id, content);
165180
});
166181

167182
/**
168183
* Receive a new line to create
169184
*/
170-
document.addEventListener('socket.receive.new-line', e => {
171-
const id = e.detail.request.data.id;
172-
const previous = e.detail.request.data.previous;
173-
const content = e.detail.request.data.content;
185+
document.addEventListener('editor.new-line', e => {
186+
const id = e.detail.id;
187+
const previous = e.detail.previous;
188+
const content = e.detail.content;
174189
this.last_request[id] = content;
175190
this.linesManager.new(id, previous, content);
176191
});
177192

178193
/**
179194
* Receive a line to delete
180195
*/
181-
document.addEventListener('socket.receive.delete-line', e => {
182-
const id = e.detail.request.data.id;
196+
document.addEventListener('editor.delete-line', e => {
197+
const id = e.detail.id;
183198
if(id in this.last_request) delete this.last_request[id];
184199
this.linesManager.remove(id);
185200
});

src/publics/js/dev/page/editor/linesManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @version 1.0.0
77
*/
88
import PrismCustom from "/js/dev/page/editor/prism/prismCustom.js";
9-
import Debug from "/js/dev/utils/debug.js";
9+
import Debug from "/js/dev/utils/debugging/debug.js";
1010
import {createElement} from "/js/dev/utils/element.js";
1111
import Stack from "/js/dev/utils/stack.js";
1212
import {htmlEncode} from "/js/dev/utils/string.js";

src/publics/js/dev/page/editor/prism/prismCustom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {patterns} from "/js/dev/page/editor/prism/patterns.js";
22
import {Prism} from "/js/dev/page/editor/prism/prism.js";
33
import Caret from "/js/dev/utils/caret.js";
4-
import Debug from "/js/dev/utils/debug.js";
4+
import Debug from "/js/dev/utils/debugging/debug.js";
55
import {getNodeFromAttribute} from "/js/dev/utils/element.js";
66
import {htmlEncode} from "/js/dev/utils/string.js";
77

src/publics/js/dev/page/editor/socket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Config from "/js/dev/config.js";
2-
import Debug from "/js/dev/utils/debug.js";
2+
import Debug from "/js/dev/utils/debugging/debug.js";
33
import Socket from "/js/dev/utils/websocket/socket.js";
44

55
const UPDATE_EVENT = 'update';

0 commit comments

Comments
 (0)