From a0ecfed4cdfbaef6dff2ad3886d1991825673950 Mon Sep 17 00:00:00 2001 From: DanielJGeiger <1852529+DanielJGeiger@users.noreply.github.com> Date: Wed, 9 Nov 2022 00:00:22 -0600 Subject: [PATCH] fix: Paste clipboard contents into unbound text elements (#5849) * Fix #5848. * Add test. * some tweaks Co-authored-by: Aakansha Doshi --- src/element/textWysiwyg.test.tsx | 21 +++++++++++++++++++++ src/element/textWysiwyg.tsx | 13 +++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/element/textWysiwyg.test.tsx b/src/element/textWysiwyg.test.tsx index 659495d6..755a76aa 100644 --- a/src/element/textWysiwyg.test.tsx +++ b/src/element/textWysiwyg.test.tsx @@ -435,6 +435,25 @@ describe("textWysiwyg", () => { ); expect(h.state.zoom.value).toBe(1); }); + + it("should paste text correctly", async () => { + Keyboard.keyPress(KEYS.ENTER); + await new Promise((r) => setTimeout(r, 0)); + const text = "A quick brown fox jumps over the lazy dog."; + + //@ts-ignore + textarea.onpaste({ + preventDefault: () => {}, + //@ts-ignore + clipboardData: { + getData: () => text, + }, + }); + + await new Promise((cb) => setTimeout(cb, 0)); + textarea.blur(); + expect(textElement.text).toBe(text); + }); }); describe("Test container-bound text", () => { @@ -916,6 +935,8 @@ describe("textWysiwyg", () => { editor.blur(); expect(rectangle.width).toBe(110); expect(rectangle.height).toBe(210); + const textElement = h.elements[1] as ExcalidrawTextElement; + expect(textElement.text).toBe(wrappedText); }); }); }); diff --git a/src/element/textWysiwyg.tsx b/src/element/textWysiwyg.tsx index 50387a7a..f208c034 100644 --- a/src/element/textWysiwyg.tsx +++ b/src/element/textWysiwyg.tsx @@ -290,17 +290,14 @@ export const textWysiwyg = ({ fontSize: app.state.currentItemFontSize, fontFamily: app.state.currentItemFontFamily, }); + const wrappedText = container + ? wrapText(data, font, getMaxContainerWidth(container)) + : data; - const wrappedText = wrapText( - data, - font, - getMaxContainerWidth(container!), - ); const dimensions = measureText(wrappedText, font); editable.style.height = `${dimensions.height}px`; - if (data) { - onChange(wrappedText); - } + + onChange(wrappedText); }; editable.oninput = () => { const updatedTextElement = Scene.getScene(element)?.getElement(