@m3ui-vue/m3ui-vue 1.6.0 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/API.md +940 -33
- package/dist/{MButton-CPghlKb0.js → MButton-OQ4wIERQ.js} +2 -2
- package/dist/{MButton-CPghlKb0.js.map → MButton-OQ4wIERQ.js.map} +1 -1
- package/dist/MColorPickerModal-NtkihoK6.js +577 -0
- package/dist/MColorPickerModal-NtkihoK6.js.map +1 -0
- package/dist/{MDialog-X50At1Gk.js → MDialog-DYMnaBlt.js} +3 -3
- package/dist/{MDialog-X50At1Gk.js.map → MDialog-DYMnaBlt.js.map} +1 -1
- package/dist/{MListItem-DVi1j1QI.js → MListItem-D2cNUyzP.js} +211 -168
- package/dist/MListItem-D2cNUyzP.js.map +1 -0
- package/dist/{MLoadingIndicator-vooA3110.js → MLoadingIndicator-DWUj0BAB.js} +2 -2
- package/dist/{MLoadingIndicator-vooA3110.js.map → MLoadingIndicator-DWUj0BAB.js.map} +1 -1
- package/dist/{MMenuItem-CRNZkVVr.js → MMenuItem-BuxYoASE.js} +133 -151
- package/dist/MMenuItem-BuxYoASE.js.map +1 -0
- package/dist/{MSeekBar-BL2W5_rD.js → MSeekBar-C4cn9RIF.js} +2 -2
- package/dist/{MSeekBar-BL2W5_rD.js.map → MSeekBar-C4cn9RIF.js.map} +1 -1
- package/dist/{MSlider-B9NcKTmZ.js → MSlider-BIkA6DiE.js} +2 -2
- package/dist/{MSlider-B9NcKTmZ.js.map → MSlider-BIkA6DiE.js.map} +1 -1
- package/dist/{MSpinner-_XpTRkAg.js → MSpinner-DZkXZJpc.js} +2 -2
- package/dist/{MSpinner-_XpTRkAg.js.map → MSpinner-DZkXZJpc.js.map} +1 -1
- package/dist/{MTextField-DpcPmR5B.js → MTextField-CNH-byir.js} +2 -2
- package/dist/{MTextField-DpcPmR5B.js.map → MTextField-CNH-byir.js.map} +1 -1
- package/dist/MTooltip-CK39Zon4.js +183 -0
- package/dist/MTooltip-CK39Zon4.js.map +1 -0
- package/dist/{_useEscapeStack-C5-BYr8y.js → _popupPosition-nL8SVKPK.js} +20 -2
- package/dist/_popupPosition-nL8SVKPK.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MAddressField.vue.d.ts +54 -0
- package/dist/components/MAudioPlayer.vue.d.ts +1 -1
- package/dist/components/MAudioRecorder.vue.d.ts +51 -0
- package/dist/components/MAvailabilityCalendar.vue.d.ts +1 -1
- package/dist/components/MCameraCapture.vue.d.ts +51 -0
- package/dist/components/MCollapse.vue.d.ts +32 -0
- package/dist/components/MCountrySelect.vue.d.ts +41 -0
- package/dist/components/MCronField.vue.d.ts +26 -0
- package/dist/components/MDashboardGrid.vue.d.ts +65 -0
- package/dist/components/MDiffViewer.vue.d.ts +56 -0
- package/dist/components/MExpansionPanel.vue.d.ts +26 -3
- package/dist/components/MFilterBuilder.vue.d.ts +22 -0
- package/dist/components/MFlex.vue.d.ts +1 -1
- package/dist/components/MMapCluster.vue.d.ts +80 -0
- package/dist/components/MMapDraw.vue.d.ts +79 -0
- package/dist/components/MMentionField.vue.d.ts +102 -0
- package/dist/components/MNotificationCenter.vue.d.ts +57 -0
- package/dist/components/MQRScanner.vue.d.ts +52 -0
- package/dist/components/MQuiz.vue.d.ts +1 -1
- package/dist/components/MSimpleCarousel.vue.d.ts +1 -1
- package/dist/components/MStack.vue.d.ts +1 -1
- package/dist/components/MTimezoneSelect.vue.d.ts +39 -0
- package/dist/components/MToolbar.vue.d.ts +1 -1
- package/dist/components/MVideoPlayer.vue.d.ts +1 -1
- package/dist/components/_MFilterGroup.vue.d.ts +17 -0
- package/dist/composables/_caretPosition.d.ts +6 -0
- package/dist/composables/_useCamera.d.ts +16 -0
- package/dist/composables/_useFollowAnchor.d.ts +15 -0
- package/dist/composables/cron.d.ts +29 -0
- package/dist/composables/dashboardLayout.d.ts +28 -0
- package/dist/composables/filterBuilder.d.ts +55 -0
- package/dist/composables/mapAdapters.d.ts +6 -1
- package/dist/composables/mapSearch.d.ts +1 -0
- package/dist/composables/textDiff.d.ts +24 -0
- package/dist/composables/useLocale.d.ts +108 -0
- package/dist/draw.js +28 -27
- package/dist/draw.js.map +1 -1
- package/dist/flow.js +1 -1
- package/dist/gantt.js +1 -1
- package/dist/index.d.ts +25 -0
- package/dist/locales/index.js +749 -0
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +10444 -7495
- package/dist/m3ui.js.map +1 -1
- package/dist/map.d.ts +5 -0
- package/dist/map.js +1509 -617
- package/dist/map.js.map +1 -1
- package/dist/media.js +5 -5
- package/dist/pdf.js +5 -5
- package/dist/rich-text-editor.js +5 -5
- package/dist/styles.css +1 -1
- package/dist/{useLocale-COav9m8C.js → useLocale-DaVSTdm0.js} +108 -1
- package/dist/useLocale-DaVSTdm0.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MAddressField.test.ts +149 -0
- package/src/__tests__/MAudioRecorder.test.ts +125 -0
- package/src/__tests__/MCameraCapture.test.ts +154 -0
- package/src/__tests__/MCountrySelect.test.ts +67 -0
- package/src/__tests__/MCronField.test.ts +100 -0
- package/src/__tests__/MDashboardGrid.test.ts +122 -0
- package/src/__tests__/MDiffViewer.test.ts +68 -0
- package/src/__tests__/MExpansionPanel.test.ts +107 -0
- package/src/__tests__/MFilterBuilder.test.ts +130 -0
- package/src/__tests__/MMapCluster.test.ts +127 -0
- package/src/__tests__/MMapDraw.test.ts +188 -0
- package/src/__tests__/MMapLayers.test.ts +56 -0
- package/src/__tests__/MMentionField.test.ts +163 -0
- package/src/__tests__/MNotificationCenter.test.ts +94 -0
- package/src/__tests__/_fakeMaplibre.ts +14 -3
- package/src/__tests__/attrsFallthrough.test.ts +4 -0
- package/src/__tests__/cron.test.ts +68 -0
- package/src/__tests__/dashboardLayout.test.ts +45 -0
- package/src/__tests__/filterBuilder.test.ts +91 -0
- package/src/__tests__/followAnchor.test.ts +162 -0
- package/src/__tests__/ssr.test.ts +3 -0
- package/src/__tests__/textDiff.test.ts +37 -0
- package/src/components/MAddressField.vue +301 -0
- package/src/components/MAudioRecorder.vue +334 -0
- package/src/components/MAutocomplete.vue +3 -7
- package/src/components/MCameraCapture.vue +156 -0
- package/src/components/MCollapse.vue +91 -0
- package/src/components/MColorPicker.vue +4 -8
- package/src/components/MCountrySelect.vue +89 -0
- package/src/components/MCronField.vue +251 -0
- package/src/components/MDashboardGrid.vue +382 -0
- package/src/components/MDatePicker.vue +10 -7
- package/src/components/MDateRangePicker.vue +10 -7
- package/src/components/MDiffViewer.vue +234 -0
- package/src/components/MExpansionPanel.vue +56 -48
- package/src/components/MFilterBuilder.vue +47 -0
- package/src/components/MMapBaseLayers.vue +29 -28
- package/src/components/MMapCluster.vue +295 -0
- package/src/components/MMapDraw.vue +601 -0
- package/src/components/MMapMeasure.vue +1 -1
- package/src/components/MMapSearch.vue +1 -9
- package/src/components/MMentionField.vue +373 -0
- package/src/components/MMultiAutocomplete.vue +3 -7
- package/src/components/MMultiSelect.vue +3 -7
- package/src/components/MNotificationCenter.vue +252 -0
- package/src/components/MPhoneInput.vue +3 -10
- package/src/components/MQRScanner.vue +177 -0
- package/src/components/MSelect.vue +3 -7
- package/src/components/MTimePicker.vue +10 -7
- package/src/components/MTimezoneSelect.vue +116 -0
- package/src/components/_MFilterGroup.vue +210 -0
- package/src/composables/_caretPosition.ts +44 -0
- package/src/composables/_useCamera.ts +66 -0
- package/src/composables/_useFollowAnchor.ts +109 -0
- package/src/composables/cron.ts +179 -0
- package/src/composables/dashboardLayout.ts +94 -0
- package/src/composables/filterBuilder.ts +195 -0
- package/src/composables/mapAdapters.ts +29 -15
- package/src/composables/mapSearch.ts +10 -0
- package/src/composables/textDiff.ts +106 -0
- package/src/composables/useLocale.ts +215 -0
- package/src/index.ts +25 -0
- package/src/locales/de.ts +107 -0
- package/src/locales/es.ts +107 -0
- package/src/locales/fr.ts +107 -0
- package/src/locales/ja.ts +107 -0
- package/src/locales/ko.ts +107 -0
- package/src/locales/pt.ts +107 -0
- package/src/locales/zh.ts +107 -0
- package/src/map.ts +5 -0
- package/dist/MListItem-DVi1j1QI.js.map +0 -1
- package/dist/MMenuItem-CRNZkVVr.js.map +0 -1
- package/dist/MTooltip-BL3jPYPT.js +0 -754
- package/dist/MTooltip-BL3jPYPT.js.map +0 -1
- package/dist/_useEscapeStack-C5-BYr8y.js.map +0 -1
- package/dist/useLocale-COav9m8C.js.map +0 -1
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* A text field with @mentions and #tags: typing a trigger character (`@`, `#`, or any of `sources`) at the start of a
|
|
4
|
+
* word opens a list at the caret, filtered as you type, from a source's `items` or its `search` function (an API,
|
|
5
|
+
* debounced). Arrow keys move, Enter or Tab picks, Escape closes; picking writes the trigger and the item's label
|
|
6
|
+
* into the text. The text is the v-model, plain; the picked items that are still in it are `v-model:mentions`, with
|
|
7
|
+
* their ids. Multi-line by default (a chat composer, a comment box). Built on `MTextField`. Keys it uses while the
|
|
8
|
+
* list is open (arrows, Enter, Tab) are `preventDefault`ed before your own `@keydown` sees them, so a composer that
|
|
9
|
+
* sends on Enter checks `e.defaultPrevented`.
|
|
10
|
+
*
|
|
11
|
+
* @slot item One entry of the list instead of the default (avatar or icon, label, description): `{ item, source, active }`
|
|
12
|
+
* @event update:modelValue The text changed
|
|
13
|
+
* @event update:mentions The picked items still in the text changed
|
|
14
|
+
* @event mention An item was picked: `(item, trigger)`
|
|
15
|
+
*/
|
|
16
|
+
import { computed, nextTick, onBeforeUnmount, ref, shallowRef, useId, watch, watchEffect } from 'vue'
|
|
17
|
+
import MAvatar from './MAvatar.vue'
|
|
18
|
+
import MIcon from './MIcon.vue'
|
|
19
|
+
import MSpinner from './MSpinner.vue'
|
|
20
|
+
import MTextField from './MTextField.vue'
|
|
21
|
+
import { useLocale } from '../composables/useLocale'
|
|
22
|
+
import { useEscapeStack } from '../composables/_useEscapeStack'
|
|
23
|
+
import { useFollowAnchor } from '../composables/_useFollowAnchor'
|
|
24
|
+
import { POPUP_MARGIN, fitPopup } from '../composables/_popupPosition'
|
|
25
|
+
import { caretRect } from '../composables/_caretPosition'
|
|
26
|
+
|
|
27
|
+
/** Something that can be mentioned. */
|
|
28
|
+
export interface MentionItem {
|
|
29
|
+
id: string | number
|
|
30
|
+
/** What's written into the text after the trigger (spaces allowed: `@Ana López`) */
|
|
31
|
+
label: string
|
|
32
|
+
/** A second line in the list */
|
|
33
|
+
description?: string
|
|
34
|
+
/** An image URL; without one, initials (or `icon`) */
|
|
35
|
+
avatar?: string
|
|
36
|
+
/** A Material Symbol instead of an avatar */
|
|
37
|
+
icon?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Where a trigger character looks things up. */
|
|
41
|
+
export interface MentionSource {
|
|
42
|
+
/** One character: `@`, `#`, `:`… */
|
|
43
|
+
trigger: string
|
|
44
|
+
/** The items, filtered here as the user types */
|
|
45
|
+
items?: MentionItem[]
|
|
46
|
+
/** Or a lookup of yours (an API): the query in, items out; `signal` aborts a replaced one */
|
|
47
|
+
search?: (query: string, signal: AbortSignal) => Promise<MentionItem[]>
|
|
48
|
+
/** How many to list (8) */
|
|
49
|
+
limit?: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A picked item still in the text. */
|
|
53
|
+
export interface MentionRef {
|
|
54
|
+
trigger: string
|
|
55
|
+
id: string | number
|
|
56
|
+
label: string
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The root is the field plus a teleported list: attributes go on the field
|
|
60
|
+
defineOptions({ inheritAttrs: false })
|
|
61
|
+
|
|
62
|
+
const props = withDefaults(
|
|
63
|
+
defineProps<{
|
|
64
|
+
/** The text (v-model) */
|
|
65
|
+
modelValue: string
|
|
66
|
+
/** The picked items still in the text (v-model:mentions) */
|
|
67
|
+
mentions?: MentionRef[]
|
|
68
|
+
/** Trigger characters and where each looks things up */
|
|
69
|
+
sources: MentionSource[]
|
|
70
|
+
/** Field label */
|
|
71
|
+
label: string
|
|
72
|
+
/** Field style */
|
|
73
|
+
variant?: 'filled' | 'outlined'
|
|
74
|
+
/** Several lines (a textarea) */
|
|
75
|
+
multiline?: boolean
|
|
76
|
+
/** Rows when multiline */
|
|
77
|
+
rows?: number
|
|
78
|
+
/** Grow with the text when multiline */
|
|
79
|
+
autoGrow?: boolean
|
|
80
|
+
/** Stop growing at this many rows */
|
|
81
|
+
maxRows?: number
|
|
82
|
+
/** The delay before a `search`, in ms */
|
|
83
|
+
debounce?: number
|
|
84
|
+
/** Material Symbol on the left */
|
|
85
|
+
leadingIcon?: string
|
|
86
|
+
/** Helper text below the field */
|
|
87
|
+
hint?: string
|
|
88
|
+
/** Error state */
|
|
89
|
+
error?: boolean
|
|
90
|
+
/** Error message shown when `error` is true */
|
|
91
|
+
errorLabel?: string
|
|
92
|
+
/** Marks it as required */
|
|
93
|
+
required?: boolean
|
|
94
|
+
/** Disables the field */
|
|
95
|
+
disabled?: boolean
|
|
96
|
+
}>(),
|
|
97
|
+
{ mentions: () => [], variant: 'filled', multiline: true, rows: 3, autoGrow: true, debounce: 200 },
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
const emit = defineEmits<{
|
|
101
|
+
'update:modelValue': [text: string]
|
|
102
|
+
'update:mentions': [mentions: MentionRef[]]
|
|
103
|
+
mention: [item: MentionItem, trigger: string]
|
|
104
|
+
}>()
|
|
105
|
+
|
|
106
|
+
const t = useLocale()
|
|
107
|
+
const uid = useId()
|
|
108
|
+
const listId = `${uid}-list`
|
|
109
|
+
const optionId = (i: number) => `${uid}-opt-${i}`
|
|
110
|
+
|
|
111
|
+
const field = ref<InstanceType<typeof MTextField>>()
|
|
112
|
+
const listEl = ref<HTMLElement>()
|
|
113
|
+
const inputEl = computed(() => (field.value?.inputEl as HTMLTextAreaElement | HTMLInputElement | undefined) ?? null)
|
|
114
|
+
|
|
115
|
+
/** The word being typed after a trigger: where it starts, which source, and the text so far */
|
|
116
|
+
const query = shallowRef<{ source: MentionSource; start: number; text: string } | null>(null)
|
|
117
|
+
const results = shallowRef<MentionItem[]>([])
|
|
118
|
+
const loading = ref(false)
|
|
119
|
+
const active = ref(0)
|
|
120
|
+
const listPos = ref<Record<string, string>>({})
|
|
121
|
+
let controller: AbortController | undefined
|
|
122
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
123
|
+
|
|
124
|
+
const open = computed(() => !!query.value && (loading.value || results.value.length > 0 || !!query.value.source.search))
|
|
125
|
+
|
|
126
|
+
// ── Finding the query ──────────────────────────────────────────────────────────────────────────
|
|
127
|
+
|
|
128
|
+
/** Accents and case don't matter when filtering */
|
|
129
|
+
const fold = (s: string) => s.normalize('NFD').replace(/\p{Diacritic}/gu, '').toLocaleLowerCase()
|
|
130
|
+
|
|
131
|
+
function detect() {
|
|
132
|
+
const el = inputEl.value
|
|
133
|
+
if (!el || props.disabled) return close()
|
|
134
|
+
const caret = el.selectionStart ?? 0
|
|
135
|
+
if (caret !== el.selectionEnd) return close()
|
|
136
|
+
const before = el.value.slice(0, caret)
|
|
137
|
+
let found: typeof query.value = null
|
|
138
|
+
for (const source of props.sources) {
|
|
139
|
+
const i = before.lastIndexOf(source.trigger)
|
|
140
|
+
if (i < 0 || (found && i < found.start)) continue
|
|
141
|
+
// At the start of a word, with no space typed since
|
|
142
|
+
if (i > 0 && !/\s/.test(before[i - 1]!)) continue
|
|
143
|
+
const text = before.slice(i + source.trigger.length)
|
|
144
|
+
if (/\s/.test(text) || text.length > 40) continue
|
|
145
|
+
found = { source, start: i, text }
|
|
146
|
+
}
|
|
147
|
+
if (!found) return close()
|
|
148
|
+
const q = query.value
|
|
149
|
+
// The same query (a caret move, a key that typed nothing) keeps its object: a search on its way checks it's still current
|
|
150
|
+
if (q && q.source === found.source && q.text === found.text && q.start === found.start) return
|
|
151
|
+
query.value = found
|
|
152
|
+
lookUp()
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function lookUp() {
|
|
156
|
+
const q = query.value
|
|
157
|
+
if (!q) return
|
|
158
|
+
const limit = q.source.limit ?? 8
|
|
159
|
+
active.value = 0
|
|
160
|
+
if (q.source.items) {
|
|
161
|
+
const f = fold(q.text)
|
|
162
|
+
const hits = q.source.items.filter((it) => fold(it.label).includes(f))
|
|
163
|
+
// Those starting with the query first
|
|
164
|
+
hits.sort((a, b) => Number(!fold(a.label).startsWith(f)) - Number(!fold(b.label).startsWith(f)))
|
|
165
|
+
results.value = hits.slice(0, limit)
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
if (!q.source.search) return
|
|
169
|
+
controller?.abort()
|
|
170
|
+
clearTimeout(timer)
|
|
171
|
+
loading.value = true
|
|
172
|
+
const search = q.source.search
|
|
173
|
+
timer = setTimeout(async () => {
|
|
174
|
+
controller = new AbortController()
|
|
175
|
+
const mine = controller
|
|
176
|
+
try {
|
|
177
|
+
const items = await search(q.text, mine.signal)
|
|
178
|
+
if (query.value === q) results.value = items.slice(0, limit)
|
|
179
|
+
} catch {
|
|
180
|
+
if (!mine.signal.aborted && query.value === q) results.value = []
|
|
181
|
+
} finally {
|
|
182
|
+
if (controller === mine) loading.value = false
|
|
183
|
+
}
|
|
184
|
+
}, props.debounce)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function close() {
|
|
188
|
+
query.value = null
|
|
189
|
+
results.value = []
|
|
190
|
+
loading.value = false
|
|
191
|
+
controller?.abort()
|
|
192
|
+
clearTimeout(timer)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ── Picking ────────────────────────────────────────────────────────────────────────────────────
|
|
196
|
+
|
|
197
|
+
function pick(item: MentionItem) {
|
|
198
|
+
const q = query.value
|
|
199
|
+
const el = inputEl.value
|
|
200
|
+
if (!q || !el) return
|
|
201
|
+
const caret = el.selectionStart ?? el.value.length
|
|
202
|
+
const inserted = `${q.source.trigger}${item.label} `
|
|
203
|
+
const text = props.modelValue.slice(0, q.start) + inserted + props.modelValue.slice(caret)
|
|
204
|
+
close()
|
|
205
|
+
emit('update:modelValue', text)
|
|
206
|
+
emit('mention', item, q.source.trigger)
|
|
207
|
+
emit('update:mentions', [...live(props.mentions, text), { trigger: q.source.trigger, id: item.id, label: item.label }])
|
|
208
|
+
const at = q.start + inserted.length
|
|
209
|
+
nextTick(() => {
|
|
210
|
+
el.focus()
|
|
211
|
+
el.setSelectionRange(at, at)
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The mentions whose text is still there, as many of each as times it appears. */
|
|
216
|
+
function live(list: MentionRef[], text: string) {
|
|
217
|
+
const left = new Map<string, number>()
|
|
218
|
+
return list.filter((m) => {
|
|
219
|
+
const token = `${m.trigger}${m.label}`
|
|
220
|
+
const used = left.get(token) ?? 0
|
|
221
|
+
const count = text.split(token).length - 1
|
|
222
|
+
if (used >= count) return false
|
|
223
|
+
left.set(token, used + 1)
|
|
224
|
+
return true
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function onText(value: string | number) {
|
|
229
|
+
const text = String(value)
|
|
230
|
+
emit('update:modelValue', text)
|
|
231
|
+
const kept = live(props.mentions, text)
|
|
232
|
+
if (kept.length !== props.mentions.length) emit('update:mentions', kept)
|
|
233
|
+
nextTick(detect)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function onKeydown(e: KeyboardEvent) {
|
|
237
|
+
if (!open.value || !results.value.length) return
|
|
238
|
+
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
239
|
+
e.preventDefault()
|
|
240
|
+
const n = results.value.length
|
|
241
|
+
active.value = (active.value + (e.key === 'ArrowDown' ? 1 : -1) + n) % n
|
|
242
|
+
nextTick(() => listEl.value?.querySelector(`#${CSS.escape(optionId(active.value))}`)?.scrollIntoView({ block: 'nearest' }))
|
|
243
|
+
} else if ((e.key === 'Enter' && !e.shiftKey) || e.key === 'Tab') {
|
|
244
|
+
e.preventDefault()
|
|
245
|
+
const item = results.value[active.value]
|
|
246
|
+
if (item) pick(item)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Caret moves without typing (clicks, arrows on a closed list, Home/End) can enter or leave a mention query
|
|
251
|
+
function onCaretMove(e: Event) {
|
|
252
|
+
if (e instanceof KeyboardEvent && open.value && ['ArrowDown', 'ArrowUp', 'Enter', 'Tab', 'Escape'].includes(e.key)) return
|
|
253
|
+
detect()
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
useEscapeStack(() => open.value, close)
|
|
257
|
+
|
|
258
|
+
// ── The list at the caret ──────────────────────────────────────────────────────────────────────
|
|
259
|
+
|
|
260
|
+
const WIDTH = 288
|
|
261
|
+
function place() {
|
|
262
|
+
const el = inputEl.value
|
|
263
|
+
const list = listEl.value
|
|
264
|
+
if (!el || !list || !query.value) return
|
|
265
|
+
const caret = caretRect(el, query.value.start)
|
|
266
|
+
const box = el.getBoundingClientRect()
|
|
267
|
+
// Kept to the field's height: a caret scrolled out of a tall textarea sits on its edge
|
|
268
|
+
const top = Math.min(Math.max(caret.top, box.top), box.bottom - caret.height)
|
|
269
|
+
const h = list.offsetHeight
|
|
270
|
+
const y = fitPopup([top + caret.height + 4, top - 4 - h], h, window.innerHeight, POPUP_MARGIN)
|
|
271
|
+
const x = fitPopup([caret.left, window.innerWidth - POPUP_MARGIN - WIDTH], WIDTH, window.innerWidth, POPUP_MARGIN)
|
|
272
|
+
listPos.value = { top: `${y}px`, left: `${x}px`, width: `${WIDTH}px` }
|
|
273
|
+
}
|
|
274
|
+
watch([open, results, loading], () => nextTick(place))
|
|
275
|
+
useFollowAnchor({ open: () => open.value, anchor: inputEl as never, popup: listEl, place, close })
|
|
276
|
+
|
|
277
|
+
// The field as an ARIA combobox while a list is open
|
|
278
|
+
watchEffect(() => {
|
|
279
|
+
const el = inputEl.value
|
|
280
|
+
if (!el) return
|
|
281
|
+
el.setAttribute('aria-autocomplete', 'list')
|
|
282
|
+
el.setAttribute('aria-controls', listId)
|
|
283
|
+
el.setAttribute('aria-expanded', String(open.value))
|
|
284
|
+
if (open.value && results.value.length) el.setAttribute('aria-activedescendant', optionId(active.value))
|
|
285
|
+
else el.removeAttribute('aria-activedescendant')
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
onBeforeUnmount(() => {
|
|
289
|
+
controller?.abort()
|
|
290
|
+
clearTimeout(timer)
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
defineExpose({
|
|
294
|
+
/** Focuses the field */
|
|
295
|
+
focus: () => field.value?.focus(),
|
|
296
|
+
/** Inserts text at the caret (a trigger, to open the list from a button: `insert('@')`) */
|
|
297
|
+
insert(text: string) {
|
|
298
|
+
const el = inputEl.value
|
|
299
|
+
if (!el) return
|
|
300
|
+
const start = el.selectionStart ?? props.modelValue.length
|
|
301
|
+
const end = el.selectionEnd ?? start
|
|
302
|
+
const value = props.modelValue.slice(0, start) + text + props.modelValue.slice(end)
|
|
303
|
+
emit('update:modelValue', value)
|
|
304
|
+
nextTick(() => {
|
|
305
|
+
el.focus()
|
|
306
|
+
el.setSelectionRange(start + text.length, start + text.length)
|
|
307
|
+
detect()
|
|
308
|
+
})
|
|
309
|
+
},
|
|
310
|
+
})
|
|
311
|
+
</script>
|
|
312
|
+
|
|
313
|
+
<template>
|
|
314
|
+
<div v-bind="$attrs" @keydown.capture="onKeydown" @keyup="onCaretMove" @click="onCaretMove" @focusout="close">
|
|
315
|
+
<MTextField
|
|
316
|
+
ref="field"
|
|
317
|
+
:model-value="modelValue"
|
|
318
|
+
:label="label"
|
|
319
|
+
:variant="variant"
|
|
320
|
+
:multiline="multiline"
|
|
321
|
+
:rows="rows"
|
|
322
|
+
:auto-grow="autoGrow"
|
|
323
|
+
:max-rows="maxRows"
|
|
324
|
+
:leading-icon="leadingIcon"
|
|
325
|
+
:hint="hint"
|
|
326
|
+
:error="error"
|
|
327
|
+
:error-label="errorLabel"
|
|
328
|
+
:required="required"
|
|
329
|
+
:disabled="disabled"
|
|
330
|
+
@update:model-value="onText"
|
|
331
|
+
/>
|
|
332
|
+
</div>
|
|
333
|
+
|
|
334
|
+
<Teleport to="body">
|
|
335
|
+
<div
|
|
336
|
+
v-if="open"
|
|
337
|
+
:id="listId"
|
|
338
|
+
ref="listEl"
|
|
339
|
+
role="listbox"
|
|
340
|
+
:aria-label="label"
|
|
341
|
+
class="fixed z-500 max-h-72 overflow-auto rounded-xs bg-surface-container py-1 shadow-elevation-2"
|
|
342
|
+
:style="listPos"
|
|
343
|
+
>
|
|
344
|
+
<div v-if="loading && !results.length" class="flex items-center gap-3 px-4 py-3 text-body-medium text-on-surface-variant" role="status">
|
|
345
|
+
<MSpinner :size="20" />{{ t.loading }}
|
|
346
|
+
</div>
|
|
347
|
+
<div v-else-if="!results.length" class="px-4 py-3 text-body-medium text-on-surface-variant" role="status">{{ t.noResults }}</div>
|
|
348
|
+
<div
|
|
349
|
+
v-for="(item, i) in results"
|
|
350
|
+
:id="optionId(i)"
|
|
351
|
+
:key="String(item.id)"
|
|
352
|
+
role="option"
|
|
353
|
+
:aria-selected="i === active"
|
|
354
|
+
class="flex cursor-pointer items-center gap-3 px-3 py-2 text-on-surface hover:bg-on-surface/8"
|
|
355
|
+
:class="i === active ? 'bg-on-surface/12' : ''"
|
|
356
|
+
@mousedown.prevent="pick(item)"
|
|
357
|
+
@mousemove="active = i"
|
|
358
|
+
>
|
|
359
|
+
<slot name="item" :item="item" :source="query!.source" :active="i === active">
|
|
360
|
+
<img v-if="item.avatar" :src="item.avatar" alt="" class="size-8 shrink-0 rounded-full object-cover">
|
|
361
|
+
<span v-else-if="item.icon" class="grid size-8 shrink-0 place-items-center rounded-full bg-secondary-container text-on-secondary-container">
|
|
362
|
+
<MIcon :name="item.icon" :size="18" />
|
|
363
|
+
</span>
|
|
364
|
+
<MAvatar v-else :name="item.label" :size="32" class="shrink-0" />
|
|
365
|
+
<span class="flex min-w-0 flex-col">
|
|
366
|
+
<span class="truncate text-body-large">{{ item.label }}</span>
|
|
367
|
+
<span v-if="item.description" class="truncate text-body-small text-on-surface-variant">{{ item.description }}</span>
|
|
368
|
+
</span>
|
|
369
|
+
</slot>
|
|
370
|
+
</div>
|
|
371
|
+
</div>
|
|
372
|
+
</Teleport>
|
|
373
|
+
</template>
|
|
@@ -11,6 +11,7 @@ import MIconButton from './MIconButton.vue'
|
|
|
11
11
|
import MCheckbox from './MCheckbox.vue'
|
|
12
12
|
import { useFieldBg } from '../composables/useFieldBg'
|
|
13
13
|
import { useLocale } from '../composables/useLocale'
|
|
14
|
+
import { useFollowAnchor } from '../composables/_useFollowAnchor'
|
|
14
15
|
import { useDebounce } from '../composables/useDebounce'
|
|
15
16
|
import type { MultiSelectOption } from './MMultiSelect.vue'
|
|
16
17
|
import { useScrollLock } from '../composables/_useScrollLock'
|
|
@@ -245,11 +246,8 @@ function onOutsideClick(e: MouseEvent) {
|
|
|
245
246
|
if (!fieldEl.value?.contains(t) && !dropdownEl.value?.contains(t)) closeDropdown()
|
|
246
247
|
}
|
|
247
248
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (dropdownEl.value?.contains(e.target as Node)) return
|
|
251
|
-
closeDropdown()
|
|
252
|
-
}
|
|
249
|
+
// Stays on its field while the page or a container scrolls, the window/zoom changes or the field resizes
|
|
250
|
+
useFollowAnchor({ open: () => open.value, anchor: fieldEl, popup: dropdownEl, place: computeDropPos, close: closeDropdown })
|
|
253
251
|
|
|
254
252
|
function onKeydown(e: KeyboardEvent) {
|
|
255
253
|
if (e.key === 'Escape') {
|
|
@@ -311,11 +309,9 @@ function getEnabledIndex(opt: MultiSelectOption): number {
|
|
|
311
309
|
|
|
312
310
|
onMounted(() => {
|
|
313
311
|
document.addEventListener('mousedown', onOutsideClick)
|
|
314
|
-
window.addEventListener('scroll', onScroll, true)
|
|
315
312
|
})
|
|
316
313
|
onUnmounted(() => {
|
|
317
314
|
document.removeEventListener('mousedown', onOutsideClick)
|
|
318
|
-
window.removeEventListener('scroll', onScroll, true)
|
|
319
315
|
})
|
|
320
316
|
|
|
321
317
|
watch(filteredOptions, () => {
|
|
@@ -11,6 +11,7 @@ import MIconButton from './MIconButton.vue'
|
|
|
11
11
|
import MCheckbox from './MCheckbox.vue'
|
|
12
12
|
import { useFieldBg } from '../composables/useFieldBg'
|
|
13
13
|
import { useLocale } from '../composables/useLocale'
|
|
14
|
+
import { useFollowAnchor } from '../composables/_useFollowAnchor'
|
|
14
15
|
import { useScrollLock } from '../composables/_useScrollLock'
|
|
15
16
|
import { FIELD_PADDING_RIGHT, FIELD_VALUE, fieldLabelClasses, vOverflowTitle, type FieldRightInset } from '../composables/_fieldLayout'
|
|
16
17
|
|
|
@@ -209,19 +210,14 @@ function onOutsideClick(e: MouseEvent) {
|
|
|
209
210
|
if (!fieldEl.value?.contains(t) && !dropdownEl.value?.contains(t)) close()
|
|
210
211
|
}
|
|
211
212
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
if (dropdownEl.value?.contains(e.target as Node)) return
|
|
215
|
-
close()
|
|
216
|
-
}
|
|
213
|
+
// Stays on its field while the page or a container scrolls, the window/zoom changes or the field resizes
|
|
214
|
+
useFollowAnchor({ open: () => open.value, anchor: fieldEl, popup: dropdownEl, place: computeDropPos, close: close })
|
|
217
215
|
|
|
218
216
|
onMounted(() => {
|
|
219
217
|
document.addEventListener('mousedown', onOutsideClick)
|
|
220
|
-
window.addEventListener('scroll', onScroll, true)
|
|
221
218
|
})
|
|
222
219
|
onUnmounted(() => {
|
|
223
220
|
document.removeEventListener('mousedown', onOutsideClick)
|
|
224
|
-
window.removeEventListener('scroll', onScroll, true)
|
|
225
221
|
})
|
|
226
222
|
|
|
227
223
|
// Chips wrap before the trailing controls; the label stops there too
|