zhou zhou
6 小时以前 3fdcf1d5e6468c735532e67bde5ff1cdf85bb0c6
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
<!-- 右键菜单 -->
<template>
  <div class="menu-right">
    <Transition name="context-menu" @before-enter="onBeforeEnter" @after-leave="onAfterLeave">
      <div
        v-show="visible"
        :style="menuStyle"
        class="context-menu art-card-xs !shadow-xl min-w-[var(--menu-width)] w-[var(--menu-width)]"
      >
        <ul class="menu-list m-0 list-none" :style="menuListStyle">
          <template v-for="item in menuItems" :key="item.key">
            <!-- 普通菜单项 -->
            <li
              v-if="!item.children"
              class="menu-item relative flex-c c-p select-none rounded text-xs transition-colors duration-150 hover:bg-g-200"
              :class="{ 'is-disabled': item.disabled, 'has-line': item.showLine }"
              :style="menuItemStyle"
              @click="handleMenuClick(item)"
            >
              <ArtSvgIcon
                v-if="item.icon"
                class="mr-2 shrink-0 text-base text-g-800"
                :icon="item.icon"
              />
              <span
                class="menu-label flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-g-800"
                >{{ item.label }}</span
              >
            </li>
 
            <!-- 子菜单 -->
            <li
              v-else
              class="menu-item submenu relative flex-c c-p select-none rounded text-xs transition-colors duration-150 hover:bg-g-200"
              :style="menuItemStyle"
            >
              <div class="submenu-title flex-c w-full">
                <ArtSvgIcon
                  v-if="item.icon"
                  class="mr-2 shrink-0 text-base text-g-800"
                  :icon="item.icon"
                />
                <span
                  class="menu-label flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-g-800"
                  >{{ item.label }}</span
                >
                <ArtSvgIcon
                  icon="ri:arrow-right-s-line"
                  class="ubmenu-arrow ml-auto mr-0 text-base text-g-500 transition-transform duration-150"
                />
              </div>
              <ul
                class="submenu-list art-card-xs absolute left-full top-0 z-[2001] hidden w-max min-w-max list-none !shadow-xl"
                :style="submenuListStyle"
              >
                <li
                  v-for="child in item.children"
                  :key="child.key"
                  class="menu-item relative mx-1.5 flex-c c-p select-none rounded text-xs transition-colors duration-150 hover:bg-g-200"
                  :class="{ 'is-disabled': child.disabled, 'has-line': child.showLine }"
                  :style="menuItemStyle"
                  @click="handleMenuClick(child)"
                >
                  <ArtSvgIcon
                    v-if="child.icon"
                    class="r-2 shrink-0 text-base text-g-800 mr-1"
                    :icon="child.icon"
                  />
                  <span
                    class="menu-label flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-g-800"
                    >{{ child.label }}</span
                  >
                </li>
              </ul>
            </li>
          </template>
        </ul>
      </div>
    </Transition>
  </div>
</template>
 
