-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
95 lines (85 loc) · 2.73 KB
/
index.js
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
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
var db = require("ep_etherpad-lite/node/db/DB").db;
//var async = require("async");
var authorColorPool = null;
exports.getLineHTMLForExport = function (hook, context) {
var authors = _authorsOfLine(context.attribLine, context.apool);
var newLineHTML = "";
var charPos = 0;
// Use this code if this function is called in an asynchronous way
/*async.series([
function(cb){
async.each(authors, function(author, callback) {
console.log(author);
db.getSub("globalAuthor:" + author, ["colorId"], function(err, result) {
if (err) {
console.log(err);
} else {
console.log("color: "+result);
newLineHTML += _getHTMLString(author.name.replace('.','_'), result, context.text.substring(charPos, charPos + author.chars));
charPos += author.chars;
}
callback();
});
}, function(err) {
if (err) {
console.log(err);
}
cb();
});
}
],
function(err, results){
return newLineHTML+'<br/>';
});*/
authors.forEach(function(author) {
var authorName = author.name;
var color = null;
if (authorName) {
authorName = authorName.replace('.','_');
color = _getAuthorColor(author.name);
}
newLineHTML += _getHTMLString(authorName, color, context.text.substring(charPos, charPos + author.chars));
charPos += author.chars;
});
if (newLineHTML == "") {
return;
}
return context.lineContent = newLineHTML;
}
function _authorsOfLine(alineAttrs, apool) {
var authors = [];
if (alineAttrs) {
var opIter = Changeset.opIterator(alineAttrs);
while (opIter.hasNext()) {
var op = opIter.next();
var author = null;
if (op.attribs) {
author= Changeset.attribsAttributeValue(op.attribs, "author", apool);
}
authors.push({ 'name': author, 'chars': op.chars});
}
}
return authors;
}
function _getHTMLString(authorName, authorColor, text) {
if (authorName) {
return '<span class="' + authorName + '" title="' + authorName + '" style="background-color:' + authorColor + '">' + text + '</span>';
} else {
return '<span class="anonymous" title="anonymous">' + text + '</span>';
}
}
function _getAuthorColor(authorName) {
var hashKey = "authorColor:"+authorName;
var color = "FF0000";
if (!authorColorPool) {
authorColorPool = [];
}
if (authorColorPool) {
if (!authorColorPool[hashKey]) {
authorColorPool[hashKey] = ('000000' + (Math.random()*0xFFFFFF<<0).toString(16)).slice(-6);
}
color = authorColorPool[hashKey];
}
return "#"+color;
}