Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions build-config/tests/list-context-menu.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from 'node:assert/strict'

import { shouldCopyListTextOnContextMenu } from '../../src/renderer/utils/listContextMenu.mjs'

const run = (name, fn) => {
try {
fn()
console.log(`PASS ${name}`)
} catch (error) {
console.error(`FAIL ${name}`)
throw error
}
}

run('does not hijack row menu when right-clicking selectable text without an active selection', () => {
assert.equal(shouldCopyListTextOnContextMenu({
isSelectTextTarget: true,
selectionText: '',
}), false)
})

run('keeps text copy behavior when right-clicking selected text', () => {
assert.equal(shouldCopyListTextOnContextMenu({
isSelectTextTarget: true,
selectionText: 'Song Name',
}), true)
})

run('ignores non-select targets', () => {
assert.equal(shouldCopyListTextOnContextMenu({
isSelectTextTarget: false,
selectionText: 'Song Name',
}), false)
})
10 changes: 7 additions & 3 deletions src/renderer/components/material/OnlineList/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import { clipboardWriteText } from '@common/utils/electron'
import { assertApiSupport } from '@renderer/store/utils'
import { ref } from '@common/utils/vueTools'
import { shouldCopyListTextOnContextMenu, formatListSelectionText } from '@renderer/utils/listContextMenu.mjs'
import useList from './useList'
import useMenu from './useMenu'
import usePlay from './usePlay'
Expand Down Expand Up @@ -218,14 +219,17 @@ export default {
menuClick(action, index)
}
const handleListRightClick = (event) => {
if (!event.target.classList.contains('select')) return
const selectionText = window.getSelection().toString()
if (!shouldCopyListTextOnContextMenu({
isSelectTextTarget: event.target.classList.contains('select'),
selectionText,
})) return
event.stopImmediatePropagation()
let classList = dom_listContent.value.classList
classList.add('copying')
window.requestAnimationFrame(() => {
let str = window.getSelection().toString()
classList.remove('copying')
str = str.split(/\n\n/).map(s => s.replace(/\n/g, ' ')).join('\n').trim()
let str = formatListSelectionText(window.getSelection().toString())
if (!str.length) return
clipboardWriteText(str)
})
Expand Down
14 changes: 14 additions & 0 deletions src/renderer/utils/listContextMenu.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const shouldCopyListTextOnContextMenu = ({
isSelectTextTarget,
selectionText,
}) => {
return isSelectTextTarget && !!selectionText.trim()
}

export const formatListSelectionText = (selectionText) => {
return selectionText
.split(/\n\n/)
.map(text => text.replace(/\n/g, ' '))
.join('\n')
.trim()
}
10 changes: 7 additions & 3 deletions src/renderer/views/List/MusicList/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<script>
import { clipboardWriteText } from '@common/utils/electron'
import { assertApiSupport } from '@renderer/store/utils'
import { shouldCopyListTextOnContextMenu, formatListSelectionText } from '@renderer/utils/listContextMenu.mjs'
import SearchList from './components/SearchList.vue'
import MusicSortModal from './components/MusicSortModal.vue'
import MusicToggleModal from './components/MusicToggleModal.vue'
Expand Down Expand Up @@ -268,14 +269,17 @@ export default {
menuClick(action, index)
}
const handleListRightClick = (event) => {
if (!event.target.classList.contains('select')) return
const selectionText = window.getSelection().toString()
if (!shouldCopyListTextOnContextMenu({
isSelectTextTarget: event.target.classList.contains('select'),
selectionText,
})) return
event.stopImmediatePropagation()
let classList = dom_listContent.value.classList
classList.add('copying')
window.requestAnimationFrame(() => {
let str = window.getSelection().toString()
classList.remove('copying')
str = str.split(/\n\n/).map(s => s.replace(/\n/g, ' ')).join('\n').trim()
let str = formatListSelectionText(window.getSelection().toString())
if (!str.length) return
clipboardWriteText(str)
})
Expand Down