From fe54eb409d200326783ca0ccba0d3d6f6a1639e9 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sun, 16 Feb 2020 18:55:21 +0100 Subject: [PATCH] Fixed automatically added new lines for inserted text --- ChangeLog.md | 1 + shared/js/ui/frames/side/chat_box.ts | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 01545411..b0bd0d34 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,7 @@ - Updated the `setup_windows.md` tutorial - Correct redirecting to `index.php` when using the serve mode - Added correct hostname resolving for the web client. + - Fixed automatically added new lines for inserted text * **02.02.20** - Added a music bot GUI diff --git a/shared/js/ui/frames/side/chat_box.ts b/shared/js/ui/frames/side/chat_box.ts index 60fdb719..863f9335 100644 --- a/shared/js/ui/frames/side/chat_box.ts +++ b/shared/js/ui/frames/side/chat_box.ts @@ -133,7 +133,6 @@ namespace chat { return message.replace(/ /g, ' '); } private _callback_paste(event: ClipboardEvent) { - const _event = (event).originalEvent as ClipboardEvent || event; const clipboard = _event.clipboardData || (window).clipboardData; if(!clipboard) return; @@ -151,7 +150,27 @@ namespace chat { const parser = new DOMParser(); const nodes = parser.parseFromString(html_xml, "text/html"); - const data = this._text(nodes.body); + let data = this._text(nodes.body); + + /* fix prefix & suffix new lines */ + { + let prefix_length = 0, suffix_length = 0; + { + for(let i = 0; i < raw_text.length; i++) + if(raw_text.charAt(i) === '\n') + prefix_length++; + else if(raw_text.charAt(i) !== '\r') + break; + for(let i = raw_text.length - 1; i >= 0; i++) + if(raw_text.charAt(i) === '\n') + suffix_length++; + else if(raw_text.charAt(i) !== '\r') + break; + } + + data = data.replace(/^[\n\r]+|[\n\r]+$/g, ''); + data = "\n".repeat(prefix_length) + data + "\n".repeat(suffix_length); + } event.preventDefault(); selection.deleteFromDocument();