|
|
@ -6,19 +6,21 @@ define([
|
|
|
|
], function (Util, SFCodeMirror, Messages, ChainPad) {
|
|
|
|
], function (Util, SFCodeMirror, Messages, ChainPad) {
|
|
|
|
var Markers = {};
|
|
|
|
var Markers = {};
|
|
|
|
|
|
|
|
|
|
|
|
Messages.cba_writtenBy = 'Written by <em>{0}</em>';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var MARK_OPACITY = 90;
|
|
|
|
var MARK_OPACITY = 90;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Messages.cba_writtenBy = 'Written by <em>{0}</em>'; // XXX
|
|
|
|
|
|
|
|
|
|
|
|
var addMark = function (Env, from, to, uid) {
|
|
|
|
var addMark = function (Env, from, to, uid) {
|
|
|
|
|
|
|
|
if (!Env.enabled) { return; }
|
|
|
|
var author = Env.authormarks.authors[uid] || {};
|
|
|
|
var author = Env.authormarks.authors[uid] || {};
|
|
|
|
uid = Number(uid);
|
|
|
|
uid = Number(uid);
|
|
|
|
var name = Util.fixHTML(author.name || Messages.anonymous);
|
|
|
|
var name = Util.fixHTML(author.name || Messages.anonymous);
|
|
|
|
return Env.editor.markText(from, to, {
|
|
|
|
return Env.editor.markText(from, to, {
|
|
|
|
inclusiveLeft: uid === Env.myAuthorId,
|
|
|
|
inclusiveLeft: uid === Env.myAuthorId,
|
|
|
|
inclusiveRight: uid === Env.myAuthorId,
|
|
|
|
inclusiveRight: uid === Env.myAuthorId,
|
|
|
|
css: "background-color: " + author.color + MARK_OPACITY,
|
|
|
|
css: "background-color: " + author.color + Env.opacity,
|
|
|
|
attributes: {
|
|
|
|
attributes: {
|
|
|
|
title: Messages._getKey('cba_writtenBy', [name]),
|
|
|
|
title: Env.opacity ? Messages._getKey('cba_writtenBy', [name]) : undefined,
|
|
|
|
'data-type': 'authormark',
|
|
|
|
'data-type': 'authormark',
|
|
|
|
'data-uid': uid
|
|
|
|
'data-uid': uid
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -60,6 +62,8 @@ define([
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var updateAuthorMarks = function (Env) {
|
|
|
|
var updateAuthorMarks = function (Env) {
|
|
|
|
|
|
|
|
if (!Env.enabled) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
// get author marks
|
|
|
|
// get author marks
|
|
|
|
var _marks = [];
|
|
|
|
var _marks = [];
|
|
|
|
var all = [];
|
|
|
|
var all = [];
|
|
|
@ -195,7 +199,8 @@ define([
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var checkAuthors = function (Env, userDoc) {
|
|
|
|
var checkMarks = function (Env, userDoc) {
|
|
|
|
|
|
|
|
|
|
|
|
var chainpad = Env.framework._.cpNfInner.chainpad;
|
|
|
|
var chainpad = Env.framework._.cpNfInner.chainpad;
|
|
|
|
var editor = Env.editor;
|
|
|
|
var editor = Env.editor;
|
|
|
|
var CodeMirror = Env.CodeMirror;
|
|
|
|
var CodeMirror = Env.CodeMirror;
|
|
|
@ -203,6 +208,8 @@ define([
|
|
|
|
|
|
|
|
|
|
|
|
setAuthorMarks(Env, userDoc.authormarks);
|
|
|
|
setAuthorMarks(Env, userDoc.authormarks);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Env.enabled) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
var authDoc = JSON.parse(chainpad.getAuthDoc() || '{}');
|
|
|
|
var authDoc = JSON.parse(chainpad.getAuthDoc() || '{}');
|
|
|
|
if (!authDoc.content || !userDoc.content) { return; }
|
|
|
|
if (!authDoc.content || !userDoc.content) { return; }
|
|
|
|
if (authDoc.content === userDoc.content) { return; } // No uncommitted work
|
|
|
|
if (authDoc.content === userDoc.content) { return; } // No uncommitted work
|
|
|
@ -280,12 +287,14 @@ define([
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var setMarks = function (Env) {
|
|
|
|
var setMarks = function (Env) {
|
|
|
|
// on remote update: remove all marks, add new marks
|
|
|
|
// on remote update: remove all marks, add new marks if colors are enabled
|
|
|
|
Env.editor.getAllMarks().forEach(function (marker) {
|
|
|
|
Env.editor.getAllMarks().forEach(function (marker) {
|
|
|
|
if (marker.attributes && marker.attributes['data-type'] === 'authormark') {
|
|
|
|
if (marker.attributes && marker.attributes['data-type'] === 'authormark') {
|
|
|
|
marker.clear();
|
|
|
|
marker.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Env.enabled) { return; }
|
|
|
|
var authormarks = Env.authormarks;
|
|
|
|
var authormarks = Env.authormarks;
|
|
|
|
authormarks.marks.forEach(function (mark) {
|
|
|
|
authormarks.marks.forEach(function (mark) {
|
|
|
|
var uid = mark[0];
|
|
|
|
var uid = mark[0];
|
|
|
@ -316,20 +325,24 @@ define([
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var setMyData = function (Env) {
|
|
|
|
var setMyData = function (Env) {
|
|
|
|
|
|
|
|
if (!Env.enabled) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
var userData = Env.common.getMetadataMgr().getUserData();
|
|
|
|
var userData = Env.common.getMetadataMgr().getUserData();
|
|
|
|
var old = Env.authormarks.authors[Env.myAuthorId];
|
|
|
|
var old = Env.authormarks.authors[Env.myAuthorId];
|
|
|
|
if (!old || (old.name === userData.data && old.color === userData.color)) { return; }
|
|
|
|
|
|
|
|
Env.authormarks.authors[Env.myAuthorId] = {
|
|
|
|
Env.authormarks.authors[Env.myAuthorId] = {
|
|
|
|
name: userData.name,
|
|
|
|
name: userData.name,
|
|
|
|
curvePublic: userData.curvePublic,
|
|
|
|
curvePublic: userData.curvePublic,
|
|
|
|
color: userData.color
|
|
|
|
color: userData.color
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!old || (old.name === userData.name && old.color === userData.color)) { return; }
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var localChange = function (Env, change, cb) {
|
|
|
|
var localChange = function (Env, change, cb) {
|
|
|
|
cb = cb || function () {};
|
|
|
|
cb = cb || function () {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Env.enabled) { return void cb(); }
|
|
|
|
|
|
|
|
|
|
|
|
if (change.origin === "setValue") {
|
|
|
|
if (change.origin === "setValue") {
|
|
|
|
// If the content is changed from a remote patch, we call localChange
|
|
|
|
// If the content is changed from a remote patch, we call localChange
|
|
|
|
// in "onContentUpdate" directly
|
|
|
|
// in "onContentUpdate" directly
|
|
|
@ -397,6 +410,31 @@ define([
|
|
|
|
cb();
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Messages.cba_show = "Show user colors"; // XXX
|
|
|
|
|
|
|
|
Messages.cba_hide = "Hide user colors"; // XXX
|
|
|
|
|
|
|
|
var setButton = function (Env, $button) {
|
|
|
|
|
|
|
|
var toggle = function () {
|
|
|
|
|
|
|
|
var tippy = $button[0] && $button[0]._tippy;
|
|
|
|
|
|
|
|
if (Env.opacity) {
|
|
|
|
|
|
|
|
Env.opacity = 0;
|
|
|
|
|
|
|
|
if (tippy) { tippy.title = Messages.cba_show; }
|
|
|
|
|
|
|
|
else { $button.attr('title', Messages.cba_show); }
|
|
|
|
|
|
|
|
$button.removeClass("cp-toolbar-button-active");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Env.opacity = MARK_OPACITY;
|
|
|
|
|
|
|
|
if (tippy) { tippy.title = Messages.cba_hide; }
|
|
|
|
|
|
|
|
else { $button.attr('title', Messages.cba_hide); }
|
|
|
|
|
|
|
|
$button.addClass("cp-toolbar-button-active");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
toggle();
|
|
|
|
|
|
|
|
Env.$button = $button;
|
|
|
|
|
|
|
|
$button.click(function() {
|
|
|
|
|
|
|
|
toggle();
|
|
|
|
|
|
|
|
setMarks(Env);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var authorUid = function (existing) {
|
|
|
|
var authorUid = function (existing) {
|
|
|
|
if (!Array.isArray(existing)) { existing = []; }
|
|
|
|
if (!Array.isArray(existing)) { existing = []; }
|
|
|
|
var n;
|
|
|
|
var n;
|
|
|
@ -422,28 +460,55 @@ define([
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return uid || authorUid(existing);
|
|
|
|
return uid || authorUid(existing);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var ready = function (Env) {
|
|
|
|
var ready = function (Env) {
|
|
|
|
|
|
|
|
var metadataMgr = Env.common.getMetadataMgr();
|
|
|
|
|
|
|
|
var md = metadataMgr.getMetadata();
|
|
|
|
|
|
|
|
Env.ready = true;
|
|
|
|
Env.myAuthorId = getAuthorId(Env);
|
|
|
|
Env.myAuthorId = getAuthorId(Env);
|
|
|
|
var changed = setMyData(Env);
|
|
|
|
Env.enabled = md.enableColors;
|
|
|
|
if (changed) {
|
|
|
|
|
|
|
|
Env.framework.localChange();
|
|
|
|
if (Env.enabled) {
|
|
|
|
|
|
|
|
if (Env.$button) { Env.$button.show(); }
|
|
|
|
setMarks(Env);
|
|
|
|
setMarks(Env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Markers.create = function (config) {
|
|
|
|
Markers.create = function (config) {
|
|
|
|
var Env = config;
|
|
|
|
var Env = config;
|
|
|
|
setAuthorMarks(Env);
|
|
|
|
Env.authormarks = {
|
|
|
|
|
|
|
|
authors: {},
|
|
|
|
|
|
|
|
marks: []
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Env.enabled = false;
|
|
|
|
Env.myAuthorId = 0;
|
|
|
|
Env.myAuthorId = 0;
|
|
|
|
|
|
|
|
|
|
|
|
var metadataMgr = Env.common.getMetadataMgr();
|
|
|
|
var metadataMgr = Env.common.getMetadataMgr();
|
|
|
|
metadataMgr.onChange(function () {
|
|
|
|
metadataMgr.onChange(function () {
|
|
|
|
|
|
|
|
var md = metadataMgr.getMetadata();
|
|
|
|
|
|
|
|
// If the state has changed in the pad, change the Env too
|
|
|
|
|
|
|
|
if (Env.enabled !== md.enableColors) {
|
|
|
|
|
|
|
|
Env.enabled = md.enableColors;
|
|
|
|
|
|
|
|
if (!Env.enabled) {
|
|
|
|
|
|
|
|
// Reset marks
|
|
|
|
|
|
|
|
Env.authormarks = {
|
|
|
|
|
|
|
|
authors: {},
|
|
|
|
|
|
|
|
marks: []
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
setMarks(Env);
|
|
|
|
|
|
|
|
if (Env.$button) { Env.$button.hide(); }
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Env.myAuthorId = getAuthorId(Env);
|
|
|
|
|
|
|
|
if (Env.$button) { Env.$button.show(); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Env.ready) { Env.framework.localChange(); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Env.enabled) { return; }
|
|
|
|
// Update my data
|
|
|
|
// Update my data
|
|
|
|
var changed = setMyData(Env);
|
|
|
|
var changed = setMyData(Env);
|
|
|
|
if (changed) {
|
|
|
|
if (changed) {
|
|
|
|
Env.framework.localChange();
|
|
|
|
|
|
|
|
setMarks(Env);
|
|
|
|
setMarks(Env);
|
|
|
|
|
|
|
|
Env.framework.localChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
@ -457,13 +522,13 @@ define([
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
addMark: call(addMark),
|
|
|
|
addMark: call(addMark),
|
|
|
|
setAuthorMarks: call(setAuthorMarks),
|
|
|
|
|
|
|
|
getAuthorMarks: call(getAuthorMarks),
|
|
|
|
getAuthorMarks: call(getAuthorMarks),
|
|
|
|
updateAuthorMarks: call(updateAuthorMarks),
|
|
|
|
updateAuthorMarks: call(updateAuthorMarks),
|
|
|
|
checkAuthors: call(checkAuthors),
|
|
|
|
checkMarks: call(checkMarks),
|
|
|
|
setMarks: call(setMarks),
|
|
|
|
setMarks: call(setMarks),
|
|
|
|
localChange: call(localChange),
|
|
|
|
localChange: call(localChange),
|
|
|
|
ready: call(ready),
|
|
|
|
ready: call(ready),
|
|
|
|
|
|
|
|
setButton: call(setButton)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|