mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-15 13:29:40 +02:00
fix: Add types to memoized functions
This commit is contained in:
@@ -561,8 +561,16 @@ export const drawSimpleText = function (
|
||||
return textElem;
|
||||
};
|
||||
|
||||
export const wrapLabel = memoize(
|
||||
(label, maxWidth, config) => {
|
||||
interface WrapLabelConfig {
|
||||
fontSize: number;
|
||||
fontFamily: string;
|
||||
fontWeight: number;
|
||||
joinWith: string;
|
||||
}
|
||||
|
||||
export const wrapLabel: (label: string, maxWidth: string, config: WrapLabelConfig) => string =
|
||||
memoize(
|
||||
(label: string, maxWidth: string, config: WrapLabelConfig): string => {
|
||||
if (!label) {
|
||||
return label;
|
||||
}
|
||||
@@ -601,14 +609,29 @@ export const wrapLabel = memoize(
|
||||
`${label}${maxWidth}${config.fontSize}${config.fontWeight}${config.fontFamily}${config.joinWith}`
|
||||
);
|
||||
|
||||
const breakString = memoize(
|
||||
(word, maxWidth, hyphenCharacter = '-', config) => {
|
||||
interface BreakStringOutput {
|
||||
hyphenatedStrings: string[];
|
||||
remainingWord: string;
|
||||
}
|
||||
|
||||
const breakString: (
|
||||
word: string,
|
||||
maxWidth: number,
|
||||
hyphenCharacter: string,
|
||||
config: WrapLabelConfig
|
||||
) => BreakStringOutput = memoize(
|
||||
(
|
||||
word: string,
|
||||
maxWidth: number,
|
||||
hyphenCharacter = '-',
|
||||
config: WrapLabelConfig
|
||||
): BreakStringOutput => {
|
||||
config = Object.assign(
|
||||
{ fontSize: 12, fontWeight: 400, fontFamily: 'Arial', margin: 0 },
|
||||
config
|
||||
);
|
||||
const characters = word.split('');
|
||||
const lines = [];
|
||||
const lines: string[] = [];
|
||||
let currentLine = '';
|
||||
characters.forEach((character, index) => {
|
||||
const nextLine = `${currentLine}${character}`;
|
||||
@@ -667,6 +690,16 @@ export function calculateTextWidth(
|
||||
return calculateTextDimensions(text, config).width;
|
||||
}
|
||||
|
||||
interface TextDimensionConfig {
|
||||
fontSize?: number;
|
||||
fontWeight?: number;
|
||||
fontFamily?: string;
|
||||
}
|
||||
interface TextDimensions {
|
||||
width: number;
|
||||
height: number;
|
||||
lineHeight?: number;
|
||||
}
|
||||
/**
|
||||
* This calculates the dimensions of the given text, font size, font family, font weight, and
|
||||
* margins.
|
||||
@@ -676,15 +709,11 @@ export function calculateTextWidth(
|
||||
* the resulting size
|
||||
* @returns The dimensions for the given text
|
||||
*/
|
||||
export const calculateTextDimensions = memoize(
|
||||
function (
|
||||
export const calculateTextDimensions: (
|
||||
text: string,
|
||||
config: {
|
||||
fontSize?: number;
|
||||
fontWeight?: number;
|
||||
fontFamily?: string;
|
||||
}
|
||||
) {
|
||||
config: TextDimensionConfig
|
||||
) => TextDimensions = memoize(
|
||||
(text: string, config: TextDimensionConfig): TextDimensions => {
|
||||
config = Object.assign({ fontSize: 12, fontWeight: 400, fontFamily: 'Arial' }, config);
|
||||
const { fontSize, fontFamily, fontWeight } = config;
|
||||
if (!text) {
|
||||
|
Reference in New Issue
Block a user