Skip to content

Commit

Permalink
Merge pull request #1138 from CleverCloud/more-typechecking
Browse files Browse the repository at this point in the history
chore(typechecking): typecheck /lib
  • Loading branch information
pdesoyres-cc authored Sep 9, 2024
2 parents 3a1af97 + 6917821 commit a0b2671
Show file tree
Hide file tree
Showing 28 changed files with 443 additions and 158 deletions.
14 changes: 12 additions & 2 deletions src/components/cc-tile-instances/cc-tile-instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,25 @@ export class CcTileInstances extends LitElement {
if (this._lastRunningCount !== runningInstancesCount) {
this.updateComplete.then(() => {
// This is not supported in Safari yet, but it's purely decorative so let's keep it like that
animate(this.shadowRoot, '.instances[data-type=running] .count-bubble', ...QUICK_SHRINK);
animate(
this.shadowRoot,
'.instances[data-type=running] .count-bubble',
QUICK_SHRINK.keyframes,
QUICK_SHRINK.options,
);
this._lastRunningCount = runningInstancesCount;
});
}

if (this._lastDeployingCount !== deployingInstancesCount) {
this.updateComplete.then(() => {
// This is not supported in Safari yet, but it's purely decorative so let's keep it like that
animate(this.shadowRoot, '.instances[data-type=deploying] .count-bubble', ...QUICK_SHRINK);
animate(
this.shadowRoot,
'.instances[data-type=deploying] .count-bubble',
QUICK_SHRINK.keyframes,
QUICK_SHRINK.options,
);
this._lastDeployingCount = deployingInstancesCount;
});
}
Expand Down
14 changes: 13 additions & 1 deletion src/components/common.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interface InvoiceAmount {
export interface Invoice {
downloadUrl: string;
emissionDate: string;
invoiceHtml: string;
invoiceHtml?: string;
number: string;
paymentUrl: string;
status: InvoiceStatusType;
Expand Down Expand Up @@ -214,3 +214,15 @@ interface EnvVarEditorStateLoaded {
/* endregion */

export type NotificationIntent = 'info' | 'success' | 'warning' | 'danger';

export interface Notification {
message: string | Node;
title?: string;
intent: NotificationIntent;
options?: NotificationOptions;
}

export interface NotificationOptions {
timeout?: number;
closeable?: boolean;
}
15 changes: 11 additions & 4 deletions src/lib/animate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/**
*
* @param {ShadowRoot} shadowRoot
* @param {string} selector
* @param {Array<Keyframe> | PropertyIndexedKeyframes} keyframes
* @param {number | KeyframeAnimationOptions} options
*/
export function animate(shadowRoot, selector, keyframes, options) {
Array.from(shadowRoot.querySelectorAll(selector)).forEach((element) => element.animate(keyframes, options));
}

export const QUICK_SHRINK = [
[{ transform: 'scale(1)' }, { transform: 'scale(0.9)' }, { transform: 'scale(1)' }],
{
export const QUICK_SHRINK = {
keyframes: [{ transform: 'scale(1)' }, { transform: 'scale(0.9)' }, { transform: 'scale(1)' }],
options: {
duration: 200,
easing: 'ease-in-out',
},
];
};
18 changes: 15 additions & 3 deletions src/lib/ansi/ansi-palette-analyser.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { getContrastRatio, hexToRgb, isDark } from '../color.js';

const colors = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan'];
/**
* @typedef {import('./ansi.types.js').AnsiPalette} AnsiPalette
* @typedef {keyof AnsiPalette} ColorsType
*/

/** @type {Array<ColorsType>} */
const COLORS = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan'];

/**
*
* @param {AnsiPalette} palette
*/
export function analyzePalette(palette) {
const background = hexToRgb(palette.background);
/** @type {Array<ColorsType>} */
const colorsToTest = getColorsToTest(isDark(background));

const colorsCount = colorsToTest.length;
let ratioSum = 0;
let compliantCount = 0;

/** @type {Partial<Record<ColorsType, { ratio: number, compliant: boolean }>>} */
const contrasts = {};

colorsToTest.forEach((colorName) => {
Expand All @@ -34,8 +41,13 @@ export function analyzePalette(palette) {
};
}

/**
* @param {boolean} isDarkPalette
* @return {Array<ColorsType>}
*/
function getColorsToTest(isDarkPalette) {
const result = [...colors, isDarkPalette ? 'white' : 'black'];
const result = [...COLORS, isDarkPalette ? 'white' : 'black'];

// @ts-ignore
return [...result, ...result.map((c) => `bright-${c}`)];
}
32 changes: 28 additions & 4 deletions src/lib/ansi/ansi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import ansiRegEx from 'ansi-regex';
import { css, html, unsafeCSS } from 'lit';
import { MemoryCache } from '../memory-cache.js';

/**
* @typedef {import('./ansi.types.js').AnsiPart} AnsiPart
*/

/** @type {AnsiParser} - Lazy loaded parser */
let ansiParser;

Expand Down Expand Up @@ -134,7 +138,7 @@ ANSI_COLORS.forEach((style) => {

/**
* Remove all ANSI escape codes from the given text.
* @param text
* @param {string} text
* @return {string}
*/
export function stripAnsi(text) {
Expand All @@ -145,14 +149,16 @@ export function stripAnsi(text) {
* Converts the given text into Lit template according to the ANSI escape codes found in text.
*
* When using this, don't forget to include the `ansiPaletteStyle` CSS rules to a parent element. (see ./ansi-palette-style)
*
* @param {string} text
*/
export function ansiToLit(text) {
if (ansiParser == null) {
ansiParser = new AnsiParser();
}

const tokens = ansiParser.parse(text);
return tokens.map((token, i) => {
return tokens.map((token) => {
if (token.styles.length === 0) {
return token.text;
}
Expand Down Expand Up @@ -232,6 +238,11 @@ class AnsiParser {
/** @type {Map<string, Set<string>>} */
const commonEscapes = new Map();

/**
* @param {string} name
* @param {string} code
* @param {Array<string>} escapes
*/
const handleCode = (name, code, escapes) => {
const ansiCode = toAnsi(code);
this.codeToStyle.set(ansiCode, name);
Expand Down Expand Up @@ -261,15 +272,15 @@ class AnsiParser {
}

/**
* @param str
* @param {string} str
* @return {Array<AnsiPart>}
*/
parse(str) {
return this.cache.get(str);
}

/**
* @param str
* @param {string} str
* @return {Array<AnsiPart>}
*/
_doParse(str) {
Expand Down Expand Up @@ -329,17 +340,30 @@ function addToIndexMap(map, key, value) {
map.get(key).add(value);
}

/**
* @param {Array<T>} arr
* @param {T} val
* @template T
*/
function removeElm(arr, val) {
const i = arr.indexOf(val);
if (i >= 0) {
arr.splice(i, 1);
}
}

/**
* @param {string|number} code
* @return {string}
*/
function toAnsi(code) {
return `\u001b[${code}m`;
}

/**
* @param {string} ansiCode
* @return {string}
*/
function fromAnsi(ansiCode) {
return ansiCode.slice(2, ansiCode.length - 1);
}
Loading

0 comments on commit a0b2671

Please sign in to comment.