frappe-ui 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/experimental/Calendar/CalendarEventRow.vue +0 -4
  2. package/experimental/Calendar/calendarUtils.ts +33 -21
  3. package/experimental/CommandPalette/CommandPaletteItem.vue +9 -1
  4. package/experimental/MultiEmailInput/MultiEmailInput.vue +64 -12
  5. package/experimental/MultiEmailInput/types.ts +12 -3
  6. package/experimental/SpriteIcons/IconPicker.vue +2 -1
  7. package/experimental/Stepper/StepIndicator.vue +129 -0
  8. package/experimental/Stepper/Stepper.api.md +117 -0
  9. package/experimental/Stepper/Stepper.md +95 -0
  10. package/experimental/Stepper/Stepper.vue +82 -0
  11. package/experimental/Stepper/StepperHorizontal.vue +93 -0
  12. package/experimental/Stepper/StepperVertical.vue +291 -0
  13. package/experimental/Stepper/index.ts +11 -0
  14. package/experimental/Stepper/resolve.ts +87 -0
  15. package/experimental/Stepper/types.ts +89 -0
  16. package/experimental.ts +2 -21
  17. package/package.json +1 -3
  18. package/src/charts/CalendarHeatmapChart.vue +353 -0
  19. package/src/charts/DonutChart.vue +34 -58
  20. package/src/charts/HeatmapChart.vue +26 -64
  21. package/src/charts/NumberCard.vue +62 -7
  22. package/src/charts/PercentageBarChart.vue +243 -0
  23. package/src/charts/SankeyChart.vue +14 -11
  24. package/src/charts/ScatterChart.vue +24 -20
  25. package/src/charts/axisChartCommon.ts +182 -18
  26. package/src/charts/axisFormat.ts +8 -3
  27. package/src/charts/calendarHeatmapOptions.ts +604 -0
  28. package/src/charts/colorMath.ts +26 -0
  29. package/src/charts/components/ChartContainer.vue +63 -53
  30. package/src/charts/components/ChartLegend.vue +216 -24
  31. package/src/charts/components/ChartScale.vue +85 -0
  32. package/src/charts/core/dataLabelFit.ts +45 -6
  33. package/src/charts/core/useAxisChart.ts +36 -19
  34. package/src/charts/core/useChart.ts +6 -1
  35. package/src/charts/core/usePlotKeyboard.ts +13 -2
  36. package/src/charts/docs/BarChart.md +11 -3
  37. package/src/charts/docs/CalendarHeatmapChart.api.md +165 -0
  38. package/src/charts/docs/CalendarHeatmapChart.md +33 -0
  39. package/src/charts/docs/ChartContainer.api.md +6 -0
  40. package/src/charts/docs/ChartLegend.api.md +1 -1
  41. package/src/charts/docs/ChartLegend.md +25 -3
  42. package/src/charts/docs/DonutChart.api.md +12 -0
  43. package/src/charts/docs/DonutChart.md +17 -0
  44. package/src/charts/docs/HeatmapChart.api.md +1 -1
  45. package/src/charts/docs/LineChart.md +5 -3
  46. package/src/charts/docs/NumberCard.api.md +6 -0
  47. package/src/charts/docs/NumberCard.md +6 -0
  48. package/src/charts/docs/Overview.md +13 -6
  49. package/src/charts/docs/PercentageBarChart.api.md +148 -0
  50. package/src/charts/docs/PercentageBarChart.md +42 -0
  51. package/src/charts/docs/ScatterChart.md +2 -3
  52. package/src/charts/donutChartOptions.ts +22 -130
  53. package/src/charts/format.ts +4 -1
  54. package/src/charts/heatmapOptions.ts +17 -12
  55. package/src/charts/hiddenSeries.ts +8 -3
  56. package/src/charts/index.ts +12 -0
  57. package/src/charts/legendOverflow.ts +40 -0
  58. package/src/charts/numberCardFormat.ts +19 -13
  59. package/src/charts/referenceLines.ts +2 -1
  60. package/src/charts/scatterOptions.ts +16 -3
  61. package/src/charts/seriesData.ts +8 -3
  62. package/src/charts/slices.ts +228 -0
  63. package/src/charts/tokens.ts +99 -0
  64. package/src/charts/types.ts +318 -9
  65. package/src/components/Avatar/Avatar.md +27 -2
  66. package/src/components/Avatar/Avatar.vue +29 -12
  67. package/src/components/Breadcrumbs/Breadcrumbs.vue +2 -1
  68. package/src/components/Combobox/Combobox.vue +9 -2
  69. package/src/components/Combobox/ComboboxResults.vue +6 -1
  70. package/src/components/Combobox/utils.ts +2 -1
  71. package/src/components/DatePicker/DatePicker.md +7 -0
  72. package/src/components/DatePicker/DatePicker.vue +6 -0
  73. package/src/components/DatePicker/DateTimePicker.vue +6 -0
  74. package/src/components/DatePicker/composables.ts +18 -1
  75. package/src/components/DatePicker/utils.ts +53 -0
  76. package/src/components/Dialog/Dialog.md +3 -1
  77. package/src/components/Duration/Duration.api.md +16 -3
  78. package/src/components/Duration/Duration.md +21 -0
  79. package/src/components/Duration/Duration.vue +29 -8
  80. package/src/components/Duration/duration.ts +105 -44
  81. package/src/components/Duration/index.ts +1 -0
  82. package/src/components/Duration/types.ts +30 -1
  83. package/src/components/HoverCard/HoverCard.vue +21 -5
  84. package/src/components/Menu/utils.ts +3 -2
  85. package/src/components/MobileNav/MobileNavItem.vue +3 -22
  86. package/src/components/MultiSelect/MultiSelect.api.md +5 -0
  87. package/src/components/MultiSelect/MultiSelect.vue +34 -0
  88. package/src/components/MultiSelect/MultiSelectResults.vue +3 -4
  89. package/src/components/MultiSelect/types.ts +6 -0
  90. package/src/components/Select/Select.vue +4 -4
  91. package/src/components/Select/utils.ts +1 -0
  92. package/src/components/SettingsDialog/SettingsDialog.api.md +7 -2
  93. package/src/components/SettingsDialog/SettingsDialog.md +2 -1
  94. package/src/components/SettingsDialog/SettingsRow.vue +12 -2
  95. package/src/components/Sidebar/SidebarItem.vue +3 -33
  96. package/src/components/SidebarRail/SidebarRailItem.vue +4 -17
  97. package/src/components/Switch/Switch.vue +3 -3
  98. package/src/components/Tooltip/Tooltip.vue +8 -0
  99. package/src/components/shared/deferredTrigger.ts +174 -0
  100. package/src/components/shared/navLink.ts +45 -0
  101. package/src/components/shared/selection/utils.ts +8 -1
  102. package/src/data-fetching/useCall/useCall.ts +25 -26
  103. package/src/data-fetching/useFrappeFetch.ts +48 -2
  104. package/src/data-fetching/useList/useList.ts +11 -3
  105. package/src/data-fetching/utils.ts +15 -1
  106. package/src/molecules/editor/components/MediaNodeView.vue +10 -1
  107. package/src/molecules/editor/components/MediaResizeGuide.vue +71 -0
  108. package/src/molecules/editor/components/media-node-view-utils.ts +7 -0
  109. package/src/molecules/editor/composables/useNodeViewResize.ts +142 -10
  110. package/src/molecules/editor/extensions/attachment/attachment-engine.ts +1 -1
  111. package/src/molecules/editor/extensions/content-paste/paste-html-utils.ts +16 -6
  112. package/src/molecules/editor/extensions/iframe/IframeNodeView.vue +29 -10
  113. package/src/molecules/editor/extensions/iframe/iframe-allowlist.ts +35 -16
  114. package/src/molecules/editor/extensions/iframe/iframe-embed-utils.ts +1 -1
  115. package/src/molecules/editor/extensions/iframe/iframe-extension.ts +30 -30
  116. package/src/molecules/editor/extensions/link/LinkEditorPopup.vue +1 -1
  117. package/src/molecules/editor/extensions/link/link-paste-plugin.ts +1 -1
  118. package/src/molecules/editor/extensions/shared/media-upload-engine.ts +1 -1
  119. package/src/molecules/editor/useEditor.ts +12 -6
  120. package/src/molecules/list/ListRow.vue +8 -1
  121. package/src/utils/dialog.ts +57 -16
  122. package/src/{molecules/editor/extensions/shared → utils}/url-safety.ts +28 -16
  123. package/tailwind/content.js +1 -1
  124. package/tailwind/plugin.js +22 -0
  125. package/vite/README.md +5 -3
  126. package/vite/codeLanguages.js +41 -17
  127. package/vite/doctypeInterfaceGenerator.js +52 -39
  128. package/vitepress/index.node.ts +0 -2
  129. package/experimental/TextEditor/TextEditor.api.md +0 -161
  130. package/experimental/TextEditor/TextEditor.vue +0 -298
  131. package/experimental/TextEditor/classIcon.ts +0 -21
  132. package/experimental/TextEditor/commands.js +0 -344
  133. package/experimental/TextEditor/components/CodeBlockComponent.vue +0 -103
  134. package/experimental/TextEditor/components/FontColor.vue +0 -153
  135. package/experimental/TextEditor/components/ImageViewerModal.vue +0 -463
  136. package/experimental/TextEditor/components/InsertImage.vue +0 -54
  137. package/experimental/TextEditor/components/InsertLink.vue +0 -13
  138. package/experimental/TextEditor/components/InsertVideo.vue +0 -14
  139. package/experimental/TextEditor/components/LinkPopup.vue +0 -99
  140. package/experimental/TextEditor/components/MediaNodeView.vue +0 -398
  141. package/experimental/TextEditor/components/MentionList.vue +0 -95
  142. package/experimental/TextEditor/components/TextEditorBubbleMenu.vue +0 -78
  143. package/experimental/TextEditor/components/TextEditorFixedMenu.vue +0 -78
  144. package/experimental/TextEditor/components/TextEditorFloatingMenu.vue +0 -61
  145. package/experimental/TextEditor/components/TextEditorMenu.vue +0 -218
  146. package/experimental/TextEditor/components/index.js +0 -0
  147. package/experimental/TextEditor/extensions/code-block.ts +0 -119
  148. package/experimental/TextEditor/extensions/color/color-extension.ts +0 -140
  149. package/experimental/TextEditor/extensions/color/color-styles.css +0 -25
  150. package/experimental/TextEditor/extensions/color/index.ts +0 -2
  151. package/experimental/TextEditor/extensions/content-paste-extension.ts +0 -160
  152. package/experimental/TextEditor/extensions/copy-styles.ts +0 -160
  153. package/experimental/TextEditor/extensions/emoji/EmojiList.vue +0 -55
  154. package/experimental/TextEditor/extensions/emoji/emoji-extension.ts +0 -55
  155. package/experimental/TextEditor/extensions/emoji/emojis.json +0 -7422
  156. package/experimental/TextEditor/extensions/heading/heading.ts +0 -20
  157. package/experimental/TextEditor/extensions/highlight/highlight-extension.ts +0 -241
  158. package/experimental/TextEditor/extensions/highlight/highlight-styles.css +0 -31
  159. package/experimental/TextEditor/extensions/highlight/index.ts +0 -2
  160. package/experimental/TextEditor/extensions/iframe/IframeNodeView.vue +0 -308
  161. package/experimental/TextEditor/extensions/iframe/InsertIframe.vue +0 -209
  162. package/experimental/TextEditor/extensions/iframe/iframe-extension.ts +0 -404
  163. package/experimental/TextEditor/extensions/iframe/index.ts +0 -11
  164. package/experimental/TextEditor/extensions/iframe/utils.ts +0 -283
  165. package/experimental/TextEditor/extensions/image/image-extension.ts +0 -661
  166. package/experimental/TextEditor/extensions/image/index.ts +0 -5
  167. package/experimental/TextEditor/extensions/image-group/ImageGroupNodeView.vue +0 -182
  168. package/experimental/TextEditor/extensions/image-group/ImageGroupUploadDialog.vue +0 -740
  169. package/experimental/TextEditor/extensions/image-group/image-group-extension.ts +0 -187
  170. package/experimental/TextEditor/extensions/image-viewer-extension.ts +0 -162
  171. package/experimental/TextEditor/extensions/link/index.ts +0 -1
  172. package/experimental/TextEditor/extensions/link/link-extension.ts +0 -323
  173. package/experimental/TextEditor/extensions/link/linkPasteHandler.ts +0 -51
  174. package/experimental/TextEditor/extensions/mention/index.ts +0 -1
  175. package/experimental/TextEditor/extensions/mention/mention-extension.ts +0 -204
  176. package/experimental/TextEditor/extensions/mention/style.css +0 -11
  177. package/experimental/TextEditor/extensions/shared/color-utils.ts +0 -236
  178. package/experimental/TextEditor/extensions/slash-commands/SlashCommandsList.vue +0 -59
  179. package/experimental/TextEditor/extensions/slash-commands/slash-commands-extension.ts +0 -184
  180. package/experimental/TextEditor/extensions/suggestion/SuggestionList.vue +0 -130
  181. package/experimental/TextEditor/extensions/suggestion/createSuggestionExtension.ts +0 -151
  182. package/experimental/TextEditor/extensions/suggestion/index.ts +0 -5
  183. package/experimental/TextEditor/extensions/tag/index.ts +0 -1
  184. package/experimental/TextEditor/extensions/tag/tag-extension.ts +0 -170
  185. package/experimental/TextEditor/extensions/toc-node/TocNodeView.vue +0 -439
  186. package/experimental/TextEditor/extensions/toc-node/index.ts +0 -3
  187. package/experimental/TextEditor/extensions/toc-node/toc-node-extension.ts +0 -167
  188. package/experimental/TextEditor/extensions/video-extension.ts +0 -653
  189. package/experimental/TextEditor/hljs-github.css +0 -197
  190. package/experimental/TextEditor/index.ts +0 -21
  191. package/experimental/TextEditor/style.css +0 -190
  192. package/experimental/TextEditor/types.ts +0 -72
  193. package/experimental/TextEditor/utils.js +0 -11
  194. package/src/utils/url-validation.ts +0 -39
