webapp/src/components/Editor/extensions/CustomImage.ts

53 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-05-07 13:47:10 +00:00
import { Image } from '@tiptap/extension-image'
2023-05-06 12:38:22 +00:00
declare module '@tiptap/core' {
interface Commands<ReturnType> {
2023-05-07 13:16:03 +00:00
customImage: {
/**
* Add an image
*/
setImage: (options: { src: string; alt?: string; title?: string }) => ReturnType
setImageFloat: (float: null | 'left' | 'right') => ReturnType
2023-05-06 12:38:22 +00:00
}
}
}
2023-05-07 13:47:10 +00:00
export const CustomImage = Image.extend({
2023-05-06 12:38:22 +00:00
addAttributes() {
return {
src: {
default: null,
2023-05-06 12:38:22 +00:00
},
alt: {
default: null,
2023-05-06 12:38:22 +00:00
},
width: {
default: null,
2023-05-06 12:38:22 +00:00
},
height: {
default: null,
2023-05-06 12:38:22 +00:00
},
'data-float': {
default: null,
},
2023-05-06 12:38:22 +00:00
}
},
addCommands() {
return {
2023-05-07 13:16:03 +00:00
setImage:
(options) =>
({ commands }) => {
return commands.insertContent({
type: this.name,
attrs: options,
2023-05-07 13:16:03 +00:00
})
},
setImageFloat:
2023-05-06 12:38:22 +00:00
(value) =>
({ commands }) => {
return commands.updateAttributes(this.name, { 'data-float': value })
},
2023-05-06 12:38:22 +00:00
}
},
2023-05-06 12:38:22 +00:00
})