From dcdb5e7f65ba1da71c12345d5dc76222544aef1d Mon Sep 17 00:00:00 2001 From: Jack Works Date: Fri, 25 Mar 2022 11:16:48 +0800 Subject: [PATCH 1/2] fix: line break --- .../dom/Renderer/utils/renderText.tsx | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/typed-message/dom/Renderer/utils/renderText.tsx b/packages/typed-message/dom/Renderer/utils/renderText.tsx index e6277d213eec..54a57aa2a499 100644 --- a/packages/typed-message/dom/Renderer/utils/renderText.tsx +++ b/packages/typed-message/dom/Renderer/utils/renderText.tsx @@ -34,12 +34,29 @@ export const RenderLinkFragment = memo(function RenderLink( }) function parseText(string: string, Text: NonNullable) { - const links = parseLink(string).flatMap((x, index) => { + const links = parseLink(string).flatMap((x) => { if (x.type === 'text') { - return x.content.split(/(\n)/g).map((x) => (x === '\n' && index !== 0 ?
: )) + return sliceString(x.content).map((x) => (x === '\n' ?
: )) } if (x.category === 'normal' && !x.content.match(/^https?:\/\//gi)) x.content = 'http://' + x.content return }) return links } + +function sliceString(x: string): string[] { + const result: string[] = [] + + let pos = 0 + let index = x.indexOf('\n') + + if (index === -1) return [x] + while (index !== -1) { + result.push(x.slice(pos, index)) + result.push('\n') + pos = index + 1 + index = x.indexOf('\n', pos) + } + result.push(x.slice(pos)) + return result.filter(Boolean) +} From ae2387c3754a2aa59460aae9ad74d7001554cf17 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Fri, 25 Mar 2022 12:58:14 +0800 Subject: [PATCH 2/2] Update packages/typed-message/dom/Renderer/utils/renderText.tsx Co-authored-by: UncleBill --- packages/typed-message/dom/Renderer/utils/renderText.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/typed-message/dom/Renderer/utils/renderText.tsx b/packages/typed-message/dom/Renderer/utils/renderText.tsx index 54a57aa2a499..c02793d2d2a8 100644 --- a/packages/typed-message/dom/Renderer/utils/renderText.tsx +++ b/packages/typed-message/dom/Renderer/utils/renderText.tsx @@ -52,8 +52,7 @@ function sliceString(x: string): string[] { if (index === -1) return [x] while (index !== -1) { - result.push(x.slice(pos, index)) - result.push('\n') + result.push(x.slice(pos, index), '\n') pos = index + 1 index = x.indexOf('\n', pos) }