Merge pull request #896 from pano9000/refactor_view-login_JS

refactor(view/login): simplify JS
This commit is contained in:
Elian Doran 2025-01-07 22:20:20 +02:00 committed by GitHub
commit d41fee8ade
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -47,20 +47,8 @@
// Required for correct loading of scripts in Electron // Required for correct loading of scripts in Electron
if (typeof module === 'object') {window.module = module; module = undefined;} if (typeof module === 'object') {window.module = module; module = undefined;}
let device; const device = getDeviceType()
if (window.location.search === '?desktop') {
device = "desktop";
}
else if (window.location.search === '?mobile') {
device = "mobile";
}
else {
device = isMobile() ? "mobile" : "desktop";
}
console.log("Setting device cookie to:", device); console.log("Setting device cookie to:", device);
setCookie("trilium-device", device); setCookie("trilium-device", device);
function setCookie(name, value) { function setCookie(name, value) {
@ -70,15 +58,20 @@
document.cookie = name + "=" + (value || "") + expires + "; path=/"; document.cookie = name + "=" + (value || "") + expires + "; path=/";
} }
function getDeviceType() {
if (window.location.search === '?desktop') return "desktop";
if (window.location.search === '?mobile') return "mobile";
return isMobile() ? "mobile" : "desktop";
}
// https://stackoverflow.com/a/73731646/944162 // https://stackoverflow.com/a/73731646/944162
function isMobile() { function isMobile() {
const mQ = matchMedia?.('(pointer:coarse)'); const mQ = matchMedia?.('(pointer:coarse)');
if (mQ?.media === '(pointer:coarse)') return !!mQ.matches; if (mQ?.media === '(pointer:coarse)') return !!mQ.matches;
if ('orientation' in window) return true; if ('orientation' in window) return true;
const userAgentsRegEx = /\b(Android|iPhone|iPad|iPod|Windows Phone|BlackBerry|webOS|IEMobile)\b/i
return /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(navigator.userAgent) || return userAgentsRegEx.test(navigator.userAgent)
/\b(Android|Windows Phone|iPad|iPod)\b/i.test(navigator.userAgent);
} }
</script> </script>
</body> </body>