Skip to content

Commit

Permalink
Merge branch 'fix-displayname-firefox'
Browse files Browse the repository at this point in the history
  • Loading branch information
schlagmichdoch committed Aug 16, 2024
2 parents 703894e + 13f7d36 commit 96a055b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ <h2 data-i18n-key="instructions.no-peers-title" data-i18n-attrs="text"></h2>
<div class="column">
<div class="known-as-wrapper">
<span data-i18n-key="footer.known-as" data-i18n-attrs="text"></span>
<div id="display-name" class="badge" data-i18n-key="footer.display-name" data-i18n-attrs="data-placeholder title" placeholder="Loading..." autocorrect="off" autocomplete="off" autocapitalize="none" spellcheck="false" contenteditable></div>
<div id="display-name" class="badge" data-i18n-key="footer.display-name" data-i18n-attrs="data-placeholder title" autocorrect="off" autocomplete="off" autocapitalize="none" spellcheck="false" contenteditable></div>
<svg class="icon edit-pen">
<use xlink:href="#edit-pen-icon"></use>
</svg>
Expand Down
47 changes: 32 additions & 15 deletions public/scripts/ui-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,11 @@ class FooterUI {
this.$discoveryWrapper = $$('footer .discovery-wrapper');

this.$displayName.addEventListener('keydown', e => this._onKeyDownDisplayName(e));
this.$displayName.addEventListener('keyup', e => this._onKeyUpDisplayName(e));
this.$displayName.addEventListener('blur', e => this._saveDisplayName(e.target.innerText));
this.$displayName.addEventListener('focus', e => this._onFocusDisplayName(e));
this.$displayName.addEventListener('blur', e => this._onBlurDisplayName(e));

Events.on('display-name', e => this._onDisplayName(e.detail.displayName));
Events.on('self-display-name-changed', e => this._insertDisplayName(e.detail));

// Load saved display name on page load
Events.on('ws-connected', _ => this._loadSavedDisplayName());

Events.on('evaluate-footer-badges', _ => this._evaluateFooterBadges());
}

Expand All @@ -234,17 +230,20 @@ class FooterUI {
}

async _loadSavedDisplayName() {
const displayName = await this._getSavedDisplayName()
const displayNameSaved = await this._getSavedDisplayName()

if (!displayName) return;
if (!displayNameSaved) return;

console.log("Retrieved edited display name:", displayName)
Events.fire('self-display-name-changed', displayName);
console.log("Retrieved edited display name:", displayNameSaved)
Events.fire('self-display-name-changed', displayNameSaved);
}

_onDisplayName(displayName){
// set display name
this.$displayName.setAttribute('placeholder', displayName);
async _onDisplayName(displayNameServer){
// load saved displayname first to prevent flickering
await this._loadSavedDisplayName();

// set original display name as placeholder
this.$displayName.setAttribute('placeholder', displayNameServer);
}


Expand All @@ -259,9 +258,27 @@ class FooterUI {
}
}

_onKeyUpDisplayName(e) {
_onFocusDisplayName(e) {
if (!e.target.innerText) {
// Fix z-position of cursor when div is completely empty (Firefox only)
e.target.innerText = "\n";

// On Chromium based browsers the cursor position is lost when adding sth. to the focused node. This adds it back.
let sel = window.getSelection();
sel.collapse(e.target.lastChild);
}
}

async _onBlurDisplayName(e) {
// fix for Firefox inserting a linebreak into div on edit which prevents the placeholder from showing automatically when it is empty
if (/^(\n|\r|\r\n)$/.test(e.target.innerText)) e.target.innerText = '';
if (/^(\n|\r|\r\n)$/.test(e.target.innerText)) {
e.target.innerText = '';
}

// Remove selection from text
window.getSelection().removeAllRanges();

await this._saveDisplayName(e.target.innerText)
}

async _saveDisplayName(newDisplayName) {
Expand Down
12 changes: 8 additions & 4 deletions public/styles/styles-main.css
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@ footer .logo {
position: relative;
}

#display-name:focus::before {
display: none;
}

html:not([dir="rtl"]) #display-name,
html:not([dir="rtl"]) .edit-pen {
margin-left: -1rem;
Expand Down Expand Up @@ -939,8 +943,8 @@ body {
--lt-dialog-bg-color: #fff;
--lt-bg-color: 255,255,255;
--lt-bg-color-secondary: #f2f2f2;
--lt-border-color: #a9a9a9;
--lt-badge-color: #a5a5a5;
--lt-border-color: #757575;
--lt-badge-color: #757575;
--lt-lang-hr-color: #DDD;

--lt-shadow-color-secondary-rgb: 0,0,0;
Expand All @@ -953,8 +957,8 @@ body {
--dt-dialog-bg-color: #141414;
--dt-bg-color: 0,0,0;
--dt-bg-color-secondary: #262628;
--dt-border-color: #919191;
--dt-badge-color: #717171;
--dt-border-color: #757575;
--dt-badge-color: #757575;
--dt-lang-hr-color: #404040;

--dt-shadow-color-secondary-rgb: 255,255,255;
Expand Down

0 comments on commit 96a055b

Please sign in to comment.