feat: support copy rich text (#904)

* feat: support copy rich text

* update changelog
This commit is contained in:
Ran Luo 2019-04-11 09:28:04 +08:00 committed by GitHub
parent 6203eb2759
commit ef59a74380
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 19 deletions

View File

@ -61,6 +61,7 @@ foo<section>bar</section>zar
- Hide titlebar control buttons in custom titlebar style
- Corrected hamburger menu offset
- Optimization of inline html displa, now you can nest other inline syntax in inline html(#849)
- Use CmdOrCtrl + C/V to copy rich text to `word`(Windows) or `page`(macOS) (#885)
**:beetle:Bug fix**
@ -115,6 +116,7 @@ foo<section>bar</section>zar
- Fixed source code mode to preview switching
- Mark Text didn't remove highlight when I delete the markdown symbol like * or `. (#893)
- After delete ``` at the beginning to paragraph by backspace, then type other text foo, the color will be strange, if you type 1. bar. error happened. (#892)
- Fix highlight error in code block (#545 #890)
### 0.13.65

View File

@ -2,6 +2,7 @@ import selection from '../selection'
import { CLASS_OR_ID } from '../config'
import { getSanitizeHtml } from '../utils/exportHtml'
import ExportMarkdown from '../utils/exportMarkdown'
import marked from '../parser/marked'
const copyCutCtrl = ContentState => {
ContentState.prototype.cutHandler = function () {
@ -125,9 +126,9 @@ const copyCutCtrl = ContentState => {
}
}
const htmlData = wrapper.innerHTML
let htmlData = wrapper.innerHTML
const textData = this.htmlToMarkdown(htmlData)
htmlData = marked(textData)
return { html: htmlData, text: textData }
}

View File

@ -82,7 +82,7 @@ const pasteCtrl = ContentState => {
return this.pasteHandler(event, type)
}
const appendHtml = () => {
const appendHtml = (text) => {
startBlock.text = startBlock.text.substring(0, start.offset) + text + startBlock.text.substring(start.offset)
const { key } = start
const offset = start.offset + text.length
@ -179,30 +179,67 @@ const pasteCtrl = ContentState => {
// handle copyAsHtml
if (copyType === 'copyAsHtml') {
// already handle code block above
if (startBlock.type === 'span' && startBlock.nextSibling) {
const afterParagraph = this.createBlock('p')
let temp = startBlock
const removeCache = []
while (temp.nextSibling) {
temp = this.getBlock(temp.nextSibling)
this.appendChild(afterParagraph, temp)
removeCache.push(temp)
}
removeCache.forEach(b => this.removeBlock(b))
this.insertAfter(afterParagraph, parent)
startBlock.nextSibling = null
}
switch (type) {
case 'normal': {
if (startBlock.type === 'span' && this.isOnlyChild(startBlock) && !startBlock.text) {
this.codeBlockUpdate(startBlock, text.trim(), 'html')
} else {
appendHtml()
const htmlBlock = this.createBlock('p')
const lines = text.trim().split(LINE_BREAKS_REG).map(line => this.createBlock('span', line))
for (const line of lines) {
this.appendChild(htmlBlock, line)
}
if (startBlock.type === 'span') {
this.insertAfter(htmlBlock, parent)
} else {
this.insertAfter(htmlBlock, startBlock)
}
if (
startBlock.type === 'span' && startBlock.text.length === 0 && this.isOnlyChild(startBlock)
) {
this.removeBlock(parent)
}
// handler heading
if (startBlock.text.length === 0 && startBlock.type !== 'span') {
this.removeBlock(startBlock)
}
this.insertHtmlBlock(htmlBlock)
break
}
case 'pasteAsPlainText': {
const lines = text.trim().split(LINE_BREAKS_REG)
let htmlBlock = null
if (!startBlock.text || lines.length > 1) {
htmlBlock = this.createBlock('p')
;(startBlock.text ? lines.slice(1) : lines).map(line => this.createBlock('span', line))
.forEach(l => {
this.appendChild(htmlBlock, l)
})
}
if (htmlBlock) {
if (startBlock.type === 'span') {
const lines = text.trim().split(LINE_BREAKS_REG).map(line => this.createBlock('span', line))
for (const line of lines) {
this.appendChild(parent, line)
}
const lastLine = lines[lines.length - 1]
const { key } = lastLine
const offset = lastLine.text.length
this.cursor = {
start: { key, offset },
end: { key, offset }
}
this.insertAfter(htmlBlock, parent)
} else {
appendHtml()
this.insertAfter(htmlBlock, startBlock)
}
this.insertHtmlBlock(htmlBlock)
}
if (startBlock.text) {
appendHtml(lines[0])
} else {
this.removeBlock(startBlock.type === 'span' ? parent : startBlock)
}
break
}