2020-05-14 12:06:01 -04:00
|
|
|
// 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 || '';
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:36:53 -04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-05-14 12:06:01 -04:00
|
|
|
// 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() {
|
2021-11-25 02:14:48 -05:00
|
|
|
const style = window.getComputedStyle(document.documentElement);
|
|
|
|
return style.getPropertyValue('--is-dark-theme').trim().toLowerCase() === 'true';
|
2020-05-14 12:06:01 -04:00
|
|
|
}
|
2020-05-20 22:00:43 -04:00
|
|
|
|
2020-10-21 07:02:24 -04:00
|
|
|
// strip <tags> from a string
|
|
|
|
export function stripTags(text) {
|
|
|
|
return text.replace(/<[^>]*>?/gm, '');
|
2020-07-27 02:24:09 -04:00
|
|
|
}
|
2021-03-18 19:43:43 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-10-22 10:34:01 -04:00
|
|
|
|
|
|
|
export function parseIssueHref(href) {
|
|
|
|
const path = (href || '').replace(/[#?].*$/, '');
|
|
|
|
const [_, owner, repo, type, index] = /([^/]+)\/([^/]+)\/(issues|pulls)\/([0-9]+)/.exec(path) || [];
|
|
|
|
return {owner, repo, type, index};
|
|
|
|
}
|
2022-06-09 07:15:08 -04:00
|
|
|
|
2022-06-12 08:08:23 -04:00
|
|
|
// pretty-print a number using locale-specific separators, e.g. 1200 -> 1,200
|
|
|
|
export function prettyNumber(num, locale = 'en-US') {
|
|
|
|
if (typeof num !== 'number') return '';
|
|
|
|
const {format} = new Intl.NumberFormat(locale);
|
|
|
|
return format(num);
|
|
|
|
}
|
2022-08-23 08:58:04 -04:00
|
|
|
|
|
|
|
// parse a URL, either relative '/path' or absolute 'https://localhost/path'
|
|
|
|
export function parseUrl(str) {
|
|
|
|
return new URL(str, str.startsWith('http') ? undefined : window.location.origin);
|
|
|
|
}
|
2022-10-28 09:48:24 -04:00
|
|
|
|
|
|
|
// return current locale chosen by user
|
|
|
|
function getCurrentLocale() {
|
|
|
|
return document.documentElement.lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a month (0-11), returns it in the documents language
|
|
|
|
export function translateMonth(month) {
|
2022-11-22 03:46:29 -05:00
|
|
|
return new Date(Date.UTC(2022, month, 12)).toLocaleString(getCurrentLocale(), {month: 'short', timeZone: 'UTC'});
|
2022-10-28 09:48:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// given a weekday (0-6, Sunday to Saturday), returns it in the documents language
|
|
|
|
export function translateDay(day) {
|
2022-11-22 03:46:29 -05:00
|
|
|
return new Date(Date.UTC(2022, 7, day)).toLocaleString(getCurrentLocale(), {weekday: 'short', timeZone: 'UTC'});
|
2022-10-28 09:48:24 -04:00
|
|
|
}
|
2022-11-21 04:59:42 -05:00
|
|
|
|
|
|
|
// convert a Blob to a DataURI
|
|
|
|
export function blobToDataURI(blob) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.addEventListener('load', (e) => {
|
|
|
|
resolve(e.target.result);
|
|
|
|
});
|
|
|
|
reader.addEventListener('error', () => {
|
|
|
|
reject(new Error('FileReader failed'));
|
|
|
|
});
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert image Blob to another mime-type format.
|
|
|
|
export function convertImage(blob, mime) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const img = new Image();
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
img.addEventListener('load', () => {
|
|
|
|
try {
|
|
|
|
canvas.width = img.naturalWidth;
|
|
|
|
canvas.height = img.naturalHeight;
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
context.drawImage(img, 0, 0);
|
|
|
|
canvas.toBlob((blob) => {
|
|
|
|
if (!(blob instanceof Blob)) return reject(new Error('imageBlobToPng failed'));
|
|
|
|
resolve(blob);
|
|
|
|
}, mime);
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
img.addEventListener('error', () => {
|
|
|
|
reject(new Error('imageBlobToPng failed'));
|
|
|
|
});
|
|
|
|
img.src = await blobToDataURI(blob);
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-02-06 13:09:18 -05:00
|
|
|
|
2023-02-07 11:08:44 -05:00
|
|
|
export function toAbsoluteUrl(url) {
|
|
|
|
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
|
|
return url;
|
2023-02-06 13:09:18 -05:00
|
|
|
}
|
2023-02-07 11:08:44 -05:00
|
|
|
if (url.startsWith('//')) {
|
|
|
|
return `${window.location.protocol}${url}`; // it's also a somewhat absolute URL (with the current scheme)
|
|
|
|
}
|
|
|
|
if (url && !url.startsWith('/')) {
|
|
|
|
throw new Error('unsupported url, it should either start with / or http(s)://');
|
|
|
|
}
|
|
|
|
return `${window.location.origin}${url}`;
|
2023-02-06 13:09:18 -05:00
|
|
|
}
|
Scoped labels (#22585)
Add a new "exclusive" option per label. This makes it so that when the
label is named `scope/name`, no other label with the same `scope/`
prefix can be set on an issue.
The scope is determined by the last occurence of `/`, so for example
`scope/alpha/name` and `scope/beta/name` are considered to be in
different scopes and can coexist.
Exclusive scopes are not enforced by any database rules, however they
are enforced when editing labels at the models level, automatically
removing any existing labels in the same scope when either attaching a
new label or replacing all labels.
In menus use a circle instead of checkbox to indicate they function as
radio buttons per scope. Issue filtering by label ensures that only a
single scoped label is selected at a time. Clicking with alt key can be
used to remove a scoped label, both when editing individual issues and
batch editing.
Label rendering refactor for consistency and code simplification:
* Labels now consistently have the same shape, emojis and tooltips
everywhere. This includes the label list and label assignment menus.
* In label list, show description below label same as label menus.
* Don't use exactly black/white text colors to look a bit nicer.
* Simplify text color computation. There is no point computing luminance
in linear color space, as this is a perceptual problem and sRGB is
closer to perceptually linear.
* Increase height of label assignment menus to show more labels. Showing
only 3-4 labels at a time leads to a lot of scrolling.
* Render all labels with a new RenderLabel template helper function.
Label creation and editing in multiline modal menu:
* Change label creation to open a modal menu like label editing.
* Change menu layout to place name, description and colors on separate
lines.
* Don't color cancel button red in label editing modal menu.
* Align text to the left in model menu for better readability and
consistent with settings layout elsewhere.
Custom exclusive scoped label rendering:
* Display scoped label prefix and suffix with slightly darker and
lighter background color respectively, and a slanted edge between them
similar to the `/` symbol.
* In menus exclusive labels are grouped with a divider line.
---------
Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
2023-02-18 14:17:39 -05:00
|
|
|
|
|
|
|
// determine if light or dark text color should be used on a given background color
|
|
|
|
// NOTE: see models/issue_label.go for similar implementation
|
|
|
|
export function useLightTextOnBackground(backgroundColor) {
|
|
|
|
if (backgroundColor[0] === '#') {
|
|
|
|
backgroundColor = backgroundColor.substring(1);
|
|
|
|
}
|
|
|
|
// Perceived brightness from: https://www.w3.org/TR/AERT/#color-contrast
|
|
|
|
// In the future WCAG 3 APCA may be a better solution.
|
|
|
|
const r = parseInt(backgroundColor.substring(0, 2), 16);
|
|
|
|
const g = parseInt(backgroundColor.substring(2, 4), 16);
|
|
|
|
const b = parseInt(backgroundColor.substring(4, 6), 16);
|
|
|
|
const brightness = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
|
|
|
return brightness < 0.35;
|
|
|
|
}
|