From 391b7690e6887900c472d245740f65e6e4fb15f1 Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Mon, 19 Sep 2016 14:52:36 +0200 Subject: [PATCH] Set the pad to readonly mode when using a 'view' url --- www/common/toolbar.js | 15 +++++++++++++-- www/pad/main.js | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/www/common/toolbar.js b/www/common/toolbar.js index 956defc4d..57e533b74 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -135,12 +135,21 @@ define([ return $span[0]; }; - var updateUserList = function (myUserName, listElement, userList, userData) { + var arrayIntersect = function(a, b) { + return $.grep(a, function(i) { + return $.inArray(i, b) > -1; + }); + }; + + var updateUserList = function (myUserName, listElement, userList, userData, readOnly) { var meIdx = userList.indexOf(myUserName); if (meIdx === -1) { listElement.textContent = Messages.synchronizing; return; } + console.log(userList); + 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) { @@ -187,6 +196,8 @@ define([ var changeNameID = config.changeNameID; 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 saveElement; var loadElement; @@ -205,7 +216,7 @@ define([ if(newUserData) { // Someone has changed his name/color userData = newUserData; } - updateUserList(myUserName, userListElement, users, userData); + updateUserList(myUserName, userListElement, users, userData, readOnly); }; var ks = function () { diff --git a/www/pad/main.js b/www/pad/main.js index 922cf1f02..348e507d8 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -65,6 +65,7 @@ define([ var andThen = function (Ckeditor) { var secret = Cryptpad.getSecrets(); + var readOnly = !secret.keys.editKeyStr; var fixThings = false; @@ -115,7 +116,9 @@ define([ } else { module.spinner.show(); } - inner.setAttribute('contenteditable', bool); + if (!readOnly || !bool) { + inner.setAttribute('contenteditable', bool); + } }; // don't let the user edit until the pad is ready @@ -191,6 +194,12 @@ define([ } } + // Do not change the contenteditable value in view mode + if (readOnly && info.node && info.node.tagName === 'BODY' && + info.diff.action === 'modifyAttribute' && info.diff.name === 'contenteditable') { + return true; + } + // no use trying to recover the cursor if it doesn't exist if (!cursor.exists()) { return; } @@ -295,7 +304,9 @@ define([ var applyHjson = function (shjson) { var userDocStateDom = hjsonToDom(JSON.parse(shjson)); - userDocStateDom.setAttribute("contenteditable", "true"); // lol wtf + if (!readOnly) { + userDocStateDom.setAttribute("contenteditable", "true"); // lol wtf + } var patch = (DD).diff(inner, userDocStateDom); (DD).apply(inner, patch); }; @@ -318,7 +329,7 @@ define([ // our public key. send -1 if view mode validateKey: secret.keys.validateKey || undefined, - readOnly: secret.keys.editKeyStr ? undefined : 1, + readOnly: readOnly, // method which allows us to get the id of the user setMyID: setMyID, @@ -383,6 +394,8 @@ define([ // build a dom from HJSON, diff, and patch the editor applyHjson(shjson); + if (readOnly) { return; } + var shjson2 = stringifyDOM(inner); if (shjson2 !== shjson) { console.error("shjson2 !== shjson"); @@ -467,9 +480,11 @@ define([ var config = { userData: userList, changeNameID: Toolbar.constants.changeName, + readOnly: readOnly }; + if (readOnly) {delete config.changeNameID; } toolbar = info.realtime.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); @@ -545,7 +560,7 @@ define([ $rightside.append($forgetPad); // set the hash - if (secret.keys.editKeyStr) { + if (!readOnly) { window.location.hash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); } console.log("View Hash : " + Cryptpad.getViewHashFromKeys(info.channel, secret.keys)); @@ -585,12 +600,17 @@ define([ } getLastName(function (err, lastName) { - if (typeof(lastName) === 'string' && lastName.length) { - setName(lastName); - } console.log("Unlocking editor"); setEditable(true); initializing = false; + myData[myID] = { + name: "" + }; + addToUserList(myData); + if (typeof(lastName) === 'string' && lastName.length) { + setName(lastName); + } + onLocal(); }); }; @@ -617,6 +637,7 @@ define([ var onLocal = realtimeOptions.onLocal = function () { if (initializing) { return; } + if (readOnly) { return; } // stringify the json and send it into chainpad var shjson = stringifyDOM(inner);