Try to beautify code by adding line breaks

This commit is contained in:
schlagmichdoch 2023-11-02 02:56:01 +01:00
parent c068a2e329
commit d9270a5560
13 changed files with 714 additions and 470 deletions

View file

@ -64,10 +64,13 @@ class PeersUI {
}
_loadSavedDisplayName() {
this._getSavedDisplayName().then(displayName => {
console.log("Retrieved edited display name:", displayName)
if (displayName) Events.fire('self-display-name-changed', displayName);
});
this._getSavedDisplayName()
.then(displayName => {
console.log("Retrieved edited display name:", displayName)
if (displayName) {
Events.fire('self-display-name-changed', displayName);
}
});
}
_onDisplayName(displayName){
@ -104,7 +107,8 @@ class PeersUI {
if (this.$discoveryWrapper.querySelectorAll('div:last-of-type > span[hidden]').length < 2) {
this.$discoveryWrapper.classList.remove('row');
this.$discoveryWrapper.classList.add('column');
} else {
}
else {
this.$discoveryWrapper.classList.remove('column');
this.$discoveryWrapper.classList.add('row');
}
@ -147,7 +151,8 @@ class PeersUI {
Events.fire('self-display-name-changed', newDisplayName);
Events.fire('broadcast-send', {type: 'self-display-name-changed', detail: newDisplayName});
});
} else {
}
else {
PersistentStorage.delete('editedDisplayName')
.catch(_ => {
console.log("This browser does not support IndexedDB. Use localStorage instead.")
@ -188,8 +193,12 @@ class PeersUI {
this._changePeerDisplayName(e.detail.peerId, e.detail.displayName);
}
_isAnyDialogShown() {
return document.querySelectorAll('x-dialog[show]').length === 0;
}
_onKeyDown(e) {
if (document.querySelectorAll('x-dialog[show]').length === 0 && window.pasteMode.activated && e.code === "Escape") {
if (this._isAnyDialogShown() && window.pasteMode.activated && e.code === "Escape") {
Events.fire('deactivate-paste-mode');
}
@ -245,7 +254,8 @@ class PeersUI {
_evaluateOverflowing() {
if (this.$xPeers.clientHeight < this.$xPeers.scrollHeight) {
this.$xPeers.classList.add('overflowing');
} else {
}
else {
this.$xPeers.classList.remove('overflowing');
}
}
@ -319,9 +329,11 @@ class PeersUI {
if (files.length === 1) {
descriptor = `<i>${files[0].name}</i>`;
} else if (files.length > 1) {
}
else if (files.length > 1) {
descriptor = `<i>${files[0].name}</i><br>${andOtherFiles}`;
} else {
}
else {
descriptor = sharedText;
}
@ -380,7 +392,8 @@ class PeersUI {
files: files,
to: peerId
});
} else if (text.length > 0) {
}
else if (text.length > 0) {
Events.fire('send-text', {
text: text,
to: peerId
@ -408,7 +421,8 @@ class PeerUI {
let input = '';
if (window.pasteMode.activated) {
title = Localization.getTranslation("peer-ui.click-to-send-paste-mode", null, {descriptor: window.pasteMode.descriptor});
} else {
}
else {
title = Localization.getTranslation("peer-ui.click-to-send");
input = '<input type="file" multiple>';
}
@ -500,7 +514,8 @@ class PeerUI {
this.$el.addEventListener('contextmenu', this._callbackContextMenu);
this.$el.addEventListener('touchstart', this._callbackTouchStart);
this.$el.addEventListener('touchend', this._callbackTouchEnd);
} else {
}
else {
// Remove Events Normal Mode
this.$el.removeEventListener('click', this._callbackClickSleep);
this.$el.removeEventListener('touchstart', this._callbackTouchStartSleep);
@ -568,7 +583,8 @@ class PeerUI {
const $progress = this.$el.querySelector('.progress');
if (0.5 < progress && progress < 1) {
$progress.classList.add('over50');
} else {
}
else {
$progress.classList.remove('over50');
}
if (progress < 1) {
@ -584,7 +600,8 @@ class PeerUI {
this.$el.querySelector('.status').innerText = statusName;
this.currentStatus = status;
}
} else {
}
else {
this.$el.removeAttribute('status');
this.$el.querySelector('.status').innerHTML = '';
progress = 0;
@ -629,7 +646,8 @@ class PeerUI {
_onTouchEnd(e) {
if (Date.now() - this._touchStart < 500) {
clearTimeout(this._touchTimer);
} else if (this._touchTimer) { // this was a long tap
}
else if (this._touchTimer) { // this was a long tap
e.preventDefault();
Events.fire('text-recipient', {
peerId: this._peer.id,
@ -643,7 +661,9 @@ class PeerUI {
class Dialog {
constructor(id) {
this.$el = $(id);
this.$el.querySelectorAll('[close]').forEach(el => el.addEventListener('click', _ => this.hide()));
this.$el.querySelectorAll('[close]').forEach(el => {
el.addEventListener('click', _ => this.hide())
});
this.$autoFocus = this.$el.querySelector('[autofocus]');
Events.on('peer-disconnected', e => this._onPeerDisconnected(e.detail));
@ -701,7 +721,8 @@ class LanguageSelectDialog extends Dialog {
show() {
if (Localization.isSystemLocale()) {
this.$languageButtons[0].focus();
} else {
}
else {
let locale = Localization.getLocale();
for (let i=0; i<this.$languageButtons.length; i++) {
const $btn = this.$languageButtons[i];
@ -720,7 +741,8 @@ class LanguageSelectDialog extends Dialog {
if (languageCode) {
localStorage.setItem('language-code', languageCode);
} else {
}
else {
localStorage.removeItem('language-code');
}
@ -747,11 +769,14 @@ class ReceiveDialog extends Dialog {
// 1024^2 = 104876; 1024^3 = 1073741824
if (bytes >= 1073741824) {
return Math.round(10 * bytes / 1073741824) / 10 + ' GB';
} else if (bytes >= 1048576) {
}
else if (bytes >= 1048576) {
return Math.round(bytes / 1048576) + ' MB';
} else if (bytes > 1024) {
}
else if (bytes > 1024) {
return Math.round(bytes / 1024) + ' KB';
} else {
}
else {
return bytes + ' Bytes';
}
}
@ -763,7 +788,8 @@ class ReceiveDialog extends Dialog {
fileOther = imagesOnly
? Localization.getTranslation("dialogs.file-other-description-image")
: Localization.getTranslation("dialogs.file-other-description-file");
} else if (files.length >= 2) {
}
else if (files.length >= 2) {
fileOther = imagesOnly
? Localization.getTranslation("dialogs.file-other-description-image-plural", null, {count: files.length - 1})
: Localization.getTranslation("dialogs.file-other-description-file-plural", null, {count: files.length - 1});
@ -847,7 +873,8 @@ class ReceiveFileDialog extends ReceiveDialog {
if (Object.keys(previewElement).indexOf(mime) === -1) {
resolve(false);
} else {
}
else {
let element = document.createElement(previewElement[mime]);
element.controls = true;
element.onload = _ => {
@ -877,7 +904,8 @@ class ReceiveFileDialog extends ReceiveDialog {
descriptor = imagesOnly
? Localization.getTranslation("dialogs.title-image")
: Localization.getTranslation("dialogs.title-file");
} else {
}
else {
descriptor = imagesOnly
? Localization.getTranslation("dialogs.title-image-plural")
: Localization.getTranslation("dialogs.title-file-plural");
@ -939,7 +967,8 @@ class ReceiveFileDialog extends ReceiveDialog {
tmpZipBtn.download = filenameDownload;
tmpZipBtn.href = url;
tmpZipBtn.click();
} else {
}
else {
this._downloadFilesIndividually(files);
}
@ -962,7 +991,8 @@ class ReceiveFileDialog extends ReceiveDialog {
setTimeout(() => {
if (canShare) {
this.$shareBtn.click();
} else {
}
else {
this.$downloadBtn.click();
}
}, 500);
@ -971,7 +1001,8 @@ class ReceiveFileDialog extends ReceiveDialog {
.then(canPreview => {
if (canPreview) {
console.log('the file is able to preview');
} else {
}
else {
console.log('the file is not able to preview');
}
})
@ -1134,10 +1165,12 @@ class InputKeyContainer {
if (e.key === "Backspace" && previousSibling && !e.target.value) {
previousSibling.value = '';
previousSibling.focus();
} else if (e.key === "ArrowRight" && nextSibling) {
}
else if (e.key === "ArrowRight" && nextSibling) {
e.preventDefault();
nextSibling.focus();
} else if (e.key === "ArrowLeft" && previousSibling) {
}
else if (e.key === "ArrowLeft" && previousSibling) {
e.preventDefault();
previousSibling.focus();
}
@ -1173,7 +1206,8 @@ class InputKeyContainer {
_evaluateKeyChars() {
if (this.$inputKeyContainer.querySelectorAll('input:placeholder-shown').length > 0) {
this._onNotAllCharsFilled();
} else {
}
else {
this._onAllCharsFilled();
const lastCharFocused = document.activeElement === this.$inputKeyChars[this.$inputKeyChars.length - 1];
@ -1243,7 +1277,10 @@ class PairDeviceDialog extends Dialog {
_onPaste(e) {
e.preventDefault();
let pastedKey = e.clipboardData.getData("Text").replace(/\D/g,'').substring(0, 6);
let pastedKey = e.clipboardData
.getData("Text")
.replace(/\D/g,'')
.substring(0, 6);
this.inputKeyContainer._onPaste(pastedKey);
}
@ -1366,7 +1403,8 @@ class PairDeviceDialog extends Dialog {
deviceName = $peer.ui._peer.name.deviceName;
}
PersistentStorage.addRoomSecret(roomSecret, displayName, deviceName)
PersistentStorage
.addRoomSecret(roomSecret, displayName, deviceName)
.then(_ => {
Events.fire('notify-user', Localization.getTranslation("notifications.pairing-success"));
this._evaluateNumberRoomSecrets();
@ -1407,18 +1445,22 @@ class PairDeviceDialog extends Dialog {
}
_onSecretRoomDeleted(roomSecret) {
PersistentStorage.deleteRoomSecret(roomSecret).then(_ => {
this._evaluateNumberRoomSecrets();
});
PersistentStorage
.deleteRoomSecret(roomSecret)
.then(_ => {
this._evaluateNumberRoomSecrets();
});
}
_evaluateNumberRoomSecrets() {
PersistentStorage.getAllRoomSecrets()
PersistentStorage
.getAllRoomSecrets()
.then(roomSecrets => {
if (roomSecrets.length > 0) {
this.$editPairedDevicesHeaderBtn.removeAttribute('hidden');
this.$footerInstructionsPairedDevices.removeAttribute('hidden');
} else {
}
else {
this.$editPairedDevicesHeaderBtn.setAttribute('hidden', '');
this.$footerInstructionsPairedDevices.setAttribute('hidden', '');
}
@ -1452,45 +1494,51 @@ class EditPairedDevicesDialog extends Dialog {
const autoAcceptString = Localization.getTranslation("dialogs.auto-accept").toLowerCase();
const roomSecretsEntries = await PersistentStorage.getAllRoomSecretEntries();
roomSecretsEntries.forEach(roomSecretsEntry => {
let $pairedDevice = document.createElement('div');
$pairedDevice.classList = ["paired-device"];
roomSecretsEntries
.forEach(roomSecretsEntry => {
let $pairedDevice = document.createElement('div');
$pairedDevice.classList = ["paired-device"];
$pairedDevice.innerHTML = `
<div class="display-name">
<span>${roomSecretsEntry.display_name}</span>
</div>
<div class="device-name">
<span>${roomSecretsEntry.device_name}</span>
</div>
<div class="button-wrapper">
<label class="auto-accept pointer">${autoAcceptString}
<input type="checkbox" ${roomSecretsEntry.auto_accept ? "checked" : ""}>
</label>
<button class="button" type="button">${unpairString}</button>
</div>`
$pairedDevice.innerHTML = `
<div class="display-name">
<span>${roomSecretsEntry.display_name}</span>
</div>
<div class="device-name">
<span>${roomSecretsEntry.device_name}</span>
</div>
<div class="button-wrapper">
<label class="auto-accept pointer">${autoAcceptString}
<input type="checkbox" ${roomSecretsEntry.auto_accept ? "checked" : ""}>
</label>
<button class="button" type="button">${unpairString}</button>
</div>`
$pairedDevice.querySelector('input[type="checkbox"]').addEventListener('click', e => {
PersistentStorage.updateRoomSecretAutoAccept(roomSecretsEntry.secret, e.target.checked).then(roomSecretsEntry => {
Events.fire('auto-accept-updated', {
'roomSecret': roomSecretsEntry.entry.secret,
'autoAccept': e.target.checked
$pairedDevice
.querySelector('input[type="checkbox"]')
.addEventListener('click', e => {
PersistentStorage.updateRoomSecretAutoAccept(roomSecretsEntry.secret, e.target.checked)
.then(roomSecretsEntry => {
Events.fire('auto-accept-updated', {
'roomSecret': roomSecretsEntry.entry.secret,
'autoAccept': e.target.checked
});
});
});
});
});
$pairedDevice.querySelector('button').addEventListener('click', e => {
PersistentStorage.deleteRoomSecret(roomSecretsEntry.secret).then(roomSecret => {
Events.fire('room-secrets-deleted', [roomSecret]);
Events.fire('evaluate-number-room-secrets');
e.target.parentNode.parentNode.remove();
});
$pairedDevice
.querySelector('button')
.addEventListener('click', e => {
PersistentStorage.deleteRoomSecret(roomSecretsEntry.secret)
.then(roomSecret => {
Events.fire('room-secrets-deleted', [roomSecret]);
Events.fire('evaluate-number-room-secrets');
e.target.parentNode.parentNode.remove();
});
})
this.$pairedDevicesWrapper.html = "";
this.$pairedDevicesWrapper.appendChild($pairedDevice)
})
this.$pairedDevicesWrapper.html = "";
this.$pairedDevicesWrapper.appendChild($pairedDevice)
})
}
hide() {
@ -1505,14 +1553,17 @@ class EditPairedDevicesDialog extends Dialog {
}
_clearRoomSecrets() {
PersistentStorage.getAllRoomSecrets()
PersistentStorage
.getAllRoomSecrets()
.then(roomSecrets => {
PersistentStorage.clearRoomSecrets().finally(() => {
Events.fire('room-secrets-deleted', roomSecrets);
Events.fire('evaluate-number-room-secrets');
Events.fire('notify-user', Localization.getTranslation("notifications.pairing-cleared"));
this.hide();
})
PersistentStorage
.clearRoomSecrets()
.finally(() => {
Events.fire('room-secrets-deleted', roomSecrets);
Events.fire('evaluate-number-room-secrets');
Events.fire('notify-user', Localization.getTranslation("notifications.pairing-cleared"));
this.hide();
})
});
}
@ -1526,9 +1577,11 @@ class EditPairedDevicesDialog extends Dialog {
if (!peer || !peer._roomIds["secret"]) return;
PersistentStorage.updateRoomSecretNames(peer._roomIds["secret"], peer.name.displayName, peer.name.deviceName).then(roomSecretEntry => {
console.log(`Successfully updated DisplayName and DeviceName for roomSecretEntry ${roomSecretEntry.key}`);
})
PersistentStorage
.updateRoomSecretNames(peer._roomIds["secret"], peer.name.displayName, peer.name.deviceName)
.then(roomSecretEntry => {
console.log(`Successfully updated DisplayName and DeviceName for roomSecretEntry ${roomSecretEntry.key}`);
})
}
}
@ -1591,7 +1644,8 @@ class PublicRoomDialog extends Dialog {
_onHeaderBtnClick() {
if (this.roomId) {
this.show();
} else {
}
else {
this._createPublicRoom();
}
}
@ -1784,7 +1838,8 @@ class SendTextDialog extends Dialog {
if (e.code === "Escape") {
this.hide();
} else if (e.code === "Enter" && (e.ctrlKey || e.metaKey)) {
}
else if (e.code === "Enter" && (e.ctrlKey || e.metaKey)) {
if (this._textInputEmpty()) return;
this._send();
}
@ -1797,7 +1852,8 @@ class SendTextDialog extends Dialog {
_onChange(e) {
if (this._textInputEmpty()) {
this.$submit.setAttribute('disabled', '');
} else {
}
else {
this.$submit.removeAttribute('disabled');
}
}
@ -1855,7 +1911,8 @@ class ReceiveTextDialog extends Dialog {
if (e.code === "KeyC" && (e.ctrlKey || e.metaKey)) {
await this._onCopy()
this.hide();
} else if (e.code === "Escape") {
}
else if (e.code === "Escape") {
this.hide();
}
}
@ -1905,7 +1962,8 @@ class ReceiveTextDialog extends Dialog {
async _onCopy() {
const sanitizedText = this.$text.innerText.replace(/\u00A0/gm, ' ');
navigator.clipboard.writeText(sanitizedText)
navigator.clipboard
.writeText(sanitizedText)
.then(_ => {
Events.fire('notify-user', Localization.getTranslation("notifications.copied-to-clipboard"));
this.hide();
@ -1939,7 +1997,8 @@ class Base64ZipDialog extends Dialog {
// ?base64text=paste
// base64 encoded string is ready to be pasted from clipboard
this.preparePasting('text');
} else if (base64Text === 'hash') {
}
else if (base64Text === 'hash') {
// ?base64text=hash#BASE64ENCODED
// base64 encoded string is url hash which is never sent to server and faster (recommended)
this.processBase64Text(base64Hash)
@ -1949,7 +2008,8 @@ class Base64ZipDialog extends Dialog {
}).finally(() => {
this.hide();
});
} else {
}
else {
// ?base64text=BASE64ENCODED
// base64 encoded string was part of url param (not recommended)
this.processBase64Text(base64Text)
@ -1960,7 +2020,8 @@ class Base64ZipDialog extends Dialog {
this.hide();
});
}
} else if (base64Zip) {
}
else if (base64Zip) {
this.show();
if (base64Zip === "hash") {
// ?base64zip=hash#BASE64ENCODED
@ -1972,7 +2033,8 @@ class Base64ZipDialog extends Dialog {
}).finally(() => {
this.hide();
});
} else {
}
else {
// ?base64zip=paste || ?base64zip=true
this.preparePasting('files');
}
@ -1993,7 +2055,8 @@ class Base64ZipDialog extends Dialog {
this.$pasteBtn.innerText = Localization.getTranslation("dialogs.base64-tap-to-paste", null, {type: translateType});
this._clickCallback = _ => this.processClipboard(type);
this.$pasteBtn.addEventListener('click', _ => this._clickCallback());
} else {
}
else {
console.log("`navigator.clipboard.readText()` is not available on your browser.\nOn Firefox you can set `dom.events.asyncClipboard.readText` to true under `about:config` for convenience.")
this.$pasteBtn.setAttribute('hidden', '');
this.$fallbackTextarea.setAttribute('placeholder', Localization.getTranslation("dialogs.base64-paste-to-send", null, {type: translateType}));
@ -2032,7 +2095,8 @@ class Base64ZipDialog extends Dialog {
try {
if (type === 'text') {
await this.processBase64Text(base64);
} else {
}
else {
await this.processBase64Zip(base64);
}
} catch(_) {
@ -2163,7 +2227,8 @@ class Notifications {
if (/^((https?:\/\/|www)[abcdefghijklmnopqrstuvwxyz0123456789\-._~:\/?#\[\]@!$&'()*+,;=]+)$/.test(message.toLowerCase())) {
const notification = this._notify(Localization.getTranslation("notifications.link-received", null, {name: peerDisplayName}), message);
this._bind(notification, _ => window.open(message, '_blank', "noreferrer"));
} else {
}
else {
const notification = this._notify(Localization.getTranslation("notifications.message-received", null, {name: peerDisplayName}), message);
this._bind(notification, _ => this._copyText(message, notification));
}
@ -2182,13 +2247,15 @@ class Notifications {
let title;
if (files.length === 1) {
title = `${files[0].name}`;
} else {
}
else {
let fileOther;
if (files.length === 2) {
fileOther = imagesOnly
? Localization.getTranslation("dialogs.file-other-description-image")
: Localization.getTranslation("dialogs.file-other-description-file");
} else {
}
else {
fileOther = imagesOnly
? Localization.getTranslation("dialogs.file-other-description-image-plural", null, {count: files.length - 1})
: Localization.getTranslation("dialogs.file-other-description-file-plural", null, {count: files.length - 1});
@ -2217,17 +2284,19 @@ class Notifications {
descriptor = imagesOnly
? Localization.getTranslation("dialogs.title-image")
: Localization.getTranslation("dialogs.title-file");
} else {
}
else {
descriptor = imagesOnly
? Localization.getTranslation("dialogs.title-image-plural")
: Localization.getTranslation("dialogs.title-file-plural");
}
let title = Localization.getTranslation("notifications.request-title", null, {
name: displayName,
count: request.header.length,
descriptor: descriptor.toLowerCase()
});
let title = Localization
.getTranslation("notifications.request-title", null, {
name: displayName,
count: request.header.length,
descriptor: descriptor.toLowerCase()
});
const notification = this._notify(title, Localization.getTranslation("notifications.click-to-show"));
}
@ -2242,17 +2311,23 @@ class Notifications {
if (await navigator.clipboard.writeText(message)) {
notification.close();
this._notify(Localization.getTranslation("notifications.copied-text"));
} else {
}
else {
this._notify(Localization.getTranslation("notifications.copied-text-error"));
}
}
_bind(notification, handler) {
if (notification.then) {
notification.then(_ => serviceWorker.getNotifications().then(_ => {
serviceWorker.addEventListener('notificationclick', handler);
}));
} else {
notification.then(_ => {
serviceWorker
.getNotifications()
.then(_ => {
serviceWorker.addEventListener('notificationclick', handler);
})
});
}
else {
notification.onclick = handler;
}
}
@ -2291,14 +2366,17 @@ class WebShareTargetUI {
if (url) {
shareTargetText = url; // we share only the link - no text.
} else if (title && text) {
}
else if (title && text) {
shareTargetText = title + '\r\n' + text;
} else {
}
else {
shareTargetText = title + text;
}
Events.fire('activate-paste-mode', {files: [], text: shareTargetText})
} else if (share_target_type === "files") {
}
else if (share_target_type === "files") {
let openRequest = window.indexedDB.open('pairdrop_store')
openRequest.onsuccess = e => {
const db = e.target.result;
@ -2820,7 +2898,8 @@ const pairDrop = new PairDrop();
const localization = new Localization();
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
navigator.serviceWorker
.register('/service-worker.js')
.then(serviceWorker => {
console.log('Service Worker registered');
window.serviceWorker = serviceWorker