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 {
|
2023-11-14 15:10:00 +00:00
|
|
|
HTMLAttributes: {},
|
2023-05-07 13:16:03 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
group: 'block',
|
2024-01-16 09:13:23 +00:00
|
|
|
content: '(image | iframe) figcaption',
|
2023-05-07 13:16:03 +00:00
|
|
|
draggable: true,
|
|
|
|
isolating: true,
|
2024-01-16 09:13:23 +00:00
|
|
|
atom: true,
|
2023-05-07 13:16:03 +00:00
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
2023-11-14 15:10:00 +00:00
|
|
|
'data-float': null,
|
2024-01-16 09:13:23 +00:00
|
|
|
'data-type': { default: null },
|
2023-05-07 13:16:03 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
2024-01-16 09:13:23 +00:00
|
|
|
tag: 'figure',
|
|
|
|
getAttrs: (node) => {
|
|
|
|
if (!(node instanceof HTMLElement)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const img = node.querySelector('img')
|
|
|
|
const iframe = node.querySelector('iframe')
|
|
|
|
let dataType = null
|
|
|
|
if (img) {
|
|
|
|
dataType = 'image'
|
|
|
|
} else if (iframe) {
|
|
|
|
dataType = 'iframe'
|
|
|
|
}
|
|
|
|
return { 'data-type': dataType }
|
|
|
|
},
|
2023-11-14 15:10:00 +00:00
|
|
|
},
|
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-11-14 15:10:00 +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-11-14 15:10:00 +00:00
|
|
|
},
|
2023-08-15 09:38:49 +00:00
|
|
|
}
|
2023-11-14 15:10:00 +00:00
|
|
|
},
|
2023-05-07 13:16:03 +00:00
|
|
|
})
|