fix: run pasterules for pasted content, fix #2408
This commit is contained in:
@@ -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: {
|
||||||
const html = event.clipboardData?.getData('text/html')
|
drop: view => {
|
||||||
|
isDroppedFromProseMirror = dragSourceElement === view.dom.parentElement
|
||||||
|
|
||||||
isProseMirrorHTML = !!html?.includes('data-pm-slice')
|
return false
|
||||||
|
},
|
||||||
|
|
||||||
return false
|
paste: (view, event) => {
|
||||||
|
const html = event.clipboardData?.getData('text/html')
|
||||||
|
|
||||||
|
isPastedFromProseMirror = !!html?.includes('data-pm-slice')
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user