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

Commit ab9df58

Browse files
authored
Merge pull request #23 from CodeWe-projet/editable-bugfix
Editable bugfix
2 parents f85733a + f9e3343 commit ab9df58

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

src/db/MongoDB.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class MongoDB {
110110

111111
async newLine (documentLink, previousUuid, uuid, content) {
112112
// Insert a line at the right place
113-
//TODO is it possible in one operation ?
113+
//TODO is it possible in one operation ?
114114
// TODO is it possible to implement with bulk?
115115
try {
116116
let doc = await this.documentsCollection.findOne({documentLink: documentLink});
@@ -206,17 +206,18 @@ class MongoDB {
206206
for (let request of requests) {
207207
let requestType = request.type;
208208
let data = request.data;
209+
let results = ""
209210
switch (requestType) {
210211
case 'set-line':
211-
let results = await this.setLine(documentLink, data.id, data.content);
212+
results = await this.setLine(documentLink, data.id, data.content);
212213
if (!results) success = false;
213214
break;
214215
case 'new-line':
215-
let results = await this.newLine(documentLink, data.previous, data.id, data.content);
216+
results = await this.newLine(documentLink, data.previous, data.id, data.content);
216217
if (!results) success = false;
217218
break;
218219
case 'delete-line':
219-
let results = await this.deleteLine(documentLink, data.id);
220+
results = await this.deleteLine(documentLink, data.id);
220221
if (!results) success = false;
221222
break;
222223
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,18 @@ export default class Editable{
137137

138138
const line = getNodeFromAttribute('uuid');
139139

140-
if(!line) return;
140+
if(!line){
141+
e.preventDefault();
142+
temporaryCardAlert('Editor', 'Sorry, your action has been canceled because you are not on any line.', 5000);
143+
return;
144+
}
141145

142146
if(!anchorParent.hasAttribute('uuid')
143147
|| !focusParent.hasAttribute('uuid')
144148
|| ((Caret.getBeginPosition(line) === 0
145149
|| Caret.getEndPosition(line) === 0)
146150
) && anchorParent !== focusParent){
147-
e.preventDefault();
148-
temporaryCardAlert('Override', 'Sorry, you can\'t override the first char of a line', 5000);
149-
return;
151+
Caret.setRangeStart(line, 1);
150152
}
151153

152154
switch (e.keyCode) {
@@ -155,6 +157,11 @@ export default class Editable{
155157
this.insertTab();
156158
break;
157159
case 13: // enter
160+
if(e.shiftKey){
161+
temporaryCardAlert('Shift+Enter', 'Please just use Enter to avoid any bugs.', 5000);
162+
e.preventDefault();
163+
return;
164+
}
158165
if(this.keepSpace){
159166
Debug.debug('Prevent action when trying to add new line (key is probably maintain).');
160167
e.preventDefault();

src/publics/js/dev/utils/caret.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ export default class Caret{
6767
}
6868
}
6969

70+
71+
/**
72+
* Set the start range of the user caret on specified position in element or children
73+
* @param {HTMLElement|Node} element
74+
* @param {number} position
75+
*/
76+
static setRangeStart(element, position) {
77+
if (position >= 0) {
78+
let selection = document.getSelection();
79+
80+
let range = Caret.createRange(element, {count: position});
81+
selection.getRangeAt(0).setStart(range.endContainer, range.endOffset);
82+
83+
}
84+
}
85+
7086
/**
7187
* Get the position of the end of the user selection
7288
* Based on https://stackoverflow.com/a/4812022/11247647

src/server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Alexandre Dewilde
44
* @date 15/11/2020
55
* @version 1.0.0
6-
*
6+
*
77
*/
88
const fs = require('fs');
99
const path = require('path');
@@ -15,7 +15,7 @@ const port = configs.PORT;
1515
const DEBUG = configs.DEBUG;
1616
const ssl = configs.SSL;
1717
const sslKeyPath = configs.KEY_FILE_SSL;
18-
const sslCertPath = configs.CERT_FILE_SSL;
18+
const sslCertPath = configs.CERT_FILE_SSL;
1919

2020
const options = ssl ? {
2121
key: fs.readFileSync(sslKeyPath.startsWith('/') ? sslKeyPath : path.join(__dirname, sslKeyPath), 'utf8'),
@@ -29,7 +29,7 @@ const http = ssl ? require('https') : require('http');
2929
const server = http.createServer(options, app);
3030
if (configs.METRICS) {
3131
const {metricsApp} = require('./metricsApp');
32-
var metricsServer = http.createServer(metricsApp);
32+
var metricsServer = require('http').createServer(metricsApp);
3333
}
3434

3535
// config websockets

src/socket/socket.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ module.exports = function (wss) {
6666
broadcastRoomExceptSender(data, 'uuid', data.uuid);
6767
const succesUpdatingDate = db.updateLastViewedDate(data.room);
6868
const succesUpdate = db.applyRequests(data.room, data.data);
69-
if (!succesUpdatingDate || !succesUpdate) socket.send(JSON.stringify({event: 'update', success: false}));
69+
// /!\ Bad event
70+
// if (!succesUpdatingDate || !succesUpdate) socket.send(JSON.stringify({event: 'update', success: false}));
7071
} catch (err) {
7172
if (config.DEBUG) {
7273
console.error(err);
@@ -82,7 +83,7 @@ module.exports = function (wss) {
8283
rooms[data.room][uuid] = socket;
8384
}
8485
break;
85-
86+
8687
case 'language':
8788
try {
8889
broadcastRoomExceptSender(data, 'language', data.language);

0 commit comments

Comments
 (0)