-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathLinkPopup.svelte
More file actions
168 lines (153 loc) · 4.89 KB
/
LinkPopup.svelte
File metadata and controls
168 lines (153 loc) · 4.89 KB
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
<!--
// Copyright © 2022 Anticrm Platform Contributors.
//
// 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 { type Ref, SortingOrder } from '@hcengineering/core'
import { createEventDispatcher, onDestroy } from 'svelte'
import textEditor from '@hcengineering/text-editor'
import { getEmbeddedLabel } from '@hcengineering/platform'
import presentation, { Card, getClient } from '@hcengineering/presentation'
import { EditBox, Label, ListView } from '@hcengineering/ui'
import { buildReferenceUrl } from './extension/reference'
import document, { type Document } from '@hcengineering/document'
export let link = ''
const dispatch = createEventDispatcher()
const client = getClient()
const linkPlaceholder = getEmbeddedLabel('URL or document name')
let items: Document[] = []
let list: ListView
let selection = 0
let searchQuery = ''
let debounceTimer: any
function isUrl (text: string): boolean {
if (text.length === 0) return false
return text.includes('://') || text.startsWith('http') || text.startsWith('www.')
}
function save (): void {
dispatch('update', link)
}
function selectItem (doc: Document): void {
const refUrl = buildReferenceUrl({
id: doc._id,
objectclass: doc._class,
label: doc.title
})
if (refUrl !== undefined) {
link = refUrl
}
}
async function doSearch (query: string): Promise<void> {
if (query.length === 0 || isUrl(query)) {
items = []
return
}
try {
const r = await client.findAll(
document.class.Document,
{ title: { $like: `%${query}%` } },
{ limit: 10, sort: { title: SortingOrder.Ascending } }
)
if (query === searchQuery) {
items = r
selection = 0
}
} catch (e) {
console.error('LinkPopup search error:', e)
items = []
}
}
function onInput (value: string): void {
searchQuery = value
clearTimeout(debounceTimer)
if (value.length === 0 || isUrl(value) || value.startsWith('ref://')) {
items = []
return
}
debounceTimer = setTimeout(() => {
void doSearch(value)
}, 200)
}
$: onInput(link)
onDestroy(() => {
clearTimeout(debounceTimer)
})
function handleKeydown (event: KeyboardEvent): void {
if (items.length === 0) return
if (event.key === 'ArrowDown') {
event.preventDefault()
event.stopPropagation()
selection = Math.min(selection + 1, items.length - 1)
list?.select(selection)
} else if (event.key === 'ArrowUp') {
event.preventDefault()
event.stopPropagation()
selection = Math.max(selection - 1, 0)
list?.select(selection)
} else if (event.key === 'Enter') {
event.preventDefault()
event.stopPropagation()
selectItem(items[selection])
}
}
$: canSave = link === '' || link.startsWith('ref://') || URL.canParse(link)
</script>
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div on:keydown|capture={handleKeydown}>
<Card
label={textEditor.string.Link}
okLabel={textEditor.string.Save}
okAction={save}
{canSave}
on:close={() => {
dispatch('close')
}}
on:changeContent
>
<EditBox placeholder={linkPlaceholder} bind:value={link} autoFocus />
{#if items.length > 0}
<div class="searchResults">
<ListView bind:this={list} bind:selection count={items.length}>
<svelte:fragment slot="item" let:item={num}>
{@const item = items[num]}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="ap-menuItem withComp h-8"
style="padding-left: 0.75rem; display: flex; align-items: center;"
on:click={() => {
selectItem(item)
}}
>
<span class="overflow-label">{item.title}</span>
</div>
</svelte:fragment>
</ListView>
</div>
{:else if searchQuery.length > 0 && !isUrl(searchQuery) && !searchQuery.startsWith('ref://')}
<div class="noResults"><Label label={presentation.string.NoResults} /></div>
{/if}
</Card>
</div>
<style lang="scss">
.searchResults {
max-height: 15rem;
overflow-y: auto;
margin-top: 0.5rem;
}
.noResults {
display: flex;
padding: 0.25rem 0;
color: var(--theme-dark-color);
}
</style>