mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-23 17:07:49 +08:00
71 lines
1.2 KiB
TypeScript
71 lines
1.2 KiB
TypeScript
import { Command, createNode, nodeInputRule } from '@tiptap/core'
|
|
|
|
export const inputRegex = /!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\)/
|
|
|
|
const Image = createNode({
|
|
name: 'image',
|
|
|
|
inline: true,
|
|
|
|
group: 'inline',
|
|
|
|
draggable: true,
|
|
|
|
addAttributes() {
|
|
return {
|
|
src: {
|
|
default: null,
|
|
},
|
|
alt: {
|
|
default: null,
|
|
},
|
|
title: {
|
|
default: null,
|
|
},
|
|
}
|
|
},
|
|
|
|
parseHTML() {
|
|
return [
|
|
{
|
|
tag: 'img[src]',
|
|
},
|
|
]
|
|
},
|
|
|
|
renderHTML({ attributes }) {
|
|
return ['img', attributes]
|
|
},
|
|
|
|
addCommands() {
|
|
return {
|
|
image: (options: { src: string, alt?: string, title?: string }): Command => ({ tr }) => {
|
|
const { selection } = tr
|
|
const node = this.type.create(options)
|
|
|
|
tr.replaceRangeWith(selection.from, selection.to, node)
|
|
|
|
return true
|
|
},
|
|
}
|
|
},
|
|
|
|
addInputRules() {
|
|
return [
|
|
nodeInputRule(inputRegex, this.type, match => {
|
|
const [, alt, src, title] = match
|
|
|
|
return { src, alt, title }
|
|
}),
|
|
]
|
|
},
|
|
})
|
|
|
|
export default Image
|
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
interface AllExtensions {
|
|
Image: typeof Image,
|
|
}
|
|
}
|