chore: improve types

This commit is contained in:
Yash Singh
2024-05-04 16:32:44 -07:00
parent e68125eb6a
commit c021f71afc
7 changed files with 789 additions and 33 deletions

View File

@@ -55,7 +55,6 @@ export const imgSnapshotTest = (
const options: CypressMermaidConfig = { const options: CypressMermaidConfig = {
..._options, ..._options,
fontFamily: _options.fontFamily || 'courier', fontFamily: _options.fontFamily || 'courier',
// @ts-ignore TODO: Fix type of fontSize
fontSize: _options.fontSize || '16px', fontSize: _options.fontSize || '16px',
sequence: { sequence: {
...(_options.sequence || {}), ...(_options.sequence || {}),

View File

@@ -158,7 +158,7 @@ export interface MermaidConfig {
block?: BlockDiagramConfig; block?: BlockDiagramConfig;
dompurifyConfig?: DOMPurifyConfiguration; dompurifyConfig?: DOMPurifyConfiguration;
wrap?: boolean; wrap?: boolean;
fontSize?: number; fontSize?: string | number;
markdownAutoWrap?: boolean; markdownAutoWrap?: boolean;
/** /**
* Suppresses inserting 'Syntax error' diagram in the DOM. * Suppresses inserting 'Syntax error' diagram in the DOM.

View File

@@ -36,7 +36,6 @@ export const draw = (txt: string, id: string, _version: string, diagObj: Diagram
.attr('height', chartConfig.height) .attr('height', chartConfig.height)
.attr('class', 'background'); .attr('class', 'background');
// @ts-ignore: TODO Fix ts errors
configureSvgSize(svg, chartConfig.height, chartConfig.width, true); configureSvgSize(svg, chartConfig.height, chartConfig.width, true);
svg.attr('viewBox', `0 0 ${chartConfig.width} ${chartConfig.height}`); svg.attr('viewBox', `0 0 ${chartConfig.width} ${chartConfig.height}`);

View File

@@ -10,10 +10,8 @@
* *
* In addition to the render function, a number of behavioral configuration options are available. * In addition to the render function, a number of behavioral configuration options are available.
*/ */
// @ts-ignore TODO: Investigate D3 issue
import { select } from 'd3'; import { select } from 'd3';
import { compile, serialize, stringify } from 'stylis'; import { compile, serialize, stringify } from 'stylis';
// @ts-ignore: TODO Fix ts errors
import { version } from '../package.json'; import { version } from '../package.json';
import * as configApi from './config.js'; import * as configApi from './config.js';
import { addDiagrams } from './diagram-api/diagram-orchestration.js'; import { addDiagrams } from './diagram-api/diagram-orchestration.js';

View File

@@ -1,3 +1,5 @@
import { interpolateToCurve } from './utils.js';
export interface Point { export interface Point {
x: number; x: number;
y: number; y: number;
@@ -30,7 +32,7 @@ export interface EdgeData {
arrowTypeEnd: string; arrowTypeEnd: string;
style: string; style: string;
labelStyle: string; labelStyle: string;
curve: any; curve: ReturnType<typeof interpolateToCurve>;
} }
export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never; export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;

View File

@@ -1,5 +1,5 @@
import { sanitizeUrl } from '@braintree/sanitize-url'; import { sanitizeUrl } from '@braintree/sanitize-url';
import type { CurveFactory } from 'd3'; import type { CurveFactory, Selection } from 'd3';
import { import {
curveBasis, curveBasis,
curveBasisClosed, curveBasisClosed,
@@ -233,13 +233,12 @@ export const isSubstringInArray = function (str: string, arr: string[]): number
export function interpolateToCurve( export function interpolateToCurve(
interpolate: string | undefined, interpolate: string | undefined,
defaultCurve: CurveFactory defaultCurve: CurveFactory
): CurveFactory { ) {
if (!interpolate) { if (!interpolate) {
return defaultCurve; return defaultCurve;
} }
const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}`; const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}`;
// @ts-ignore TODO: Fix issue with curve type
return d3CurveTypes[curveName as keyof typeof d3CurveTypes] ?? defaultCurve; return d3CurveTypes[curveName as keyof typeof d3CurveTypes] ?? defaultCurve;
} }
@@ -487,6 +486,25 @@ export const random = (options: { length: number }) => {
return makeRandomHex(options.length); return makeRandomHex(options.length);
}; };
interface TextData {
text: string;
x: number;
y: number;
anchor: 'start' | 'middle' | 'end';
fontFamily?: string;
fontSize?: string | number;
fontWeight?: string | number;
fill?: string;
class?: string;
textMargin: number;
style?: string;
width?: number;
height?: number;
rx?: number;
ry?: number;
valign?: string;
}
export const getTextObj = function () { export const getTextObj = function () {
return { return {
x: 0, x: 0,
@@ -501,7 +519,7 @@ export const getTextObj = function () {
ry: 0, ry: 0,
valign: undefined, valign: undefined,
text: '', text: '',
}; } satisfies TextData;
}; };
/** /**
@@ -512,20 +530,9 @@ export const getTextObj = function () {
* @returns Text element with given styling and content * @returns Text element with given styling and content
*/ */
export const drawSimpleText = function ( export const drawSimpleText = function (
elem: SVGElement, elem: Selection<SVGSVGElement, any, HTMLElement, any>,
textData: { textData: TextData
text: string; ) {
x: number;
y: number;
anchor: 'start' | 'middle' | 'end';
fontFamily: string;
fontSize: string | number;
fontWeight: string | number;
fill: string;
class: string | undefined;
textMargin: number;
}
): SVGTextElement {
// Remove and ignore br:s // Remove and ignore br:s
const nText = textData.text.replace(common.lineBreakRegex, ' '); const nText = textData.text.replace(common.lineBreakRegex, ' ');
@@ -689,7 +696,7 @@ export const calculateTextDimensions: (
text: string, text: string,
config: TextDimensionConfig config: TextDimensionConfig
) => TextDimensions = memoize( ) => TextDimensions = memoize(
(text: string, config: TextDimensionConfig): TextDimensions => { (text: string, config: TextDimensionConfig) => {
const { fontSize = 12, fontFamily = 'Arial', fontWeight = 400 } = config; const { fontSize = 12, fontFamily = 'Arial', fontWeight = 400 } = config;
if (!text) { if (!text) {
return { width: 0, height: 0 }; return { width: 0, height: 0 };
@@ -719,9 +726,7 @@ export const calculateTextDimensions: (
for (const line of lines) { for (const line of lines) {
const textObj = getTextObj(); const textObj = getTextObj();
textObj.text = line || ZERO_WIDTH_SPACE; textObj.text = line || ZERO_WIDTH_SPACE;
// @ts-ignore TODO: Fix D3 types
const textElem = drawSimpleText(g, textObj) const textElem = drawSimpleText(g, textObj)
// @ts-ignore TODO: Fix D3 types
.style('font-size', _fontSizePx) .style('font-size', _fontSizePx)
.style('font-weight', fontWeight) .style('font-weight', fontWeight)
.style('font-family', fontFamily); .style('font-family', fontFamily);

765
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff