delta-component
Version:
embeddable react component
157 lines (132 loc) • 4.21 kB
JavaScript
'use strict';
const log = require('../../../common/logger').log;
const colors = require('../../../common/logic/colors');
class Avatar {
constructor(avatarId){
}
getUrl() {
return 'https://center.yandex-team.ru/api/v1/user/tet4esdf/avatar/100.jpg';
}
}
class Participant {
constructor(siteId, uid, displayName, avatarId) {
this.siteId = siteId;
this.uid = uid;
this.displayName = displayName || 'anonymous';
this.avatar = new Avatar(avatarId);
this.cursorPosition = null; // по-умолчанию пользователь лишь наблюдатель
}
getColorNumber() {
/**
* Для вычисления номера цвета курсора
*/
if(this.uid === null) {
return this.siteId;
} else {
return colors.string2Int(this.uid) % 10000000;
}
}
getColor(){
return colors.participant2Color(this);
}
lostCursor() {
this.cursorPosition = null;
}
toString() {
return `${this.constructor.name} "${this.displayName}" (${this.siteId})`;
}
}
class Participants {
/**
* Список людей участвующих в документе
*/
constructor(participant) {
this.currentUser = participant; // тот кто сейчас работает над документом
this._others = {}
}
joined(participant) {
this._others[participant.siteId.toString()] = participant;
}
left(siteId) {
siteId = siteId.toString();
if(siteId in this._others) {
const participant = this._others[siteId];
delete this._others[siteId]
return participant;
}
this.logUnknown(siteId);
return null;
}
replaceEveryone(newParticipants) {
this._others = {};
let that = this;
newParticipants.map((participant) => {
that.joined(participant);
})
}
logUnknown(siteId) {
log.trace(`${siteId} is not yet known Participant`)
}
cursorsChanged(treedocCursorMap) {
// this._removeCursors(treedocCursorMap);
for(let siteId of Object.keys(treedocCursorMap)) {
this._changeCursor(siteId, treedocCursorMap[siteId]);
}
}
_changeCursor(siteId, position) {
const participant = this.getBySiteId(siteId);
if (participant === null) {
this.logUnknown(siteId);
} else {
participant.cursorPosition = position;
}
}
getBySiteId(siteId) {
if(this.currentUser.siteId === parseInt(siteId)) {
return this.currentUser;
}
siteId = siteId.toString();
if(this._others[siteId] === undefined) {
return null;
}
return this._others[siteId];
}
_removeCursors(editors) {
for(let siteId of Object.keys(this._others)) {
if(!(siteId in editors)) {
this._others[siteId].lostCursor();
}
}
}
listAll() {
/**
* Вывести всех без всяких правил обработки
*/
let all = this.toList();
all.push(this.currentUser);
return all;
}
toList(editorsOnly = false) {
let result = [];
for(let siteId in this._others) {
result.push(this._others[siteId]);
}
// TODO: залогиновых выводить первыми, анонимных последними
return result.sort((alice, bob) => alice.siteId - bob.siteId);
}
toString() {
const maxToShow = 3;
const list = this.toList();
const shortList = list.slice(0, maxToShow);
let shortListRepr = shortList.length > 0 ? shortList.join(', ') : 'no one';
let ellipsis = '';
if (shortList.length < list.length) {
ellipsis = `,…`;
}
const total = list.length + 1;
return `${this.constructor.name}: ${shortListRepr}${ellipsis} and you, ${total} total`;
}
}
module.exports = {
Participant, Participants
};