mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-21 10:45:20 -04:00
Update to move authorship calculation code to note model and support update authorship after making revision of docs
This commit is contained in:
parent
a5e6b5dd3b
commit
d23ced1fba
2 changed files with 177 additions and 101 deletions
lib
101
lib/realtime.js
101
lib/realtime.js
|
@ -642,106 +642,7 @@ function operationCallback(socket, operation) {
|
|||
}
|
||||
}
|
||||
// save authorship
|
||||
var index = 0;
|
||||
var authorships = note.authorship;
|
||||
var timestamp = Date.now();
|
||||
for (var i = 0; i < operation.length; i++) {
|
||||
var op = operation[i];
|
||||
if (ot.TextOperation.isRetain(op)) {
|
||||
index += op;
|
||||
} else if (ot.TextOperation.isInsert(op)) {
|
||||
var opStart = index;
|
||||
var opEnd = index + op.length;
|
||||
var inserted = false;
|
||||
// authorship format: [userId, startPos, endPos, createdAt, updatedAt]
|
||||
if (authorships.length <= 0) authorships.push([userId, opStart, opEnd, timestamp, timestamp]);
|
||||
else {
|
||||
for (var j = 0; j < authorships.length; j++) {
|
||||
var authorship = authorships[j];
|
||||
if (!inserted) {
|
||||
var nextAuthorship = authorships[j + 1] || -1;
|
||||
if (nextAuthorship != -1 && nextAuthorship[1] >= opEnd || j >= authorships.length - 1) {
|
||||
if (authorship[1] < opStart && authorship[2] > opStart) {
|
||||
// divide
|
||||
var postLength = authorship[2] - opStart;
|
||||
authorship[2] = opStart;
|
||||
authorship[4] = timestamp;
|
||||
authorships.splice(j + 1, 0, [userId, opStart, opEnd, timestamp, timestamp]);
|
||||
authorships.splice(j + 2, 0, [authorship[0], opEnd, opEnd + postLength, authorship[3], timestamp]);
|
||||
j += 2;
|
||||
inserted = true;
|
||||
} else if (authorship[1] >= opStart) {
|
||||
authorships.splice(j, 0, [userId, opStart, opEnd, timestamp, timestamp]);
|
||||
j += 1;
|
||||
inserted = true;
|
||||
} else if (authorship[2] <= opStart) {
|
||||
authorships.splice(j + 1, 0, [userId, opStart, opEnd, timestamp, timestamp]);
|
||||
j += 1;
|
||||
inserted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (authorship[1] >= opStart) {
|
||||
authorship[1] += op.length;
|
||||
authorship[2] += op.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
index += op.length;
|
||||
} else if (ot.TextOperation.isDelete(op)) {
|
||||
var opStart = index;
|
||||
var opEnd = index - op;
|
||||
if (operation.length == 1) {
|
||||
authorships = [];
|
||||
} else if (authorships.length > 0) {
|
||||
for (var j = 0; j < authorships.length; j++) {
|
||||
var authorship = authorships[j];
|
||||
if (authorship[1] >= opStart && authorship[1] <= opEnd && authorship[2] >= opStart && authorship[2] <= opEnd) {
|
||||
authorships.splice(j, 1);
|
||||
j -= 1;
|
||||
} else if (authorship[1] < opStart && authorship[1] < opEnd && authorship[2] > opStart && authorship[2] > opEnd) {
|
||||
authorship[2] += op;
|
||||
authorship[4] = timestamp;
|
||||
} else if (authorship[2] >= opStart && authorship[2] <= opEnd) {
|
||||
authorship[2] = opStart;
|
||||
authorship[4] = timestamp;
|
||||
} else if (authorship[1] >= opStart && authorship[1] <= opEnd) {
|
||||
authorship[1] = opEnd;
|
||||
authorship[4] = timestamp;
|
||||
}
|
||||
if (authorship[1] >= opEnd) {
|
||||
authorship[1] += op;
|
||||
authorship[2] += op;
|
||||
}
|
||||
}
|
||||
}
|
||||
index += op;
|
||||
}
|
||||
}
|
||||
// merge
|
||||
for (var j = 0; j < authorships.length; j++) {
|
||||
var authorship = authorships[j];
|
||||
for (var k = j + 1; k < authorships.length; k++) {
|
||||
var nextAuthorship = authorships[k];
|
||||
if (nextAuthorship && authorship[0] === nextAuthorship[0] && authorship[2] === nextAuthorship[1]) {
|
||||
var minTimestamp = Math.min(authorship[3], nextAuthorship[3]);
|
||||
var maxTimestamp = Math.max(authorship[3], nextAuthorship[3]);
|
||||
authorships.splice(j, 1, [authorship[0], authorship[1], nextAuthorship[2], minTimestamp, maxTimestamp]);
|
||||
authorships.splice(k, 1);
|
||||
j -= 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// clear
|
||||
for (var j = 0; j < authorships.length; j++) {
|
||||
var authorship = authorships[j];
|
||||
if (!authorship[0]) {
|
||||
authorships.splice(j, 1);
|
||||
j -= 1;
|
||||
}
|
||||
}
|
||||
note.authorship = authorships;
|
||||
note.authorship = models.Note.updateAuthorshipByOperation(operation, userId, note.authorship);
|
||||
}
|
||||
|
||||
function connection(socket) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue