little refactor

This commit is contained in:
Anton Lavrenov 2022-08-05 10:07:22 -05:00
parent 40267e0d44
commit 0f8f2f4548
2 changed files with 20 additions and 27 deletions

View File

@ -493,9 +493,10 @@ export class Text extends Shape<TextConfig> {
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<TextConfig> {
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) {
@ -561,38 +564,28 @@ export class Text extends Shape<TextConfig> {
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) {
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);
}
}

View File

@ -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();'
);
}
});