Skip to content

Commit 0f0d3f8

Browse files
committed
修复列表文字区域右键菜单
1 parent be48e78 commit 0f0d3f8

4 files changed

Lines changed: 62 additions & 6 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import assert from 'node:assert/strict'
2+
3+
import { shouldCopyListTextOnContextMenu } from '../../src/renderer/utils/listContextMenu.mjs'
4+
5+
const run = (name, fn) => {
6+
try {
7+
fn()
8+
console.log(`PASS ${name}`)
9+
} catch (error) {
10+
console.error(`FAIL ${name}`)
11+
throw error
12+
}
13+
}
14+
15+
run('does not hijack row menu when right-clicking selectable text without an active selection', () => {
16+
assert.equal(shouldCopyListTextOnContextMenu({
17+
isSelectTextTarget: true,
18+
selectionText: '',
19+
}), false)
20+
})
21+
22+
run('keeps text copy behavior when right-clicking selected text', () => {
23+
assert.equal(shouldCopyListTextOnContextMenu({
24+
isSelectTextTarget: true,
25+
selectionText: 'Song Name',
26+
}), true)
27+
})
28+
29+
run('ignores non-select targets', () => {
30+
assert.equal(shouldCopyListTextOnContextMenu({
31+
isSelectTextTarget: false,
32+
selectionText: 'Song Name',
33+
}), false)
34+
})

src/renderer/components/material/OnlineList/index.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
import { clipboardWriteText } from '@common/utils/electron'
103103
import { assertApiSupport } from '@renderer/store/utils'
104104
import { ref } from '@common/utils/vueTools'
105+
import { shouldCopyListTextOnContextMenu, formatListSelectionText } from '@renderer/utils/listContextMenu.mjs'
105106
import useList from './useList'
106107
import useMenu from './useMenu'
107108
import usePlay from './usePlay'
@@ -218,14 +219,17 @@ export default {
218219
menuClick(action, index)
219220
}
220221
const handleListRightClick = (event) => {
221-
if (!event.target.classList.contains('select')) return
222+
const selectionText = window.getSelection().toString()
223+
if (!shouldCopyListTextOnContextMenu({
224+
isSelectTextTarget: event.target.classList.contains('select'),
225+
selectionText,
226+
})) return
222227
event.stopImmediatePropagation()
223228
let classList = dom_listContent.value.classList
224229
classList.add('copying')
225230
window.requestAnimationFrame(() => {
226-
let str = window.getSelection().toString()
227231
classList.remove('copying')
228-
str = str.split(/\n\n/).map(s => s.replace(/\n/g, ' ')).join('\n').trim()
232+
let str = formatListSelectionText(window.getSelection().toString())
229233
if (!str.length) return
230234
clipboardWriteText(str)
231235
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const shouldCopyListTextOnContextMenu = ({
2+
isSelectTextTarget,
3+
selectionText,
4+
}) => {
5+
return isSelectTextTarget && !!selectionText.trim()
6+
}
7+
8+
export const formatListSelectionText = (selectionText) => {
9+
return selectionText
10+
.split(/\n\n/)
11+
.map(text => text.replace(/\n/g, ' '))
12+
.join('\n')
13+
.trim()
14+
}

src/renderer/views/List/MusicList/index.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<script>
107107
import { clipboardWriteText } from '@common/utils/electron'
108108
import { assertApiSupport } from '@renderer/store/utils'
109+
import { shouldCopyListTextOnContextMenu, formatListSelectionText } from '@renderer/utils/listContextMenu.mjs'
109110
import SearchList from './components/SearchList.vue'
110111
import MusicSortModal from './components/MusicSortModal.vue'
111112
import MusicToggleModal from './components/MusicToggleModal.vue'
@@ -268,14 +269,17 @@ export default {
268269
menuClick(action, index)
269270
}
270271
const handleListRightClick = (event) => {
271-
if (!event.target.classList.contains('select')) return
272+
const selectionText = window.getSelection().toString()
273+
if (!shouldCopyListTextOnContextMenu({
274+
isSelectTextTarget: event.target.classList.contains('select'),
275+
selectionText,
276+
})) return
272277
event.stopImmediatePropagation()
273278
let classList = dom_listContent.value.classList
274279
classList.add('copying')
275280
window.requestAnimationFrame(() => {
276-
let str = window.getSelection().toString()
277281
classList.remove('copying')
278-
str = str.split(/\n\n/).map(s => s.replace(/\n/g, ' ')).join('\n').trim()
282+
let str = formatListSelectionText(window.getSelection().toString())
279283
if (!str.length) return
280284
clipboardWriteText(str)
281285
})

0 commit comments

Comments
 (0)