Fix cba reset on join. Change how the cba state is stored

pull/1/head
yflory 5 years ago
parent d8855ed5f7
commit 1d0feeb323

@ -1181,6 +1181,7 @@
&.fa-print { order: 4; } &.fa-print { order: 4; }
&.fa-arrows-h { order: 5; } &.fa-arrows-h { order: 5; }
&.fa-cog { order: 5; } &.fa-cog { order: 5; }
&.fa-paint-brush { order: 5; }
&.fa-info-circle { order: 6; } &.fa-info-circle { order: 6; }
&.fa-help { order: 7; } &.fa-help { order: 7; }
} }

@ -3,8 +3,10 @@ define([
'/common/diffMarked.js', '/common/diffMarked.js',
'/bower_components/nthen/index.js', '/bower_components/nthen/index.js',
'/common/sframe-common.js', '/common/sframe-common.js',
'/common/hyperscript.js',
'/common/sframe-app-framework.js', '/common/sframe-app-framework.js',
'/common/sframe-common-codemirror.js', '/common/sframe-common-codemirror.js',
'/common/common-interface.js',
'/common/common-util.js', '/common/common-util.js',
'/common/common-hash.js', '/common/common-hash.js',
'/code/markers.js', '/code/markers.js',
@ -48,8 +50,10 @@ define([
DiffMd, DiffMd,
nThen, nThen,
SFCommon, SFCommon,
h,
Framework, Framework,
SFCodeMirror, SFCodeMirror,
UI,
Util, Util,
Hash, Hash,
Markers, Markers,
@ -276,6 +280,54 @@ define([
}; };
}; };
var mkColorByAuthor = function (framework, markers) {
var common = framework._.sfCommon;
var $cbaButton = framework._.sfCommon.createButton(null, true, {
icon: 'fa-paint-brush',
text: Messages.cba_title,
name: 'cba'
}, function () {
var div = h('div');
var $div = $(div);
var content = h('div', [
h('h4', Messages.cba_properties),
h('p', Messages.cba_hint),
div
]);
var setButton = function (state) {
var button = h('button.btn');
var $button = $(button);
$div.html('').append($button);
if (state) {
// Add "enable" button
$button.addClass('btn-secondary').text(Messages.cba_enable);
UI.confirmButton(button, {
classes: 'btn-primary'
}, function () {
$button.remove();
markers.setState(true);
common.setAttribute(['code', 'enableColors'], true);
setButton(false);
});
return;
}
// Add "disable" button
$button.addClass('btn-danger-alt').text(Messages.cba_disable);
UI.confirmButton(button, {
classes: 'btn-danger'
}, function () {
$button.remove();
markers.setState(false);
common.setAttribute(['code', 'enableColors'], false);
setButton(true);
});
};
setButton(!markers.getState());
UI.alert(content);
});
framework._.toolbar.$drawer.append($cbaButton);
};
var mkFilePicker = function (framework, editor, evModeChange) { var mkFilePicker = function (framework, editor, evModeChange) {
evModeChange.reg(function (mode) { evModeChange.reg(function (mode) {
if (MEDIA_TAG_MODES.indexOf(mode) !== -1) { if (MEDIA_TAG_MODES.indexOf(mode) !== -1) {
@ -397,14 +449,17 @@ define([
//console.log("%s => %s", CodeMirror.highlightMode, CodeMirror.$language.val()); //console.log("%s => %s", CodeMirror.highlightMode, CodeMirror.$language.val());
} }
if (newPad && Util.find(privateData, ['settings', 'code', 'enableColors'])) {
var metadataMgr = common.getMetadataMgr();
var md = Util.clone(metadataMgr.getMetadata());
md.enableColors = true;
metadataMgr.updateMetadata(md);
}
markers.ready(); markers.ready();
common.getPadMetadata(null, function (md) {
if (md && md.error) { return; }
if (!common.isOwned(md.owners)) { return; }
// We're the owner: add the button and enable the colors if needed
mkColorByAuthor(framework, markers);
if (newPad && Util.find(privateData, ['settings', 'code', 'enableColors'])) {
markers.setState(true);
}
});
var fmConfig = { var fmConfig = {
dropArea: $('.CodeMirror'), dropArea: $('.CodeMirror'),

@ -336,6 +336,7 @@ define([
var editor = Env.editor; var editor = Env.editor;
var CodeMirror = Env.CodeMirror; var CodeMirror = Env.CodeMirror;
Env.enabled = Boolean(userDoc.authormarks && userDoc.authormarks.marks);
setAuthorMarks(Env, userDoc.authormarks); setAuthorMarks(Env, userDoc.authormarks);
if (!Env.enabled) { return; } if (!Env.enabled) { return; }
@ -654,12 +655,38 @@ define([
var md = metadataMgr.getMetadata(); var md = metadataMgr.getMetadata();
Env.ready = true; Env.ready = true;
Env.myAuthorId = getAuthorId(Env); Env.myAuthorId = getAuthorId(Env);
Env.enabled = md.enableColors;
if (Env.enabled) { if (!Env.enabled) { return; }
if (Env.$button) { Env.$button.show(); } if (Env.$button) { Env.$button.show(); }
if (!Env.authormarks.marks || !Env.authormarks.marks.length) {
Env.authormarks = Util.clone(DEFAULT);
}
setMarks(Env);
};
var getState = function (Env) {
return Boolean(Env.authormarks && Env.authormarks.marks);
};
var setState = function (Env, enabled) {
// If the state has changed in the pad, change the Env too
if (!Env.ready) { return; }
if (Env.enabled === enabled) { return; }
Env.enabled = enabled;
if (!Env.enabled) {
// Reset marks
Env.authormarks = {};
setMarks(Env); setMarks(Env);
if (Env.$button) { Env.$button.hide(); }
} else {
Env.myAuthorId = getAuthorId(Env);
// If it's a reset, add initial marker
if (!Env.authormarks.marks || !Env.authormarks.marks.length) {
Env.authormarks = Util.clone(DEFAULT);
setMarks(Env);
}
if (Env.$button) { Env.$button.show(); }
} }
if (Env.ready) { Env.framework.localChange(); }
}; };
Markers.create = function (config) { Markers.create = function (config) {
@ -680,30 +707,10 @@ define([
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 = {};
setMarks(Env);
if (Env.$button) { Env.$button.hide(); }
} else {
Env.myAuthorId = getAuthorId(Env);
// If it's a reset, add initial marker
if (!Env.authormarks.marks || !Env.authormarks.marks.length) {
Env.authormarks = Util.clone(DEFAULT);
setMarks(Env);
}
if (Env.$button) { Env.$button.show(); }
}
if (Env.ready) { Env.framework.localChange(); }
}
// If the markers are disabled or if I haven't pushed content since the last reset, // If the markers are disabled or if I haven't pushed content since the last reset,
// don't update my data // don't update my data
if (!Env.enabled || !Env.myAuthorId || !Env.authormarks.authors[Env.myAuthorId]) { if (!Env.enabled || !Env.myAuthorId || !Env.authormarks.authors ||
!Env.authormarks.authors[Env.myAuthorId]) {
return; return;
} }
@ -735,7 +742,9 @@ define([
setMarks: call(setMarks), setMarks: call(setMarks),
localChange: call(localChange), localChange: call(localChange),
ready: call(ready), ready: call(ready),
setButton: call(setButton) setButton: call(setButton),
getState: call(getState),
setState: call(setState),
}; };
}; };

@ -1622,18 +1622,7 @@ define([
return void UI.alert(Messages.autostore_notAvailable); return void UI.alert(Messages.autostore_notAvailable);
} }
var metadataMgr = common.getMetadataMgr(); var metadataMgr = common.getMetadataMgr();
sframeChan.query('Q_PROPERTIES_OPEN', { sframeChan.query('EV_PROPERTIES_OPEN');
metadata: metadataMgr.getMetadata()
}, function (err, data) {
if (!data || !data.cmd) { return; }
if (data.cmd === "UPDATE_METADATA") {
if (!data.key) { return; }
var md = Util.clone(metadataMgr.getMetadata());
md[data.key] = data.value;
if (!data.value) { delete md[data.key]; }
metadataMgr.updateMetadata(md);
}
});
}); });
}); });
break; break;

@ -69,23 +69,7 @@ define([
Modal.isOwned = function (Env, data) { Modal.isOwned = function (Env, data) {
var common = Env.common; var common = Env.common;
data = data || {}; data = data || {};
var priv = common.getMetadataMgr().getPrivateData(); return common.isOwned(data.owners);
var edPublic = priv.edPublic;
var owned = false;
if (Array.isArray(data.owners) && data.owners.length) {
if (data.owners.indexOf(edPublic) !== -1) {
owned = true;
} else {
Object.keys(priv.teams || {}).some(function (id) {
var team = priv.teams[id] || {};
if (team.viewer) { return; }
if (data.owners.indexOf(team.edPublic) === -1) { return; }
owned = Number(id);
return true;
});
}
}
return owned;
}; };
var blocked = false; var blocked = false;

@ -50,56 +50,6 @@ define([
// File and history size... // File and history size...
var owned = Modal.isOwned(Env, data); var owned = Modal.isOwned(Env, data);
var metadataMgr = common.getMetadataMgr();
var priv = metadataMgr.getPrivateData();
if (owned && priv.app === 'code') {
(function () {
var sframeChan = common.getSframeChannel();
var md = (opts.data && opts.data.metadata) || {};
var div = h('div');
var hint = h('div.cp-app-prop-hint', Messages.cba_hint);
var $div = $(div);
var setButton = function (state) {
var button = h('button.btn');
var $button = $(button);
$div.html('').append($button);
if (state) {
// Add "enable" button
$button.addClass('btn-secondary').text(Messages.cba_enable);
UI.confirmButton(button, {
classes: 'btn-primary'
}, function () {
$button.remove();
sframeChan.event("EV_SECURE_ACTION", {
cmd: 'UPDATE_METADATA',
key: 'enableColors',
value: true
});
common.setAttribute(['code', 'enableColors'], true);
setButton(false);
});
return;
}
// Add "disable" button
$button.addClass('btn-danger-alt').text(Messages.cba_disable);
UI.confirmButton(button, {
classes: 'btn-danger'
}, function () {
$button.remove();
sframeChan.event("EV_SECURE_ACTION", {
cmd: 'UPDATE_METADATA',
key: 'enableColors',
value: false
});
common.setAttribute(['code', 'enableColors'], false);
setButton(true);
});
};
setButton(!md.enableColors);
$d.append(h('div.cp-app-prop', [Messages.cba_properties, hint, div]));
})();
}
// check the size of this file, including additional channels // check the size of this file, including additional channels
var bytes = 0; var bytes = 0;
var historyBytes; var historyBytes;

@ -1035,16 +1035,16 @@ define([
initSecureModal('filepicker', data || {}, cb); initSecureModal('filepicker', data || {}, cb);
}); });
sframeChan.on('Q_PROPERTIES_OPEN', function (data, cb) { sframeChan.on('EV_PROPERTIES_OPEN', function (data, cb) {
initSecureModal('properties', data || {}, cb); initSecureModal('properties', data || {});
}); });
sframeChan.on('EV_ACCESS_OPEN', function (data) { sframeChan.on('EV_ACCESS_OPEN', function (data) {
initSecureModal('access', data || {}, null); initSecureModal('access', data || {});
}); });
sframeChan.on('EV_SHARE_OPEN', function (data) { sframeChan.on('EV_SHARE_OPEN', function (data) {
initSecureModal('share', data || {}, null); initSecureModal('share', data || {});
}); });
sframeChan.on('Q_TEMPLATE_USE', function (data, cb) { sframeChan.on('Q_TEMPLATE_USE', function (data, cb) {

@ -331,6 +331,26 @@ define([
}, cb); }, cb);
}; };
funcs.isOwned = function (owners) {
var priv = ctx.metadataMgr.getPrivateData();
var edPublic = priv.edPublic;
var owned = false;
if (Array.isArray(owners) && owners.length) {
if (owners.indexOf(edPublic) !== -1) {
owned = true;
} else {
Object.keys(priv.teams || {}).some(function (id) {
var team = priv.teams[id] || {};
if (team.viewer) { return; }
if (owners.indexOf(team.edPublic) === -1) { return; }
owned = Number(id);
return true;
});
}
}
return owned;
};
funcs.isPadStored = function (cb) { funcs.isPadStored = function (cb) {
ctx.sframeChan.query("Q_IS_PAD_STORED", null, function (err, obj) { ctx.sframeChan.query("Q_IS_PAD_STORED", null, function (err, obj) {
cb (err || (obj && obj.error), obj); cb (err || (obj && obj.error), obj);

@ -74,10 +74,9 @@ define([
}; };
// Properties modal // Properties modal
create['properties'] = function (data) { create['properties'] = function () {
require(['/common/inner/properties.js'], function (Properties) { require(['/common/inner/properties.js'], function (Properties) {
Properties.getPropertiesModal(common, { Properties.getPropertiesModal(common, {
data: data,
onClose: function () { onClose: function () {
hideIframe(); hideIframe();
} }

Loading…
Cancel
Save