diff --git a/src/index.tsx b/src/index.tsx index c20ea6180..009968050 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1191,6 +1191,47 @@ function getSelectedBackgroundColor() { return backgroundColors.length === 1 ? backgroundColors[0] : null; } +function addTextElement(element: ExcalidrawElement) { + if (isTextElement(element)) { + resetCursor(); + const text = prompt("What text do you want?"); + if (text === null) { + return; + } + const fontSize = 20; + element.text = text; + element.font = `${fontSize}px Virgil`; + const font = context.font; + context.font = element.font; + const textMeasure = context.measureText(element.text); + const width = textMeasure.width; + const actualBoundingBoxAscent = + textMeasure.actualBoundingBoxAscent || fontSize; + const actualBoundingBoxDescent = textMeasure.actualBoundingBoxDescent || 0; + element.actualBoundingBoxAscent = actualBoundingBoxAscent; + context.font = font; + const height = actualBoundingBoxAscent + actualBoundingBoxDescent; + // Center the text + element.x -= width / 2; + element.y -= actualBoundingBoxAscent; + element.width = width; + element.height = height; + } +} + +function checkHitElement(x: number, y: number) { + let hitElement = null; + // We need to to hit testing from front (end of the array) to back (beginning of the array) + for (let i = elements.length - 1; i >= 0; --i) { + if (hitTest(elements[i], x, y)) { + hitElement = elements[i]; + break; + } + } + + return hitElement; +} + const ELEMENT_SHIFT_TRANSLATE_AMOUNT = 5; const ELEMENT_TRANSLATE_AMOUNT = 1; @@ -1897,14 +1938,7 @@ class App extends React.Component<{}, AppState> { document.documentElement.style.cursor = `${resizeHandle}-resize`; isResizingElements = true; } else { - let hitElement = null; - // We need to to hit testing from front (end of the array) to back (beginning of the array) - for (let i = elements.length - 1; i >= 0; --i) { - if (hitTest(elements[i], x, y)) { - hitElement = elements[i]; - break; - } - } + const hitElement = checkHitElement(x, y); // If we click on something if (hitElement) { @@ -1932,32 +1966,7 @@ class App extends React.Component<{}, AppState> { } } - if (isTextElement(element)) { - resetCursor(); - const text = prompt("What text do you want?"); - if (text === null) { - return; - } - const fontSize = 20; - element.text = text; - element.font = `${fontSize}px Virgil`; - const font = context.font; - context.font = element.font; - const textMeasure = context.measureText(element.text); - const width = textMeasure.width; - const actualBoundingBoxAscent = - textMeasure.actualBoundingBoxAscent || fontSize; - const actualBoundingBoxDescent = - textMeasure.actualBoundingBoxDescent || 0; - element.actualBoundingBoxAscent = actualBoundingBoxAscent; - context.font = font; - const height = actualBoundingBoxAscent + actualBoundingBoxDescent; - // Center the text - element.x -= width / 2; - element.y -= actualBoundingBoxAscent; - element.width = width; - element.height = height; - } + addTextElement(element); generateDraw(element); elements.push(element); @@ -2160,14 +2169,7 @@ class App extends React.Component<{}, AppState> { e.clientX - CANVAS_WINDOW_OFFSET_LEFT - this.state.scrollX; const y = e.clientY - CANVAS_WINDOW_OFFSET_TOP - this.state.scrollY; - let hitElement = null; - // We need to to hit testing from front (end of the array) to back (beginning of the array) - for (let i = elements.length - 1; i >= 0; --i) { - if (hitTest(elements[i], x, y)) { - hitElement = elements[i]; - break; - } - } + const hitElement = checkHitElement(x, y); if (hitElement) { return; @@ -2185,33 +2187,7 @@ class App extends React.Component<{}, AppState> { 100 ); - if (isTextElement(element)) { - resetCursor(); - const text = prompt("What text do you want?"); - if (text === null) { - return; - } - const fontSize = 20; - element.text = text; - element.font = `${fontSize}px Virgil`; - const font = context.font; - context.font = element.font; - const textMeasure = context.measureText(element.text); - const width = textMeasure.width; - const actualBoundingBoxAscent = - textMeasure.actualBoundingBoxAscent || fontSize; - const actualBoundingBoxDescent = - textMeasure.actualBoundingBoxDescent || 0; - element.actualBoundingBoxAscent = actualBoundingBoxAscent; - context.font = font; - const height = actualBoundingBoxAscent + actualBoundingBoxDescent; - // Center the text - element.x -= width / 2; - element.y -= actualBoundingBoxAscent; - element.width = width; - element.height = height; - } - + addTextElement(element); generateDraw(element); elements.push(element);