<script setup>
  defineOptions({ name: 'ArtMenuRight' })
  const props = defineProps({
    menuItems: { required: false, default: () => [] },
    menuWidth: { required: false, default: 120 },
    submenuWidth: { required: false, default: 150 },
    itemHeight: { required: false, default: 32 },
    boundaryDistance: { required: false, default: 10 },
    menuPadding: { required: false, default: 5 },
    itemPaddingX: { required: false, default: 6 },
    borderRadius: { required: false, default: 6 },
    animationDuration: { required: false, default: 100 }
  })
  const emit = defineEmits(['select', 'show', 'hide'])
  const visible = ref(false)
  const position = ref({ x: 0, y: 0 })
  let showTimer = null
  let eventListenersAdded = false
  const menuStyle = computed(() => ({
    position: 'fixed',
    left: `${position.value.x}px`,
    top: `${position.value.y}px`,
    zIndex: 2e3,
    width: `${props.menuWidth}px`
  }))
  const menuListStyle = computed(() => ({
    padding: `${props.menuPadding}px`
  }))
  const menuItemStyle = computed(() => ({
    height: `${props.itemHeight}px`,
    padding: `0 ${props.itemPaddingX}px`,
    borderRadius: '4px'
  }))
  const submenuListStyle = computed(() => ({
    minWidth: `${props.submenuWidth}px`,
    padding: `${props.menuPadding}px 0`,
    borderRadius: `${props.borderRadius}px`
  }))
  const calculateMenuHeight = () => {
    let totalHeight = props.menuPadding * 2
    props.menuItems.forEach((item) => {
      totalHeight += props.itemHeight
      if (item.showLine) {
        totalHeight += 10
      }
    })
    return totalHeight
  }
  const calculatePosition = (e) => {
    const screenWidth = window.innerWidth
    const screenHeight = window.innerHeight
    const menuHeight = calculateMenuHeight()
    let x = e.clientX
    let y = e.clientY
    if (x + props.menuWidth > screenWidth - props.boundaryDistance) {
      x = Math.max(props.boundaryDistance, x - props.menuWidth)
    }
    if (y + menuHeight > screenHeight - props.boundaryDistance) {
      y = Math.max(props.boundaryDistance, screenHeight - menuHeight - props.boundaryDistance)
    }
    x = Math.max(
      props.boundaryDistance,
      Math.min(x, screenWidth - props.menuWidth - props.boundaryDistance)
    )
    y = Math.max(
      props.boundaryDistance,
      Math.min(y, screenHeight - menuHeight - props.boundaryDistance)
    )
    return { x, y }
  }
  const addEventListeners = () => {
    if (eventListenersAdded) return
    document.addEventListener('click', handleDocumentClick)
    document.addEventListener('contextmenu', handleDocumentContextmenu)
    document.addEventListener('keydown', handleKeydown)
    eventListenersAdded = true
  }
  const removeEventListeners = () => {
    if (!eventListenersAdded) return
    document.removeEventListener('click', handleDocumentClick)
    document.removeEventListener('contextmenu', handleDocumentContextmenu)
    document.removeEventListener('keydown', handleKeydown)
    eventListenersAdded = false
  }
  const handleDocumentClick = (e) => {
    const target = e.target
    const menuElement = document.querySelector('.context-menu')
    if (menuElement && menuElement.contains(target)) {
      return
    }
    hide()
  }
  const handleDocumentContextmenu = () => {
    hide()
  }
  const handleKeydown = (e) => {
    if (e.key === 'Escape') {
      hide()
    }
  }
  const show = (e) => {
    e.preventDefault()
    e.stopPropagation()
    if (showTimer) {
      window.clearTimeout(showTimer)
      showTimer = null
    }
    position.value = calculatePosition(e)
    visible.value = true
    emit('show')
    showTimer = window.setTimeout(() => {
      if (visible.value) {
        addEventListeners()
      }
      showTimer = null
    }, 50)
  }
  const hide = () => {
    if (!visible.value) return
    visible.value = false
    emit('hide')
    if (showTimer) {
      window.clearTimeout(showTimer)
      showTimer = null
    }
    removeEventListeners()
  }
  const handleMenuClick = (item) => {
    if (item.disabled) return
    emit('select', item)
    hide()
  }
  const onBeforeEnter = (el) => {
    const element = el
    element.style.transformOrigin = 'top left'
  }
  const onAfterLeave = () => {
    removeEventListeners()
    if (showTimer) {
      window.clearTimeout(showTimer)
      showTimer = null
    }
  }
  onUnmounted(() => {
    removeEventListeners()
    if (showTimer) {
      window.clearTimeout(showTimer)
      showTimer = null
    }
  })
  defineExpose({
    show,
    hide,
    visible: computed(() => visible.value)
  })
</script>
 
<style scoped>
  .menu-right {
    --menu-width: v-bind('props.menuWidth + "px"');
    --border-radius: v-bind('props.borderRadius + "px"');
  }
 
  .menu-item.has-line {
    margin-bottom: 10px;
  }
 
  .menu-item.has-line::after {
    position: absolute;
    right: 0;
    bottom: -5px;
    left: 0;
    height: 1px;
    content: '';
    background-color: var(--art-gray-300);
  }
 
  .menu-item.is-disabled {
    color: var(--el-text-color-disabled);
    cursor: not-allowed;
  }
 
  .menu-item.is-disabled:hover {
    background-color: transparent !important;
  }
 
  .menu-item.is-disabled i:not(.submenu-arrow),
  .menu-item.is-disabled :deep(.art-svg-icon) {
    color: var(--el-text-color-disabled) !important;
  }
 
  .menu-item.is-disabled .menu-label {
    color: var(--el-text-color-disabled) !important;
  }
 
  .menu-item.submenu:hover .submenu-list {
    display: block;
  }
 
  .menu-item.submenu:hover .submenu-title .submenu-arrow {
    transform: rotate(90deg);
  }
 
  /* 动画样式 */
  .context-menu-enter-active,
  .context-menu-leave-active {
    transition: all v-bind('props.animationDuration + "ms"') ease-out;
  }
 
  .context-menu-enter-from,
  .context-menu-leave-to {
    opacity: 0;
    transform: scale(0.9);
  }
 
  .context-menu-enter-to,
  .context-menu-leave-from {
    opacity: 1;
    transform: scale(1);
  }
</style>