fix: web text maxLines

This commit is contained in:
钱泽虹 2023-09-12 14:33:36 +08:00 committed by osborn
parent 6d8e3bb9df
commit 49b9232f92
3 changed files with 78 additions and 34 deletions

View File

@ -7267,6 +7267,8 @@ var doric_web = (function (exports, axios, sandbox) {
break;
case "maxLines":
this.maxLines = prop;
this.view.style.whiteSpace = 'normal';
this.view.style.overflow = 'hidden';
break;
case "maxWidth":
if (prop) {
@ -7297,32 +7299,51 @@ var doric_web = (function (exports, axios, sandbox) {
}
configSize() {
if (this.maxLines > 0) {
const currentHeight = this.view.offsetHeight;
const computedStyle = window.getComputedStyle(this.view);
const maxHeight = this.getLineHeight(computedStyle) * this.maxLines;
if (currentHeight > maxHeight) {
this.view.style.height = toPixelString(maxHeight);
this.view.style.alignItems = "flex-start";
this.view.style.overflow = 'hidden';
this.textElement.innerText = this.getTruncationText(computedStyle, maxHeight);
const currentContentHeight = this.view.clientHeight - pixelString2Number(computedStyle.paddingTop) - pixelString2Number(computedStyle.paddingBottom);
let maxHeight = this.maxLinesHeight(computedStyle);
if (currentContentHeight > 0) {
maxHeight = Math.min(maxHeight, Math.ceil(currentContentHeight));
}
if (this.computedHeight(computedStyle) > maxHeight) {
this.textElement.innerText = this.truncationText(computedStyle, maxHeight);
}
}
}
getLineHeight(style) {
computedHeight(style) {
const tempEle = document.createElement('div');
tempEle.style.cssText = style.cssText;
tempEle.textContent = 'Test';
tempEle.style.font = style.font;
tempEle.textContent = this.textElement.innerText;
tempEle.style.whiteSpace = 'normal';
tempEle.style.width = this.view.style.width;
document.body.appendChild(tempEle);
const lineHeight = tempEle.offsetHeight;
const height = tempEle.offsetHeight;
document.body.removeChild(tempEle);
return lineHeight;
return height;
}
getTruncationText(style, maxHeight) {
maxLinesHeight(style) {
let text = '';
for (let i = 0; i < this.maxLines; i++) {
text += 'Test测试😊\n';
}
const tempEle = document.createElement('div');
tempEle.style.font = style.font;
tempEle.textContent = text;
tempEle.style.width = this.view.style.width;
tempEle.style.whiteSpace = 'pre';
tempEle.style.overflow = 'hidden';
document.body.appendChild(tempEle);
const height = tempEle.offsetHeight;
document.body.removeChild(tempEle);
return height;
}
truncationText(style, maxHeight) {
const originalText = this.textElement.innerText;
let start = 0, end = originalText.length;
const tempEle = document.createElement('div');
tempEle.style.cssText = style.cssText;
tempEle.style.alignItems = "flex-start";
tempEle.style.font = style.font;
tempEle.style.whiteSpace = 'normal';
this.view.style.overflow = 'hidden';
tempEle.style.width = this.view.style.width;
document.body.appendChild(tempEle);
while (start <= end) {

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
import { DoricViewNode, LEFT, RIGHT, CENTER_X, CENTER_Y, TOP, BOTTOM, toPixelString } from "./DoricViewNode";
import { DoricViewNode, LEFT, RIGHT, CENTER_X, CENTER_Y, TOP, BOTTOM, toPixelString, pixelString2Number } from "./DoricViewNode";
import { toRGBAString } from "../utils/color";
export class DoricTextNode extends DoricViewNode {
@ -68,6 +68,8 @@ export class DoricTextNode extends DoricViewNode {
break
case "maxLines":
this.maxLines = prop as number
this.view.style.whiteSpace = 'normal'
this.view.style.overflow = 'hidden'
break
case "maxWidth":
if (prop) {
@ -98,35 +100,56 @@ export class DoricTextNode extends DoricViewNode {
configSize() {
if (this.maxLines > 0) {
const currentHeight = this.view.offsetHeight
const computedStyle = window.getComputedStyle(this.view)
const maxHeight = this.getLineHeight(computedStyle) * this.maxLines
if (currentHeight > maxHeight) {
this.view.style.height = toPixelString(maxHeight)
this.view.style.alignItems = "flex-start"
this.view.style.overflow = 'hidden'
this.textElement.innerText = this.getTruncationText(computedStyle, maxHeight)
const currentContentHeight = this.view.clientHeight - pixelString2Number(computedStyle.paddingTop) - pixelString2Number(computedStyle.paddingBottom)
let maxHeight = this.maxLinesHeight(computedStyle)
if (currentContentHeight > 0) {
maxHeight = Math.min(maxHeight, Math.ceil(currentContentHeight))
}
if (this.computedHeight(computedStyle) > maxHeight) {
this.textElement.innerText = this.truncationText(computedStyle, maxHeight)
}
}
}
getLineHeight(style:CSSStyleDeclaration) {
computedHeight(style:CSSStyleDeclaration) {
const tempEle = document.createElement('div')
tempEle.style.cssText = style.cssText
tempEle.textContent = 'Test'
document.body.appendChild(tempEle);
const lineHeight = tempEle.offsetHeight;
document.body.removeChild(tempEle);
return lineHeight;
tempEle.style.font = style.font
tempEle.textContent = this.textElement.innerText
tempEle.style.whiteSpace = 'normal'
tempEle.style.width = this.view.style.width
document.body.appendChild(tempEle)
const height = tempEle.offsetHeight
document.body.removeChild(tempEle)
return height
}
getTruncationText(style:CSSStyleDeclaration, maxHeight:number) {
maxLinesHeight(style:CSSStyleDeclaration) {
let text = ''
for (let i = 0; i < this.maxLines; i++) {
text += 'Test测试😊\n'
}
const tempEle = document.createElement('div')
tempEle.style.font = style.font
tempEle.textContent = text
tempEle.style.width = this.view.style.width
tempEle.style.whiteSpace = 'pre';
tempEle.style.overflow = 'hidden';
document.body.appendChild(tempEle)
const height = tempEle.offsetHeight
document.body.removeChild(tempEle)
return height
}
truncationText(style:CSSStyleDeclaration, maxHeight:number) {
const originalText = this.textElement.innerText
let start = 0, end = originalText.length
const tempEle = document.createElement('div')
tempEle.style.cssText = style.cssText
tempEle.style.alignItems = "flex-start"
tempEle.style.font = style.font
tempEle.style.whiteSpace = 'normal'
this.view.style.overflow = 'hidden'
tempEle.style.width = this.view.style.width
document.body.appendChild(tempEle);