From 99b50465c7b56b8d8d5c3a17c1badb10e548ba0e Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Mon, 19 Sep 2016 17:52:37 +0200 Subject: [PATCH] Add the read-only mode for /pad and /code --- customize.dist/toolbar.css | 5 +++ customize.dist/translations/messages.fr.js | 6 +++ customize.dist/translations/messages.js | 6 +++ www/code/main.js | 52 +++++++++++++++------- www/common/toolbar.js | 40 +++++++++++++---- www/pad/main.js | 8 +++- 6 files changed, 91 insertions(+), 26 deletions(-) diff --git a/customize.dist/toolbar.css b/customize.dist/toolbar.css index 61b592eda..b03039473 100644 --- a/customize.dist/toolbar.css +++ b/customize.dist/toolbar.css @@ -83,3 +83,8 @@ .cryptpad-spinner { float: left; } +.cryptpad-readonly { + margin-right: 20px; + font-weight: bold; + text-transform: uppercase; +} \ No newline at end of file diff --git a/customize.dist/translations/messages.fr.js b/customize.dist/translations/messages.fr.js index 4ddd2a2f6..813620dfb 100644 --- a/customize.dist/translations/messages.fr.js +++ b/customize.dist/translations/messages.fr.js @@ -20,6 +20,12 @@ define(function () { out.synchronizing = 'Synchronisation'; out.reconnecting = 'Reconnexion...'; out.lag = 'Latence'; + out.readonly = 'Lecture seule'; + out.nobodyIsEditing = "Personne n'édite le document"; + out.onePersonIsEditing = 'Une personne édite le document'; + out.peopleAreEditing = '{0} personnes éditent le document'; + out.oneViewer = '1 lecteur'; + out.viewers = '{0} lecteurs'; out.importButton = 'IMPORTER'; out.importButtonTitle = 'Importer un document depuis un fichier local'; diff --git a/customize.dist/translations/messages.js b/customize.dist/translations/messages.js index 3a43d9209..e4dec777e 100644 --- a/customize.dist/translations/messages.js +++ b/customize.dist/translations/messages.js @@ -20,6 +20,12 @@ define(function () { out.synchronizing = 'Synchronizing'; out.reconnecting = 'Reconnecting...'; out.lag = 'Lag'; + out.readonly = 'Read only'; + out.nobodyIsEditing = 'Nobody is editing'; + out.onePersonIsEditing = 'One person is editing'; + out.peopleAreEditing = '{0} people are editing'; + out.oneViewer = '1 viewer'; + out.viewers = '{0} viewers'; out.importButton = 'IMPORT'; out.importButtonTitle = 'Import a document from a local file'; diff --git a/www/code/main.js b/www/code/main.js index 764ff6698..db84ec3ec 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -39,6 +39,7 @@ define([ toolbar; var secret = Cryptpad.getSecrets(); + var readOnly = !secret.keys.editKeyStr; var andThen = function (CMeditor) { var CodeMirror = module.CodeMirror = CMeditor; @@ -106,6 +107,7 @@ define([ }()); var setEditable = module.setEditable = function (bool) { + if (readOnly && bool) { return; } editor.setOption('readOnly', !bool); }; @@ -132,8 +134,10 @@ define([ userName: userName, websocketURL: Config.websocketURL, channel: secret.channel, - //cryptKey: key, - crypto: Crypto.createEncryptor(secret.key), + // our public key + validateKey: secret.keys.validateKey || undefined, + readOnly: readOnly, + crypto: Crypto.createEncryptor(secret.keys), setMyID: setMyID, transformFunction: JsonOT.validate }; @@ -274,9 +278,11 @@ define([ var config = { userData: userList, changeNameID: Toolbar.constants.changeName, + readOnly: readOnly }; + if (readOnly) {delete config.changeNameID; } toolbar = module.toolbar = Toolbar.create($bar, info.myID, info.realtime, info.getLag, info.userList, config); - createChangeName(Toolbar.constants.changeName, $bar); + if (!readOnly) { createChangeName(Toolbar.constants.changeName, $bar); } var $rightside = $bar.find('.' + Toolbar.constants.rightside); @@ -443,7 +449,11 @@ define([ configureTheme(); }); - window.location.hash = Cryptpad.getHashFromKeys(info.channel, secret.key); + // set the hash + if (!readOnly) { + window.location.hash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); + } + console.log("View Hash : " + Cryptpad.getViewHashFromKeys(info.channel, secret.keys)); Cryptpad.getPadTitle(function (err, title) { if (err) { console.log("Unable to get pad title"); @@ -510,7 +520,7 @@ define([ } // Update the user list (metadata) from the hyperjson - //updateUserList(shjson); + updateUserList(userDoc); editor.setValue(newDoc || Messages.codeInitialState); @@ -531,9 +541,17 @@ define([ console.error(err); return; } + // Update the toolbar list: + // Add the current user in the metadata if he has edit rights + if (readOnly) { return; } + myData[myID] = { + name: "" + }; + addToUserList(myData); if (typeof(lastName) === 'string' && lastName.length) { setName(lastName); } + onLocal(); }); }; @@ -604,17 +622,19 @@ define([ editor.scrollTo(scroll.left, scroll.top); - var localDoc = canonicalize($textarea.val()); - var hjson2 = { - content: localDoc, - metadata: userList, - highlightMode: highlightMode, - }; - var shjson2 = stringify(hjson2); - if (shjson2 !== shjson) { - console.error("shjson2 !== shjson"); - TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2)); - module.patchText(shjson2); + if (!readOnly) { + var localDoc = canonicalize($textarea.val()); + var hjson2 = { + content: localDoc, + metadata: userList, + highlightMode: highlightMode, + }; + var shjson2 = stringify(hjson2); + if (shjson2 !== shjson) { + console.error("shjson2 !== shjson"); + TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2)); + module.patchText(shjson2); + } } notify(); diff --git a/www/common/toolbar.js b/www/common/toolbar.js index 2f9ca2ce6..865529d78 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -141,22 +141,44 @@ define([ }); }; + var getViewers = function (n) { + if (!n || !parseInt(n) || n === 0) { return ''; } + if (n === 1) { return '; + ' + Messages.oneViewer; } + return '; + ' + Messages._getKey('viewers', [n]); + } var updateUserList = function (myUserName, listElement, userList, userData, readOnly) { var meIdx = userList.indexOf(myUserName); if (meIdx === -1) { listElement.textContent = Messages.synchronizing; return; } - console.log(userList); + var numberOfUsers = userList.length; userList = readOnly === -1 ? userList : arrayIntersect(userList, Object.keys(userData)); - console.log(userList); - if (userList.length === 1) { - listElement.innerHTML = Messages.editingAlone; - } else if (userList.length === 2) { - listElement.innerHTML = Messages.editingWithOneOtherPerson + getOtherUsers(myUserName, userList, userData); - } else { - listElement.innerHTML = Messages.editingWith + ' ' + (userList.length - 1) + ' ' + Messages.otherPeople + getOtherUsers(myUserName, userList, userData); + var innerHTML; + var numberOfViewUsers = numberOfUsers - userList.length; + if (readOnly === 1) { + innerHTML = '' + Messages.readonly + ''; + if (userList.length === 0) { + innerHTML += Messages.nobodyIsEditing; + } else if (userList.length === 1) { + innerHTML += Messages.onePersonIsEditing + getOtherUsers(myUserName, userList, userData); + } else { + innerHTML += Messages._getKey('peopleAreEditing', [userList.length]) + getOtherUsers(myUserName, userList, userData); + } + // Remove the current user + numberOfViewUsers--; + } + else { + if (userList.length === 1) { + innerHTML = Messages.editingAlone; + } else if (userList.length === 2) { + innerHTML = Messages.editingWithOneOtherPerson + getOtherUsers(myUserName, userList, userData); + } else { + innerHTML = Messages.editingWith + ' ' + (userList.length - 1) + ' ' + Messages.otherPeople + getOtherUsers(myUserName, userList, userData); + } } + innerHTML += getViewers(numberOfViewUsers); + listElement.innerHTML = innerHTML; }; var createLagElement = function ($container) { @@ -197,7 +219,7 @@ define([ var saveContentID = config.saveContentID || config.exportContentID; var loadContentID = config.loadContentID || config.importContentID; // readOnly = 1 (readOnly enabled), 0 (disabled), -1 (old pad without readOnly mode) - var readOnly = (typeof config.readOnly !== "undefined") ? (readOnly ? 1 : 0) : -1; + var readOnly = (typeof config.readOnly !== "undefined") ? (config.readOnly ? 1 : 0) : -1; var saveElement; var loadElement; diff --git a/www/pad/main.js b/www/pad/main.js index 348e507d8..f94c86981 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -327,7 +327,7 @@ define([ // the channel we will communicate over channel: secret.channel, - // our public key. send -1 if view mode + // our public key validateKey: secret.keys.validateKey || undefined, readOnly: readOnly, @@ -593,6 +593,9 @@ define([ var shjson = info.realtime.getUserDoc(); applyHjson(shjson); + // Update the user list (metadata) from the hyperjson + updateUserList(shjson); + if (Visible.isSupported()) { Visible.onChange(function (yes) { if (yes) { unnotify(); } @@ -603,6 +606,9 @@ define([ console.log("Unlocking editor"); setEditable(true); initializing = false; + // Update the toolbar list: + // Add the current user in the metadata if he has edit rights + if (readOnly) { return; } myData[myID] = { name: "" };