Skip to content

Commit

Permalink
fix: Properly copy selection as markdown to the plaintext clipboard
Browse files Browse the repository at this point in the history
Using clipboardTextSerializer we can copy the current selection as markdown and don't need the extra keyboard shortcut, which was broken anyways as it always selected the full text

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Jan 8, 2024
1 parent 994c86c commit ad26e8b
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 @@ -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),
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

0 comments on commit ad26e8b

Please sign in to comment.