frappe-ui 1.1.0 → 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/CommandPalette/CommandPaletteItem.vue +9 -1
- package/experimental/SpriteIcons/IconPicker.vue +2 -1
- package/experimental.ts +0 -21
- package/package.json +1 -3
- package/src/components/Breadcrumbs/Breadcrumbs.vue +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/HoverCard/HoverCard.vue +21 -5
- package/src/components/MobileNav/MobileNavItem.vue +3 -22
- 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/Tooltip/Tooltip.vue +8 -0
- package/src/components/shared/deferredTrigger.ts +174 -0
- package/src/components/shared/navLink.ts +45 -0
- 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 +26 -9
- 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/list/ListRow.vue +8 -1
- package/src/utils/dialog.ts +20 -2
- package/src/{molecules/editor/extensions/shared → utils}/url-safety.ts +28 -16
- package/tailwind/content.js +0 -1
- package/tailwind/plugin.js +22 -0
- 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,653 +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
|
-
import { localFileMap } from './image/image-extension'
|
|
15
|
-
|
|
16
|
-
export interface VideoExtensionOptions {
|
|
17
|
-
/**
|
|
18
|
-
* Function to handle video uploads
|
|
19
|
-
* @default null
|
|
20
|
-
*/
|
|
21
|
-
uploadFunction: ((file: File) => Promise<UploadedFile>) | null
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* HTML attributes to add to the video element
|
|
25
|
-
* @default {}
|
|
26
|
-
*/
|
|
27
|
-
HTMLAttributes: Record<string, any>
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface SetVideoOptions {
|
|
31
|
-
src: string
|
|
32
|
-
alt?: string
|
|
33
|
-
title?: string
|
|
34
|
-
width?: string | number | null
|
|
35
|
-
height?: string | number | null
|
|
36
|
-
autoplay?: boolean
|
|
37
|
-
loop?: boolean
|
|
38
|
-
muted?: boolean
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
declare module '@tiptap/core' {
|
|
42
|
-
interface Commands<ReturnType> {
|
|
43
|
-
video: {
|
|
44
|
-
/**
|
|
45
|
-
* Insert a video
|
|
46
|
-
*/
|
|
47
|
-
setVideo: (options: SetVideoOptions) => ReturnType
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Upload and insert a video
|
|
51
|
-
*/
|
|
52
|
-
uploadVideo: (file: File) => ReturnType
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Select a video file using the file picker and upload it
|
|
56
|
-
*/
|
|
57
|
-
selectAndUploadVideo: () => ReturnType
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Set video floating
|
|
61
|
-
*/
|
|
62
|
-
setVideoFloat: (float: 'left' | 'right' | null) => ReturnType
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Re-upload a failed video using the file stored in localFileMap
|
|
66
|
-
*/
|
|
67
|
-
reuploadVideo: (uploadId: string) => ReturnType
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Matches markdown-style video syntax (custom): !video[alt](src "title")
|
|
74
|
-
*/
|
|
75
|
-
const inputRegex =
|
|
76
|
-
/(?:^|\s)(!video\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/
|
|
77
|
-
|
|
78
|
-
export const VideoExtension = NodeExtension.create<VideoExtensionOptions>({
|
|
79
|
-
name: 'video',
|
|
80
|
-
group: 'block',
|
|
81
|
-
selectable: true,
|
|
82
|
-
draggable: true,
|
|
83
|
-
atom: true,
|
|
84
|
-
|
|
85
|
-
addOptions() {
|
|
86
|
-
return {
|
|
87
|
-
uploadFunction: null,
|
|
88
|
-
HTMLAttributes: {},
|
|
89
|
-
}
|
|
90
|
-
},
|
|
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
|
-
autoplay: { default: false },
|
|
100
|
-
loop: { default: false },
|
|
101
|
-
muted: { default: false },
|
|
102
|
-
loading: {
|
|
103
|
-
default: false,
|
|
104
|
-
parseHTML: () => false,
|
|
105
|
-
},
|
|
106
|
-
float: {
|
|
107
|
-
default: null,
|
|
108
|
-
parseHTML: (element) => {
|
|
109
|
-
const float =
|
|
110
|
-
element.getAttribute('data-float') || element.getAttribute('float')
|
|
111
|
-
|
|
112
|
-
if (['left', 'right', null].includes(float)) {
|
|
113
|
-
return float as 'left' | 'right' | null
|
|
114
|
-
}
|
|
115
|
-
return null
|
|
116
|
-
},
|
|
117
|
-
renderHTML: (attributes) => {
|
|
118
|
-
return {
|
|
119
|
-
'data-float': attributes.float || null,
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
},
|
|
123
|
-
uploadId: {
|
|
124
|
-
default: null,
|
|
125
|
-
parseHTML: () => null,
|
|
126
|
-
},
|
|
127
|
-
error: {
|
|
128
|
-
default: null,
|
|
129
|
-
parseHTML: () => null,
|
|
130
|
-
},
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
parseHTML() {
|
|
135
|
-
return [
|
|
136
|
-
{
|
|
137
|
-
tag: 'video',
|
|
138
|
-
getAttrs: (node) => {
|
|
139
|
-
if (typeof node === 'string') return {}
|
|
140
|
-
const element = node as HTMLElement
|
|
141
|
-
return {
|
|
142
|
-
src: element.getAttribute('src'),
|
|
143
|
-
alt: element.getAttribute('alt'),
|
|
144
|
-
title: element.getAttribute('title'),
|
|
145
|
-
width: element.getAttribute('width'),
|
|
146
|
-
height: element.getAttribute('height'),
|
|
147
|
-
autoplay: element.hasAttribute('autoplay'),
|
|
148
|
-
loop: element.hasAttribute('loop'),
|
|
149
|
-
muted: element.hasAttribute('muted'),
|
|
150
|
-
}
|
|
151
|
-
},
|
|
152
|
-
},
|
|
153
|
-
]
|
|
154
|
-
},
|
|
155
|
-
|
|
156
|
-
renderHTML({ HTMLAttributes }) {
|
|
157
|
-
return [
|
|
158
|
-
'video',
|
|
159
|
-
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
|
160
|
-
controls: '',
|
|
161
|
-
}),
|
|
162
|
-
]
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
addNodeView() {
|
|
166
|
-
return VueNodeViewRenderer(MediaNodeView)
|
|
167
|
-
},
|
|
168
|
-
|
|
169
|
-
addCommands() {
|
|
170
|
-
return {
|
|
171
|
-
setVideoFloat:
|
|
172
|
-
(float: 'left' | 'right' | null) =>
|
|
173
|
-
({ commands }) => {
|
|
174
|
-
return commands.updateAttributes(this.name, { float })
|
|
175
|
-
},
|
|
176
|
-
|
|
177
|
-
setVideo:
|
|
178
|
-
(attributes: SetVideoOptions) =>
|
|
179
|
-
({ commands, editor }) => {
|
|
180
|
-
const result = commands.insertContent({
|
|
181
|
-
type: this.name,
|
|
182
|
-
attrs: attributes,
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
if (result && attributes.src) {
|
|
186
|
-
findVideoNodeBySource(editor.view, attributes.src, (node, pos) => {
|
|
187
|
-
updateNodeWithDimensions(attributes.src, editor.view, pos)
|
|
188
|
-
})
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return result
|
|
192
|
-
},
|
|
193
|
-
|
|
194
|
-
uploadVideo:
|
|
195
|
-
(file: File) =>
|
|
196
|
-
({ editor }) => {
|
|
197
|
-
return uploadVideo(file, editor.view, null, this.options)
|
|
198
|
-
},
|
|
199
|
-
|
|
200
|
-
selectAndUploadVideo:
|
|
201
|
-
() =>
|
|
202
|
-
({ editor }) => {
|
|
203
|
-
const input = document.createElement('input')
|
|
204
|
-
input.type = 'file'
|
|
205
|
-
input.accept = 'video/*'
|
|
206
|
-
input.onchange = (event) => {
|
|
207
|
-
const target = event.target as HTMLInputElement
|
|
208
|
-
if (target.files && target.files.length) {
|
|
209
|
-
const file = target.files[0]
|
|
210
|
-
editor.commands.uploadVideo(file)
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
input.click()
|
|
214
|
-
return true
|
|
215
|
-
},
|
|
216
|
-
|
|
217
|
-
reuploadVideo:
|
|
218
|
-
(uploadId: string) =>
|
|
219
|
-
({ editor }) => {
|
|
220
|
-
const fileData = localFileMap.get(uploadId)
|
|
221
|
-
if (!fileData) {
|
|
222
|
-
console.error(
|
|
223
|
-
'reuploadVideo: no file found in localFileMap for uploadId',
|
|
224
|
-
uploadId,
|
|
225
|
-
)
|
|
226
|
-
return false
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// Find the node position
|
|
230
|
-
let nodePos: number | null = null
|
|
231
|
-
editor.view.state.doc.descendants((node, pos) => {
|
|
232
|
-
if (
|
|
233
|
-
node.type.name === 'video' &&
|
|
234
|
-
node.attrs.uploadId === uploadId
|
|
235
|
-
) {
|
|
236
|
-
nodePos = pos
|
|
237
|
-
return false
|
|
238
|
-
}
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
if (nodePos === null) {
|
|
242
|
-
console.error(
|
|
243
|
-
'reuploadVideo: could not find node with uploadId',
|
|
244
|
-
uploadId,
|
|
245
|
-
)
|
|
246
|
-
return false
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// Re-run the upload using the stored file, replacing the node at its position
|
|
250
|
-
return uploadVideoBase(
|
|
251
|
-
fileData.file,
|
|
252
|
-
editor.view,
|
|
253
|
-
nodePos,
|
|
254
|
-
this.options,
|
|
255
|
-
'replace',
|
|
256
|
-
)
|
|
257
|
-
},
|
|
258
|
-
}
|
|
259
|
-
},
|
|
260
|
-
|
|
261
|
-
addInputRules() {
|
|
262
|
-
return [
|
|
263
|
-
nodeInputRule({
|
|
264
|
-
find: inputRegex,
|
|
265
|
-
type: this.type,
|
|
266
|
-
getAttributes: (match) => {
|
|
267
|
-
const [, , alt, src, title] = match
|
|
268
|
-
return { src, alt, title }
|
|
269
|
-
},
|
|
270
|
-
}),
|
|
271
|
-
]
|
|
272
|
-
},
|
|
273
|
-
|
|
274
|
-
addProseMirrorPlugins() {
|
|
275
|
-
const extensionThis = this
|
|
276
|
-
|
|
277
|
-
return [
|
|
278
|
-
new Plugin({
|
|
279
|
-
props: {
|
|
280
|
-
handleDOMEvents: {
|
|
281
|
-
drop: (view, event) => {
|
|
282
|
-
const hasFiles = event.dataTransfer?.files?.length
|
|
283
|
-
|
|
284
|
-
if (!hasFiles || !extensionThis.options.uploadFunction) {
|
|
285
|
-
return false
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const videos = Array.from(event.dataTransfer.files).filter(
|
|
289
|
-
(file) => /video/i.test(file.type),
|
|
290
|
-
)
|
|
291
|
-
|
|
292
|
-
if (videos.length === 0) {
|
|
293
|
-
return false
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
event.preventDefault()
|
|
297
|
-
|
|
298
|
-
const coordinates = view.posAtCoords({
|
|
299
|
-
left: event.clientX,
|
|
300
|
-
top: event.clientY,
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
let pos: number | null = null
|
|
304
|
-
if (coordinates) {
|
|
305
|
-
pos = coordinates.pos
|
|
306
|
-
const transaction = view.state.tr.setSelection(
|
|
307
|
-
Selection.near(view.state.doc.resolve(pos)),
|
|
308
|
-
)
|
|
309
|
-
view.dispatch(transaction)
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
processMultipleVideos(videos, view, pos, extensionThis.options)
|
|
313
|
-
return true
|
|
314
|
-
},
|
|
315
|
-
|
|
316
|
-
paste: (view, event) => {
|
|
317
|
-
if (!extensionThis.options.uploadFunction) {
|
|
318
|
-
return false
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
const clipboardItems = event.clipboardData?.items
|
|
322
|
-
if (!clipboardItems || clipboardItems.length === 0) {
|
|
323
|
-
return false
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
const videos: File[] = []
|
|
327
|
-
|
|
328
|
-
for (let i = 0; i < clipboardItems.length; i++) {
|
|
329
|
-
const item = clipboardItems[i]
|
|
330
|
-
if (
|
|
331
|
-
item.kind === 'file' &&
|
|
332
|
-
item.type.indexOf('video/') !== -1
|
|
333
|
-
) {
|
|
334
|
-
const file = item.getAsFile()
|
|
335
|
-
if (file) videos.push(file)
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
if (videos.length === 0) return false
|
|
340
|
-
|
|
341
|
-
event.preventDefault()
|
|
342
|
-
processMultipleVideos(videos, view, null, extensionThis.options)
|
|
343
|
-
return true
|
|
344
|
-
},
|
|
345
|
-
},
|
|
346
|
-
},
|
|
347
|
-
|
|
348
|
-
appendTransaction(
|
|
349
|
-
transactions: readonly Transaction[],
|
|
350
|
-
oldState: EditorState,
|
|
351
|
-
newState: EditorState,
|
|
352
|
-
): Transaction | null {
|
|
353
|
-
const newVideoNodes: { node: Node; pos: number }[] = []
|
|
354
|
-
|
|
355
|
-
if (transactions.some((tr) => tr.docChanged)) {
|
|
356
|
-
newState.doc.descendants((node, pos) => {
|
|
357
|
-
if (
|
|
358
|
-
node.type.name === 'video' &&
|
|
359
|
-
node.attrs.src &&
|
|
360
|
-
(!node.attrs.width || !node.attrs.height) &&
|
|
361
|
-
!node.attrs.loading
|
|
362
|
-
) {
|
|
363
|
-
newVideoNodes.push({ node, pos })
|
|
364
|
-
}
|
|
365
|
-
})
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (newVideoNodes.length === 0) return null
|
|
369
|
-
|
|
370
|
-
newVideoNodes.forEach(({ node, pos }) => {
|
|
371
|
-
const editor = extensionThis.editor
|
|
372
|
-
if (editor) {
|
|
373
|
-
updateNodeWithDimensions(node.attrs.src, editor.view, pos)
|
|
374
|
-
}
|
|
375
|
-
})
|
|
376
|
-
|
|
377
|
-
return null
|
|
378
|
-
},
|
|
379
|
-
}),
|
|
380
|
-
]
|
|
381
|
-
},
|
|
382
|
-
})
|
|
383
|
-
|
|
384
|
-
function findInsertPosition(
|
|
385
|
-
view: EditorView,
|
|
386
|
-
lastNodeId: string | null,
|
|
387
|
-
): number | null {
|
|
388
|
-
if (!lastNodeId) {
|
|
389
|
-
return null
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
let insertPos = null
|
|
393
|
-
|
|
394
|
-
view.state.doc.descendants((node, pos) => {
|
|
395
|
-
if (node.type.name === 'video' && node.attrs.uploadId === lastNodeId) {
|
|
396
|
-
insertPos = pos + node.nodeSize
|
|
397
|
-
return false
|
|
398
|
-
}
|
|
399
|
-
})
|
|
400
|
-
|
|
401
|
-
return insertPos
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// Base upload function shared by all video upload methods
|
|
405
|
-
type VideoDimensions = { width: number | null; height: number | null }
|
|
406
|
-
|
|
407
|
-
function uploadVideoBase(
|
|
408
|
-
file: File,
|
|
409
|
-
view: EditorView,
|
|
410
|
-
pos: number | null | undefined,
|
|
411
|
-
options: Record<string, any>,
|
|
412
|
-
insertMode: 'insert' | 'replace',
|
|
413
|
-
onComplete?: (nodeId: string) => void,
|
|
414
|
-
moveCursor = false,
|
|
415
|
-
): boolean {
|
|
416
|
-
if (!options.uploadFunction) {
|
|
417
|
-
console.error('uploadFunction option is not provided for videos.')
|
|
418
|
-
return false
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
const uploadId = `upload-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`
|
|
422
|
-
|
|
423
|
-
fileToBase64(file)
|
|
424
|
-
.then((base64Result: string) => {
|
|
425
|
-
localFileMap.set(uploadId, { b64: base64Result, file })
|
|
426
|
-
|
|
427
|
-
const objectUrl = URL.createObjectURL(file)
|
|
428
|
-
return getVideoDimensions(objectUrl)
|
|
429
|
-
.catch((): VideoDimensions => ({ width: null, height: null }))
|
|
430
|
-
.then((dimensions: VideoDimensions) => {
|
|
431
|
-
URL.revokeObjectURL(objectUrl)
|
|
432
|
-
return dimensions
|
|
433
|
-
})
|
|
434
|
-
})
|
|
435
|
-
.then((dimensions: VideoDimensions) => {
|
|
436
|
-
const node = view.state.schema.nodes.video.create({
|
|
437
|
-
loading: true,
|
|
438
|
-
uploadId,
|
|
439
|
-
src: null,
|
|
440
|
-
width: dimensions.width,
|
|
441
|
-
height: dimensions.height,
|
|
442
|
-
})
|
|
443
|
-
|
|
444
|
-
const tr = view.state.tr
|
|
445
|
-
if (insertMode === 'replace') {
|
|
446
|
-
if (pos === null) tr.replaceSelectionWith(node)
|
|
447
|
-
else {
|
|
448
|
-
const nodeAtPos = view.state.doc.nodeAt(pos)
|
|
449
|
-
if (nodeAtPos) tr.replaceWith(pos, pos + nodeAtPos.nodeSize, node)
|
|
450
|
-
}
|
|
451
|
-
} else {
|
|
452
|
-
if (pos != null) tr.insert(pos, node)
|
|
453
|
-
else {
|
|
454
|
-
const insertPos = view.state.selection.from
|
|
455
|
-
tr.insert(insertPos, node)
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
view.dispatch(tr)
|
|
460
|
-
|
|
461
|
-
// Optionally move cursor after the node
|
|
462
|
-
if (moveCursor) {
|
|
463
|
-
const nodeSize = node.nodeSize || 1
|
|
464
|
-
setTimeout(() => {
|
|
465
|
-
try {
|
|
466
|
-
let nodePos = null
|
|
467
|
-
view.state.doc.descendants((n, p) => {
|
|
468
|
-
if (n.type.name === 'video' && n.attrs.uploadId === uploadId) {
|
|
469
|
-
nodePos = p
|
|
470
|
-
return false
|
|
471
|
-
}
|
|
472
|
-
})
|
|
473
|
-
|
|
474
|
-
if (nodePos !== null) {
|
|
475
|
-
const posAfter = nodePos + nodeSize
|
|
476
|
-
const transaction = view.state.tr.setSelection(
|
|
477
|
-
Selection.near(view.state.doc.resolve(posAfter)),
|
|
478
|
-
)
|
|
479
|
-
view.dispatch(transaction)
|
|
480
|
-
}
|
|
481
|
-
} catch (e) {
|
|
482
|
-
console.error('Error moving cursor:', e)
|
|
483
|
-
}
|
|
484
|
-
}, 10)
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
return options.uploadFunction(file)
|
|
488
|
-
})
|
|
489
|
-
.then((uploadedVideo) => {
|
|
490
|
-
const transaction = view.state.tr
|
|
491
|
-
|
|
492
|
-
view.state.doc.descendants((node, pos) => {
|
|
493
|
-
if (node.type.name === 'video' && node.attrs.uploadId === uploadId) {
|
|
494
|
-
transaction.setNodeMarkup(pos, undefined, {
|
|
495
|
-
...node.attrs,
|
|
496
|
-
src: uploadedVideo.file_url,
|
|
497
|
-
width: uploadedVideo.width || node.attrs.width,
|
|
498
|
-
height: uploadedVideo.height || node.attrs.height,
|
|
499
|
-
loading: false,
|
|
500
|
-
})
|
|
501
|
-
localFileMap.delete(node.attrs.uploadId)
|
|
502
|
-
return false
|
|
503
|
-
}
|
|
504
|
-
})
|
|
505
|
-
|
|
506
|
-
view.dispatch(transaction)
|
|
507
|
-
|
|
508
|
-
if (onComplete) onComplete(uploadId)
|
|
509
|
-
})
|
|
510
|
-
.catch((error: Error) => {
|
|
511
|
-
console.error('Video upload failed:', error)
|
|
512
|
-
|
|
513
|
-
try {
|
|
514
|
-
const transaction = view.state.tr
|
|
515
|
-
|
|
516
|
-
view.state.doc.descendants((node, pos) => {
|
|
517
|
-
if (node.type.name === 'video' && node.attrs.uploadId === uploadId) {
|
|
518
|
-
// width/height are preserved from ...node.attrs (pre-fetched from local file)
|
|
519
|
-
transaction.setNodeMarkup(pos, undefined, {
|
|
520
|
-
...node.attrs,
|
|
521
|
-
loading: false,
|
|
522
|
-
error: error.message || 'Failed to upload video',
|
|
523
|
-
})
|
|
524
|
-
return false
|
|
525
|
-
}
|
|
526
|
-
})
|
|
527
|
-
|
|
528
|
-
view.dispatch(transaction)
|
|
529
|
-
} catch (e) {
|
|
530
|
-
console.error('Error updating failed node:', e)
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
if (onComplete) onComplete(uploadId)
|
|
534
|
-
})
|
|
535
|
-
|
|
536
|
-
return true
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
function uploadVideoWithTracking(
|
|
540
|
-
file: File,
|
|
541
|
-
view: EditorView,
|
|
542
|
-
pos: number | null | undefined,
|
|
543
|
-
options: Record<string, any>,
|
|
544
|
-
onComplete?: (nodeId: string) => void,
|
|
545
|
-
): boolean {
|
|
546
|
-
return uploadVideoBase(file, view, pos, options, 'insert', onComplete, true)
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
function uploadVideo(
|
|
550
|
-
file: File,
|
|
551
|
-
view: EditorView,
|
|
552
|
-
pos: number | null | undefined,
|
|
553
|
-
options: Record<string, any>,
|
|
554
|
-
): boolean {
|
|
555
|
-
return uploadVideoBase(file, view, pos, options, 'replace')
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
function findVideoNodeBySource(
|
|
559
|
-
view: EditorView,
|
|
560
|
-
src: string,
|
|
561
|
-
callback: (node: Node, pos: number) => void,
|
|
562
|
-
) {
|
|
563
|
-
view.state.doc.descendants((node, pos) => {
|
|
564
|
-
if (node.type.name === 'video' && node.attrs.src === src) {
|
|
565
|
-
callback(node, pos)
|
|
566
|
-
return false
|
|
567
|
-
}
|
|
568
|
-
})
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
function updateNodeWithDimensions(
|
|
572
|
-
src: string,
|
|
573
|
-
view: EditorView,
|
|
574
|
-
pos: number,
|
|
575
|
-
): void {
|
|
576
|
-
getVideoDimensions(src)
|
|
577
|
-
.then((dimensions) => {
|
|
578
|
-
const node = view.state.doc.nodeAt(pos)
|
|
579
|
-
if (!node || node.type.name !== 'video') {
|
|
580
|
-
return
|
|
581
|
-
}
|
|
582
|
-
const currentAttrs = node.attrs
|
|
583
|
-
|
|
584
|
-
if (currentAttrs.width == null || currentAttrs.height == null) {
|
|
585
|
-
const transaction = view.state.tr.setNodeMarkup(pos, undefined, {
|
|
586
|
-
...currentAttrs,
|
|
587
|
-
width: currentAttrs.width ?? dimensions.width,
|
|
588
|
-
height: currentAttrs.height ?? dimensions.height,
|
|
589
|
-
})
|
|
590
|
-
view.dispatch(transaction)
|
|
591
|
-
}
|
|
592
|
-
})
|
|
593
|
-
.catch((error) => {
|
|
594
|
-
console.error('Could not get video dimensions', error)
|
|
595
|
-
})
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
function getVideoDimensions(
|
|
599
|
-
src: string,
|
|
600
|
-
): Promise<{ width: number; height: number }> {
|
|
601
|
-
return new Promise((resolve, reject) => {
|
|
602
|
-
const video = document.createElement('video')
|
|
603
|
-
video.preload = 'metadata'
|
|
604
|
-
|
|
605
|
-
video.onloadedmetadata = () => {
|
|
606
|
-
resolve({
|
|
607
|
-
width: video.videoWidth,
|
|
608
|
-
height: video.videoHeight,
|
|
609
|
-
})
|
|
610
|
-
// Clean up
|
|
611
|
-
video.src = ''
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
video.onerror = reject
|
|
615
|
-
video.src = src
|
|
616
|
-
})
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
/**
|
|
620
|
-
* Process multiple video uploads sequentially
|
|
621
|
-
*/
|
|
622
|
-
export function processMultipleVideos(
|
|
623
|
-
videos: File[],
|
|
624
|
-
view: EditorView,
|
|
625
|
-
pos: number | null,
|
|
626
|
-
options: Record<string, any>,
|
|
627
|
-
) {
|
|
628
|
-
if (videos.length === 1) {
|
|
629
|
-
uploadVideo(videos[0], view, pos, options)
|
|
630
|
-
return
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
let videoQueue = [...videos]
|
|
634
|
-
let lastInsertedNodeId: string | null = null
|
|
635
|
-
|
|
636
|
-
const processNextVideo = () => {
|
|
637
|
-
if (videoQueue.length === 0) return
|
|
638
|
-
|
|
639
|
-
const file = videoQueue.shift()
|
|
640
|
-
if (!file) return
|
|
641
|
-
|
|
642
|
-
const currentPos = lastInsertedNodeId
|
|
643
|
-
? findInsertPosition(view, lastInsertedNodeId)
|
|
644
|
-
: pos
|
|
645
|
-
|
|
646
|
-
uploadVideoWithTracking(file, view, currentPos, options, (newNodeId) => {
|
|
647
|
-
lastInsertedNodeId = newNodeId
|
|
648
|
-
setTimeout(processNextVideo, 100)
|
|
649
|
-
})
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
processNextVideo()
|
|
653
|
-
}
|