fix: 粘贴和拖拽插入文本时,保留换行

This commit is contained in:
pipipi-pikachu 2021-06-22 12:04:42 +08:00
parent 1501203a39
commit 7428cac662
3 changed files with 17 additions and 2 deletions

View File

@ -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 {

9
src/utils/textParser.ts Normal file
View File

@ -0,0 +1,9 @@
export const parseText2Paragraphs = (text: string) => {
const htmlText = text.replace(/[\n\r]+/g, '<br>')
const paragraphs = htmlText.split('<br>')
let string = ''
for (const paragraph of paragraphs) {
if (paragraph) string += `<div>${paragraph}</div>`
}
return string
}

View File

@ -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<HTMLElement | undefined>) => {
@ -24,12 +25,13 @@ export default (elementRef: Ref<HTMLElement | undefined>) => {
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)
})
}
}