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.
- package/experimental/Calendar/CalendarEventRow.vue +0 -4
- package/experimental/Calendar/calendarUtils.ts +33 -21
- package/experimental/CommandPalette/CommandPaletteItem.vue +9 -1
- package/experimental/MultiEmailInput/MultiEmailInput.vue +64 -12
- package/experimental/MultiEmailInput/types.ts +12 -3
- package/experimental/SpriteIcons/IconPicker.vue +2 -1
- package/experimental/Stepper/StepIndicator.vue +129 -0
- package/experimental/Stepper/Stepper.api.md +117 -0
- package/experimental/Stepper/Stepper.md +95 -0
- package/experimental/Stepper/Stepper.vue +82 -0
- package/experimental/Stepper/StepperHorizontal.vue +93 -0
- package/experimental/Stepper/StepperVertical.vue +291 -0
- package/experimental/Stepper/index.ts +11 -0
- package/experimental/Stepper/resolve.ts +87 -0
- package/experimental/Stepper/types.ts +89 -0
- package/experimental.ts +2 -21
- package/package.json +1 -3
- package/src/charts/CalendarHeatmapChart.vue +353 -0
- package/src/charts/DonutChart.vue +34 -58
- package/src/charts/HeatmapChart.vue +26 -64
- package/src/charts/NumberCard.vue +62 -7
- package/src/charts/PercentageBarChart.vue +243 -0
- package/src/charts/SankeyChart.vue +14 -11
- package/src/charts/ScatterChart.vue +24 -20
- package/src/charts/axisChartCommon.ts +182 -18
- package/src/charts/axisFormat.ts +8 -3
- package/src/charts/calendarHeatmapOptions.ts +604 -0
- package/src/charts/colorMath.ts +26 -0
- package/src/charts/components/ChartContainer.vue +63 -53
- package/src/charts/components/ChartLegend.vue +216 -24
- package/src/charts/components/ChartScale.vue +85 -0
- package/src/charts/core/dataLabelFit.ts +45 -6
- package/src/charts/core/useAxisChart.ts +36 -19
- package/src/charts/core/useChart.ts +6 -1
- package/src/charts/core/usePlotKeyboard.ts +13 -2
- package/src/charts/docs/BarChart.md +11 -3
- package/src/charts/docs/CalendarHeatmapChart.api.md +165 -0
- package/src/charts/docs/CalendarHeatmapChart.md +33 -0
- package/src/charts/docs/ChartContainer.api.md +6 -0
- package/src/charts/docs/ChartLegend.api.md +1 -1
- package/src/charts/docs/ChartLegend.md +25 -3
- package/src/charts/docs/DonutChart.api.md +12 -0
- package/src/charts/docs/DonutChart.md +17 -0
- package/src/charts/docs/HeatmapChart.api.md +1 -1
- package/src/charts/docs/LineChart.md +5 -3
- package/src/charts/docs/NumberCard.api.md +6 -0
- package/src/charts/docs/NumberCard.md +6 -0
- package/src/charts/docs/Overview.md +13 -6
- package/src/charts/docs/PercentageBarChart.api.md +148 -0
- package/src/charts/docs/PercentageBarChart.md +42 -0
- package/src/charts/docs/ScatterChart.md +2 -3
- package/src/charts/donutChartOptions.ts +22 -130
- package/src/charts/format.ts +4 -1
- package/src/charts/heatmapOptions.ts +17 -12
- package/src/charts/hiddenSeries.ts +8 -3
- package/src/charts/index.ts +12 -0
- package/src/charts/legendOverflow.ts +40 -0
- package/src/charts/numberCardFormat.ts +19 -13
- package/src/charts/referenceLines.ts +2 -1
- package/src/charts/scatterOptions.ts +16 -3
- package/src/charts/seriesData.ts +8 -3
- package/src/charts/slices.ts +228 -0
- package/src/charts/tokens.ts +99 -0
- package/src/charts/types.ts +318 -9
- package/src/components/Avatar/Avatar.md +27 -2
- package/src/components/Avatar/Avatar.vue +29 -12
- package/src/components/Breadcrumbs/Breadcrumbs.vue +2 -1
- package/src/components/Combobox/Combobox.vue +9 -2
- package/src/components/Combobox/ComboboxResults.vue +6 -1
- package/src/components/Combobox/utils.ts +2 -1
- package/src/components/DatePicker/DatePicker.md +7 -0
- package/src/components/DatePicker/DatePicker.vue +6 -0
- package/src/components/DatePicker/DateTimePicker.vue +6 -0
- package/src/components/DatePicker/composables.ts +18 -1
- package/src/components/DatePicker/utils.ts +53 -0
- package/src/components/Dialog/Dialog.md +3 -1
- package/src/components/Duration/Duration.api.md +16 -3
- package/src/components/Duration/Duration.md +21 -0
- package/src/components/Duration/Duration.vue +29 -8
- package/src/components/Duration/duration.ts +105 -44
- package/src/components/Duration/index.ts +1 -0
- package/src/components/Duration/types.ts +30 -1
- package/src/components/HoverCard/HoverCard.vue +21 -5
- package/src/components/Menu/utils.ts +3 -2
- package/src/components/MobileNav/MobileNavItem.vue +3 -22
- package/src/components/MultiSelect/MultiSelect.api.md +5 -0
- package/src/components/MultiSelect/MultiSelect.vue +34 -0
- package/src/components/MultiSelect/MultiSelectResults.vue +3 -4
- package/src/components/MultiSelect/types.ts +6 -0
- package/src/components/Select/Select.vue +4 -4
- package/src/components/Select/utils.ts +1 -0
- package/src/components/SettingsDialog/SettingsDialog.api.md +7 -2
- package/src/components/SettingsDialog/SettingsDialog.md +2 -1
- package/src/components/SettingsDialog/SettingsRow.vue +12 -2
- package/src/components/Sidebar/SidebarItem.vue +3 -33
- package/src/components/SidebarRail/SidebarRailItem.vue +4 -17
- package/src/components/Switch/Switch.vue +3 -3
- package/src/components/Tooltip/Tooltip.vue +8 -0
- package/src/components/shared/deferredTrigger.ts +174 -0
- package/src/components/shared/navLink.ts +45 -0
- package/src/components/shared/selection/utils.ts +8 -1
- package/src/data-fetching/useCall/useCall.ts +25 -26
- package/src/data-fetching/useFrappeFetch.ts +48 -2
- package/src/data-fetching/useList/useList.ts +11 -3
- package/src/data-fetching/utils.ts +15 -1
- package/src/molecules/editor/components/MediaNodeView.vue +10 -1
- package/src/molecules/editor/components/MediaResizeGuide.vue +71 -0
- package/src/molecules/editor/components/media-node-view-utils.ts +7 -0
- package/src/molecules/editor/composables/useNodeViewResize.ts +142 -10
- package/src/molecules/editor/extensions/attachment/attachment-engine.ts +1 -1
- package/src/molecules/editor/extensions/content-paste/paste-html-utils.ts +16 -6
- package/src/molecules/editor/extensions/iframe/IframeNodeView.vue +29 -10
- package/src/molecules/editor/extensions/iframe/iframe-allowlist.ts +35 -16
- package/src/molecules/editor/extensions/iframe/iframe-embed-utils.ts +1 -1
- package/src/molecules/editor/extensions/iframe/iframe-extension.ts +30 -30
- package/src/molecules/editor/extensions/link/LinkEditorPopup.vue +1 -1
- package/src/molecules/editor/extensions/link/link-paste-plugin.ts +1 -1
- package/src/molecules/editor/extensions/shared/media-upload-engine.ts +1 -1
- package/src/molecules/editor/useEditor.ts +12 -6
- package/src/molecules/list/ListRow.vue +8 -1
- package/src/utils/dialog.ts +57 -16
- package/src/{molecules/editor/extensions/shared → utils}/url-safety.ts +28 -16
- package/tailwind/content.js +1 -1
- package/tailwind/plugin.js +22 -0
- package/vite/README.md +5 -3
- package/vite/codeLanguages.js +41 -17
- package/vite/doctypeInterfaceGenerator.js +52 -39
- package/vitepress/index.node.ts +0 -2
- package/experimental/TextEditor/TextEditor.api.md +0 -161
- package/experimental/TextEditor/TextEditor.vue +0 -298
- package/experimental/TextEditor/classIcon.ts +0 -21
- package/experimental/TextEditor/commands.js +0 -344
- package/experimental/TextEditor/components/CodeBlockComponent.vue +0 -103
- package/experimental/TextEditor/components/FontColor.vue +0 -153
- package/experimental/TextEditor/components/ImageViewerModal.vue +0 -463
- package/experimental/TextEditor/components/InsertImage.vue +0 -54
- package/experimental/TextEditor/components/InsertLink.vue +0 -13
- package/experimental/TextEditor/components/InsertVideo.vue +0 -14
- package/experimental/TextEditor/components/LinkPopup.vue +0 -99
- package/experimental/TextEditor/components/MediaNodeView.vue +0 -398
- package/experimental/TextEditor/components/MentionList.vue +0 -95
- package/experimental/TextEditor/components/TextEditorBubbleMenu.vue +0 -78
- package/experimental/TextEditor/components/TextEditorFixedMenu.vue +0 -78
- package/experimental/TextEditor/components/TextEditorFloatingMenu.vue +0 -61
- package/experimental/TextEditor/components/TextEditorMenu.vue +0 -218
- package/experimental/TextEditor/components/index.js +0 -0
- package/experimental/TextEditor/extensions/code-block.ts +0 -119
- package/experimental/TextEditor/extensions/color/color-extension.ts +0 -140
- package/experimental/TextEditor/extensions/color/color-styles.css +0 -25
- package/experimental/TextEditor/extensions/color/index.ts +0 -2
- package/experimental/TextEditor/extensions/content-paste-extension.ts +0 -160
- package/experimental/TextEditor/extensions/copy-styles.ts +0 -160
- package/experimental/TextEditor/extensions/emoji/EmojiList.vue +0 -55
- package/experimental/TextEditor/extensions/emoji/emoji-extension.ts +0 -55
- package/experimental/TextEditor/extensions/emoji/emojis.json +0 -7422
- package/experimental/TextEditor/extensions/heading/heading.ts +0 -20
- package/experimental/TextEditor/extensions/highlight/highlight-extension.ts +0 -241
- package/experimental/TextEditor/extensions/highlight/highlight-styles.css +0 -31
- package/experimental/TextEditor/extensions/highlight/index.ts +0 -2
- package/experimental/TextEditor/extensions/iframe/IframeNodeView.vue +0 -308
- package/experimental/TextEditor/extensions/iframe/InsertIframe.vue +0 -209
- package/experimental/TextEditor/extensions/iframe/iframe-extension.ts +0 -404
- package/experimental/TextEditor/extensions/iframe/index.ts +0 -11
- package/experimental/TextEditor/extensions/iframe/utils.ts +0 -283
- package/experimental/TextEditor/extensions/image/image-extension.ts +0 -661
- package/experimental/TextEditor/extensions/image/index.ts +0 -5
- package/experimental/TextEditor/extensions/image-group/ImageGroupNodeView.vue +0 -182
- package/experimental/TextEditor/extensions/image-group/ImageGroupUploadDialog.vue +0 -740
- package/experimental/TextEditor/extensions/image-group/image-group-extension.ts +0 -187
- package/experimental/TextEditor/extensions/image-viewer-extension.ts +0 -162
- package/experimental/TextEditor/extensions/link/index.ts +0 -1
- package/experimental/TextEditor/extensions/link/link-extension.ts +0 -323
- package/experimental/TextEditor/extensions/link/linkPasteHandler.ts +0 -51
- package/experimental/TextEditor/extensions/mention/index.ts +0 -1
- package/experimental/TextEditor/extensions/mention/mention-extension.ts +0 -204
- package/experimental/TextEditor/extensions/mention/style.css +0 -11
- package/experimental/TextEditor/extensions/shared/color-utils.ts +0 -236
- package/experimental/TextEditor/extensions/slash-commands/SlashCommandsList.vue +0 -59
- package/experimental/TextEditor/extensions/slash-commands/slash-commands-extension.ts +0 -184
- package/experimental/TextEditor/extensions/suggestion/SuggestionList.vue +0 -130
- package/experimental/TextEditor/extensions/suggestion/createSuggestionExtension.ts +0 -151
- package/experimental/TextEditor/extensions/suggestion/index.ts +0 -5
- package/experimental/TextEditor/extensions/tag/index.ts +0 -1
- package/experimental/TextEditor/extensions/tag/tag-extension.ts +0 -170
- package/experimental/TextEditor/extensions/toc-node/TocNodeView.vue +0 -439
- package/experimental/TextEditor/extensions/toc-node/index.ts +0 -3
- package/experimental/TextEditor/extensions/toc-node/toc-node-extension.ts +0 -167
- package/experimental/TextEditor/extensions/video-extension.ts +0 -653
- package/experimental/TextEditor/hljs-github.css +0 -197
- package/experimental/TextEditor/index.ts +0 -21
- package/experimental/TextEditor/style.css +0 -190
- package/experimental/TextEditor/types.ts +0 -72
- package/experimental/TextEditor/utils.js +0 -11
- package/src/utils/url-validation.ts +0 -39
|
@@ -1,661 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck -- frozen v0 code, kept out of type-check (see tsconfig.app.json)
|
|
2
|
-
import {
|
|
3
|
-
Node as NodeExtension,
|
|
4
|
-
nodeInputRule,
|
|
5
|
-
mergeAttributes,
|
|
6
|
-
} from '@tiptap/core'
|
|
7
|
-
import { VueNodeViewRenderer } from '@tiptap/vue-3'
|
|
8
|
-
import MediaNodeView from '../../components/MediaNodeView.vue'
|
|
9
|
-
import { Plugin, Selection, Transaction, EditorState } from '@tiptap/pm/state'
|
|
10
|
-
import { EditorView } from '@tiptap/pm/view'
|
|
11
|
-
import { Node } from '@tiptap/pm/model'
|
|
12
|
-
import fileToBase64 from '#utils/file-to-base64'
|
|
13
|
-
import { UploadedFile } from '#utils/useFileUpload'
|
|
14
|
-
|
|
15
|
-
export const localFileMap = new Map()
|
|
16
|
-
|
|
17
|
-
export interface ImageExtensionOptions {
|
|
18
|
-
/**
|
|
19
|
-
* Function to handle image uploads
|
|
20
|
-
* @default null
|
|
21
|
-
*/
|
|
22
|
-
uploadFunction: ((file: File) => Promise<UploadedFile>) | null
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* HTML attributes to add to the image element
|
|
26
|
-
* @default {}
|
|
27
|
-
*/
|
|
28
|
-
HTMLAttributes: Record<string, any>
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface SetImageOptions {
|
|
32
|
-
src: string
|
|
33
|
-
alt?: string
|
|
34
|
-
/**
|
|
35
|
-
* Visible caption. Only the modern `#molecules/editor` image node renders it;
|
|
36
|
-
* this legacy extension ignores it. The field is declared here because both
|
|
37
|
-
* extensions augment `Commands['image']`, and TypeScript requires the two
|
|
38
|
-
* declarations to be identical.
|
|
39
|
-
*/
|
|
40
|
-
caption?: string
|
|
41
|
-
title?: string
|
|
42
|
-
width?: string | number | null
|
|
43
|
-
height?: string | number | null
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
declare module '@tiptap/core' {
|
|
47
|
-
interface Commands<ReturnType> {
|
|
48
|
-
image: {
|
|
49
|
-
/**
|
|
50
|
-
* Insert an image
|
|
51
|
-
*/
|
|
52
|
-
setImage: (options: SetImageOptions) => ReturnType
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Upload and insert an image
|
|
56
|
-
*/
|
|
57
|
-
uploadImage: (file: File) => ReturnType
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Select an image file using the file picker and upload it
|
|
61
|
-
*/
|
|
62
|
-
selectAndUploadImage: () => ReturnType
|
|
63
|
-
/**
|
|
64
|
-
* Set image alignment
|
|
65
|
-
*/
|
|
66
|
-
setImageAlign: (align: 'left' | 'center' | 'right') => ReturnType
|
|
67
|
-
/**
|
|
68
|
-
* Set image float for text wrapping
|
|
69
|
-
*/
|
|
70
|
-
setImageFloat: (float: 'left' | 'right' | null) => ReturnType
|
|
71
|
-
/**
|
|
72
|
-
* Re-upload a failed image using the file stored in localFileMap
|
|
73
|
-
*/
|
|
74
|
-
reuploadImage: (uploadId: string) => ReturnType
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Matches markdown image syntax: 
|
|
81
|
-
*/
|
|
82
|
-
const inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/
|
|
83
|
-
|
|
84
|
-
export const ImageExtension = NodeExtension.create<ImageExtensionOptions>({
|
|
85
|
-
name: 'image',
|
|
86
|
-
|
|
87
|
-
group: 'inline',
|
|
88
|
-
inline: true,
|
|
89
|
-
draggable: true,
|
|
90
|
-
selectable: true,
|
|
91
|
-
|
|
92
|
-
addAttributes() {
|
|
93
|
-
return {
|
|
94
|
-
src: { default: null },
|
|
95
|
-
alt: { default: null },
|
|
96
|
-
title: { default: null },
|
|
97
|
-
width: { default: null },
|
|
98
|
-
height: { default: null },
|
|
99
|
-
loading: {
|
|
100
|
-
default: false,
|
|
101
|
-
parseHTML: () => false,
|
|
102
|
-
},
|
|
103
|
-
align: {
|
|
104
|
-
default: 'center',
|
|
105
|
-
parseHTML: (element) => {
|
|
106
|
-
const align = (
|
|
107
|
-
element.getAttribute('data-align') ||
|
|
108
|
-
element.getAttribute('align') ||
|
|
109
|
-
'left'
|
|
110
|
-
).toLowerCase()
|
|
111
|
-
|
|
112
|
-
if (['left', 'center', 'right'].includes(align)) {
|
|
113
|
-
return align as 'left' | 'center' | 'right'
|
|
114
|
-
}
|
|
115
|
-
return 'left'
|
|
116
|
-
},
|
|
117
|
-
renderHTML: (attributes) => {
|
|
118
|
-
return {
|
|
119
|
-
'data-align': attributes.align || 'left',
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
},
|
|
123
|
-
float: {
|
|
124
|
-
default: null,
|
|
125
|
-
parseHTML: (element) => {
|
|
126
|
-
return element.getAttribute('data-float') || null
|
|
127
|
-
},
|
|
128
|
-
renderHTML: (attributes) => {
|
|
129
|
-
if (!attributes.float) return {}
|
|
130
|
-
return {
|
|
131
|
-
'data-float': attributes.float,
|
|
132
|
-
}
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
uploadId: {
|
|
136
|
-
default: null,
|
|
137
|
-
parseHTML: () => null,
|
|
138
|
-
},
|
|
139
|
-
error: {
|
|
140
|
-
default: null,
|
|
141
|
-
parseHTML: () => null,
|
|
142
|
-
},
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
|
-
|
|
146
|
-
parseHTML() {
|
|
147
|
-
return [
|
|
148
|
-
{
|
|
149
|
-
tag: 'img[src]',
|
|
150
|
-
getAttrs: (node) => {
|
|
151
|
-
if (typeof node === 'string') return {}
|
|
152
|
-
const element = node as HTMLElement
|
|
153
|
-
return {
|
|
154
|
-
src: element.getAttribute('src'),
|
|
155
|
-
alt: element.getAttribute('alt'),
|
|
156
|
-
title: element.getAttribute('title'),
|
|
157
|
-
width: element.getAttribute('width'),
|
|
158
|
-
height: element.getAttribute('height'),
|
|
159
|
-
}
|
|
160
|
-
},
|
|
161
|
-
},
|
|
162
|
-
]
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
renderHTML({ HTMLAttributes }) {
|
|
166
|
-
return [
|
|
167
|
-
'img',
|
|
168
|
-
mergeAttributes(this.options.HTMLAttributes || {}, HTMLAttributes),
|
|
169
|
-
]
|
|
170
|
-
},
|
|
171
|
-
|
|
172
|
-
addNodeView() {
|
|
173
|
-
return VueNodeViewRenderer(MediaNodeView)
|
|
174
|
-
},
|
|
175
|
-
|
|
176
|
-
addOptions() {
|
|
177
|
-
return {
|
|
178
|
-
uploadFunction: null,
|
|
179
|
-
HTMLAttributes: {},
|
|
180
|
-
}
|
|
181
|
-
},
|
|
182
|
-
|
|
183
|
-
addCommands() {
|
|
184
|
-
return {
|
|
185
|
-
setImageAlign:
|
|
186
|
-
(align: 'left' | 'center' | 'right') =>
|
|
187
|
-
({ commands }) => {
|
|
188
|
-
return commands.updateAttributes(this.name, { align })
|
|
189
|
-
},
|
|
190
|
-
setImageFloat:
|
|
191
|
-
(float: 'left' | 'right' | null) =>
|
|
192
|
-
({ commands }) => {
|
|
193
|
-
return commands.updateAttributes(this.name, { float })
|
|
194
|
-
},
|
|
195
|
-
|
|
196
|
-
setImage:
|
|
197
|
-
(attributes: SetImageOptions) =>
|
|
198
|
-
({ commands, editor }) => {
|
|
199
|
-
const result = commands.insertContent({
|
|
200
|
-
type: this.name,
|
|
201
|
-
attrs: attributes,
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
if (result && attributes.src) {
|
|
205
|
-
findImageNodeBySource(editor.view, attributes.src, (node, pos) => {
|
|
206
|
-
updateNodeWithDimensions(attributes.src, editor.view, pos)
|
|
207
|
-
})
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return result
|
|
211
|
-
},
|
|
212
|
-
|
|
213
|
-
uploadImage:
|
|
214
|
-
(file: File) =>
|
|
215
|
-
({ editor }) => {
|
|
216
|
-
return uploadImage(file, editor.view, null, this.options)
|
|
217
|
-
},
|
|
218
|
-
|
|
219
|
-
selectAndUploadImage:
|
|
220
|
-
() =>
|
|
221
|
-
({ editor }) => {
|
|
222
|
-
const input = document.createElement('input')
|
|
223
|
-
input.type = 'file'
|
|
224
|
-
input.accept = 'image/*'
|
|
225
|
-
input.onchange = (event) => {
|
|
226
|
-
const target = event.target as HTMLInputElement
|
|
227
|
-
if (target.files && target.files.length) {
|
|
228
|
-
const file = target.files[0]
|
|
229
|
-
editor.commands.uploadImage(file)
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
input.click()
|
|
233
|
-
return true
|
|
234
|
-
},
|
|
235
|
-
|
|
236
|
-
reuploadImage:
|
|
237
|
-
(uploadId: string) =>
|
|
238
|
-
({ editor }) => {
|
|
239
|
-
const fileData = localFileMap.get(uploadId)
|
|
240
|
-
if (!fileData) {
|
|
241
|
-
console.error('reuploadImage: no file with uploadId', uploadId)
|
|
242
|
-
return false
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Find the node position
|
|
246
|
-
let nodePos: number | null = null
|
|
247
|
-
editor.view.state.doc.descendants((node, pos) => {
|
|
248
|
-
if (
|
|
249
|
-
node.type.name === 'image' &&
|
|
250
|
-
node.attrs.uploadId === uploadId
|
|
251
|
-
) {
|
|
252
|
-
nodePos = pos
|
|
253
|
-
return false
|
|
254
|
-
}
|
|
255
|
-
})
|
|
256
|
-
|
|
257
|
-
if (nodePos === null) {
|
|
258
|
-
console.error(
|
|
259
|
-
'reuploadImage: could not find node with uploadId',
|
|
260
|
-
uploadId,
|
|
261
|
-
)
|
|
262
|
-
return false
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// Re-run the upload using the stored file, replacing the node at its position
|
|
266
|
-
return uploadImageBase(
|
|
267
|
-
fileData.file,
|
|
268
|
-
editor.view,
|
|
269
|
-
nodePos,
|
|
270
|
-
this.options,
|
|
271
|
-
'replace',
|
|
272
|
-
)
|
|
273
|
-
},
|
|
274
|
-
}
|
|
275
|
-
},
|
|
276
|
-
|
|
277
|
-
addInputRules() {
|
|
278
|
-
return [
|
|
279
|
-
nodeInputRule({
|
|
280
|
-
find: inputRegex,
|
|
281
|
-
type: this.type,
|
|
282
|
-
getAttributes: (match) => {
|
|
283
|
-
const [, , alt, src, title] = match
|
|
284
|
-
return { src, alt, title }
|
|
285
|
-
},
|
|
286
|
-
}),
|
|
287
|
-
]
|
|
288
|
-
},
|
|
289
|
-
|
|
290
|
-
addProseMirrorPlugins() {
|
|
291
|
-
const extensionThis = this
|
|
292
|
-
|
|
293
|
-
return [
|
|
294
|
-
new Plugin({
|
|
295
|
-
props: {
|
|
296
|
-
handleDOMEvents: {
|
|
297
|
-
drop: (view, event) => {
|
|
298
|
-
const hasFiles = event.dataTransfer?.files?.length
|
|
299
|
-
|
|
300
|
-
if (!hasFiles || !extensionThis.options.uploadFunction) {
|
|
301
|
-
return false
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const images = Array.from(event.dataTransfer.files).filter(
|
|
305
|
-
(file) => /image/i.test(file.type),
|
|
306
|
-
)
|
|
307
|
-
|
|
308
|
-
if (images.length === 0) {
|
|
309
|
-
return false
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
event.preventDefault()
|
|
313
|
-
|
|
314
|
-
const coordinates = view.posAtCoords({
|
|
315
|
-
left: event.clientX,
|
|
316
|
-
top: event.clientY,
|
|
317
|
-
})
|
|
318
|
-
|
|
319
|
-
let pos: number | null = null
|
|
320
|
-
if (coordinates) {
|
|
321
|
-
pos = coordinates.pos
|
|
322
|
-
const transaction = view.state.tr.setSelection(
|
|
323
|
-
Selection.near(view.state.doc.resolve(pos)),
|
|
324
|
-
)
|
|
325
|
-
view.dispatch(transaction)
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
processMultipleImages(images, view, pos, extensionThis.options)
|
|
329
|
-
return true
|
|
330
|
-
},
|
|
331
|
-
|
|
332
|
-
handlePaste: (view, event) => {
|
|
333
|
-
if (!extensionThis.options.uploadFunction) {
|
|
334
|
-
return false
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
const clipboardItems = event.clipboardData?.items
|
|
338
|
-
if (!clipboardItems || clipboardItems.length === 0) {
|
|
339
|
-
return false
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
const images: File[] = []
|
|
343
|
-
|
|
344
|
-
for (let i = 0; i < clipboardItems.length; i++) {
|
|
345
|
-
const item = clipboardItems[i]
|
|
346
|
-
if (
|
|
347
|
-
item.kind === 'file' &&
|
|
348
|
-
item.type.indexOf('image/') !== -1
|
|
349
|
-
) {
|
|
350
|
-
const file = item.getAsFile()
|
|
351
|
-
if (file) {
|
|
352
|
-
images.push(file)
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if (images.length === 0) {
|
|
358
|
-
return false
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
event.preventDefault()
|
|
362
|
-
processMultipleImages(images, view, null, extensionThis.options)
|
|
363
|
-
return true
|
|
364
|
-
},
|
|
365
|
-
},
|
|
366
|
-
},
|
|
367
|
-
|
|
368
|
-
appendTransaction(
|
|
369
|
-
transactions: readonly Transaction[],
|
|
370
|
-
oldState: EditorState,
|
|
371
|
-
newState: EditorState,
|
|
372
|
-
): Transaction | null {
|
|
373
|
-
const newImageNodes: { node: Node; pos: number }[] = []
|
|
374
|
-
|
|
375
|
-
if (transactions.some((tr) => tr.docChanged)) {
|
|
376
|
-
newState.doc.descendants((node, pos) => {
|
|
377
|
-
if (
|
|
378
|
-
node.type.name === 'image' &&
|
|
379
|
-
node.attrs.src &&
|
|
380
|
-
(!node.attrs.width || !node.attrs.height) &&
|
|
381
|
-
!node.attrs.loading
|
|
382
|
-
) {
|
|
383
|
-
newImageNodes.push({ node, pos })
|
|
384
|
-
}
|
|
385
|
-
})
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
if (newImageNodes.length === 0) return null
|
|
389
|
-
|
|
390
|
-
newImageNodes.forEach(({ node, pos }) => {
|
|
391
|
-
const editor = extensionThis.editor
|
|
392
|
-
if (editor) {
|
|
393
|
-
updateNodeWithDimensions(node.attrs.src, editor.view, pos)
|
|
394
|
-
}
|
|
395
|
-
})
|
|
396
|
-
|
|
397
|
-
return null
|
|
398
|
-
},
|
|
399
|
-
}),
|
|
400
|
-
]
|
|
401
|
-
},
|
|
402
|
-
})
|
|
403
|
-
|
|
404
|
-
function findInsertPosition(
|
|
405
|
-
view: EditorView,
|
|
406
|
-
lastNodeId: string | null,
|
|
407
|
-
): number | null {
|
|
408
|
-
if (!lastNodeId) {
|
|
409
|
-
return null
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
let insertPos = null
|
|
413
|
-
|
|
414
|
-
view.state.doc.descendants((node, pos) => {
|
|
415
|
-
if (node.type.name === 'image' && node.attrs.uploadId === lastNodeId) {
|
|
416
|
-
insertPos = pos + node.nodeSize
|
|
417
|
-
return false
|
|
418
|
-
}
|
|
419
|
-
})
|
|
420
|
-
|
|
421
|
-
return insertPos
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
// Base upload function shared by all image upload methods
|
|
425
|
-
type ImageDimensions = { width: number | null; height: number | null }
|
|
426
|
-
function uploadImageBase(
|
|
427
|
-
file: File,
|
|
428
|
-
view: EditorView,
|
|
429
|
-
pos: number | null | undefined,
|
|
430
|
-
options: Record<string, any>,
|
|
431
|
-
insertMode: 'insert' | 'replace',
|
|
432
|
-
onComplete?: (nodeId: string) => void,
|
|
433
|
-
moveCursor = false,
|
|
434
|
-
): boolean {
|
|
435
|
-
if (!options.uploadFunction) {
|
|
436
|
-
console.error('uploadFunction option is not provided')
|
|
437
|
-
return false
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
const uploadId = `upload-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`
|
|
441
|
-
|
|
442
|
-
fileToBase64(file)
|
|
443
|
-
.then((base64Result: string) => {
|
|
444
|
-
localFileMap.set(uploadId, { b64: base64Result, file })
|
|
445
|
-
|
|
446
|
-
return getImageDimensions(base64Result)
|
|
447
|
-
.catch(() => ({ width: null, height: null }))
|
|
448
|
-
.then((dimensions) => dimensions)
|
|
449
|
-
})
|
|
450
|
-
.then((dimensions: ImageDimensions) => {
|
|
451
|
-
const node = view.state.schema.nodes.image.create({
|
|
452
|
-
loading: true,
|
|
453
|
-
uploadId,
|
|
454
|
-
src: null,
|
|
455
|
-
width: dimensions.width,
|
|
456
|
-
height: dimensions.height,
|
|
457
|
-
})
|
|
458
|
-
|
|
459
|
-
const tr = view.state.tr
|
|
460
|
-
if (insertMode === 'replace') {
|
|
461
|
-
if (pos === null) tr.replaceSelectionWith(node)
|
|
462
|
-
else {
|
|
463
|
-
const nodeAtPos = view.state.doc.nodeAt(pos)
|
|
464
|
-
if (nodeAtPos) tr.replaceWith(pos, pos + nodeAtPos.nodeSize, node)
|
|
465
|
-
}
|
|
466
|
-
} else {
|
|
467
|
-
if (pos != null) tr.insert(pos, node)
|
|
468
|
-
else {
|
|
469
|
-
const insertPos = view.state.selection.from
|
|
470
|
-
tr.insert(insertPos, node)
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
view.dispatch(tr)
|
|
475
|
-
|
|
476
|
-
// Optionally move cursor after the node
|
|
477
|
-
if (moveCursor) {
|
|
478
|
-
const nodeSize = node.nodeSize || 1
|
|
479
|
-
setTimeout(() => {
|
|
480
|
-
try {
|
|
481
|
-
let nodePos = null
|
|
482
|
-
view.state.doc.descendants((n, p) => {
|
|
483
|
-
if (n.type.name === 'image' && n.attrs.uploadId === uploadId) {
|
|
484
|
-
nodePos = p
|
|
485
|
-
return false
|
|
486
|
-
}
|
|
487
|
-
})
|
|
488
|
-
|
|
489
|
-
if (nodePos !== null) {
|
|
490
|
-
const posAfter = nodePos + nodeSize
|
|
491
|
-
const transaction = view.state.tr.setSelection(
|
|
492
|
-
Selection.near(view.state.doc.resolve(posAfter)),
|
|
493
|
-
)
|
|
494
|
-
view.dispatch(transaction)
|
|
495
|
-
}
|
|
496
|
-
} catch (e) {
|
|
497
|
-
console.error('Error moving cursor:', e)
|
|
498
|
-
}
|
|
499
|
-
}, 10)
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
return options.uploadFunction(file)
|
|
503
|
-
})
|
|
504
|
-
.then((uploadedImage) => {
|
|
505
|
-
const transaction = view.state.tr
|
|
506
|
-
|
|
507
|
-
view.state.doc.descendants((node, pos) => {
|
|
508
|
-
if (node.type.name === 'image' && node.attrs.uploadId === uploadId) {
|
|
509
|
-
transaction.setNodeMarkup(pos, undefined, {
|
|
510
|
-
...node.attrs,
|
|
511
|
-
src: uploadedImage.file_url,
|
|
512
|
-
width: uploadedImage.width || node.attrs.width,
|
|
513
|
-
height: uploadedImage.height || node.attrs.height,
|
|
514
|
-
loading: false,
|
|
515
|
-
})
|
|
516
|
-
localFileMap.delete(node.attrs.uploadId)
|
|
517
|
-
return false
|
|
518
|
-
}
|
|
519
|
-
})
|
|
520
|
-
|
|
521
|
-
view.dispatch(transaction)
|
|
522
|
-
|
|
523
|
-
if (onComplete) onComplete(uploadId)
|
|
524
|
-
})
|
|
525
|
-
.catch((error: Error) => {
|
|
526
|
-
console.error('Image upload failed:', error)
|
|
527
|
-
|
|
528
|
-
try {
|
|
529
|
-
const transaction = view.state.tr
|
|
530
|
-
|
|
531
|
-
view.state.doc.descendants((node, pos) => {
|
|
532
|
-
if (node.type.name === 'image' && node.attrs.uploadId === uploadId) {
|
|
533
|
-
transaction.setNodeMarkup(pos, undefined, {
|
|
534
|
-
...node.attrs,
|
|
535
|
-
loading: false,
|
|
536
|
-
error: error.message || 'Failed to upload image',
|
|
537
|
-
})
|
|
538
|
-
return false
|
|
539
|
-
}
|
|
540
|
-
})
|
|
541
|
-
|
|
542
|
-
view.dispatch(transaction)
|
|
543
|
-
} catch (e) {
|
|
544
|
-
console.error('Error updating failed node:', e)
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
if (onComplete) onComplete(uploadId)
|
|
548
|
-
})
|
|
549
|
-
|
|
550
|
-
return true
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
function uploadImageWithTracking(
|
|
554
|
-
file: File,
|
|
555
|
-
view: EditorView,
|
|
556
|
-
pos: number | null | undefined,
|
|
557
|
-
options: Record<string, any>,
|
|
558
|
-
onComplete?: (nodeId: string) => void,
|
|
559
|
-
): boolean {
|
|
560
|
-
return uploadImageBase(file, view, pos, options, 'insert', onComplete, true)
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
function uploadImage(
|
|
564
|
-
file: File,
|
|
565
|
-
view: EditorView,
|
|
566
|
-
pos: number | null | undefined,
|
|
567
|
-
options: Record<string, any>,
|
|
568
|
-
): boolean {
|
|
569
|
-
return uploadImageBase(file, view, pos, options, 'replace')
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
function findImageNodeBySource(
|
|
573
|
-
view: EditorView,
|
|
574
|
-
src: string,
|
|
575
|
-
callback: (node: Node, pos: number) => void,
|
|
576
|
-
) {
|
|
577
|
-
view.state.doc.descendants((node, pos) => {
|
|
578
|
-
if (node.type.name === 'image' && node.attrs.src === src) {
|
|
579
|
-
callback(node, pos)
|
|
580
|
-
return false
|
|
581
|
-
}
|
|
582
|
-
})
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
function updateNodeWithDimensions(
|
|
586
|
-
src: string,
|
|
587
|
-
view: EditorView,
|
|
588
|
-
pos: number,
|
|
589
|
-
): void {
|
|
590
|
-
getImageDimensions(src)
|
|
591
|
-
.then((dimensions) => {
|
|
592
|
-
const node = view.state.doc.nodeAt(pos)
|
|
593
|
-
if (!node || node.type.name !== 'image') {
|
|
594
|
-
return
|
|
595
|
-
}
|
|
596
|
-
const currentAttrs = node.attrs
|
|
597
|
-
|
|
598
|
-
if (currentAttrs.width == null || currentAttrs.height == null) {
|
|
599
|
-
const transaction = view.state.tr.setNodeMarkup(pos, undefined, {
|
|
600
|
-
...currentAttrs,
|
|
601
|
-
width: currentAttrs.width ?? dimensions.width,
|
|
602
|
-
height: currentAttrs.height ?? dimensions.height,
|
|
603
|
-
})
|
|
604
|
-
view.dispatch(transaction)
|
|
605
|
-
}
|
|
606
|
-
})
|
|
607
|
-
.catch((error) => {
|
|
608
|
-
// Don't log error if it's just about dimensions for an existing node
|
|
609
|
-
})
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
function getImageDimensions(
|
|
613
|
-
src: string,
|
|
614
|
-
): Promise<{ width: number; height: number }> {
|
|
615
|
-
return new Promise((resolve, reject) => {
|
|
616
|
-
const img = new Image()
|
|
617
|
-
img.onload = () =>
|
|
618
|
-
resolve({
|
|
619
|
-
width: img.naturalWidth,
|
|
620
|
-
height: img.naturalHeight,
|
|
621
|
-
})
|
|
622
|
-
img.onerror = reject
|
|
623
|
-
img.src = src
|
|
624
|
-
})
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
/**
|
|
628
|
-
* Process multiple image uploads sequentially
|
|
629
|
-
*/
|
|
630
|
-
export function processMultipleImages(
|
|
631
|
-
images: File[],
|
|
632
|
-
view: EditorView,
|
|
633
|
-
pos: number | null,
|
|
634
|
-
options: Record<string, any>,
|
|
635
|
-
) {
|
|
636
|
-
if (images.length === 1) {
|
|
637
|
-
uploadImage(images[0], view, pos, options)
|
|
638
|
-
return
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
let imageQueue = [...images]
|
|
642
|
-
let lastInsertedNodeId: string | null = null
|
|
643
|
-
|
|
644
|
-
const processNextImage = () => {
|
|
645
|
-
if (imageQueue.length === 0) return
|
|
646
|
-
|
|
647
|
-
const file = imageQueue.shift()
|
|
648
|
-
if (!file) return
|
|
649
|
-
|
|
650
|
-
const currentPos = lastInsertedNodeId
|
|
651
|
-
? findInsertPosition(view, lastInsertedNodeId)
|
|
652
|
-
: pos
|
|
653
|
-
|
|
654
|
-
uploadImageWithTracking(file, view, currentPos, options, (newNodeId) => {
|
|
655
|
-
lastInsertedNodeId = newNodeId
|
|
656
|
-
setTimeout(processNextImage, 100)
|
|
657
|
-
})
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
processNextImage()
|
|
661
|
-
}
|