2023-08-15 09:38:49 +00:00
|
|
|
import { mergeAttributes, Node } from '@tiptap/core'
|
|
|
|
import { Plugin } from '@tiptap/pm/state'
|
2023-05-07 13:16:03 +00:00
|
|
|
|
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface Commands<ReturnType> {
|
2023-08-15 09:38:49 +00:00
|
|
|
Figure: {
|
|
|
|
setFigureFloat: (float: null | 'left' | 'right') => ReturnType
|
2023-05-07 13:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-15 09:38:49 +00:00
|
|
|
export const Figure = Node.create({
|
2023-05-07 13:16:03 +00:00
|
|
|
name: 'figure',
|
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
HTMLAttributes: {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
group: 'block',
|
2023-08-15 09:38:49 +00:00
|
|
|
content: 'block figcaption',
|
2023-05-07 13:16:03 +00:00
|
|
|
draggable: true,
|
|
|
|
isolating: true,
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
2023-08-15 09:38:49 +00:00
|
|
|
'data-float': null
|
2023-05-07 13:16:03 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
2023-08-15 09:38:49 +00:00
|
|
|
tag: `figure[data-type="${this.name}"]`
|
2023-05-07 13:16:03 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
renderHTML({ HTMLAttributes }) {
|
2023-08-15 09:38:49 +00:00
|
|
|
return ['figure', mergeAttributes(HTMLAttributes, { 'data-type': this.name }), 0]
|
2023-05-07 13:16:03 +00:00
|
|
|
},
|
|
|
|
|
2023-08-15 09:38:49 +00:00
|
|
|
addProseMirrorPlugins() {
|
|
|
|
return [
|
|
|
|
new Plugin({
|
|
|
|
props: {
|
|
|
|
handleDOMEvents: {
|
|
|
|
// prevent dragging nodes out of the figure
|
|
|
|
dragstart: (view, event) => {
|
|
|
|
if (!event.target) {
|
2023-05-09 23:15:26 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-08-15 09:38:49 +00:00
|
|
|
const pos = view.posAtDOM(event.target as HTMLElement, 0)
|
|
|
|
const $pos = view.state.doc.resolve(pos)
|
|
|
|
if ($pos.parent.type === this.type) {
|
|
|
|
event.preventDefault()
|
2023-05-09 23:15:26 +00:00
|
|
|
}
|
2023-08-15 09:38:49 +00:00
|
|
|
return false
|
2023-05-07 13:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-15 09:38:49 +00:00
|
|
|
})
|
|
|
|
]
|
2023-05-07 13:16:03 +00:00
|
|
|
},
|
|
|
|
|
2023-08-15 09:38:49 +00:00
|
|
|
addCommands() {
|
|
|
|
return {
|
|
|
|
setFigureFloat:
|
|
|
|
(value) =>
|
|
|
|
({ commands }) => {
|
|
|
|
return commands.updateAttributes(this.name, { 'data-float': value })
|
2023-05-07 13:16:03 +00:00
|
|
|
}
|
2023-08-15 09:38:49 +00:00
|
|
|
}
|
2023-05-07 13:16:03 +00:00
|
|
|
}
|
|
|
|
})
|