mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 08:22:44 +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);
|
textWidth = Math.max(textWidth, matchWidth);
|
||||||
currentHeightPx += lineHeightPx;
|
currentHeightPx += lineHeightPx;
|
||||||
|
|
||||||
var shouldHandleEllipsis = this._shouldHandleEllipsis(currentHeightPx);
|
var shouldHandleEllipsis =
|
||||||
|
this._shouldHandleEllipsis(currentHeightPx);
|
||||||
if (shouldHandleEllipsis) {
|
if (shouldHandleEllipsis) {
|
||||||
this._addEllipsisIfNecessary(shouldHandleEllipsis);
|
this._tryToAddEllipsisToLastLine();
|
||||||
/*
|
/*
|
||||||
* stop wrapping if wrapping is disabled or if adding
|
* stop wrapping if wrapping is disabled or if adding
|
||||||
* one more line would overflow the fixed height
|
* one more line would overflow the fixed height
|
||||||
@ -526,7 +527,9 @@ export class Text extends Shape<TextConfig> {
|
|||||||
currentHeightPx += lineHeightPx;
|
currentHeightPx += lineHeightPx;
|
||||||
textWidth = Math.max(textWidth, lineWidth);
|
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 element height is fixed, abort if adding one more line would overflow
|
||||||
if (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) {
|
if (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) {
|
||||||
@ -561,38 +564,28 @@ export class Text extends Shape<TextConfig> {
|
|||||||
wrap = this.wrap(),
|
wrap = this.wrap(),
|
||||||
shouldWrap = wrap !== NONE;
|
shouldWrap = wrap !== NONE;
|
||||||
|
|
||||||
return (!shouldWrap || (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx));
|
return (
|
||||||
|
!shouldWrap ||
|
||||||
|
(fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
_tryToAddEllipsisToLastLine(): void {
|
||||||
* add ellipses at the end of the line if necessary
|
|
||||||
* @param {Boolean} shouldHandleEllipsis
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
_addEllipsisIfNecessary(shouldHandleEllipsis: boolean): void {
|
|
||||||
var width = this.attrs.width,
|
var width = this.attrs.width,
|
||||||
fixedWidth = width !== AUTO && width !== undefined,
|
fixedWidth = width !== AUTO && width !== undefined,
|
||||||
padding = this.padding(),
|
padding = this.padding(),
|
||||||
maxWidth = width - padding * 2,
|
maxWidth = width - padding * 2,
|
||||||
shouldAddEllipsis = this.ellipsis();
|
shouldAddEllipsis = this.ellipsis();
|
||||||
|
|
||||||
if (!shouldHandleEllipsis) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var lastLine = this.textArr[this.textArr.length - 1];
|
var lastLine = this.textArr[this.textArr.length - 1];
|
||||||
if (!lastLine || !shouldAddEllipsis) {
|
if (!lastLine || !shouldAddEllipsis) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fixedWidth) {
|
if (fixedWidth) {
|
||||||
var haveSpace =
|
var haveSpace = this._getTextWidth(lastLine.text + ELLIPSIS) < maxWidth;
|
||||||
this._getTextWidth(lastLine.text + ELLIPSIS) < maxWidth;
|
|
||||||
if (!haveSpace) {
|
if (!haveSpace) {
|
||||||
lastLine.text = lastLine.text.slice(
|
lastLine.text = lastLine.text.slice(0, lastLine.text.length - 3);
|
||||||
0,
|
|
||||||
lastLine.text.length - 3
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
assertAlmostEqual,
|
assertAlmostEqual,
|
||||||
} from './test-utils';
|
} from './test-utils';
|
||||||
|
|
||||||
describe('Text', function () {
|
describe.only('Text', function () {
|
||||||
// ======================================================
|
// ======================================================
|
||||||
it('text with empty config is allowed', function () {
|
it('text with empty config is allowed', function () {
|
||||||
var stage = addStage();
|
var stage = addStage();
|
||||||
@ -515,7 +515,7 @@ describe('Text', function () {
|
|||||||
if (isBrowser) {
|
if (isBrowser) {
|
||||||
assert.equal(
|
assert.equal(
|
||||||
layer.getContext().getTrace(false, true),
|
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