mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-04 08:17:24 -05:00
4e0cca3f7d
Add a new default theme `auto`, which will automatically switch between `gitea` (light) and `arc-green` (dark) themes depending on the user's operating system settings. Closes: #8183
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
// transform /path/to/file.ext to file.ext
|
|
export function basename(path = '') {
|
|
return path ? path.replace(/^.*\//, '') : '';
|
|
}
|
|
|
|
// transform /path/to/file.ext to .ext
|
|
export function extname(path = '') {
|
|
const [_, ext] = /.+(\.[^.]+)$/.exec(path) || [];
|
|
return ext || '';
|
|
}
|
|
|
|
// join a list of path segments with slashes, ensuring no double slashes
|
|
export function joinPaths(...parts) {
|
|
let str = '';
|
|
for (const part of parts) {
|
|
if (!part) continue;
|
|
str = !str ? part : `${str.replace(/\/$/, '')}/${part.replace(/^\//, '')}`;
|
|
}
|
|
return str;
|
|
}
|
|
|
|
// test whether a variable is an object
|
|
export function isObject(obj) {
|
|
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
}
|
|
|
|
// returns whether a dark theme is enabled
|
|
export function isDarkTheme() {
|
|
if (document.documentElement.classList.contains('theme-auto')) {
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
}
|
|
if (document.documentElement.classList.contains('theme-arc-green')) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// removes duplicate elements in an array
|
|
export function uniq(arr) {
|
|
return Array.from(new Set(arr));
|
|
}
|
|
|
|
// strip <tags> from a string
|
|
export function stripTags(text) {
|
|
return text.replace(/<[^>]*>?/gm, '');
|
|
}
|
|
|
|
// searches the inclusive range [minValue, maxValue].
|
|
// credits: https://matthiasott.com/notes/write-your-media-queries-in-pixels-not-ems
|
|
export function mqBinarySearch(feature, minValue, maxValue, step, unit) {
|
|
if (maxValue - minValue < step) {
|
|
return minValue;
|
|
}
|
|
const mid = Math.ceil((minValue + maxValue) / 2 / step) * step;
|
|
if (matchMedia(`screen and (min-${feature}:${mid}${unit})`).matches) {
|
|
return mqBinarySearch(feature, mid, maxValue, step, unit); // feature is >= mid
|
|
}
|
|
return mqBinarySearch(feature, minValue, mid - step, step, unit); // feature is < mid
|
|
}
|