forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlashCommandMenu.vue
More file actions
397 lines (358 loc) · 9.52 KB
/
Copy pathSlashCommandMenu.vue
File metadata and controls
397 lines (358 loc) · 9.52 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<script setup lang="ts">
import type { SlashCommandItem } from '@/composables/useSlashCommand'
import {
Braces,
Heading1,
Heading2,
Heading3,
Heading4,
Heading5,
Heading6,
Image,
Link,
List,
ListOrdered,
Minus,
Quote,
Sigma,
Table,
} from '@lucide/vue'
const props = defineProps<{
visible: boolean
position: { x: number, y: number }
activeIndex: number
basicCommands: SlashCommandItem[]
commonCommands: SlashCommandItem[]
filteredCommands: SlashCommandItem[]
}>()
const emit = defineEmits<{
execute: [command: SlashCommandItem]
close: []
}>()
// Map icon names to lucide components
const iconMap: Record<string, any> = {
'H1': Heading1,
'H2': Heading2,
'H3': Heading3,
'H4': Heading4,
'H5': Heading5,
'H6': Heading6,
'ordered-list': ListOrdered,
'unordered-list': List,
'blockquote': Quote,
'divider': Minus,
'link': Link,
'code': Braces,
'formula': Sigma,
'image': Image,
'table': Table,
}
// Heading items use text labels in the grid; others use icon + label in list
const headingIds = [`h1`, `h2`, `h3`, `h4`, `h5`, `h6`]
const menuRef = useTemplateRef<HTMLDivElement>(`menuRef`)
// Calculate adjusted position so the menu stays in viewport
const adjustedPosition = computed(() => {
const MENU_HEIGHT = 320
const MENU_WIDTH = 240
const PADDING = 8
let { x, y } = props.position
// Adjust horizontal
if (x + MENU_WIDTH > window.innerWidth - PADDING) {
x = window.innerWidth - MENU_WIDTH - PADDING
}
if (x < PADDING) {
x = PADDING
}
// Adjust vertical — prefer below cursor, flip above if insufficient space
if (y + MENU_HEIGHT > window.innerHeight - PADDING) {
// Position above cursor (need cursor top, approximate with -24px offset)
y = y - MENU_HEIGHT - 28
}
return { x, y }
})
// Close on outside click
function handleOutsideClick(e: MouseEvent) {
if (menuRef.value && !menuRef.value.contains(e.target as Node)) {
emit(`close`)
}
}
watch(
() => props.visible,
(val) => {
if (val) {
nextTick(() => {
document.addEventListener(`mousedown`, handleOutsideClick)
scrollActiveIntoView()
})
}
else {
document.removeEventListener(`mousedown`, handleOutsideClick)
}
},
)
onUnmounted(() => {
document.removeEventListener(`mousedown`, handleOutsideClick)
})
// Scroll active item into view when navigating
watch(
() => props.activeIndex,
() => {
scrollActiveIntoView()
},
)
function scrollActiveIntoView() {
nextTick(() => {
const el = menuRef.value?.querySelector(`[data-active="true"]`)
el?.scrollIntoView({ block: `nearest` })
})
}
function getGlobalIndex(command: SlashCommandItem): number {
return props.filteredCommands.indexOf(command)
}
</script>
<template>
<Transition name="slash-menu">
<div
v-if="visible"
ref="menuRef"
class="slash-command-menu"
:style="{
left: `${adjustedPosition.x}px`,
top: `${adjustedPosition.y}px`,
}"
>
<!-- Basic group -->
<template v-if="basicCommands.length > 0">
<div class="slash-group-label">
基础
<span class="slash-group-count">{{ basicCommands.length }}</span>
</div>
<!-- Heading grid row -->
<div v-if="basicCommands.some(c => headingIds.includes(c.id))" class="slash-heading-grid">
<button
v-for="cmd in basicCommands.filter(c => headingIds.includes(c.id))"
:key="cmd.id"
class="slash-heading-btn"
:class="{ active: getGlobalIndex(cmd) === activeIndex }"
:data-active="getGlobalIndex(cmd) === activeIndex"
@mousedown.prevent="emit('execute', cmd)"
>
{{ cmd.label }}
</button>
</div>
<!-- Non-heading basic items grid -->
<div class="slash-misc-grid">
<button
v-for="cmd in basicCommands.filter(c => !headingIds.includes(c.id))"
:key="cmd.id"
class="slash-misc-btn"
:class="{ active: getGlobalIndex(cmd) === activeIndex }"
:data-active="getGlobalIndex(cmd) === activeIndex"
@mousedown.prevent="emit('execute', cmd)"
>
<span class="slash-misc-icon">
<component :is="iconMap[cmd.icon]" v-if="iconMap[cmd.icon]" class="w-4 h-4" />
</span>
<span class="slash-misc-label">{{ cmd.label }}</span>
</button>
</div>
<div v-if="commonCommands.length > 0" class="slash-separator" />
</template>
<!-- Common group -->
<template v-if="commonCommands.length > 0">
<div class="slash-group-label">
常用
<span class="slash-group-count">{{ commonCommands.length }}</span>
</div>
<div class="slash-common-list">
<button
v-for="cmd in commonCommands"
:key="cmd.id"
class="slash-common-item"
:class="{ active: getGlobalIndex(cmd) === activeIndex }"
:data-active="getGlobalIndex(cmd) === activeIndex"
@mousedown.prevent="emit('execute', cmd)"
>
<span class="slash-common-icon">
<!-- Special icon for markdown -->
<template v-if="cmd.id === 'markdown'">
<svg viewBox="0 0 24 24" class="w-4 h-4" fill="currentColor" aria-hidden="true">
<path d="M20.56 18H3.44C2.65 18 2 17.37 2 16.59V7.41C2 6.63 2.65 6 3.44 6h17.12C21.35 6 22 6.63 22 7.41v9.18c0 .78-.65 1.41-1.44 1.41zM7 15V9l2 2 2-2v6h2V9l2 2 2-2v6h1V9h-1l-2 2-2-2H9L7 9v6H6V9H5v6h2z" />
</svg>
</template>
<component :is="iconMap[cmd.icon]" v-else-if="iconMap[cmd.icon]" class="w-4 h-4" />
</span>
<span class="slash-common-label">{{ cmd.label }}</span>
</button>
</div>
</template>
</div>
</Transition>
</template>
<style scoped>
.slash-command-menu {
position: fixed;
z-index: 9999;
width: 240px;
border-radius: 10px;
border: 1px solid hsl(var(--border));
background-color: hsl(var(--popover));
color: hsl(var(--popover-foreground));
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.08);
padding: 6px;
font-size: 13px;
}
.slash-group-label {
display: flex;
align-items: center;
gap: 6px;
padding: 4px 6px 4px;
font-size: 11px;
font-weight: 600;
color: hsl(var(--muted-foreground));
letter-spacing: 0.04em;
text-transform: uppercase;
user-select: none;
}
.slash-group-count {
background: hsl(var(--muted));
color: hsl(var(--muted-foreground));
border-radius: 10px;
padding: 0 5px;
font-size: 10px;
font-weight: 500;
line-height: 1.6;
}
/* ── Heading grid ── */
.slash-heading-grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 3px;
padding: 2px 2px 4px;
}
.slash-heading-btn {
display: flex;
align-items: center;
justify-content: center;
height: 36px;
border-radius: 6px;
font-weight: 700;
font-size: 13px;
cursor: pointer;
background: transparent;
border: none;
color: hsl(var(--foreground));
transition: background 0.12s;
outline: none;
}
.slash-heading-btn:hover,
.slash-heading-btn.active {
background: hsl(var(--accent));
color: hsl(var(--accent-foreground));
}
/* ── Misc grid (ordered/unordered list, quote, divider) ── */
.slash-misc-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 3px;
padding: 2px 2px 4px;
}
.slash-misc-btn {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 3px;
height: 52px;
border-radius: 6px;
cursor: pointer;
background: transparent;
border: none;
color: hsl(var(--foreground));
transition: background 0.12s;
outline: none;
}
.slash-misc-btn:hover,
.slash-misc-btn.active {
background: hsl(var(--accent));
color: hsl(var(--accent-foreground));
}
.slash-misc-icon {
display: flex;
align-items: center;
justify-content: center;
color: hsl(var(--foreground));
}
.slash-misc-label {
font-size: 10px;
color: hsl(var(--muted-foreground));
white-space: nowrap;
}
.slash-misc-btn:hover .slash-misc-label,
.slash-misc-btn.active .slash-misc-label {
color: hsl(var(--accent-foreground));
}
.slash-separator {
height: 1px;
background: hsl(var(--border));
margin: 4px 4px;
}
/* ── Common list ── */
.slash-common-list {
display: flex;
flex-direction: column;
gap: 1px;
padding: 2px;
}
.slash-common-item {
display: flex;
align-items: center;
gap: 10px;
padding: 7px 8px;
border-radius: 6px;
cursor: pointer;
background: transparent;
border: none;
color: hsl(var(--foreground));
text-align: left;
width: 100%;
transition: background 0.12s;
outline: none;
}
.slash-common-item:hover,
.slash-common-item.active {
background: hsl(var(--accent));
color: hsl(var(--accent-foreground));
}
.slash-common-icon {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
border-radius: 6px;
background: hsl(var(--muted));
color: hsl(var(--foreground));
flex-shrink: 0;
transition: background 0.12s, color 0.12s;
}
.slash-common-item:hover .slash-common-icon,
.slash-common-item.active .slash-common-icon {
background: hsl(var(--primary));
color: hsl(var(--primary-foreground));
}
.slash-common-label {
font-size: 13px;
font-weight: 500;
}
/* ── Transition ── */
.slash-menu-enter-active,
.slash-menu-leave-active {
transition: opacity 0.12s ease, transform 0.12s ease;
}
.slash-menu-enter-from,
.slash-menu-leave-to {
opacity: 0;
transform: translateY(-4px) scale(0.98);
}
</style>