From 23b785de68528bb48ffda23ff6aa7da5a110d598 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Wed, 18 Mar 2020 13:01:33 +0100 Subject: [PATCH] Trim trailing newlines (#999) * trim newlines for text elements * fix comment --- src/element/textWysiwyg.tsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/element/textWysiwyg.tsx b/src/element/textWysiwyg.tsx index f4c23a63..f01d02a4 100644 --- a/src/element/textWysiwyg.tsx +++ b/src/element/textWysiwyg.tsx @@ -1,6 +1,17 @@ import { KEYS } from "../keys"; import { selectNode } from "../utils"; +function trimText(text: string) { + // whitespace only → trim all because we'd end up inserting invisible element + if (!text.trim()) { + return ""; + } + // replace leading/trailing newlines (only) otherwise it messes up bounding + // box calculation (there's also a bug in FF which inserts trailing newline + // for multiline texts) + return text.replace(/^\n+|\n+$/g, ""); +} + type TextWysiwygParams = { initText: string; x: number; @@ -101,7 +112,7 @@ export function textWysiwyg({ function handleSubmit() { if (editable.innerText) { - onSubmit(editable.innerText); + onSubmit(trimText(editable.innerText)); } else { onCancel(); }