-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnippet.ts
48 lines (38 loc) · 1.31 KB
/
snippet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* eslint-disable import/no-extraneous-dependencies */
import { Extension } from '@tiptap/core'
import { Plugin } from 'prosemirror-state'
import { DOMParser } from 'prosemirror-model'
function wrapHtmlInTemplate(value: string): HTMLSpanElement {
const element = document.createElement('span')
element.innerHTML = `${value.trim()}`
return element
}
export const SnippetExtension = Extension.create({
name: 'snippet',
addProseMirrorPlugins() {
return [
new Plugin({
props: {
handleDrop(view, event: DragEvent | any): boolean {
if (!event) return false
event.preventDefault()
const coordinates = view.posAtCoords({
left: event.clientX,
top: event.clientY,
})
const snippetContent = event.dataTransfer.getData('snippet')
const parsedContent = DOMParser
.fromSchema(view.state.schema)
.parseSlice(wrapHtmlInTemplate(snippetContent))
if (coordinates) {
const dropTransaction = view.state.tr.insert(coordinates.pos, parsedContent.content)
dropTransaction.setMeta('isSnippetDropTransaction', true)
view.dispatch(dropTransaction)
}
return false
},
},
}),
]
},
})