import { defineNodeSpec, type Extension } from 'prosekit/core'
import type { Attrs } from '@prosekit/pm/model'
export interface ImageAttrs {
src: string
alt?: string
title?: string
width?: number
height?: number
align?: 'left' | 'center' | 'right'
}
export type ImageSpecExtension = Extension<{
Nodes: {
image: ImageAttrs
}
}>
export function defineImageSpec(): ImageSpecExtension {
return defineNodeSpec({
name: 'image',
inline: true,
attrs: {
src: { default: '' },
alt: { default: null },
title: { default: null },
width: { default: null },
height: { default: null },
align: { default: 'left' },
},
group: 'inline',
draggable: true,
parseDOM: [
{
tag: 'img[src]',
getAttrs: (node: string | HTMLElement) => {
if (typeof node === 'string') return false
return {
src: node.getAttribute('src'),
alt: node.getAttribute('alt'),
title: node.getAttribute('title'),
width: node.getAttribute('width'),
height: node.getAttribute('height'),
align: node.getAttribute('data-align') || 'left',
}
},
},
],
toDOM(node) {
return [
'img',
{
src: node.attrs.src,
alt: node.attrs.alt,
title: node.attrs.title,
width: node.attrs.width,
height: node.attrs.height,
'data-align': node.attrs.align,
},
]
},
})
}