Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly copy selection as markdown to the plaintext clipboard #5212

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -111,7 +111,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 @@ -371,8 +371,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),
max-nextcloud marked this conversation as resolved.
Show resolved Hide resolved
getDocumentState: () => getDocumentState(this.$ydoc),
})

Expand Down Expand Up @@ -526,16 +526,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