Don't copy (or on cut) empty data to the clipboard, matches native behavior (#3130)

This commit is contained in:
Mitch Capper (they, them) 2022-03-19 12:01:44 -07:00 committed by GitHub
parent 5cf4c73383
commit c841facdfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -243,8 +243,10 @@ const copyCutCtrl = ContentState => {
if (row === 1 && column === 1) { if (row === 1 && column === 1) {
// Copy cells text if only one is selected // Copy cells text if only one is selected
event.clipboardData.setData('text/html', '') if (tableContents[0][0].text.length > 0) {
event.clipboardData.setData('text/plain', tableContents[0][0].text) event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', tableContents[0][0].text)
}
} else { } else {
// Copy as markdown table // Copy as markdown table
const figureBlock = this.createBlock('figure', { const figureBlock = this.createBlock('figure', {
@ -254,9 +256,10 @@ const copyCutCtrl = ContentState => {
this.appendChild(figureBlock, table) this.appendChild(figureBlock, table)
const { isGitlabCompatibilityEnabled, listIndentation } = this const { isGitlabCompatibilityEnabled, listIndentation } = this
const markdown = new ExportMarkdown([figureBlock], listIndentation, isGitlabCompatibilityEnabled).generate() const markdown = new ExportMarkdown([figureBlock], listIndentation, isGitlabCompatibilityEnabled).generate()
if (markdown.length > 0) {
event.clipboardData.setData('text/html', '') event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', markdown) event.clipboardData.setData('text/plain', markdown)
}
} }
} }
} }
@ -270,30 +273,38 @@ const copyCutCtrl = ContentState => {
const { selectedImage } = this const { selectedImage } = this
if (selectedImage) { if (selectedImage) {
const { token } = selectedImage const { token } = selectedImage
event.clipboardData.setData('text/html', token.raw) if (token.raw.length > 0) {
event.clipboardData.setData('text/plain', token.raw) event.clipboardData.setData('text/html', token.raw)
event.clipboardData.setData('text/plain', token.raw)
}
return return
} }
const { html, text } = this.getClipBoardData() const { html, text } = this.getClipBoardData()
switch (type) { switch (type) {
case 'normal': { case 'normal': {
event.clipboardData.setData('text/html', html) if (text.length > 0) {
event.clipboardData.setData('text/plain', text) event.clipboardData.setData('text/html', html)
event.clipboardData.setData('text/plain', text)
}
break break
} }
case 'copyAsMarkdown': { case 'copyAsMarkdown': {
event.clipboardData.setData('text/html', '') if (text.length > 0) {
event.clipboardData.setData('text/plain', text) event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', text)
}
break break
} }
case 'copyAsHtml': { case 'copyAsHtml': {
event.clipboardData.setData('text/html', '') if (text.length > 0) {
event.clipboardData.setData('text/plain', getSanitizeHtml(text, { event.clipboardData.setData('text/html', '')
superSubScript: this.muya.options.superSubScript, event.clipboardData.setData('text/plain', getSanitizeHtml(text, {
footnote: this.muya.options.footnote, superSubScript: this.muya.options.superSubScript,
isGitlabCompatibilityEnabled: this.muya.options.isGitlabCompatibilityEnabled footnote: this.muya.options.footnote,
})) isGitlabCompatibilityEnabled: this.muya.options.isGitlabCompatibilityEnabled
}))
}
break break
} }
@ -303,8 +314,10 @@ const copyCutCtrl = ContentState => {
const anchor = this.getAnchor(block) const anchor = this.getAnchor(block)
const { isGitlabCompatibilityEnabled, listIndentation } = this const { isGitlabCompatibilityEnabled, listIndentation } = this
const markdown = new ExportMarkdown([anchor], listIndentation, isGitlabCompatibilityEnabled).generate() const markdown = new ExportMarkdown([anchor], listIndentation, isGitlabCompatibilityEnabled).generate()
event.clipboardData.setData('text/html', '') if (markdown.length > 0) {
event.clipboardData.setData('text/plain', markdown) event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', markdown)
}
break break
} }
@ -313,8 +326,10 @@ const copyCutCtrl = ContentState => {
if (typeof codeContent !== 'string') { if (typeof codeContent !== 'string') {
return return
} }
event.clipboardData.setData('text/html', '') if (codeContent.length > 0) {
event.clipboardData.setData('text/plain', codeContent) event.clipboardData.setData('text/html', '')
event.clipboardData.setData('text/plain', codeContent)
}
} }
} }
} }