-
Notifications
You must be signed in to change notification settings - Fork 31
/
image.ts
141 lines (132 loc) · 3.69 KB
/
image.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { GeneralOptions } from '@/type'
import type { ImageOptions as TiptapImageOptions } from '@tiptap/extension-image'
import type { Display, ImageAttrsOptions, ImageTab, ImageTabKey } from './components/image/types'
import { IMAGE_SIZE } from '@/constants/define'
import { Image as TiptapImage } from '@tiptap/extension-image'
import { VueNodeViewRenderer } from '@tiptap/vue-3'
import ImageDialog from './components/image/ImageDialog.vue'
import ImageView from './components/image/ImageView.vue'
import ImageActionButton from './components/ImageActionButton.vue'
/**
* Represents the type for the upload function, which takes a File parameter and returns a Promise of type string.
*/
type Upload = (file: File) => Promise<string>
/**
* Represents the interface for image options, extending TiptapImageOptions and GeneralOptions.
*/
export interface ImageOptions extends TiptapImageOptions, GeneralOptions<ImageOptions> {
/** Function for uploading images */
upload?: Upload
/** image default width */
width?: string | number
/** image default display */
display: Display
/** List of image tabs */
imageTabs: ImageTab[]
/** List of hidden image tab keys */
hiddenTabs: ImageTabKey[]
/** Component for the image dialog */
dialogComponent: any
}
/**
* Represents the interface for options to set image attributes, extending ImageAttrsOptions and including the src property.
*/
interface SetImageAttrsOptions extends ImageAttrsOptions {
/** The source URL of the image. */
src: string
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
imageResize: {
/**
* Add an image
*/
setImage: (options: Partial<SetImageAttrsOptions>) => ReturnType
/**
* Update an image
*/
updateImage: (options: Partial<SetImageAttrsOptions>) => ReturnType
}
}
}
export const Image = /* @__PURE__*/ TiptapImage.extend<ImageOptions>({
addAttributes() {
return {
...this.parent?.(),
src: {
default: null
},
alt: {
default: null
},
lockAspectRatio: {
default: true
},
width: {
default: this.options.width
},
height: {
default: null
},
display: {
default: this.options.display,
renderHTML: ({ display }) => {
if (!display) {
return {}
}
return {
'data-display': display
}
},
parseHTML: element => {
const display = element.getAttribute('data-display')
return display || 'inline'
}
}
}
},
addNodeView() {
return VueNodeViewRenderer(ImageView as any)
},
addCommands() {
return {
...this.parent?.(),
updateImage:
options =>
({ commands }) => {
return commands.updateAttributes(this.name, options)
}
}
},
addOptions() {
return {
...this.parent?.(),
upload: undefined,
width: IMAGE_SIZE['size-large'],
display: 'inline',
imageTabs: [],
hiddenTabs: [],
inline: true,
dialogComponent: () => ImageDialog,
button: ({ editor, extension, t }) => {
const { upload, imageTabs, hiddenTabs, dialogComponent } = extension.options
return {
component: ImageActionButton,
componentProps: {
editor,
upload,
imageTabs,
hiddenTabs,
isActive: () => editor.isActive('image') || false,
disabled: !editor.can().setImage({}),
icon: 'image',
tooltip: t('editor.image.tooltip')
},
componentSlots: {
dialog: dialogComponent()
}
}
}
}
}
})