From fb9796948c5d82b8156ce9415ab90dbea5a69ba8 Mon Sep 17 00:00:00 2001 From: SamBobBarnes Date: Sun, 13 Oct 2024 12:37:17 -0500 Subject: [PATCH] removed separate method and tests --- .../loot-core/src/shared/text-width.spec.ts | 44 ------------------- packages/loot-core/src/shared/text-width.ts | 30 ------------- 2 files changed, 74 deletions(-) delete mode 100644 packages/loot-core/src/shared/text-width.spec.ts delete mode 100644 packages/loot-core/src/shared/text-width.ts diff --git a/packages/loot-core/src/shared/text-width.spec.ts b/packages/loot-core/src/shared/text-width.spec.ts deleted file mode 100644 index 73983715aa6..00000000000 --- a/packages/loot-core/src/shared/text-width.spec.ts +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @jest-environment jsdom - */ - -import { getStringWidth, getTextWidth } from './text-width'; - -describe('InfoBubble', () => { - let context: CanvasRenderingContext2D; - const measureText = jest.fn(); - beforeEach(() => { - jest.resetAllMocks(); - measureText.mockReturnValue({ width: 495.6 }); - context = { - font: '', - measureText, - } as any; - jest.spyOn(document, 'createElement').mockReturnValue({ - getContext: jest.fn().mockReturnValue(context), - } as any); - }); - describe('getStringWidth', () => { - it('should return the correct width of the string rounded up to the nearest px', () => { - const text = 'This text width should be 500px'; - const result = getStringWidth(text); - expect(result).toBe(496); - }); - }); - describe('getTextWidth', () => { - it('should return the correct width of the text rounded up to the nearest px', () => { - measureText.mockReturnValueOnce({ width: 595.6 }); - const text = 'This text should not be broken up.'; - const result = getTextWidth(text); - expect(result).toBe(596); - }); - it('should return the correct width of the text rounded up to the nearest px when the text is longer than the maxWidth', () => { - measureText - .mockReturnValueOnce({ width: 595.6 }) - .mockReturnValueOnce({ width: 495.6 }); - const text = 'This text should be broken up.'; - const result = getTextWidth(text, 500); - expect(result).toBe(300); - }); - }); -}); diff --git a/packages/loot-core/src/shared/text-width.ts b/packages/loot-core/src/shared/text-width.ts deleted file mode 100644 index b8530f7b1c4..00000000000 --- a/packages/loot-core/src/shared/text-width.ts +++ /dev/null @@ -1,30 +0,0 @@ -/** Calculates the width of a text string in pixels - * and returns the width rounded up to the nearest pixel. - * - * If the text is longer than the maxWidth, it will be split into multiple lines. - * The width of the longest line will be returned. - * - * @param {string} [text] - The text string to calculate the width of. - * @param {number} [maxWidth=0] - The maximum width of the text after which it will be split into multiple lines. `0` will not split. - */ -export function getTextWidth(text: string, maxWidth: number = 0): number { - const textWidth = getStringWidth(text); - - if (maxWidth && textWidth > maxWidth) { - const words = text.split(' '); - const half = Math.ceil(words.length / 2); - const firstHalf = words.slice(0, half).join(' '); - const secondHalf = words.slice(half).join(' '); - - return Math.max(getStringWidth(firstHalf), getStringWidth(secondHalf)); - } - return textWidth; -} - -export function getStringWidth(text: string): number { - const canvas = document.createElement('canvas'); - const context = canvas.getContext('2d')!; - context.font = getComputedStyle(document.body).font; - - return Math.ceil(context.measureText(text).width); -}