-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathNodeSearch.vue
More file actions
169 lines (147 loc) · 3.12 KB
/
NodeSearch.vue
File metadata and controls
169 lines (147 loc) · 3.12 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
<template>
<div>
<!-- 搜索遮罩层 -->
<Teleport to="body">
<div v-if="showSearch" class="search-mask" @click.self="closeSearch">
<div class="search-container">
<el-input
ref="searchInputRef"
v-model="searchText"
placeholder="搜索..."
:prefix-icon="Search"
clearable
@keyup.enter="handleSearch"
@keyup.esc="closeSearch"
>
<template #append>
<el-button @click="closeSearch">取消</el-button>
</template>
</el-input>
</div>
</div>
</Teleport>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import { Search } from '@element-plus/icons-vue'
// Props定义
interface Props {
onSearch?: (keyword: string) => void // 搜索回调
}
const props = withDefaults(defineProps<Props>(), {
useElementPlus: false,
onSearch: undefined,
})
// 状态
const showSearch = ref(false)
const searchText = ref('')
const searchInputRef = ref<any>(null)
const nativeInputRef = ref<HTMLInputElement | null>(null)
// 快捷键处理
const handleKeyDown = (e: KeyboardEvent) => {
// Ctrl+F 或 Cmd+F (Mac)
if ((e.ctrlKey || e.metaKey) && e.key === 'f') {
e.preventDefault() // 阻止浏览器默认搜索
openSearch()
}
// 按ESC关闭
if (e.key === 'Escape' && showSearch.value) {
closeSearch()
}
}
// 打开搜索
const openSearch = () => {
showSearch.value = true
searchText.value = ''
nextTick(() => {
searchInputRef.value?.focus()
})
}
// 关闭搜索
const closeSearch = () => {
showSearch.value = false
searchText.value = ''
}
// 执行搜索
const handleSearch = () => {
if (searchText.value.trim()) {
props.onSearch?.(searchText.value)
}
}
// 生命周期
onMounted(() => {
window.addEventListener('keydown', handleKeyDown)
})
onUnmounted(() => {
window.removeEventListener('keydown', handleKeyDown)
})
</script>
<style scoped>
.search-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.3);
display: flex;
justify-content: center;
z-index: 9999;
padding-top: 20vh;
}
.search-container {
width: 500px;
max-width: 90%;
animation: slideDown 0.2s ease;
}
/* 原生输入框样式 */
.native-search {
display: flex;
gap: 8px;
background: white;
padding: 16px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.native-search input {
flex: 1;
padding: 10px 12px;
border: 1px solid #dcdfe6;
border-radius: 4px;
font-size: 14px;
outline: none;
}
.native-search input:focus {
border-color: #409eff;
}
.native-search button {
padding: 0 16px;
background: white;
border: 1px solid #dcdfe6;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.native-search button:hover {
border-color: #409eff;
color: #409eff;
}
.content {
padding: 20px;
}
.item {
padding: 8px;
border-bottom: 1px solid #eee;
}
@keyframes slideDown {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>