feat(front): add MarkdownPaste extension for enhanced paste functionality in Tiptap editor

This commit is contained in:
keven1024
2025-05-20 11:14:16 +08:00
parent 5dd3458d66
commit daf5a036e1
2 changed files with 27 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ import { Editor, EditorContent } from '@tiptap/vue-3'
import StarterKit from '@tiptap/starter-kit'
import { Markdown } from 'tiptap-markdown';
import Placeholder from '@tiptap/extension-placeholder'
import { MarkdownPaste } from './Tiptap/ MarkdownPaste';
const props = defineProps<{
modelValue: string
placeholder?: string
@@ -15,7 +16,7 @@ const editor = ref<Editor | undefined>(undefined)
onMounted(() => {
editor.value = new Editor({
content: props.modelValue,
extensions: [StarterKit, Markdown, Placeholder.configure({
extensions: [StarterKit, Markdown, MarkdownPaste, Placeholder.configure({
placeholder: props.placeholder ?? ''
})],
onUpdate: () => {

View File

@@ -0,0 +1,25 @@
import { Extension } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state'
import { marked } from 'marked'
export const MarkdownPaste = Extension.create({
name: 'markdownPaste',
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey('markdownPaste'),
props: {
handlePaste: (view, event) => {
const clipboardText = event.clipboardData?.getData('text/plain')
if (clipboardText) {
const html = marked(clipboardText)
this.editor.commands.insertContent(html)
return true
}
return false
},
},
}),
]
},
})