diff --git a/src/muya/lib/contentState/pasteCtrl.js b/src/muya/lib/contentState/pasteCtrl.js index 439ac52e..7c04ffcc 100644 --- a/src/muya/lib/contentState/pasteCtrl.js +++ b/src/muya/lib/contentState/pasteCtrl.js @@ -76,6 +76,7 @@ const pasteCtrl = ContentState => { const startBlock = this.getBlock(start.key) const endBlock = this.getBlock(end.key) const parent = this.getParent(startBlock) + if (start.key !== end.key) { this.cutHandler() return this.pasteHandler(event, type) @@ -287,19 +288,31 @@ const pasteCtrl = ContentState => { }) } else { if (firstFragment.type === 'p') { - startBlock.text += firstFragment.children[0].text - firstFragment.children.slice(1).forEach(line => { - if (startBlock.functionType) line.functionType = startBlock.functionType - if (startBlock.lang) line.lang = startBlock.lang - this.appendChild(parent, line) - }) + if (/^h\d$/.test(startBlock.type)) { + // handle paste into header + startBlock.text += firstFragment.children[0].text + if (firstFragment.children.length > 1) { + const newParagraph = this.createBlock('p') + firstFragment.children.slice(1).forEach(line => { + this.appendChild(newParagraph, line) + }) + this.insertAfter(newParagraph, startBlock) + } + } else { + startBlock.text += firstFragment.children[0].text + firstFragment.children.slice(1).forEach(line => { + if (startBlock.functionType) line.functionType = startBlock.functionType + if (startBlock.lang) line.lang = startBlock.lang + this.appendChild(parent, line) + }) + } } else if (/^h\d$/.test(firstFragment.type)) { startBlock.text += firstFragment.text.split(/\s+/)[1] } else { startBlock.text += firstFragment.text } - let target = parent + let target = /^h\d$/.test(startBlock.type) ? startBlock : parent tailFragments.forEach(block => { this.insertAfter(block, target) target = block