-
zhangwei
2025-06-18 9143fcdfc8b0c934d17523d6ab9fd27f316f24c5
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
<script setup lang="ts">
import Sortable from "sortablejs";
import SearchHistoryItem from "./SearchHistoryItem.vue";
import type { optionsItem, dragItem, Props } from "../types";
import { useEpThemeStoreHook } from "@/store/modules/epTheme";
import { useResizeObserver, isArray, delay } from "@pureadmin/utils";
import { ref, watch, nextTick, computed, getCurrentInstance } from "vue";
 
interface Emits {
  (e: "update:value", val: string): void;
  (e: "enter"): void;
  (e: "collect", val: optionsItem): void;
  (e: "delete", val: optionsItem): void;
  (e: "drag", val: dragItem): void;
}
 
const historyRef = ref();
const innerHeight = ref();
/** 判断是否停止鼠标移入事件处理 */
const stopMouseEvent = ref(false);
 
const emit = defineEmits<Emits>();
const instance = getCurrentInstance()!;
const props = withDefaults(defineProps<Props>(), {});
 
const itemStyle = computed(() => {
  return item => {
    return {
      background:
        item?.path === active.value ? useEpThemeStoreHook().epThemeColor : "",
      color: item.path === active.value ? "#fff" : "",
      fontSize: item.path === active.value ? "16px" : "14px"
    };
  };
});
 
const titleStyle = computed(() => {
  return {
    color: useEpThemeStoreHook().epThemeColor,
    fontWeight: 500
  };
});
 
const active = computed({
  get() {
    return props.value;
  },
  set(val: string) {
    emit("update:value", val);
  }
});
 
watch(
  () => props.value,
  newValue => {
    if (newValue) {
      if (stopMouseEvent.value) {
        delay(100).then(() => (stopMouseEvent.value = false));
      }
    }
  }
);
 
const historyList = computed(() => {
  return props.options.filter(item => item.type === "history");
});
 
const collectList = computed(() => {
  return props.options.filter(item => item.type === "collect");
});
 
function handleCollect(item) {
  emit("collect", item);
}
 
function handleDelete(item) {
  stopMouseEvent.value = true;
  emit("delete", item);
}
 
/** 鼠标移入 */
async function handleMouse(item) {
  if (!stopMouseEvent.value) active.value = item.path;
}
 
function handleTo() {
  emit("enter");
}
 
function resizeResult() {
  // el-scrollbar max-height="calc(90vh - 140px)"
  innerHeight.value = window.innerHeight - window.innerHeight / 10 - 140;
}
 
useResizeObserver(historyRef, resizeResult);
 
function handleScroll(index: number) {
  const curInstance = instance?.proxy?.$refs[`historyItemRef${index}`];
  if (!curInstance) return 0;
  const curRef = isArray(curInstance)
    ? (curInstance[0] as ElRef)
    : (curInstance as ElRef);
  const scrollTop = curRef.offsetTop + 128; // 128 两个history-item(56px+56px=112px)高度加上下margin(8px+8px=16px)
  return scrollTop > innerHeight.value ? scrollTop - innerHeight.value : 0;
}
 
const handleChangeIndex = (evt): void => {
  emit("drag", { oldIndex: evt.oldIndex, newIndex: evt.newIndex });
};
 
let sortableInstance = null;
 
watch(
  collectList,
  val => {
    if (val.length > 1) {
      nextTick(() => {
        const wrapper: HTMLElement =
          document.querySelector(".collect-container");
        if (!wrapper || sortableInstance) return;
        sortableInstance = Sortable.create(wrapper, {
          animation: 160,
          onStart: event => {
            event.item.style.cursor = "move";
          },
          onEnd: event => {
            event.item.style.cursor = "pointer";
          },
          onUpdate: handleChangeIndex
        });
        resizeResult();
      });
    }
  },
  { deep: true, immediate: true }
);
 
defineExpose({ handleScroll });
</script>
 
<template>
  <div ref="historyRef" class="history">
    <template v-if="historyList.length">
      <div :style="titleStyle">搜索历史</div>
      <div
        v-for="(item, index) in historyList"
        :key="item.path"
        :ref="'historyItemRef' + index"
        class="history-item dark:bg-[#1d1d1d]"
        :style="itemStyle(item)"
        @click="handleTo"
        @mouseenter="handleMouse(item)"
      >
        <SearchHistoryItem
          :item="item"
          @delete-item="handleDelete"
          @collect-item="handleCollect"
        />
      </div>
    </template>
    <template v-if="collectList.length">
      <div :style="titleStyle">
        {{ `收藏${collectList.length > 1 ? "(可拖拽排序)" : ""}` }}
      </div>
      <div class="collect-container">
        <div
          v-for="(item, index) in collectList"
          :key="item.path"
          :ref="'historyItemRef' + (index + historyList.length)"
          class="history-item dark:bg-[#1d1d1d]"
          :style="itemStyle(item)"
          @click="handleTo"
          @mouseenter="handleMouse(item)"
        >
          <SearchHistoryItem :item="item" @delete-item="handleDelete" />
        </div>
      </div>
    </template>
  </div>
</template>
 
<style lang="scss" scoped>
.history {
  padding-bottom: 12px;
 
  &-item {
    display: flex;
    align-items: center;
    height: 56px;
    padding: 14px;
    margin: 8px auto 10px;
    cursor: pointer;
    border: 0.1px solid #ccc;
    border-radius: 4px;
    transition: font-size 0.16s;
  }
}
</style>