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;
|
return textElem;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const wrapLabel = memoize(
|
interface WrapLabelConfig {
|
||||||
(label, maxWidth, config) => {
|
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) {
|
if (!label) {
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
@@ -599,16 +607,31 @@ export const wrapLabel = memoize(
|
|||||||
},
|
},
|
||||||
(label, maxWidth, config) =>
|
(label, maxWidth, config) =>
|
||||||
`${label}${maxWidth}${config.fontSize}${config.fontWeight}${config.fontFamily}${config.joinWith}`
|
`${label}${maxWidth}${config.fontSize}${config.fontWeight}${config.fontFamily}${config.joinWith}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const breakString = memoize(
|
interface BreakStringOutput {
|
||||||
(word, maxWidth, hyphenCharacter = '-', config) => {
|
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(
|
config = Object.assign(
|
||||||
{ fontSize: 12, fontWeight: 400, fontFamily: 'Arial', margin: 0 },
|
{ fontSize: 12, fontWeight: 400, fontFamily: 'Arial', margin: 0 },
|
||||||
config
|
config
|
||||||
);
|
);
|
||||||
const characters = word.split('');
|
const characters = word.split('');
|
||||||
const lines = [];
|
const lines: string[] = [];
|
||||||
let currentLine = '';
|
let currentLine = '';
|
||||||
characters.forEach((character, index) => {
|
characters.forEach((character, index) => {
|
||||||
const nextLine = `${currentLine}${character}`;
|
const nextLine = `${currentLine}${character}`;
|
||||||
@@ -667,6 +690,16 @@ export function calculateTextWidth(
|
|||||||
return calculateTextDimensions(text, config).width;
|
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
|
* This calculates the dimensions of the given text, font size, font family, font weight, and
|
||||||
* margins.
|
* margins.
|
||||||
@@ -676,15 +709,11 @@ export function calculateTextWidth(
|
|||||||
* the resulting size
|
* the resulting size
|
||||||
* @returns The dimensions for the given text
|
* @returns The dimensions for the given text
|
||||||
*/
|
*/
|
||||||
export const calculateTextDimensions = memoize(
|
export const calculateTextDimensions: (
|
||||||
function (
|
|
||||||
text: string,
|
text: string,
|
||||||
config: {
|
config: TextDimensionConfig
|
||||||
fontSize?: number;
|
) => TextDimensions = memoize(
|
||||||
fontWeight?: number;
|
(text: string, config: TextDimensionConfig): TextDimensions => {
|
||||||
fontFamily?: string;
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
config = Object.assign({ fontSize: 12, fontWeight: 400, fontFamily: 'Arial' }, config);
|
config = Object.assign({ fontSize: 12, fontWeight: 400, fontFamily: 'Arial' }, config);
|
||||||
const { fontSize, fontFamily, fontWeight } = config;
|
const { fontSize, fontFamily, fontWeight } = config;
|
||||||
if (!text) {
|
if (!text) {
|
||||||
|
Reference in New Issue
Block a user