2020-11-25 16:50:54 +08:00
|
|
|
import {
|
2022-06-08 20:10:25 +08:00
|
|
|
mergeAttributes,
|
2020-11-25 16:50:54 +08:00
|
|
|
Node,
|
|
|
|
nodeInputRule,
|
|
|
|
} from '@tiptap/core'
|
2020-10-27 22:32:55 +08:00
|
|
|
|
2020-10-30 23:57:55 +08:00
|
|
|
export interface ImageOptions {
|
|
|
|
inline: boolean,
|
2022-02-09 19:21:03 +08:00
|
|
|
allowBase64: boolean,
|
2021-04-21 15:43:31 +08:00
|
|
|
HTMLAttributes: Record<string, any>,
|
2020-10-30 23:57:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
2021-06-05 03:56:29 +08:00
|
|
|
interface Commands<ReturnType> {
|
2021-02-16 18:27:58 +08:00
|
|
|
image: {
|
|
|
|
/**
|
|
|
|
* Add an image
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
setImage: (options: { src: string, alt?: string, title?: string }) => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
}
|
2021-02-10 16:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:44:21 +08:00
|
|
|
export const inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/
|
2020-10-27 22:32:55 +08:00
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
export const Image = Node.create<ImageOptions>({
|
2020-10-27 22:32:55 +08:00
|
|
|
name: 'image',
|
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
inline: false,
|
2022-02-09 19:21:03 +08:00
|
|
|
allowBase64: false,
|
2021-10-27 00:31:13 +08:00
|
|
|
HTMLAttributes: {},
|
|
|
|
}
|
2020-10-30 23:57:55 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
inline() {
|
|
|
|
return this.options.inline
|
|
|
|
},
|
2020-10-27 22:32:55 +08:00
|
|
|
|
2020-10-30 23:57:55 +08:00
|
|
|
group() {
|
|
|
|
return this.options.inline ? 'inline' : 'block'
|
|
|
|
},
|
2020-10-27 22:32:55 +08:00
|
|
|
|
2020-10-28 04:38:29 +08:00
|
|
|
draggable: true,
|
|
|
|
|
2020-10-27 22:32:55 +08:00
|
|
|
addAttributes() {
|
|
|
|
return {
|
|
|
|
src: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
alt: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
2022-02-09 19:21:03 +08:00
|
|
|
tag: this.options.allowBase64
|
|
|
|
? 'img[src]'
|
|
|
|
: 'img[src]:not([src^="data:"])',
|
2020-10-27 22:32:55 +08:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2020-11-13 23:07:20 +08:00
|
|
|
renderHTML({ HTMLAttributes }) {
|
2020-11-25 16:50:54 +08:00
|
|
|
return ['img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]
|
2020-10-27 22:32:55 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-05-05 20:50:43 +08:00
|
|
|
setImage: options => ({ commands }) => {
|
|
|
|
return commands.insertContent({
|
|
|
|
type: this.name,
|
|
|
|
attrs: options,
|
|
|
|
})
|
2020-10-27 22:32:55 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addInputRules() {
|
|
|
|
return [
|
2021-10-08 21:02:09 +08:00
|
|
|
nodeInputRule({
|
|
|
|
find: inputRegex,
|
|
|
|
type: this.type,
|
|
|
|
getAttributes: match => {
|
2021-10-12 02:35:33 +08:00
|
|
|
const [,, alt, src, title] = match
|
2020-10-27 22:32:55 +08:00
|
|
|
|
2021-10-08 21:02:09 +08:00
|
|
|
return { src, alt, title }
|
|
|
|
},
|
2020-10-27 22:32:55 +08:00
|
|
|
}),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|