Files
huly-platform/plugins/text-editor-resources/src/components/MentionPopup.svelte
T
05f1d8395a Merge 9522 9513 (#8103)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
Signed-off-by: Victor Ilyushchenko <alt13ri@gmail.com>
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
Co-authored-by: Denis Bykhov <bykhov.denis@gmail.com>
Co-authored-by: Alexander Onnikov <aonnikov@hardcoreeng.com>
Co-authored-by: Victor Ilyushchenko <alt13ri@gmail.com>
2025-02-27 16:57:36 +07:00

143 lines
4.5 KiB
Svelte

<!--
// Copyright © 2020, 2021 Anticrm Platform Contributors.
// Copyright © 2021, 2023, 2024 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 { SearchResultDoc } from '@hcengineering/core'
import presentation, { SearchResult, reduceCalls, searchFor, type SearchItem } from '@hcengineering/presentation'
import { Label, ListView, resizeObserver } from '@hcengineering/ui'
import { createEventDispatcher } from 'svelte'
import { getReferenceLabel, getReferenceObject } from './extension/reference'
export let query: string = ''
let items: SearchItem[] = []
const dispatch = createEventDispatcher()
let list: ListView
let scrollContainer: HTMLElement
let selection = 0
async function handleSelectItem (item: SearchResultDoc): Promise<void> {
const obj = (await getReferenceObject(item.doc._class, item.doc._id)) ?? item.doc
const label = await getReferenceLabel(obj._class, obj._id)
dispatch('close', {
id: obj._id,
label,
objectclass: obj._class
})
}
export function onKeyDown (key: KeyboardEvent): boolean {
if (key.key === 'ArrowDown') {
key.stopPropagation()
key.preventDefault()
list?.select(selection + 1)
return true
}
if (key.key === 'ArrowUp') {
key.stopPropagation()
key.preventDefault()
if (selection === 0 && scrollContainer !== undefined) {
scrollContainer.scrollTop = 0
}
list?.select(selection - 1)
return true
}
if (key.key === 'Enter' || key.key === 'Tab') {
key.preventDefault()
key.stopPropagation()
if (selection < items.length) {
const searchItem = items[selection]
void handleSelectItem(searchItem.item)
return true
} else {
return false
}
}
return false
}
const updateItems = reduceCalls(async function (localQuery: string): Promise<void> {
const r = await searchFor('mention', localQuery)
if (r.query === query) {
items = r.items
}
})
$: void updateItems(query)
</script>
{#if (items.length === 0 && query !== '') || items.length > 0}
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<form class="antiPopup mentionPoup" on:keydown={onKeyDown} use:resizeObserver={() => dispatch('changeSize')}>
<div class="ap-scroll" bind:this={scrollContainer}>
<div class="ap-box">
{#if items.length === 0 && query !== ''}
<div class="noResults"><Label label={presentation.string.NoResults} /></div>
{/if}
{#if items.length > 0}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<ListView bind:this={list} bind:selection count={items.length}>
<svelte:fragment slot="category" let:item={num}>
{@const item = items[num]}
{#if item.num === 0}
<div class="mentonCategory">
<Label label={item.category.title} />
</div>
{/if}
</svelte:fragment>
<svelte:fragment slot="item" let:item={num}>
{@const item = items[num]}
{@const doc = item.item}
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="ap-menuItem withComp h-8"
on:click={() => {
void handleSelectItem(doc)
}}
>
<SearchResult value={doc} />
</div>
</svelte:fragment>
</ListView>
{/if}
</div>
</div>
<div class="ap-space x2" />
</form>
{/if}
<style lang="scss">
.noResults {
display: flex;
padding: 0.25rem 1rem;
align-items: center;
align-self: stretch;
}
.mentionPoup {
padding-top: 0.5rem;
}
.mentonCategory {
padding: 0.5rem 1rem;
font-size: 0.625rem;
letter-spacing: 0.0625rem;
color: var(--theme-dark-color);
text-transform: uppercase;
line-height: 1rem;
}
</style>