Merge remote-tracking branch 'origin/release' into danieljgeiger-mathjax

This commit is contained in:
Daniel J. Geiger 2023-03-17 11:19:18 -05:00
commit bdb0dd064b
9 changed files with 86 additions and 44 deletions

View File

@ -26,11 +26,6 @@
"@testing-library/jest-dom": "5.16.2", "@testing-library/jest-dom": "5.16.2",
"@testing-library/react": "12.1.5", "@testing-library/react": "12.1.5",
"@tldraw/vec": "1.7.1", "@tldraw/vec": "1.7.1",
"@types/jest": "27.4.0",
"@types/pica": "5.1.3",
"@types/react": "18.0.15",
"@types/react-dom": "18.0.6",
"@types/socket.io-client": "1.4.36",
"browser-fs-access": "0.29.1", "browser-fs-access": "0.29.1",
"clsx": "1.1.1", "clsx": "1.1.1",
"cross-env": "7.0.3", "cross-env": "7.0.3",
@ -58,7 +53,6 @@
"sass": "1.51.0", "sass": "1.51.0",
"socket.io-client": "2.3.1", "socket.io-client": "2.3.1",
"tunnel-rat": "0.1.0", "tunnel-rat": "0.1.0",
"typescript": "4.9.4",
"workbox-background-sync": "^6.5.4", "workbox-background-sync": "^6.5.4",
"workbox-broadcast-update": "^6.5.4", "workbox-broadcast-update": "^6.5.4",
"workbox-cacheable-response": "^6.5.4", "workbox-cacheable-response": "^6.5.4",
@ -76,9 +70,14 @@
"@excalidraw/eslint-config": "1.0.0", "@excalidraw/eslint-config": "1.0.0",
"@excalidraw/prettier-config": "1.0.2", "@excalidraw/prettier-config": "1.0.2",
"@types/chai": "4.3.0", "@types/chai": "4.3.0",
"@types/jest": "27.4.0",
"@types/lodash.throttle": "4.1.7", "@types/lodash.throttle": "4.1.7",
"@types/pako": "1.0.3", "@types/pako": "1.0.3",
"@types/pica": "5.1.3",
"@types/react": "18.0.15",
"@types/react-dom": "18.0.6",
"@types/resize-observer-browser": "0.1.7", "@types/resize-observer-browser": "0.1.7",
"@types/socket.io-client": "1.4.36",
"chai": "4.3.6", "chai": "4.3.6",
"dotenv": "16.0.1", "dotenv": "16.0.1",
"eslint-config-prettier": "8.5.0", "eslint-config-prettier": "8.5.0",
@ -89,7 +88,8 @@
"lint-staged": "12.3.7", "lint-staged": "12.3.7",
"pepjs": "0.5.3", "pepjs": "0.5.3",
"prettier": "2.6.2", "prettier": "2.6.2",
"rewire": "6.0.0" "rewire": "6.0.0",
"typescript": "4.9.4"
}, },
"engines": { "engines": {
"node": ">=14.0.0" "node": ">=14.0.0"

View File

@ -745,16 +745,19 @@ export const actionChangeTextAlign = register({
value: "left", value: "left",
text: t("labels.left"), text: t("labels.left"),
icon: TextAlignLeftIcon, icon: TextAlignLeftIcon,
testId: "align-left",
}, },
{ {
value: "center", value: "center",
text: t("labels.center"), text: t("labels.center"),
icon: TextAlignCenterIcon, icon: TextAlignCenterIcon,
testId: "align-horizontal-center",
}, },
{ {
value: "right", value: "right",
text: t("labels.right"), text: t("labels.right"),
icon: TextAlignRightIcon, icon: TextAlignRightIcon,
testId: "align-right",
}, },
]} ]}
value={getFormValue( value={getFormValue(

View File

@ -31,7 +31,10 @@ import clsx from "clsx";
import { actionToggleZenMode } from "../actions"; import { actionToggleZenMode } from "../actions";
import "./Actions.scss"; import "./Actions.scss";
import { Tooltip } from "./Tooltip"; import { Tooltip } from "./Tooltip";
import { shouldAllowVerticalAlign } from "../element/textElement"; import {
shouldAllowVerticalAlign,
suppportsHorizontalAlign,
} from "../element/textElement";
export const SelectedShapeActions = ({ export const SelectedShapeActions = ({
appState, appState,
@ -124,7 +127,8 @@ export const SelectedShapeActions = ({
{renderAction("changeFontFamily")} {renderAction("changeFontFamily")}
{renderAction("changeTextAlign")} {suppportsHorizontalAlign(targetElements) &&
renderAction("changeTextAlign")}
</> </>
)} )}

View File

@ -9,6 +9,10 @@
text-align: center; text-align: center;
padding: var(--preview-padding); padding: var(--preview-padding);
margin-bottom: calc(var(--space-factor) * 3); margin-bottom: calc(var(--space-factor) * 3);
display: flex;
justify-content: center;
align-items: center;
} }
.ExportDialog__preview canvas { .ExportDialog__preview canvas {

View File

@ -157,7 +157,7 @@ export const loadSceneOrLibraryFromBlob = async (
}, },
localAppState, localAppState,
localElements, localElements,
{ repairBindings: true }, { repairBindings: true, refreshDimensions: true },
), ),
}; };
} else if (isValidLibrary(data)) { } else if (isValidLibrary(data)) {

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

@ -351,25 +351,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]);
@ -381,8 +394,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(
@ -393,10 +406,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;
@ -404,11 +413,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
@ -416,7 +425,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
@ -426,8 +434,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;
} }
@ -438,22 +445,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);
} }
}); });
@ -692,14 +692,24 @@ export const shouldAllowVerticalAlign = (
} }
return true; return true;
} }
const boundTextElement = getBoundTextElement(element); return false;
if (boundTextElement) { });
if (isArrowElement(element)) { };
export const suppportsHorizontalAlign = (
selectedElements: NonDeletedExcalidrawElement[],
) => {
return selectedElements.some((element) => {
const hasBoundContainer = isBoundToContainer(element);
if (hasBoundContainer) {
const container = getContainerElement(element);
if (isTextElement(element) && isArrowElement(container)) {
return false; return false;
} }
return true; return true;
} }
return false;
return isTextElement(element);
}); });
}; };

View File

@ -263,7 +263,7 @@ export const loadScene = async (
await importFromBackend(id, privateKey), await importFromBackend(id, privateKey),
localDataState?.appState, localDataState?.appState,
localDataState?.elements, localDataState?.elements,
{ repairBindings: true }, { repairBindings: true, refreshDimensions: true },
); );
} else { } else {
data = restore(localDataState || null, null, null, { data = restore(localDataState || null, null, null, {

View File

@ -1179,5 +1179,17 @@ describe("Test Linear Elements", () => {
easy" easy"
`); `);
}); });
it("should not render horizontal align tool when element selected", () => {
createTwoPointerLinearElement("arrow");
const arrow = h.elements[0] as ExcalidrawLinearElement;
createBoundTextElement(DEFAULT_TEXT, arrow);
API.setSelectedElements([arrow]);
expect(queryByTestId(container, "align-left")).toBeNull();
expect(queryByTestId(container, "align-horizontal-center")).toBeNull();
expect(queryByTestId(container, "align-right")).toBeNull();
});
}); });
}); });