2024-11-08 03:21:13 +01:00
|
|
|
import {decode, encode} from 'uint8-to-base64';
|
|
|
|
import type {IssuePageInfo, IssuePathInfo} from './types.ts';
|
2023-06-06 07:29:37 +02:00
|
|
|
|
2020-05-14 18:06:01 +02:00
|
|
|
// transform /path/to/file.ext to file.ext
|
2024-08-28 18:32:38 +02:00
|
|
|
export function basename(path: string): string {
|
2024-02-25 15:31:15 +01:00
|
|
|
const lastSlashIndex = path.lastIndexOf('/');
|
|
|
|
return lastSlashIndex < 0 ? path : path.substring(lastSlashIndex + 1);
|
2020-05-14 18:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// transform /path/to/file.ext to .ext
|
2024-08-28 18:32:38 +02:00
|
|
|
export function extname(path: string): string {
|
2024-06-27 11:31:49 +02:00
|
|
|
const lastSlashIndex = path.lastIndexOf('/');
|
2024-02-25 15:31:15 +01:00
|
|
|
const lastPointIndex = path.lastIndexOf('.');
|
2024-06-27 11:31:49 +02:00
|
|
|
if (lastSlashIndex > lastPointIndex) return '';
|
2024-02-25 15:31:15 +01:00
|
|
|
return lastPointIndex < 0 ? '' : path.substring(lastPointIndex);
|
2020-05-14 18:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// test whether a variable is an object
|
2024-08-28 18:32:38 +02:00
|
|
|
export function isObject(obj: any): boolean {
|
2020-05-14 18:06:01 +02:00
|
|
|
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns whether a dark theme is enabled
|
2024-08-28 18:32:38 +02:00
|
|
|
export function isDarkTheme(): boolean {
|
2021-11-25 08:14:48 +01:00
|
|
|
const style = window.getComputedStyle(document.documentElement);
|
|
|
|
return style.getPropertyValue('--is-dark-theme').trim().toLowerCase() === 'true';
|
2020-05-14 18:06:01 +02:00
|
|
|
}
|
2020-05-21 04:00:43 +02:00
|
|
|
|
2020-10-21 13:02:24 +02:00
|
|
|
// strip <tags> from a string
|
2024-08-28 18:32:38 +02:00
|
|
|
export function stripTags(text: string): string {
|
2023-04-27 04:08:16 +02:00
|
|
|
return text.replace(/<[^>]*>?/g, '');
|
2020-07-27 08:24:09 +02:00
|
|
|
}
|
2021-03-19 00:43:43 +01:00
|
|
|
|
2024-10-30 21:06:36 +01:00
|
|
|
export function parseIssueHref(href: string): IssuePathInfo {
|
2021-10-22 16:34:01 +02:00
|
|
|
const path = (href || '').replace(/[#?].*$/, '');
|
2024-10-30 21:06:36 +01:00
|
|
|
const [_, ownerName, repoName, pathType, indexString] = /([^/]+)\/([^/]+)\/(issues|pulls)\/([0-9]+)/.exec(path) || [];
|
|
|
|
return {ownerName, repoName, pathType, indexString};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseIssueNewHref(href: string): IssuePathInfo {
|
|
|
|
const path = (href || '').replace(/[#?].*$/, '');
|
|
|
|
const [_, ownerName, repoName, pathType, indexString] = /([^/]+)\/([^/]+)\/(issues|pulls)\/new/.exec(path) || [];
|
|
|
|
return {ownerName, repoName, pathType, indexString};
|
2021-10-22 16:34:01 +02:00
|
|
|
}
|
2022-06-09 13:15:08 +02:00
|
|
|
|
2024-11-08 03:21:13 +01:00
|
|
|
export function parseIssuePageInfo(): IssuePageInfo {
|
|
|
|
const el = document.querySelector('#issue-page-info');
|
|
|
|
return {
|
|
|
|
issueNumber: parseInt(el?.getAttribute('data-issue-index')),
|
|
|
|
issueDependencySearchType: el?.getAttribute('data-issue-dependency-search-type') || '',
|
|
|
|
repoId: parseInt(el?.getAttribute('data-issue-repo-id')),
|
|
|
|
repoLink: el?.getAttribute('data-issue-repo-link') || '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-23 14:58:04 +02:00
|
|
|
// parse a URL, either relative '/path' or absolute 'https://localhost/path'
|
2024-08-28 18:32:38 +02:00
|
|
|
export function parseUrl(str: string): URL {
|
2022-08-23 14:58:04 +02:00
|
|
|
return new URL(str, str.startsWith('http') ? undefined : window.location.origin);
|
|
|
|
}
|
2022-10-28 15:48:24 +02:00
|
|
|
|
|
|
|
// return current locale chosen by user
|
2024-08-28 18:32:38 +02:00
|
|
|
export function getCurrentLocale(): string {
|
2022-10-28 15:48:24 +02:00
|
|
|
return document.documentElement.lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a month (0-11), returns it in the documents language
|
2024-08-28 18:32:38 +02:00
|
|
|
export function translateMonth(month: number) {
|
2022-11-22 09:46:29 +01:00
|
|
|
return new Date(Date.UTC(2022, month, 12)).toLocaleString(getCurrentLocale(), {month: 'short', timeZone: 'UTC'});
|
2022-10-28 15:48:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// given a weekday (0-6, Sunday to Saturday), returns it in the documents language
|
2024-08-28 18:32:38 +02:00
|
|
|
export function translateDay(day: number) {
|
2022-11-22 09:46:29 +01:00
|
|
|
return new Date(Date.UTC(2022, 7, day)).toLocaleString(getCurrentLocale(), {weekday: 'short', timeZone: 'UTC'});
|
2022-10-28 15:48:24 +02:00
|
|
|
}
|
2022-11-21 10:59:42 +01:00
|
|
|
|
|
|
|
// convert a Blob to a DataURI
|
2024-08-28 18:32:38 +02:00
|
|
|
export function blobToDataURI(blob: Blob): Promise<string> {
|
2022-11-21 10:59:42 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.addEventListener('load', (e) => {
|
2024-08-28 18:32:38 +02:00
|
|
|
resolve(e.target.result as string);
|
2022-11-21 10:59:42 +01:00
|
|
|
});
|
|
|
|
reader.addEventListener('error', () => {
|
|
|
|
reject(new Error('FileReader failed'));
|
|
|
|
});
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert image Blob to another mime-type format.
|
2024-08-28 18:32:38 +02:00
|
|
|
export function convertImage(blob: Blob, mime: string): Promise<Blob> {
|
2022-11-21 10:59:42 +01:00
|
|
|
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 19:09:18 +01:00
|
|
|
|
2024-08-28 18:32:38 +02:00
|
|
|
export function toAbsoluteUrl(url: string): string {
|
2023-02-07 17:08:44 +01:00
|
|
|
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
|
|
return url;
|
2023-02-06 19:09:18 +01:00
|
|
|
}
|
2023-02-07 17:08:44 +01: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 19:09:18 +01: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 20:17:39 +01:00
|
|
|
|
2023-06-06 07:29:37 +02:00
|
|
|
// Encode an ArrayBuffer into a URLEncoded base64 string.
|
2024-08-28 18:32:38 +02:00
|
|
|
export function encodeURLEncodedBase64(arrayBuffer: ArrayBuffer): string {
|
2023-06-06 07:29:37 +02:00
|
|
|
return encode(arrayBuffer)
|
|
|
|
.replace(/\+/g, '-')
|
|
|
|
.replace(/\//g, '_')
|
|
|
|
.replace(/=/g, '');
|
|
|
|
}
|
|
|
|
|
2024-08-28 18:32:38 +02:00
|
|
|
// Decode a URLEncoded base64 to an ArrayBuffer.
|
|
|
|
export function decodeURLEncodedBase64(base64url: string): ArrayBuffer {
|
2023-06-06 07:29:37 +02:00
|
|
|
return decode(base64url
|
|
|
|
.replace(/_/g, '/')
|
|
|
|
.replace(/-/g, '+'));
|
|
|
|
}
|
2023-10-11 14:34:21 +02:00
|
|
|
|
|
|
|
const domParser = new DOMParser();
|
|
|
|
const xmlSerializer = new XMLSerializer();
|
|
|
|
|
2024-08-28 18:32:38 +02:00
|
|
|
export function parseDom(text: string, contentType: DOMParserSupportedType): Document {
|
2023-10-11 14:34:21 +02:00
|
|
|
return domParser.parseFromString(text, contentType);
|
|
|
|
}
|
|
|
|
|
2024-08-28 18:32:38 +02:00
|
|
|
export function serializeXml(node: Element | Node): string {
|
2023-10-11 14:34:21 +02:00
|
|
|
return xmlSerializer.serializeToString(node);
|
|
|
|
}
|
2024-02-24 00:41:24 +01:00
|
|
|
|
2024-08-28 18:32:38 +02:00
|
|
|
export function sleep(ms: number): Promise<void> {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
}
|
2024-06-27 11:31:49 +02:00
|
|
|
|
2024-08-28 18:32:38 +02:00
|
|
|
export function isImageFile({name, type}: {name: string, type?: string}): boolean {
|
2024-11-15 01:55:50 +01:00
|
|
|
return /\.(avif|jpe?g|png|gif|webp|svg|heic)$/i.test(name || '') || type?.startsWith('image/');
|
2024-06-27 11:31:49 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 18:32:38 +02:00
|
|
|
export function isVideoFile({name, type}: {name: string, type?: string}): boolean {
|
2024-06-27 11:31:49 +02:00
|
|
|
return /\.(mpe?g|mp4|mkv|webm)$/i.test(name || '') || type?.startsWith('video/');
|
|
|
|
}
|