2022-03-01 14:14:51 +00:00
|
|
|
<!--
|
|
|
|
// Copyright © 2022 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">
|
2023-03-24 10:23:44 +00:00
|
|
|
import { createQuery, DraftController, draftsStore, getClient } from '@hcengineering/presentation'
|
2022-09-21 08:08:25 +00:00
|
|
|
import { ReferenceInput } from '@hcengineering/text-editor'
|
2023-01-13 03:10:29 +00:00
|
|
|
import type { RefAction } from '@hcengineering/text-editor'
|
2022-03-01 14:14:51 +00:00
|
|
|
import { deleteFile, uploadFile } from '../utils'
|
|
|
|
import attachment from '../plugin'
|
2023-01-13 03:10:29 +00:00
|
|
|
import { IntlString, setPlatformStatus, unknownError, Asset } from '@hcengineering/platform'
|
2023-09-28 10:50:37 +00:00
|
|
|
import { createEventDispatcher, onDestroy, tick } from 'svelte'
|
2023-01-18 09:57:13 +00:00
|
|
|
import { Account, Class, Doc, generateId, IdMap, Ref, Space, toIdMap } from '@hcengineering/core'
|
2023-09-28 10:50:37 +00:00
|
|
|
import { Loading, type AnySvelteComponent } from '@hcengineering/ui'
|
2022-09-21 08:08:25 +00:00
|
|
|
import { Attachment } from '@hcengineering/attachment'
|
2022-03-01 14:14:51 +00:00
|
|
|
import AttachmentPresenter from './AttachmentPresenter.svelte'
|
|
|
|
|
|
|
|
export let objectId: Ref<Doc>
|
|
|
|
export let space: Ref<Space>
|
|
|
|
export let _class: Ref<Class<Doc>>
|
2022-03-05 09:05:49 +00:00
|
|
|
export let content: string = ''
|
2023-01-13 03:10:29 +00:00
|
|
|
export let iconSend: Asset | AnySvelteComponent | undefined = undefined
|
|
|
|
export let labelSend: IntlString | undefined = undefined
|
2022-03-05 09:05:49 +00:00
|
|
|
export let showSend = true
|
2022-12-20 04:25:45 +00:00
|
|
|
export let shouldSaveDraft: boolean = false
|
2023-01-18 09:57:13 +00:00
|
|
|
export let attachments: IdMap<Attachment> = new Map()
|
2023-01-25 03:12:26 +00:00
|
|
|
export let loading = false
|
2023-05-04 13:21:06 +00:00
|
|
|
export let focusIndex: number = -1
|
2022-03-05 09:05:49 +00:00
|
|
|
export function submit (): void {
|
|
|
|
refInput.submit()
|
|
|
|
}
|
2023-01-06 07:31:30 +00:00
|
|
|
export let placeholder: IntlString | undefined = undefined
|
2023-01-13 03:10:29 +00:00
|
|
|
export let extraActions: RefAction[] | undefined = undefined
|
2023-01-06 07:31:30 +00:00
|
|
|
|
2022-03-05 09:05:49 +00:00
|
|
|
let refInput: ReferenceInput
|
2022-03-01 14:14:51 +00:00
|
|
|
|
|
|
|
let inputFile: HTMLInputElement
|
|
|
|
let saved = false
|
|
|
|
const dispatch = createEventDispatcher()
|
|
|
|
|
|
|
|
const client = getClient()
|
|
|
|
const query = createQuery()
|
2022-12-05 11:08:36 +00:00
|
|
|
|
2023-03-24 10:23:44 +00:00
|
|
|
const draftKey = `${objectId}_attachments`
|
|
|
|
const draftController = new DraftController<Record<Ref<Attachment>, Attachment>>(draftKey)
|
|
|
|
|
2022-12-20 04:25:45 +00:00
|
|
|
let draftAttachments: Record<Ref<Attachment>, Attachment> | undefined = undefined
|
2022-03-05 09:05:49 +00:00
|
|
|
let originalAttachments: Set<Ref<Attachment>> = new Set<Ref<Attachment>>()
|
2022-04-28 06:45:07 +00:00
|
|
|
const newAttachments: Set<Ref<Attachment>> = new Set<Ref<Attachment>>()
|
|
|
|
const removedAttachments: Set<Attachment> = new Set<Attachment>()
|
|
|
|
|
2023-09-28 10:50:37 +00:00
|
|
|
let progress = false
|
|
|
|
|
2022-06-27 08:16:09 +00:00
|
|
|
let refContainer: HTMLElement
|
|
|
|
|
2022-12-20 04:25:45 +00:00
|
|
|
$: objectId && updateAttachments(objectId)
|
|
|
|
|
|
|
|
async function updateAttachments (objectId: Ref<Doc>) {
|
2023-03-24 10:23:44 +00:00
|
|
|
draftAttachments = $draftsStore[draftKey]
|
2022-12-20 04:25:45 +00:00
|
|
|
if (draftAttachments && shouldSaveDraft) {
|
|
|
|
attachments.clear()
|
|
|
|
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()
|
|
|
|
} else {
|
|
|
|
query.query(
|
|
|
|
attachment.class.Attachment,
|
|
|
|
{
|
|
|
|
attachedTo: objectId
|
|
|
|
},
|
|
|
|
(res) => {
|
|
|
|
originalAttachments = new Set(res.map((p) => p._id))
|
2023-01-18 09:57:13 +00:00
|
|
|
attachments = toIdMap(res)
|
2022-12-20 04:25:45 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function saveDraft () {
|
2023-03-24 10:23:44 +00:00
|
|
|
if (shouldSaveDraft) {
|
2022-12-20 04:25:45 +00:00
|
|
|
draftAttachments = Object.fromEntries(attachments)
|
2023-03-24 10:23:44 +00:00
|
|
|
draftController.save(draftAttachments)
|
2022-12-20 04:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-01 14:14:51 +00:00
|
|
|
|
|
|
|
async function createAttachment (file: File) {
|
|
|
|
try {
|
2023-01-05 04:45:38 +00:00
|
|
|
const uuid = await uploadFile(file)
|
2022-03-05 09:05:49 +00:00
|
|
|
const _id: Ref<Attachment> = generateId()
|
|
|
|
attachments.set(_id, {
|
|
|
|
_id,
|
|
|
|
_class: attachment.class.Attachment,
|
|
|
|
collection: 'attachments',
|
|
|
|
modifiedOn: 0,
|
|
|
|
modifiedBy: '' as Ref<Account>,
|
|
|
|
space,
|
|
|
|
attachedTo: objectId,
|
|
|
|
attachedToClass: _class,
|
2022-03-01 14:14:51 +00:00
|
|
|
name: file.name,
|
|
|
|
file: uuid,
|
|
|
|
type: file.type,
|
|
|
|
size: file.size,
|
|
|
|
lastModified: file.lastModified
|
|
|
|
})
|
2022-03-05 09:05:49 +00:00
|
|
|
newAttachments.add(_id)
|
|
|
|
attachments = attachments
|
2022-12-20 04:25:45 +00:00
|
|
|
saveDraft()
|
2022-03-01 14:14:51 +00:00
|
|
|
} catch (err: any) {
|
|
|
|
setPlatformStatus(unknownError(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
async function saveAttachment (doc: Attachment): Promise<void> {
|
|
|
|
await client.addCollection(attachment.class.Attachment, space, objectId, _class, 'attachments', doc, doc._id)
|
2022-03-05 09:05:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 10:50:37 +00:00
|
|
|
async function fileSelected (): Promise<void> {
|
|
|
|
progress = true
|
|
|
|
await tick()
|
2022-03-01 14:14:51 +00:00
|
|
|
const list = inputFile.files
|
|
|
|
if (list === null || list.length === 0) return
|
|
|
|
for (let index = 0; index < list.length; index++) {
|
|
|
|
const file = list.item(index)
|
2023-09-28 10:50:37 +00:00
|
|
|
if (file !== null) {
|
|
|
|
await createAttachment(file)
|
|
|
|
}
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
2022-04-25 08:32:24 +00:00
|
|
|
inputFile.value = ''
|
2023-09-28 10:50:37 +00:00
|
|
|
progress = false
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 10:50:37 +00:00
|
|
|
async function fileDrop (e: DragEvent): Promise<void> {
|
|
|
|
progress = true
|
2022-03-01 14:14:51 +00:00
|
|
|
const list = e.dataTransfer?.files
|
|
|
|
if (list === undefined || list.length === 0) return
|
|
|
|
for (let index = 0; index < list.length; index++) {
|
|
|
|
const file = list.item(index)
|
2023-09-28 10:50:37 +00:00
|
|
|
if (file !== null) {
|
|
|
|
await createAttachment(file)
|
|
|
|
}
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
2023-09-28 10:50:37 +00:00
|
|
|
progress = false
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeAttachment (attachment: Attachment): Promise<void> {
|
2022-03-05 09:05:49 +00:00
|
|
|
removedAttachments.add(attachment)
|
|
|
|
attachments.delete(attachment._id)
|
2022-12-20 04:25:45 +00:00
|
|
|
if (shouldSaveDraft) {
|
2022-12-05 11:08:36 +00:00
|
|
|
await createAttachments()
|
|
|
|
}
|
2022-03-05 09:05:49 +00:00
|
|
|
attachments = attachments
|
2022-12-20 04:25:45 +00:00
|
|
|
saveDraft()
|
2022-03-05 09:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteAttachment (attachment: Attachment): Promise<void> {
|
|
|
|
if (originalAttachments.has(attachment._id)) {
|
2022-04-28 06:45:07 +00:00
|
|
|
await client.removeCollection(
|
|
|
|
attachment._class,
|
|
|
|
attachment.space,
|
|
|
|
attachment._id,
|
|
|
|
attachment.attachedTo,
|
|
|
|
attachment.attachedToClass,
|
|
|
|
'attachments'
|
|
|
|
)
|
2022-03-05 09:05:49 +00:00
|
|
|
} else {
|
|
|
|
await deleteFile(attachment.file)
|
|
|
|
}
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onDestroy(() => {
|
2022-12-20 04:25:45 +00:00
|
|
|
if (!saved && !shouldSaveDraft) {
|
2022-03-05 09:05:49 +00:00
|
|
|
newAttachments.forEach(async (p) => {
|
|
|
|
const attachment = attachments.get(p)
|
|
|
|
if (attachment !== undefined) {
|
|
|
|
await deleteAttachment(attachment)
|
|
|
|
}
|
2022-03-01 14:14:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-12-20 04:25:45 +00:00
|
|
|
export function removeDraft (removeFiles: boolean) {
|
2023-03-24 10:23:44 +00:00
|
|
|
draftController.remove()
|
2022-12-20 04:25:45 +00:00
|
|
|
if (removeFiles) {
|
|
|
|
newAttachments.forEach(async (p) => {
|
|
|
|
const attachment = attachments.get(p)
|
|
|
|
if (attachment !== undefined) {
|
|
|
|
await deleteFile(attachment.file)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:08:36 +00:00
|
|
|
export function createAttachments (): Promise<void> {
|
2022-03-01 14:14:51 +00:00
|
|
|
saved = true
|
2022-03-05 09:05:49 +00:00
|
|
|
const promises: Promise<any>[] = []
|
|
|
|
newAttachments.forEach((p) => {
|
|
|
|
const attachment = attachments.get(p)
|
|
|
|
if (attachment !== undefined) {
|
|
|
|
promises.push(saveAttachment(attachment))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
removedAttachments.forEach((p) => {
|
|
|
|
promises.push(deleteAttachment(p))
|
|
|
|
})
|
2022-12-05 11:08:36 +00:00
|
|
|
return Promise.all(promises).then()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onMessage (event: CustomEvent) {
|
2023-01-25 03:12:26 +00:00
|
|
|
loading = true
|
2023-02-15 03:14:20 +00:00
|
|
|
await createAttachments()
|
|
|
|
loading = false
|
|
|
|
dispatch('message', { message: event.detail, attachments: attachments.size })
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
2022-06-14 06:40:35 +00:00
|
|
|
|
2022-12-05 11:08:36 +00:00
|
|
|
async function onUpdate (event: CustomEvent) {
|
|
|
|
dispatch('update', { message: event.detail, attachments: attachments.size })
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:50:37 +00:00
|
|
|
async function pasteAction (evt: ClipboardEvent): Promise<void> {
|
2022-06-27 08:16:09 +00:00
|
|
|
let t: HTMLElement | null = evt.target as HTMLElement
|
|
|
|
let allowed = false
|
|
|
|
while (t != null) {
|
|
|
|
t = t.parentElement
|
|
|
|
if (t === refContainer) {
|
|
|
|
allowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!allowed) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 06:40:35 +00:00
|
|
|
const items = evt.clipboardData?.items ?? []
|
2023-09-28 10:50:37 +00:00
|
|
|
progress = true
|
2022-06-14 06:40:35 +00:00
|
|
|
for (const index in items) {
|
|
|
|
const item = items[index]
|
|
|
|
if (item.kind === 'file') {
|
|
|
|
const blob = item.getAsFile()
|
|
|
|
if (blob !== null) {
|
2023-09-28 10:50:37 +00:00
|
|
|
await createAttachment(blob)
|
2022-06-14 06:40:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 10:50:37 +00:00
|
|
|
progress = false
|
2022-06-14 06:40:35 +00:00
|
|
|
}
|
2022-03-01 14:14:51 +00:00
|
|
|
</script>
|
|
|
|
|
2023-04-21 07:43:01 +00:00
|
|
|
<div bind:this={refContainer} on:paste={pasteAction}>
|
2022-06-27 08:16:09 +00:00
|
|
|
<input
|
|
|
|
bind:this={inputFile}
|
|
|
|
multiple
|
|
|
|
type="file"
|
|
|
|
name="file"
|
|
|
|
id="file"
|
|
|
|
style="display: none"
|
|
|
|
on:change={fileSelected}
|
2022-04-28 06:45:07 +00:00
|
|
|
/>
|
2022-06-27 08:16:09 +00:00
|
|
|
<div
|
|
|
|
class="container"
|
|
|
|
on:dragover|preventDefault={() => {}}
|
|
|
|
on:dragleave={() => {}}
|
|
|
|
on:drop|preventDefault|stopPropagation={fileDrop}
|
|
|
|
>
|
2023-09-28 10:50:37 +00:00
|
|
|
{#if attachments.size || progress}
|
2022-06-27 08:16:09 +00:00
|
|
|
<div class="flex-row-center list scroll-divider-color">
|
2023-09-28 10:50:37 +00:00
|
|
|
{#if progress}
|
|
|
|
<div class="flex p-3">
|
|
|
|
<Loading />
|
|
|
|
</div>
|
|
|
|
{/if}
|
2022-06-27 08:16:09 +00:00
|
|
|
{#each Array.from(attachments.values()) as attachment}
|
|
|
|
<div class="item flex">
|
|
|
|
<AttachmentPresenter
|
|
|
|
value={attachment}
|
|
|
|
removable
|
|
|
|
on:remove={(result) => {
|
|
|
|
if (result !== undefined) removeAttachment(attachment)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
<ReferenceInput
|
2023-05-04 13:21:06 +00:00
|
|
|
{focusIndex}
|
2022-06-27 08:16:09 +00:00
|
|
|
bind:this={refInput}
|
|
|
|
{content}
|
2023-01-13 03:10:29 +00:00
|
|
|
{iconSend}
|
|
|
|
{labelSend}
|
2022-06-27 08:16:09 +00:00
|
|
|
{showSend}
|
2023-01-25 03:12:26 +00:00
|
|
|
{loading}
|
2023-06-08 10:29:42 +00:00
|
|
|
on:focus
|
|
|
|
on:blur
|
2022-06-27 08:16:09 +00:00
|
|
|
on:message={onMessage}
|
2022-11-22 05:51:29 +00:00
|
|
|
haveAttachment={attachments.size > 0}
|
2022-06-27 08:16:09 +00:00
|
|
|
withoutTopBorder={attachments.size > 0}
|
|
|
|
on:attach={() => {
|
2023-09-28 15:13:04 +00:00
|
|
|
dispatch('focus')
|
2022-06-27 08:16:09 +00:00
|
|
|
inputFile.click()
|
|
|
|
}}
|
2022-12-05 11:08:36 +00:00
|
|
|
on:update={onUpdate}
|
2023-01-06 07:31:30 +00:00
|
|
|
{placeholder}
|
2023-01-13 03:10:29 +00:00
|
|
|
{extraActions}
|
2022-06-27 08:16:09 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2022-03-01 14:14:51 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.list {
|
2022-05-05 14:34:27 +00:00
|
|
|
padding: 0.5rem;
|
2023-04-23 17:37:24 +00:00
|
|
|
color: var(--theme-caption-color);
|
2022-03-01 14:14:51 +00:00
|
|
|
overflow-x: auto;
|
2022-05-05 14:34:27 +00:00
|
|
|
overflow-y: hidden;
|
2023-04-23 17:37:24 +00:00
|
|
|
background-color: var(--theme-refinput-color);
|
|
|
|
border: 1px solid var(--theme-divider-color);
|
2022-05-05 14:34:27 +00:00
|
|
|
border-radius: 0.5rem 0.5rem 0 0;
|
|
|
|
border-bottom: none;
|
2022-03-01 14:14:51 +00:00
|
|
|
|
|
|
|
.item + .item {
|
|
|
|
padding-left: 1rem;
|
2023-04-23 17:37:24 +00:00
|
|
|
border-left: 1px solid var(--theme-divider-color);
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|