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">
|
|
|
|
import { createQuery, getClient } from '@anticrm/presentation'
|
|
|
|
import { ReferenceInput } from '@anticrm/text-editor'
|
|
|
|
import { deleteFile, uploadFile } from '../utils'
|
|
|
|
import attachment from '../plugin'
|
|
|
|
import { setPlatformStatus, unknownError } from '@anticrm/platform'
|
|
|
|
import { createEventDispatcher, onDestroy } from 'svelte'
|
2022-03-05 09:05:49 +00:00
|
|
|
import { Account, Class, Doc, generateId, Ref, Space } from '@anticrm/core'
|
2022-03-01 14:14:51 +00:00
|
|
|
import { Attachment } from '@anticrm/attachment'
|
|
|
|
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 = ''
|
|
|
|
export let showSend = true
|
|
|
|
export function submit (): void {
|
|
|
|
refInput.submit()
|
|
|
|
}
|
|
|
|
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-03-05 09:05:49 +00:00
|
|
|
let attachments: Map<Ref<Attachment>, Attachment> = new Map<Ref<Attachment>, Attachment>()
|
|
|
|
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>()
|
|
|
|
|
|
|
|
$: objectId &&
|
|
|
|
query.query(
|
|
|
|
attachment.class.Attachment,
|
|
|
|
{
|
|
|
|
attachedTo: objectId
|
|
|
|
},
|
|
|
|
(res) => {
|
|
|
|
originalAttachments = new Set(res.map((p) => p._id))
|
|
|
|
attachments = new Map(res.map((p) => [p._id, p]))
|
|
|
|
}
|
|
|
|
)
|
2022-03-01 14:14:51 +00:00
|
|
|
|
|
|
|
async function createAttachment (file: File) {
|
|
|
|
try {
|
|
|
|
const uuid = await uploadFile(file, { space, attachedTo: objectId })
|
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-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
|
|
|
}
|
|
|
|
|
2022-03-01 14:14:51 +00:00
|
|
|
function fileSelected () {
|
|
|
|
const list = inputFile.files
|
|
|
|
if (list === null || list.length === 0) return
|
|
|
|
for (let index = 0; index < list.length; index++) {
|
|
|
|
const file = list.item(index)
|
|
|
|
if (file !== null) createAttachment(file)
|
|
|
|
}
|
2022-04-25 08:32:24 +00:00
|
|
|
inputFile.value = ''
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fileDrop (e: DragEvent) {
|
|
|
|
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)
|
|
|
|
if (file !== null) createAttachment(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeAttachment (attachment: Attachment): Promise<void> {
|
2022-03-05 09:05:49 +00:00
|
|
|
removedAttachments.add(attachment)
|
|
|
|
attachments.delete(attachment._id)
|
|
|
|
attachments = attachments
|
|
|
|
}
|
|
|
|
|
|
|
|
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(() => {
|
|
|
|
if (!saved) {
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
async function onMessage (event: CustomEvent) {
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
await Promise.all(promises)
|
|
|
|
dispatch('message', { message: event.detail, attachments: attachments.size })
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
2022-06-14 06:40:35 +00:00
|
|
|
|
|
|
|
function pasteAction (evt: ClipboardEvent): void {
|
|
|
|
const items = evt.clipboardData?.items ?? []
|
|
|
|
for (const index in items) {
|
|
|
|
const item = items[index]
|
|
|
|
if (item.kind === 'file') {
|
|
|
|
const blob = item.getAsFile()
|
|
|
|
if (blob !== null) {
|
|
|
|
createAttachment(blob)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-01 14:14:51 +00:00
|
|
|
</script>
|
|
|
|
|
2022-06-14 06:40:35 +00:00
|
|
|
<svelte:window on:paste={pasteAction} />
|
|
|
|
|
2022-03-01 14:14:51 +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
|
|
|
/>
|
|
|
|
<div
|
|
|
|
class="container"
|
2022-03-01 14:14:51 +00:00
|
|
|
on:dragover|preventDefault={() => {}}
|
|
|
|
on:dragleave={() => {}}
|
|
|
|
on:drop|preventDefault|stopPropagation={fileDrop}
|
2022-04-28 06:45:07 +00:00
|
|
|
>
|
2022-03-05 09:05:49 +00:00
|
|
|
{#if attachments.size}
|
2022-05-05 14:34:27 +00:00
|
|
|
<div class="flex-row-center list scroll-divider-color">
|
2022-03-05 09:05:49 +00:00
|
|
|
{#each Array.from(attachments.values()) as attachment}
|
2022-04-28 06:45:07 +00:00
|
|
|
<div class="item flex">
|
2022-05-05 14:34:27 +00:00
|
|
|
<AttachmentPresenter
|
|
|
|
value={attachment}
|
|
|
|
removable
|
|
|
|
on:remove={(result) => {
|
|
|
|
if (result !== undefined) removeAttachment(attachment)
|
|
|
|
}}
|
|
|
|
/>
|
2022-03-01 14:14:51 +00:00
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{/if}
|
2022-04-28 06:45:07 +00:00
|
|
|
<ReferenceInput
|
|
|
|
bind:this={refInput}
|
|
|
|
{content}
|
|
|
|
{showSend}
|
|
|
|
on:message={onMessage}
|
|
|
|
withoutTopBorder={attachments.size > 0}
|
|
|
|
on:attach={() => {
|
|
|
|
inputFile.click()
|
|
|
|
}}
|
|
|
|
/>
|
2022-03-01 14:14:51 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.list {
|
2022-05-05 14:34:27 +00:00
|
|
|
padding: 0.5rem;
|
2022-03-01 14:14:51 +00:00
|
|
|
color: var(--theme-caption-color);
|
|
|
|
overflow-x: auto;
|
2022-05-05 14:34:27 +00:00
|
|
|
overflow-y: hidden;
|
|
|
|
background-color: var(--accent-bg-color);
|
|
|
|
border: 1px solid var(--divider-color);
|
|
|
|
border-radius: 0.5rem 0.5rem 0 0;
|
|
|
|
border-bottom: none;
|
2022-03-01 14:14:51 +00:00
|
|
|
|
|
|
|
.item + .item {
|
|
|
|
padding-left: 1rem;
|
2022-05-05 14:34:27 +00:00
|
|
|
border-left: 1px solid var(--divider-color);
|
2022-03-01 14:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|