From b8a3a4d1886127d129d0433f4854f79570cec8aa Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 22 Jul 2016 14:31:40 +0200 Subject: [PATCH] persist usernames across sessions via localStorage --- www/code/main.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/www/code/main.js b/www/code/main.js index 2647f01a2..a97020be5 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -168,21 +168,32 @@ define([ } }; + var setName = module.setName = function (newName) { + if (!(typeof(newName) === 'string' && newName.trim())) { return; } + var myUserNameTemp = newName.trim(); + if(newName.trim().length > 32) { + myUserNameTemp = myUserNameTemp.substr(0, 32); + } + myUserName = myUserNameTemp; + myData[myID] = { + name: myUserName + }; + addToUserList(myData); + Cryptpad.setPadAttribute('username', myUserName); + onLocal(); + }; + + var getLastName = function () { + return Cryptpad.getPadAttribute('username') || ''; + }; + var createChangeName = function(id, $container) { var buttonElmt = $container.find('#'+id)[0]; + + var lastName = getLastName(); buttonElmt.addEventListener("click", function() { - Cryptpad.prompt(Messages.changeNamePrompt, '', function (newName) { - if (!(typeof(newName) === 'string' && newName.trim())) { return; } - var myUserNameTemp = newName.trim(); - if(newName.trim().length > 32) { - myUserNameTemp = myUserNameTemp.substr(0, 32); - } - myUserName = myUserNameTemp; - myData[myID] = { - name: myUserName - }; - addToUserList(myData); - onLocal(); + Cryptpad.prompt(Messages.changeNamePrompt, lastName, function (newName) { + setName(newName); }); }); }; @@ -427,9 +438,16 @@ define([ }); } + setEditable(true); initializing = false; //Cryptpad.log("Your document is ready"); + + var lastName = getLastName(); + if (typeof(lastName) === 'string' && lastName.length) { + console.log("Setting previous name to %s", lastName); + setName(lastName); + } }; var cursorToPos = function(cursor, oldText) {