fix: run pasterules for pasted content, fix #2408

This commit is contained in:
Philipp Kühn
2022-01-20 15:04:34 +01:00
parent 3215d8d6f8
commit bc03c0d778

View File

@@ -158,24 +158,53 @@ function run(config: {
*/ */
export function pasteRulesPlugin(props: { editor: Editor, rules: PasteRule[] }): Plugin[] { export function pasteRulesPlugin(props: { editor: Editor, rules: PasteRule[] }): Plugin[] {
const { editor, rules } = props const { editor, rules } = props
let isProseMirrorHTML = false let dragSourceElement: Element | null = null
let isPastedFromProseMirror = false
let isDroppedFromProseMirror = false
const plugins = rules.map(rule => { const plugins = rules.map(rule => {
return new Plugin({ return new Plugin({
// we register a global drag handler to track the current drag source element
view(view) {
const handleDragstart = (event: DragEvent) => {
dragSourceElement = view.dom.parentElement?.contains(event.target as Element)
? view.dom.parentElement
: null
}
window.addEventListener('dragstart', handleDragstart)
return {
destroy() {
window.removeEventListener('dragstart', handleDragstart)
},
}
},
props: { props: {
handlePaste: (view, event) => { handleDOMEvents: {
drop: view => {
isDroppedFromProseMirror = dragSourceElement === view.dom.parentElement
return false
},
paste: (view, event) => {
const html = event.clipboardData?.getData('text/html') const html = event.clipboardData?.getData('text/html')
isProseMirrorHTML = !!html?.includes('data-pm-slice') isPastedFromProseMirror = !!html?.includes('data-pm-slice')
return false return false
}, },
}, },
},
appendTransaction: (transactions, oldState, state) => { appendTransaction: (transactions, oldState, state) => {
const transaction = transactions[0] const transaction = transactions[0]
const isPaste = transaction.getMeta('uiEvent') === 'paste' && !isPastedFromProseMirror
const isDrop = transaction.getMeta('uiEvent') === 'drop' && !isDroppedFromProseMirror
// stop if there is not a paste event if (!isPaste && !isDrop) {
if (!transaction.getMeta('paste') || isProseMirrorHTML) {
return return
} }