From 0f8f2f4548ba9becb7b477b44c3a3ab9abf45622 Mon Sep 17 00:00:00 2001 From: Anton Lavrenov Date: Fri, 5 Aug 2022 10:07:22 -0500 Subject: [PATCH] little refactor --- src/shapes/Text.ts | 43 ++++++++++++++++++------------------------ test/unit/Text-test.ts | 4 ++-- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/shapes/Text.ts b/src/shapes/Text.ts index b4781923..3e370d4b 100644 --- a/src/shapes/Text.ts +++ b/src/shapes/Text.ts @@ -493,9 +493,10 @@ export class Text extends Shape { textWidth = Math.max(textWidth, matchWidth); currentHeightPx += lineHeightPx; - var shouldHandleEllipsis = this._shouldHandleEllipsis(currentHeightPx); + var shouldHandleEllipsis = + this._shouldHandleEllipsis(currentHeightPx); if (shouldHandleEllipsis) { - this._addEllipsisIfNecessary(shouldHandleEllipsis); + this._tryToAddEllipsisToLastLine(); /* * stop wrapping if wrapping is disabled or if adding * one more line would overflow the fixed height @@ -526,7 +527,9 @@ export class Text extends Shape { currentHeightPx += lineHeightPx; textWidth = Math.max(textWidth, lineWidth); - this._addEllipsisIfNecessary(this._shouldHandleEllipsis(currentHeightPx)); + if (this._shouldHandleEllipsis(currentHeightPx)) { + this._tryToAddEllipsisToLastLine(); + } } // if element height is fixed, abort if adding one more line would overflow if (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) { @@ -549,9 +552,9 @@ export class Text extends Shape { * 1. the current line is the last line * 2. wrap is NONE * @param {Number} currentHeightPx - * @returns + * @returns */ - _shouldHandleEllipsis(currentHeightPx: number): boolean { + _shouldHandleEllipsis(currentHeightPx: number): boolean { var fontSize = +this.fontSize(), lineHeightPx = this.lineHeight() * fontSize, height = this.attrs.height, @@ -561,38 +564,28 @@ export class Text extends Shape { wrap = this.wrap(), shouldWrap = wrap !== NONE; - return (!shouldWrap || (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx)); + return ( + !shouldWrap || + (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) + ); } - /** - * add ellipses at the end of the line if necessary - * @param {Boolean} shouldHandleEllipsis - * @returns - */ - _addEllipsisIfNecessary(shouldHandleEllipsis: boolean): void { + _tryToAddEllipsisToLastLine(): void { var width = this.attrs.width, fixedWidth = width !== AUTO && width !== undefined, padding = this.padding(), maxWidth = width - padding * 2, shouldAddEllipsis = this.ellipsis(); - - if (!shouldHandleEllipsis) { - return; - } - + var lastLine = this.textArr[this.textArr.length - 1]; - if (!lastLine || !shouldAddEllipsis) { + if (!lastLine || !shouldAddEllipsis) { return; } - + if (fixedWidth) { - var haveSpace = - this._getTextWidth(lastLine.text + ELLIPSIS) < maxWidth; + var haveSpace = this._getTextWidth(lastLine.text + ELLIPSIS) < maxWidth; if (!haveSpace) { - lastLine.text = lastLine.text.slice( - 0, - lastLine.text.length - 3 - ); + lastLine.text = lastLine.text.slice(0, lastLine.text.length - 3); } } diff --git a/test/unit/Text-test.ts b/test/unit/Text-test.ts index 77c88016..d59c3ace 100644 --- a/test/unit/Text-test.ts +++ b/test/unit/Text-test.ts @@ -12,7 +12,7 @@ import { assertAlmostEqual, } from './test-utils'; -describe('Text', function () { +describe.only('Text', function () { // ====================================================== it('text with empty config is allowed', function () { var stage = addStage(); @@ -515,7 +515,7 @@ describe('Text', function () { if (isBrowser) { assert.equal( layer.getContext().getTrace(false, true), - "clearRect(0,0,578,200);save();transform(1,0,0,1,10,10);font=normal normal 14px Arial;textBaseline=middle;textAlign=left;translate(0,0);save();fillStyle=black;fillText(HEADING,18,7);restore();save();fillStyle=black;fillText(All the…,23,21);restore();restore();" + 'clearRect(0,0,578,200);save();transform(1,0,0,1,10,10);font=normal normal 14px Arial;textBaseline=middle;textAlign=left;translate(0,0);save();fillStyle=black;fillText(HEADING,18,7);restore();save();fillStyle=black;fillText(All the…,23,21);restore();restore();' ); } });