From 7428cac6629e7e9aa309485d7da7d381d7e04f93 Mon Sep 17 00:00:00 2001 From: pipipi-pikachu Date: Tue, 22 Jun 2021 12:04:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=B2=98=E8=B4=B4=E5=92=8C=E6=8B=96?= =?UTF-8?q?=E6=8B=BD=E6=8F=92=E5=85=A5=E6=96=87=E6=9C=AC=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E4=BF=9D=E7=95=99=E6=8D=A2=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/usePasteTextClipboardData.ts | 6 +++++- src/utils/textParser.ts | 9 +++++++++ src/views/Editor/Canvas/hooks/useDropImageOrText.ts | 4 +++- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 src/utils/textParser.ts diff --git a/src/hooks/usePasteTextClipboardData.ts b/src/hooks/usePasteTextClipboardData.ts index 8d7100a4..d382067e 100644 --- a/src/hooks/usePasteTextClipboardData.ts +++ b/src/hooks/usePasteTextClipboardData.ts @@ -3,6 +3,7 @@ import { MutationTypes, useStore } from '@/store' import { decrypt } from '@/utils/crypto' import { PPTElement, Slide } from '@/types/slides' import { createRandomCode } from '@/utils/common' +import { parseText2Paragraphs } from '@/utils/textParser' import useHistorySnapshot from '@/hooks/useHistorySnapshot' import useCreateElement from '@/hooks/useCreateElement' @@ -103,7 +104,10 @@ export default () => { } // 普通文本 - else if (!onlyElements && !onlySlide) pasteText(clipboardData) + else if (!onlyElements && !onlySlide) { + const string = parseText2Paragraphs(clipboardData) + pasteText(string) + } } return { diff --git a/src/utils/textParser.ts b/src/utils/textParser.ts new file mode 100644 index 00000000..00578043 --- /dev/null +++ b/src/utils/textParser.ts @@ -0,0 +1,9 @@ +export const parseText2Paragraphs = (text: string) => { + const htmlText = text.replace(/[\n\r]+/g, '
') + const paragraphs = htmlText.split('
') + let string = '' + for (const paragraph of paragraphs) { + if (paragraph) string += `
${paragraph}
` + } + return string +} \ No newline at end of file diff --git a/src/views/Editor/Canvas/hooks/useDropImageOrText.ts b/src/views/Editor/Canvas/hooks/useDropImageOrText.ts index fe7e0483..8cb3357e 100644 --- a/src/views/Editor/Canvas/hooks/useDropImageOrText.ts +++ b/src/views/Editor/Canvas/hooks/useDropImageOrText.ts @@ -1,6 +1,7 @@ import { computed, onMounted, onUnmounted, Ref } from 'vue' import { useStore } from '@/store' import { getImageDataURL } from '@/utils/image' +import { parseText2Paragraphs } from '@/utils/textParser' import useCreateElement from '@/hooks/useCreateElement' export default (elementRef: Ref) => { @@ -24,12 +25,13 @@ export default (elementRef: Ref) => { else if (dataTransferItem.kind === 'string' && dataTransferItem.type === 'text/plain') { dataTransferItem.getAsString(text => { if (disableHotkeys.value) return + const string = parseText2Paragraphs(text) createTextElement({ left: 0, top: 0, width: 600, height: 50, - }, text) + }, string) }) } }