@@ -1,740 +0,0 @@
1
- <template>
2
- <Dialog
3
- v-model="modelValue"
4
- :title="props.mode === 'edit' ? 'Edit Images' : 'Upload Images'"
5
- size="3xl"
6
- @close="$emit('close')"
7
- :dismissible="false"
8
- >
9
- <template #default>
10
- <div class="space-y-2">
11
- <div class="flex items-center gap-2">
12
- <Button @click="triggerFileInput">
13
- <template #prefix>
14
- <LucideImagePlus class="size-4" />
15
- </template>
16
- Add images
17
- </Button>
18
- <Select
19
- id="columns-select"
20
- :options="selectOptions"
21
- v-model="internalColumns"
22
- size="sm"
23
- variant="subtle"
24
- class="w-28"
25
- @update:modelValue="(val) => (columns = +val)"
26
- />
27
- </div>
28
- <div
29
- v-if="images && images.length"
30
- class="grid gap-px mb-4"
31
- :style="gridStyle"
32
- >
33
- <div
34
- v-for="(item, idx) in images"
35
- :key="item.id"
36
- class="relative aspect-square w-full h-full overflow-hidden group bg-surface-base"
37
- :draggable="true"
38
- @dragstart="onDragStart(idx)"
39
- @dragover="onDragOver($event, idx)"
40
- @drop="onDrop($event)"
41
- @dragend="onDragEnd"
42
- @dragleave="onDragLeave($event, idx)"
43
- :class="{ 'ring-2 ring-primary-400 z-10': isDropTarget(idx) }"
44
- >
45
- <button
46
- type="button"
47
- class="absolute top-1 right-1 z-10 bg-white/80 hover:bg-white rounded-full p-1 shadow transition-opacity opacity-0 group-hover:opacity-100 [@media(hover:none)]:opacity-100 focus:opacity-100"
48
- aria-label="Remove image"
49
- @click.stop="removeImage(idx)"
50
- >
51
- <LucideX class="w-4 h-4 text-gray-700" />
52
- </button>
53
-
54
- <!-- Existing images from edit mode -->
55
- <template v-if="item.type === 'existing'">
56
- <img
57
- :src="item.existing?.src"
58
- :alt="item.existing?.alt || ''"
59
- class="object-cover w-full h-full rounded-[2px]"
60
- />
61
- <!-- Caption overlay -->
62
- <div
63
- class="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/60 to-transparent rounded-b-[2px] transition-opacity"
64
- :class="
65
- editingCaption === `${item.type}-${idx}`
66
- ? 'opacity-100'
67
- : 'opacity-0 group-hover:opacity-100 [@media(hover:none)]:opacity-100'
68
- "
69
- >
70
- <div
71
- v-if="editingCaption !== `${item.type}-${idx}`"
72
- class="p-2 cursor-pointer"
73
- @click.stop="
74
- startEditingCaption(
75
- `${item.type}-${idx}`,
76
- item.existing?.alt || '',
77
- )
78
- "
79
- >
80
- <div
81
- class="text-white text-xs truncate"
82
- :title="item.existing?.alt || 'Click to add caption'"
83
- >
84
- {{ item.existing?.alt || 'Add caption...' }}
85
- </div>
86
- </div>
87
- <div v-else class="p-2" @click.stop>
88
- <input
89
- :ref="(el) => (captionInputRef = el as HTMLInputElement)"
90
- v-model="captionEditValue"
91
- @blur="handleCaptionBlur(`${item.type}-${idx}`, idx)"
92
- @keydown.enter.prevent="
93
- saveCaption(`${item.type}-${idx}`, idx)
94
- "
95
- @keydown.escape="cancelEditingCaption"
96
- class="w-full text-xs bg-white/90 text-gray-900 px-1 py-0.5 rounded-1 border-none outline-none"
97
- placeholder="Add caption..."
98
- maxlength="200"
99
- />
100
- </div>
101
- </div>
102
- </template>
103
-
104
- <!-- New file uploads -->
105
- <template v-else-if="item.type === 'file' && item.file">
106
- <template v-if="!isImageSupported(item.file)">
107
- <div
108
- class="flex flex-col items-center justify-center w-full h-full text-ink-gray-4 bg-surface-gray-1 rounded-[2px]"
109
- >
110
- <span
111
- class="text-p-xs text-ink-gray-4 w-full text-center px-2 mt-1"
112
- :title="item.file.name"
113
- >
114
- {{ item.file.name }}
115
- </span>
116
- </div>
117
- </template>
118
- <template v-else-if="item.file.type.startsWith('image/')">
119
- <img
120
- :src="filePreview(item.file)"
121
- class="object-cover w-full h-full rounded-[2px]"
122
- :alt="item.alt || ''"
123
- />
124
- <!-- Caption overlay -->
125
- <div
126
- class="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/60 to-transparent rounded-b-[2px] transition-opacity"
127
- :class="
128
- editingCaption === `${item.type}-${idx}`
129
- ? 'opacity-100'
130
- : 'opacity-0 group-hover:opacity-100 [@media(hover:none)]:opacity-100'
131
- "
132
- >
133
- <div
134
- v-if="editingCaption !== `${item.type}-${idx}`"
135
- class="p-2 cursor-pointer"
136
- @click.stop="
137
- startEditingCaption(`${item.type}-${idx}`, item.alt || '')
138
- "
139
- >
140
- <div
141
- class="text-white text-xs truncate"
142
- :title="item.alt || 'Click to add caption'"
143
- >
144
- {{ item.alt || 'Add caption...' }}
145
- </div>
146
- </div>
147
- <div v-else class="p-2" @click.stop>
148
- <input
149
- ref="captionInput"
150
- v-model="captionEditValue"
151
- @blur="handleCaptionBlur(`${item.type}-${idx}`, idx)"
152
- @keydown.enter.prevent="
153
- saveCaption(`${item.type}-${idx}`, idx)
154
- "
155
- @keydown.escape="cancelEditingCaption"
156
- class="w-full text-xs bg-white/90 text-gray-900 px-1 py-0.5 rounded-4 border-none outline-none"
157
- placeholder="Add caption..."
158
- maxlength="200"
159
- />
160
- </div>
161
- </div>
162
- </template>
163
- </template>
164
- </div>
165
- </div>
166
- <div v-if="images && images.length" class="text-p-sm text-ink-gray-5">
167
- Upload more images by dropping them anywhere in this window. Reorder
168
- images by dragging them. Hover over an image to edit caption.
169
- </div>
170
- <div
171
- v-else
172
- class="flex flex-col items-center justify-center min-h-[200px]"
173
- >
174
- <div
175
- class="w-full flex flex-1 flex-col items-center justify-center border border-outline-gray-2 rounded-6 bg-surface-gray-1 h-full cursor-pointer transition hover:border-primary-400 hover:bg-primary-50 text-center"
176
- @click="triggerFileInput"
177
- @dragover.prevent
178
- @drop.prevent="onDrop"
179
- >
180
- <div class="text-ink-gray-4 mb-2">
181
- <LucideImagePlus class="size-6" />
182
- </div>
183
- <div class="text-ink-gray-5 text-sm-medium">
184
- Drag & drop images here or click to select
185
- </div>
186
- </div>
187
- </div>
188
- <div v-if="uploading">
189
- <div class="mb-2 text-sm">
190
- Uploading: {{ uploadedCount }}/{{ totalCount }}
191
- </div>
192
- <div class="w-full bg-gray-200 rounded-4 h-2 overflow-hidden">
193
- <div
194
- class="bg-surface-gray-8 h-2 transition-all"
195
- :style="{ width: uploadProgress + '%' }"
196
- ></div>
197
- </div>
198
- <div
199
- v-if="uploadErrors && uploadErrors.some((e) => e)"
200
- class="mt-2 text-red-500 text-xs"
201
- >
202
- Some files failed to upload.
203
- </div>
204
- </div>
205
- </div>
206
- </template>
207
- <template #actions>
208
- <div class="flex justify-end gap-2">
209
- <Button variant="ghost" :disabled="uploading" @click="handleCancel">
210
- Cancel
211
- </Button>
212
- <Button
213
- v-if="props.mode === 'edit'"
214
- variant="solid"
215
- :loading="uploading"
216
- @click="handleSave"
217
- >
218
- Save
219
- </Button>
220
- <Button
221
- v-if="props.mode === 'new'"
222
- variant="solid"
223
- :loading="uploading"
224
- @click="handleUpload"
225
- >
226
- Upload
227
- </Button>
228
- </div>
229
- </template>
230
- </Dialog>
231
- <Teleport :to="portalTarget ?? 'body'">
232
- <Transition
233
- name="fade"
234
- enter-active-class="transition-opacity duration-200"
235
- leave-active-class="transition-opacity duration-200"
236
- enter-from-class="opacity-0"
237
- enter-to-class="opacity-100"
238
- leave-from-class="opacity-100"
239
- leave-to-class="opacity-0"
240
- >
241
- <div
242
- v-if="isFileDragging"
243
- class="fixed inset-0 z-50 bg-gray-900/60 pointer-events-none flex items-center justify-center"
244
- >
245
- <div class="text-ink-gray-1 text-base-medium">
246
- Drop images anywhere
247
- </div>
248
- </div>
249
- </Transition>
250
- </Teleport>
251
- </template>
252
-
253
- <script setup lang="ts">
254
- // @ts-nocheck -- frozen v0 code, kept out of type-check (see tsconfig.app.json)
255
- import { usePortalTarget } from '#composables/usePortalTarget'
256
- import {
257
- computed,
258
- ref,
259
- onMounted,
260
- onUnmounted,
261
- watch,
262
- nextTick,
263
- useTemplateRef,
264
- } from 'vue'
265
- import Dialog from '#components/Dialog/Dialog.vue'
266
- import Button from '#components/Button/Button.vue'
267
- import Select from '#components/Select/Select.vue'
268
- import type { Editor } from '@tiptap/vue-3'
269
- import type { UploadedFile } from '#utils/useFileUpload'
270
- import { classIcon } from '../../classIcon'
271
- const LucideX = classIcon('lucide-x')
272
- const LucideImagePlus = classIcon('lucide-image-plus')
273
- interface UploadResult {
274
- success: boolean
275
- file?: UploadedFile
276
- error?: Error
277
- }
278
-
279
- interface ExistingImage {
280
- src: string
281
- alt: string
282
- }
283
-
284
- interface ImageItem {
285
- type: 'file' | 'existing'
286
- file?: File
287
- existing?: ExistingImage
288
- alt?: string
289
- id: string
290
- }
291
-
292
- const props = withDefaults(
293
- defineProps<{
294
- modelValue: boolean
295
- files: File[]
296
- editor: Editor
297
- mode: 'new' | 'edit'
298
- existingImages?: ExistingImage[]
299
- initialColumns?: number
300
- }>(),
301
- {
302
- mode: 'new',
303
- },
304
- )
305
-
306
- const portalTarget = usePortalTarget()
307
-
308
- const emit = defineEmits(['update:modelValue', 'close', 'update:files', 'save'])
309
-
310
- const modelValue = computed({
311
- get: () => props.modelValue,
312
- set: (val) => emit('update:modelValue', val),
313
- })
314
-
315
- const isFileDragging = ref(false)
316
- const draggedIndex = ref<number | null>(null)
317
- const overIndex = ref<number | null>(null)
318
- const columnsInitialized = ref(false)
319
-
320
- // Caption editing state
321
- const editingCaption = ref<string | null>(null)
322
- const captionEditValue = ref('')
323
- const captionInputRef = ref<HTMLInputElement | null>(null)
324
-
325
- const uploading = ref(false)
326
- const columns = ref(4)
327
- const uploadProgress = ref(0)
328
- const uploadedCount = ref(0)
329
- const totalCount = ref(0)
330
- const uploadErrors = ref<(Error | null)[]>([])
331
- const uploadResults = ref<UploadResult[]>([])
332
-
333
- // Unified images array handling both files and existing images
334
- const images = ref<ImageItem[]>([])
335
-
336
- // Initialize images when modal opens
337
- watch(
338
- () => props.modelValue,
339
- (isOpen) => {
340
- if (isOpen && props.mode === 'edit') {
341
- const existingItems = (props.existingImages || []).map(
342
- createExistingImageItem,
343
- )
344
- const fileItems = (props.files || []).map(createImageItem)
345
- images.value = [...existingItems, ...fileItems]
346
-
347
- if (props.initialColumns) {
348
- columns.value = props.initialColumns
349
- columnsInitialized.value = true
350
- }
351
- } else if (isOpen && props.mode === 'new') {
352
- images.value = (props.files || []).map(createImageItem)
353
- columns.value = getDefaultColumns(images.value.length)
354
- columnsInitialized.value = true
355
- }
356
- },
357
- { immediate: true },
358
- )
359
-
360
- watch(
361
- () => props.existingImages,
362
- (newExistingImages) => {
363
- if (props.modelValue && props.mode === 'edit') {
364
- const existingItems = (newExistingImages || []).map(
365
- createExistingImageItem,
366
- )
367
- const fileItems = (props.files || []).map(createImageItem)
368
- images.value = [...existingItems, ...fileItems]
369
- }
370
- },
371
- { deep: true },
372
- )
373
-
374
- watch(
375
- () => props.files,
376
- (newFiles) => {
377
- if (props.mode === 'new') {
378
- images.value = (newFiles || []).map(createImageItem)
379
- }
380
- },
381
- { deep: true },
382
- )
383
-
384
- onMounted(() => {
385
- window.addEventListener('dragover', onDragOverWindow)
386
- window.addEventListener('drop', onDrop)
387
- window.addEventListener('dragleave', onDragLeaveWindow)
388
- })
389
-
390
- onUnmounted(() => {
391
- window.removeEventListener('dragover', onDragOverWindow)
392
- window.removeEventListener('drop', onDrop)
393
- window.removeEventListener('dragleave', onDragLeaveWindow)
394
- })
395
-
396
- async function handleSave() {
397
- // Upload new files and combine with existing images
398
- const newFiles = images.value
399
- .filter((item) => item.type === 'file' && item.file)
400
- .map((item) => item.file!)
401
-
402
- const existingImages = images.value
403
- .filter((item) => item.type === 'existing' && item.existing)
404
- .map((item) => item.existing!)
405
-
406
- let uploadResults: UploadResult[] = []
407
-
408
- if (newFiles.length > 0) {
409
- uploadResults = await uploadFiles(newFiles)
410
- }
411
-
412
- // Map files to their upload results for reliable lookup
413
- const fileToUploadMap = new Map<File, ExistingImage>()
414
- newFiles.forEach((file, index) => {
415
- const result = uploadResults[index]
416
- if (result?.success && result.file) {
417
- fileToUploadMap.set(file, {
418
- src: result.file.file_url || '',
419
- alt: result.file.file_name || '',
420
- })
421
- }
422
- })
423
-
424
- // Build final images maintaining UI order
425
- const finalImages: ExistingImage[] = []
426
- for (const item of images.value) {
427
- if (item.type === 'existing' && item.existing) {
428
- finalImages.push(item.existing)
429
- } else if (item.type === 'file' && item.file) {
430
- const uploadedImage = fileToUploadMap.get(item.file)
431
- if (uploadedImage) {
432
- finalImages.push(uploadedImage)
433
- }
434
- }
435
- }
436
-
437
- emit('save', {
438
- images: finalImages,
439
- columns: columns.value,
440
- })
441
-
442
- uploading.value = false
443
- modelValue.value = false
444
- }
445
-
446
- async function handleUpload() {
447
- if (!props.files || props.files.length === 0) return
448
-
449
- const results = await uploadFiles(props.files)
450
-
451
- const uploadedImages = results
452
- .filter((result) => result.success && result.file)
453
- .map((result) => ({
454
- src: result.file?.file_url || '',
455
- alt: result.file?.file_name || '',
456
- }))
457
-
458
- if (uploadedImages.length > 1) {
459
- props.editor
460
- .chain()
461
- .focus()
462
- .setImageGroup({ images: uploadedImages, columns: columns.value })
463
- .run()
464
- }
465
-
466
- uploading.value = false
467
- modelValue.value = false
468
- emit('close')
469
- }
470
-
471
- function handleCancel() {
472
- modelValue.value = false
473
- emit('close')
474
- }
475
-
476
- function removeImage(idx: number) {
477
- images.value.splice(idx, 1)
478
-
479
- if (props.mode === 'new') {
480
- const newFiles = images.value
481
- .filter((item) => item.type === 'file' && item.file)
482
- .map((item) => item.file!)
483
- emit('update:files', newFiles)
484
- }
485
- }
486
-
487
- function triggerFileInput() {
488
- const fileInput = document.createElement('input')
489
- fileInput.type = 'file'
490
- fileInput.accept = 'image/*'
491
- fileInput.multiple = true
492
- fileInput.onchange = (e) => {
493
- const files = (e.target as HTMLInputElement).files
494
- if (files) {
495
- addFiles(Array.from(files))
496
- }
497
- }
498
- fileInput.click()
499
- }
500
-
501
- function addFiles(files: File[]) {
502
- const imageFiles = files.filter((file) => file.type.startsWith('image/'))
503
- const newImageItems = imageFiles.map(createImageItem)
504
-
505
- const existingFileSignatures = images.value
506
- .filter((item) => item.type === 'file' && item.file)
507
- .map((item) => `${item.file!.name}-${item.file!.size}`)
508
-
509
- const uniqueNewItems = newImageItems.filter(
510
- (item) =>
511
- !existingFileSignatures.includes(`${item.file!.name}-${item.file!.size}`),
512
- )
513
-
514
- images.value.push(...uniqueNewItems)
515
-
516
- if (props.mode === 'new') {
517
- const newFiles = images.value
518
- .filter((item) => item.type === 'file' && item.file)
519
- .map((item) => item.file!)
520
- emit('update:files', newFiles)
521
- }
522
- }
523
-
524
- function onDrop(e: DragEvent) {
525
- e.preventDefault()
526
- e.stopPropagation()
527
- const droppedFiles = Array.from(e.dataTransfer?.files || [])
528
- if (droppedFiles.length) {
529
- addFiles(droppedFiles)
530
- } else if (
531
- draggedIndex.value !== null &&
532
- overIndex.value !== null &&
533
- draggedIndex.value !== overIndex.value
534
- ) {
535
- // Handle image reordering
536
- const [moved] = images.value.splice(draggedIndex.value, 1)
537
- images.value.splice(overIndex.value, 0, moved)
538
-
539
- if (props.mode === 'new') {
540
- const newFiles = images.value
541
- .filter((item) => item.type === 'file' && item.file)
542
- .map((item) => item.file!)
543
- emit('update:files', newFiles)
544
- }
545
- }
546
- isFileDragging.value = false
547
- draggedIndex.value = null
548
- overIndex.value = null
549
- }
550
-
551
- function createImageItem(file: File): ImageItem {
552
- return {
553
- type: 'file',
554
- file,
555
- id: `file-${file.name}-${file.size}-${Date.now()}`,
556
- }
557
- }
558
-
559
- function createExistingImageItem(existing: ExistingImage): ImageItem {
560
- return {
561
- type: 'existing',
562
- existing,
563
- id: `existing-${existing.src}-${Date.now()}`,
564
- }
565
- }
566
-
567
- function getDefaultColumns(count: number) {
568
- if (count <= 4) return count
569
- if (count % 4 === 0) return 4
570
- if (count % 3 === 0) return 3
571
- if (count > 10) return 4
572
- return 3
573
- }
574
-
575
- function getUploadFunction() {
576
- const imageGroupExtension = props.editor.extensionManager.extensions.find(
577
- (ext) => ext.name === 'imageGroup',
578
- )
579
- return imageGroupExtension?.options?.uploadFunction
580
- }
581
-
582
- function filePreview(file: File) {
583
- const url = URL.createObjectURL(file)
584
-
585
- onUnmounted(() => {
586
- URL.revokeObjectURL(url)
587
- })
588
-
589
- return url
590
- }
591
-
592
- function isImageSupported(file: File): boolean {
593
- // HEIC/HEIF files can't be previewed in browsers
594
- const unsupportedTypes = ['image/heic', 'image/heif']
595
- const unsupportedExtensions = ['.heic', '.heif']
596
-
597
- const hasUnsupportedType = unsupportedTypes.includes(file.type)
598
- const hasUnsupportedExtension = unsupportedExtensions.some((ext) =>
599
- file.name?.toLowerCase().endsWith(ext),
600
- )
601
-
602
- return !hasUnsupportedType && !hasUnsupportedExtension
603
- }
604
-
605
- function onDragOver(e: DragEvent, idx: number) {
606
- e.preventDefault()
607
- overIndex.value = idx
608
- }
609
-
610
- function onDragStart(idx: number) {
611
- draggedIndex.value = idx
612
- }
613
-
614
- function onDragEnd() {
615
- draggedIndex.value = null
616
- overIndex.value = null
617
- }
618
-
619
- function onDragLeave(e: DragEvent, idx: number) {
620
- if (overIndex.value === idx) {
621
- overIndex.value = null
622
- }
623
- }
624
-
625
- function isDropTarget(idx: number) {
626
- return (
627
- overIndex.value === idx &&
628
- draggedIndex.value !== null &&
629
- draggedIndex.value !== idx
630
- )
631
- }
632
-
633
- function onDragOverWindow(e: DragEvent) {
634
- e.preventDefault()
635
- if (e.dataTransfer && Array.from(e.dataTransfer.types).includes('Files')) {
636
- isFileDragging.value = true
637
- }
638
- }
639
-
640
- function onDragLeaveWindow(e: DragEvent) {
641
- if (e.target === window || e.relatedTarget === null) {
642
- isFileDragging.value = false
643
- }
644
- }
645
-
646
- const gridStyle = computed(() => ({
647
- gridTemplateColumns: `repeat(${columns.value || 4}, minmax(0, 1fr))`,
648
- }))
649
-
650
- const internalColumns = computed({
651
- get: () => String(columns.value ?? 4),
652
- set: (val) => {
653
- columns.value = +val
654
- },
655
- })
656
-
657
- const selectOptions = [
658
- { label: '2 columns', value: '2' },
659
- { label: '3 columns', value: '3' },
660
- { label: '4 columns', value: '4' },
661
- ]
662
-
663
- function startEditingCaption(itemId: string, currentAlt: string) {
664
- editingCaption.value = itemId
665
- captionEditValue.value = currentAlt
666
-
667
- nextTick(() => {
668
- if (captionInputRef.value) {
669
- captionInputRef.value.focus()
670
- captionInputRef.value.select()
671
- }
672
- })
673
- }
674
-
675
- function saveCaption(itemId: string, itemIndex: number) {
676
- const [type, indexStr] = itemId.split('-')
677
- const newCaption = captionEditValue.value.trim()
678
-
679
- if (type === 'existing') {
680
- if (images.value[itemIndex]?.existing) {
681
- images.value[itemIndex].existing.alt = newCaption
682
- }
683
- } else if (type === 'file') {
684
- if (images.value[itemIndex]) {
685
- images.value[itemIndex].alt = newCaption
686
- }
687
- }
688
-
689
- editingCaption.value = null
690
- captionEditValue.value = ''
691
- }
692
-
693
- function cancelEditingCaption() {
694
- editingCaption.value = null
695
- captionEditValue.value = ''
696
- }
697
-
698
- function handleCaptionBlur(itemId: string, itemIndex: number) {
699
- if (editingCaption.value === itemId) {
700
- saveCaption(itemId, itemIndex)
701
- }
702
- }
703
-
704
- async function uploadFiles(files: File[]): Promise<UploadResult[]> {
705
- const uploadFunction = getUploadFunction()
706
- if (!uploadFunction) {
707
- console.error('uploadFunction not found')
708
- throw new Error('Upload function not found')
709
- }
710
-
711
- uploading.value = true
712
- totalCount.value = files.length
713
- uploadedCount.value = 0
714
- uploadProgress.value = 0
715
- uploadErrors.value = Array(files.length).fill(null)
716
- uploadResults.value = Array(files.length).fill(null)
717
-
718
- const uploadPromises = files.map(async (file, index) => {
719
- try {
720
- const result = await uploadFunction(file)
721
- uploadResults.value[index] = { success: true, file: result }
722
- return { success: true, file: result }
723
- } catch (error) {
724
- const err = error as Error
725
- uploadErrors.value[index] = err
726
- uploadResults.value[index] = { success: false, error: err }
727
- return { success: false, error: err }
728
- } finally {
729
- uploadedCount.value++
730
- uploadProgress.value = Math.round(
731
- (uploadedCount.value / totalCount.value) * 100,
732
- )
733
- }
734
- })
735
-
736
- // Execute all uploads in parallel
737
- const results = await Promise.all(uploadPromises)
738
- return results
739
- }
740
- </script>