Skip to content

Commit

Permalink
Merge pull request #5266 from nextcloud/backport/5212/stable28
Browse files Browse the repository at this point in the history
[stable28] fix: Properly copy selection as markdown to the plaintext clipboard
  • Loading branch information
juliusknorr authored Jan 15, 2024
2 parents 933497c + 20914b5 commit 8a7d7ef
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 32 deletions.
20 changes: 2 additions & 18 deletions src/EditorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,8 @@ const createEditor = ({ language, onCreate, onUpdate = () => {}, extensions, ena
})
}

const SerializeException = function(message) {
this.message = message
}

const serializePlainText = (tiptap) => {
const doc = tiptap.getJSON()

if (doc.content.length !== 1 || typeof doc.content[0].content === 'undefined' || doc.content[0].content.length !== 1) {
if (doc.content[0].type === 'codeBlock' && typeof doc.content[0].content === 'undefined') {
return ''
}
throw new SerializeException('Failed to serialize document to plain text')
}
const codeBlock = doc.content[0].content[0]
if (codeBlock.type !== 'text') {
throw new SerializeException('Failed to serialize document to plain text')
}
return codeBlock.text
const serializePlainText = (doc) => {
return doc.textContent
}

export default createEditor
Expand Down
16 changes: 3 additions & 13 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import { createEditor, serializePlainText, loadSyntaxHighlight } from './../Edit
import { createMarkdownSerializer } from './../extensions/Markdown.js'
import markdownit from './../markdownit/index.js'
import { CollaborationCursor, Keymap } from '../extensions/index.js'
import { CollaborationCursor } from '../extensions/index.js'
import DocumentStatus from './Editor/DocumentStatus.vue'
import isMobile from './../mixins/isMobile.js'
import setContent from './../mixins/setContent.js'
Expand Down Expand Up @@ -372,8 +372,8 @@ export default {
filePath: this.relativePath,
forceRecreate: this.forceRecreate,
serialize: this.isRichEditor
? () => createMarkdownSerializer(this.$editor.schema).serialize(this.$editor.state.doc)
: () => serializePlainText(this.$editor),
? (content) => createMarkdownSerializer(this.$editor.schema).serialize(content ?? this.$editor.state.doc)
: (content) => serializePlainText(content ?? this.$editor.state.doc),
getDocumentState: () => getDocumentState(this.$ydoc),
})
Expand Down Expand Up @@ -527,16 +527,6 @@ export default {
clientId: this.$ydoc.clientID,
},
}),
Keymap.configure({
'Shift-Mod-c': ({ editor }) => {
if (!navigator?.clipboard) {
console.error('Clipboard API is not available')
}
const proseMirrorMarkdown = this.$syncService.serialize(editor.state.doc)
navigator.clipboard.writeText(proseMirrorMarkdown)
},
}),
],
enableRichEditing: this.isRichEditor,
})
Expand Down
3 changes: 3 additions & 0 deletions src/extensions/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ const Markdown = Extension.create({

return parser.parseSlice(dom, { preserveWhitespace: true, context: $context })
},
clipboardTextSerializer: (slice) => {
return createMarkdownSerializer(this.editor.schema).serialize(slice.content)
},
transformPastedHTML,
},
}),
Expand Down
2 changes: 1 addition & 1 deletion src/tests/plaintext.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const plaintextThroughEditor = (markdown) => {
enableRichEditing: false
})
tiptap.commands.setContent(content)
return serializePlainText(tiptap) || 'failed'
return serializePlainText(tiptap.state.doc) || 'failed'
}

describe('commonmark as plaintext', () => {
Expand Down

0 comments on commit 8a7d7ef

Please sign in to comment.