mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 05:01:41 +08:00
little refactor
This commit is contained in:
parent
40267e0d44
commit
0f8f2f4548
@ -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) {
|
||||
@ -549,9 +552,9 @@ export class Text extends Shape<TextConfig> {
|
||||
* 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<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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();'
|
||||
);
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user