perf: break early if the line width <= max width of the container (#6347)

* fix: break early if the line width <= max width of the container

* Remove dead code

* remove dead code

* lint

* remove
This commit is contained in:
Aakansha Doshi 2023-03-14 17:18:16 +05:30 committed by GitHub
parent 6aeb18b784
commit ab49cad6b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 29 deletions

View File

@ -16,7 +16,7 @@ describe("Test wrapText", () => {
const text = "Hello whats up "; const text = "Hello whats up ";
const maxWidth = 200 - BOUND_TEXT_PADDING * 2; const maxWidth = 200 - BOUND_TEXT_PADDING * 2;
const res = wrapText(text, font, maxWidth); const res = wrapText(text, font, maxWidth);
expect(res).toBe("Hello whats up "); expect(res).toBe(text);
}); });
it("should work with emojis", () => { it("should work with emojis", () => {
@ -26,7 +26,7 @@ describe("Test wrapText", () => {
expect(res).toBe("😀"); expect(res).toBe("😀");
}); });
it("should show the text correctly when min width reached", () => { it("should show the text correctly when max width reached", () => {
const text = "Hello😀"; const text = "Hello😀";
const maxWidth = 10; const maxWidth = 10;
const res = wrapText(text, font, maxWidth); const res = wrapText(text, font, maxWidth);
@ -136,6 +136,7 @@ whats up`,
}); });
}); });
}); });
describe("When text is long", () => { describe("When text is long", () => {
const text = `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg break it now`; const text = `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg break it now`;
[ [
@ -175,6 +176,14 @@ break it now`,
}); });
}); });
}); });
it("should wrap the text correctly when word length is exactly equal to max width", () => {
const text = "Hello Excalidraw";
// Length of "Excalidraw" is 100 and exacty equal to max width
const res = wrapText(text, font, 100);
expect(res).toEqual(`Hello
Excalidraw`);
});
}); });
describe("Test measureText", () => { describe("Test measureText", () => {

View File

@ -327,25 +327,38 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
const lines: Array<string> = []; const lines: Array<string> = [];
const originalLines = text.split("\n"); const originalLines = text.split("\n");
const spaceWidth = getLineWidth(" ", font); const spaceWidth = getLineWidth(" ", font);
let currentLine = "";
let currentLineWidthTillNow = 0;
const push = (str: string) => { const push = (str: string) => {
if (str.trim()) { if (str.trim()) {
lines.push(str); lines.push(str);
} }
}; };
const resetParams = () => {
currentLine = "";
currentLineWidthTillNow = 0;
};
originalLines.forEach((originalLine) => { originalLines.forEach((originalLine) => {
const words = originalLine.split(" "); const currentLineWidth = getTextWidth(originalLine, font);
// This means its newline so push it
if (words.length === 1 && words[0] === "") { //Push the line if its <= maxWidth
lines.push(words[0]); if (currentLineWidth <= maxWidth) {
lines.push(originalLine);
return; // continue return; // continue
} }
let currentLine = ""; const words = originalLine.split(" ");
let currentLineWidthTillNow = 0;
resetParams();
let index = 0; let index = 0;
while (index < words.length) { while (index < words.length) {
const currentWordWidth = getLineWidth(words[index], font); const currentWordWidth = getLineWidth(words[index], font);
// This will only happen when single word takes entire width // This will only happen when single word takes entire width
if (currentWordWidth === maxWidth) { if (currentWordWidth === maxWidth) {
push(words[index]); push(words[index]);
@ -357,8 +370,8 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
// push current line since the current word exceeds the max width // push current line since the current word exceeds the max width
// so will be appended in next line // so will be appended in next line
push(currentLine); push(currentLine);
currentLine = "";
currentLineWidthTillNow = 0; resetParams();
while (words[index].length > 0) { while (words[index].length > 0) {
const currentChar = String.fromCodePoint( const currentChar = String.fromCodePoint(
@ -369,10 +382,6 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
words[index] = words[index].slice(currentChar.length); words[index] = words[index].slice(currentChar.length);
if (currentLineWidthTillNow >= maxWidth) { if (currentLineWidthTillNow >= maxWidth) {
// only remove last trailing space which we have added when joining words
if (currentLine.slice(-1) === " ") {
currentLine = currentLine.slice(0, -1);
}
push(currentLine); push(currentLine);
currentLine = currentChar; currentLine = currentChar;
currentLineWidthTillNow = width; currentLineWidthTillNow = width;
@ -380,11 +389,11 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
currentLine += currentChar; currentLine += currentChar;
} }
} }
// push current line if appending space exceeds max width // push current line if appending space exceeds max width
if (currentLineWidthTillNow + spaceWidth >= maxWidth) { if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
push(currentLine); push(currentLine);
currentLine = ""; resetParams();
currentLineWidthTillNow = 0;
} else { } else {
// space needs to be appended before next word // space needs to be appended before next word
// as currentLine contains chars which couldn't be appended // as currentLine contains chars which couldn't be appended
@ -392,7 +401,6 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
currentLine += " "; currentLine += " ";
currentLineWidthTillNow += spaceWidth; currentLineWidthTillNow += spaceWidth;
} }
index++; index++;
} else { } else {
// Start appending words in a line till max width reached // Start appending words in a line till max width reached
@ -402,8 +410,7 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
if (currentLineWidthTillNow > maxWidth) { if (currentLineWidthTillNow > maxWidth) {
push(currentLine); push(currentLine);
currentLineWidthTillNow = 0; resetParams();
currentLine = "";
break; break;
} }
@ -414,22 +421,15 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
if (currentLineWidthTillNow + spaceWidth >= maxWidth) { if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
const word = currentLine.slice(0, -1); const word = currentLine.slice(0, -1);
push(word); push(word);
currentLine = ""; resetParams();
currentLineWidthTillNow = 0;
break; break;
} }
} }
if (currentLineWidthTillNow === maxWidth) {
currentLine = "";
currentLineWidthTillNow = 0;
}
} }
} }
if (currentLine) { if (currentLine.slice(-1) === " ") {
// only remove last trailing space which we have added when joining words // only remove last trailing space which we have added when joining words
if (currentLine.slice(-1) === " ") { currentLine = currentLine.slice(0, -1);
currentLine = currentLine.slice(0, -1);
}
push(currentLine); push(currentLine);
} }
}); });