From a9908337f9867c65424bcd48d9c87dc0c067acc7 Mon Sep 17 00:00:00 2001 From: MYCyw <104288043+MYCyw@users.noreply.github.com> Date: Fri, 28 Oct 2022 12:53:52 +0800 Subject: [PATCH] fix blank cells at start and end test each row by regex and remove blank rows, instead of using trim() --- script.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/script.js b/script.js index d12f98d..65bfbc4 100644 --- a/script.js +++ b/script.js @@ -12,7 +12,7 @@ function looksLikeTable(data) { editor.addEventListener("paste", function(event) { var clipboard = event.clipboardData - var data = clipboard.getData('text/plain').trim() + var data = clipboard.getData('text/plain') if(looksLikeTable(data)) { event.preventDefault() @@ -20,10 +20,15 @@ editor.addEventListener("paste", function(event) { return } - var rows = data.split((/[\u0085\u2028\u2029]|\r\n?/g)).map(function(row) { - row = row.replace('\n', ' ') - return row.split("\t") - }) + var rows = data + .split(/[\u0085\u2028\u2029]|\r?\n/g) + .reduce(function (result, row) { + const re = /^[\t\n]*$/; + if (!re.test(row)) { + result.push(row.replace("\n", " ").split("\t")); + } + return result; + }, []); var colAlignments = []