-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathAttachmentRefInput.svelte
505 lines (465 loc) · 14.1 KB
/
AttachmentRefInput.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<!--
// Copyright © 2022, 2025 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<script lang="ts">
import { Attachment, AttachmentMetadata } from '@hcengineering/attachment'
import {
Class,
Doc,
IdMap,
Markup,
PersonId,
RateLimiter,
Ref,
Space,
generateId,
toIdMap
} from '@hcengineering/core'
import { Asset, IntlString, setPlatformStatus, unknownError } from '@hcengineering/platform'
import {
DraftController,
canDisplayLinkPreview,
createQuery,
deleteFile,
draftsStore,
fetchLinkPreviewDetails,
getClient,
getFileMetadata,
isLinkPreviewEnabled,
uploadFile,
LinkPreviewAttachmentMetadata
} from '@hcengineering/presentation'
import { EmptyMarkup } from '@hcengineering/text'
import textEditor, { type RefAction } from '@hcengineering/text-editor'
import { AttachIcon, ReferenceInput } from '@hcengineering/text-editor-resources'
import { Loading, type AnySvelteComponent } from '@hcengineering/ui'
import { createEventDispatcher, onDestroy, tick } from 'svelte'
import attachment from '../plugin'
import AttachmentPresenter from './AttachmentPresenter.svelte'
export let objectId: Ref<Doc>
export let space: Ref<Space>
export let _class: Ref<Class<Doc>>
export let content: Markup = EmptyMarkup
export let iconSend: Asset | AnySvelteComponent | undefined = undefined
export let labelSend: IntlString | undefined = undefined
export let showSend = true
export let showActions = true
export let shouldSaveDraft: boolean = false
export let attachments: IdMap<Attachment> = new Map()
export let loading = false
export let focusIndex: number = -1
export let autofocus = false
export function submit (): void {
refInput.submit()
}
export let placeholder: IntlString | undefined = undefined
export let extraActions: RefAction[] = []
export let boundary: HTMLElement | undefined = undefined
export let skipAttachmentsPreload = false
let refInput: ReferenceInput
let inputFile: HTMLInputElement
let saved = false
const dispatch = createEventDispatcher()
const client = getClient()
const query = createQuery()
$: draftKey = `${objectId}_attachments`
$: draftController = new DraftController<Record<Ref<Attachment>, Attachment>>(draftKey)
let draftAttachments: Record<Ref<Attachment>, Attachment> | undefined = undefined
let originalAttachments: Set<Ref<Attachment>> = new Set<Ref<Attachment>>()
const newAttachments: Set<Ref<Attachment>> = new Set<Ref<Attachment>>()
const removedAttachments: Set<Attachment> = new Set<Attachment>()
const maxLinkPreviewCount = 3
const urlSet = new Set<string>()
let progress = false
let refContainer: HTMLElement
const existingAttachmentsQuery = createQuery()
let existingAttachments: Ref<Attachment>[] = []
$: if (Array.from(attachments.keys()).length > 0) {
existingAttachmentsQuery.query(
attachment.class.Attachment,
{
space,
attachedTo: objectId,
attachedToClass: _class,
_id: { $in: Array.from(attachments.keys()) }
},
(res) => {
existingAttachments = res.map((p) => {
if (p.type === 'application/link-preview') {
urlSet.add(getUrlKey(p.name))
}
return p._id
})
}
)
} else {
existingAttachments = []
existingAttachmentsQuery.unsubscribe()
}
function isValidUrl (s: string): boolean {
let url: URL
try {
url = new URL(s)
} catch {
return false
}
return url.protocol.startsWith('http')
}
function getUrlKey (s: string): string {
return s
}
$: objectId && updateAttachments(objectId)
async function updateAttachments (objectId: Ref<Doc>): Promise<void> {
draftAttachments = $draftsStore[draftKey]
if (draftAttachments && shouldSaveDraft) {
attachments = new Map()
newAttachments.clear()
Object.entries(draftAttachments).map((file) => {
return attachments.set(file[0] as Ref<Attachment>, file[1])
})
Object.entries(draftAttachments).map((file) => {
return newAttachments.add(file[0] as Ref<Attachment>)
})
originalAttachments.clear()
removedAttachments.clear()
urlSet.clear()
query.unsubscribe()
} else if (!skipAttachmentsPreload) {
query.query(
attachment.class.Attachment,
{
attachedTo: objectId
},
(res) => {
originalAttachments = new Set(res.map((p) => p._id))
attachments = toIdMap(res)
}
)
} else {
attachments = new Map()
newAttachments.clear()
originalAttachments.clear()
removedAttachments.clear()
urlSet.clear()
query.unsubscribe()
}
}
function saveDraft (): void {
if (shouldSaveDraft) {
draftAttachments = Object.fromEntries(attachments)
draftController.save(draftAttachments)
}
}
async function createAttachment (file: File, meta?: AttachmentMetadata): Promise<void> {
try {
const uuid = await uploadFile(file)
const metadata = meta ?? (await getFileMetadata(file, uuid))
const _id: Ref<Attachment> = generateId()
attachments.set(_id, {
_id,
_class: attachment.class.Attachment,
collection: 'attachments',
modifiedOn: 0,
modifiedBy: '' as PersonId,
space,
attachedTo: objectId,
attachedToClass: _class,
name: file.name,
file: uuid,
type: file.type,
size: file.size,
lastModified: file.lastModified,
metadata
})
newAttachments.add(_id)
attachments = attachments
saved = false
saveDraft()
dispatch('update', { message: content, attachments: attachments.size })
} catch (err: any) {
void setPlatformStatus(unknownError(err))
}
}
async function saveAttachment (doc: Attachment): Promise<void> {
if (!existingAttachments.includes(doc._id)) {
await client.addCollection(attachment.class.Attachment, space, objectId, _class, 'attachments', doc, doc._id)
newAttachments.delete(doc._id)
}
}
async function fileSelected (): Promise<void> {
progress = true
await tick()
const list = inputFile.files
if (list === null || list.length === 0) return
const limiter = new RateLimiter(10)
for (let index = 0; index < list.length; index++) {
const file = list.item(index)
if (file !== null) {
await limiter.add(() => createAttachment(file))
}
}
await limiter.waitProcessing()
inputFile.value = ''
progress = false
}
async function fileDrop (e: DragEvent): Promise<void> {
const list = e.dataTransfer?.files
const limiter = new RateLimiter(10)
if (list === undefined || list.length === 0) return
progress = true
for (let index = 0; index < list.length; index++) {
const file = list.item(index)
if (file !== null) {
await limiter.add(() => createAttachment(file))
}
}
await limiter.waitProcessing()
progress = false
}
async function removeAttachment (attachment: Attachment): Promise<void> {
removedAttachments.add(attachment)
attachments.delete(attachment._id)
attachments = attachments
saveDraft()
dispatch('update', { message: content, attachments: attachments.size })
}
async function deleteAttachment (attachment: Attachment): Promise<void> {
if (attachment.type === 'application/link-preview') {
urlSet.delete(getUrlKey(attachment.name))
}
if (originalAttachments.has(attachment._id)) {
await client.removeCollection(
attachment._class,
attachment.space,
attachment._id,
attachment.attachedTo,
attachment.attachedToClass,
'attachments'
)
} else {
await deleteFile(attachment.file)
}
}
onDestroy(() => {
if (!saved && !shouldSaveDraft) {
newAttachments.forEach((p) => {
const attachment = attachments.get(p)
if (attachment !== undefined) {
void deleteAttachment(attachment)
}
})
}
})
export function removeDraft (removeFiles: boolean): void {
draftController.remove()
if (removeFiles) {
newAttachments.forEach((p) => {
const attachment = attachments.get(p)
if (attachment !== undefined) {
void deleteFile(attachment.file)
}
})
}
}
export async function createAttachments (): Promise<void> {
if (saved) {
return
}
saved = true
const limiter = new RateLimiter(10)
newAttachments.forEach((p) => {
const attachment = attachments.get(p)
if (attachment !== undefined) {
void limiter.add(() => saveAttachment(attachment))
}
})
removedAttachments.forEach((p) => {
void limiter.add(() => deleteAttachment(p))
})
await limiter.waitProcessing()
newAttachments.clear()
urlSet.clear()
removedAttachments.clear()
saveDraft()
}
async function onMessage (event: CustomEvent): Promise<void> {
loading = true
await createAttachments()
loading = false
dispatch('message', { message: event.detail, attachments: attachments.size })
}
function updateLinkPreview (): void {
const hrefs = refContainer.getElementsByTagName('a')
const newUrls: string[] = []
for (let i = 0; i < hrefs.length; i++) {
if (hrefs[i].target !== '_blank' || !isValidUrl(hrefs[i].href) || hrefs[i].rel === '') {
continue
}
const key = getUrlKey(hrefs[i].href)
if (urlSet.has(key)) {
continue
}
urlSet.add(key)
newUrls.push(hrefs[i].href)
}
if (newUrls.length > 0) {
void loadLinks(newUrls)
}
}
function onUpdate (event: CustomEvent): void {
if (isLinkPreviewEnabled() && !loading && urlSet.size < maxLinkPreviewCount) {
updateLinkPreview()
}
dispatch('update', { message: event.detail, attachments: attachments.size })
}
async function loadLinks (urls: string[]): Promise<void> {
progress = true
for (const url of urls) {
try {
const meta = await fetchLinkPreviewDetails(url)
if (canDisplayLinkPreview(meta) && meta.url !== undefined) {
const blob = new Blob([JSON.stringify(meta)])
const file = new File([blob], meta.url, { type: 'application/link-preview' })
const metadata: LinkPreviewAttachmentMetadata = {
title: meta.title,
image: meta.image,
description: meta.description,
imageWidth: meta.imageWidth,
imageHeight: meta.imageHeight
}
await createAttachment(file, metadata)
}
} catch (err: any) {
void setPlatformStatus(unknownError(err))
}
}
progress = false
}
async function loadFiles (evt: ClipboardEvent): Promise<void> {
progress = true
const files = (evt.clipboardData?.files ?? []) as File[]
for (const file of files) {
await createAttachment(file)
}
progress = false
}
function pasteAction (_: any, evt: ClipboardEvent): boolean {
let target: HTMLElement | null = evt.target as HTMLElement
let allowed = false
while (target != null) {
target = target.parentElement
if (target === refContainer) {
allowed = true
}
}
if (!allowed) {
return false
}
const hasFiles = Array.from(evt.clipboardData?.items ?? []).some((i) => i.kind === 'file')
if (hasFiles) {
void loadFiles(evt)
return true
}
return false
}
</script>
<div class="flex-col no-print" bind:this={refContainer}>
<input
bind:this={inputFile}
disabled={inputFile == null}
multiple
type="file"
name="file"
id="file"
style="display: none"
on:change={fileSelected}
/>
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="flex-col"
on:dragover|preventDefault={() => {}}
on:dragleave={() => {}}
on:drop|preventDefault|stopPropagation={fileDrop}
>
<ReferenceInput
{focusIndex}
bind:this={refInput}
bind:content
{iconSend}
{labelSend}
{showSend}
{showActions}
autofocus={autofocus ? 'end' : false}
loading={loading || progress}
{boundary}
canEmbedFiles={false}
canEmbedImages={false}
extraActions={[
...extraActions,
{
label: textEditor.string.Attach,
icon: AttachIcon,
action: () => {
dispatch('focus')
inputFile.click()
},
order: 1001
}
]}
showHeader={attachments.size > 0 || progress}
haveAttachment={attachments.size > 0}
on:focus
on:blur
on:message={onMessage}
on:update={onUpdate}
onPaste={pasteAction}
on:keydown
{placeholder}
>
<div slot="header">
{#if attachments.size > 0 || progress}
<div class="flex-row-center list scroll-divider-color">
{#if progress}
<div class="flex p-3">
<Loading />
</div>
{/if}
{#each Array.from(attachments.values()) as attachment}
<div class="item flex">
<AttachmentPresenter
value={attachment}
removable
on:remove={(result) => {
if (result !== undefined) void removeAttachment(attachment)
}}
/>
</div>
{/each}
</div>
{/if}
</div>
</ReferenceInput>
</div>
</div>
<style lang="scss">
.list {
padding: 0.5rem;
overflow-x: auto;
overflow-y: hidden;
.item + .item {
padding-left: 1rem;
border-left: 1px solid var(--theme-divider-color);
}
}
</style>