feat: 支持跨标签共享选中状态

This commit is contained in:
ZiuChen 2022-09-09 10:57:04 +08:00
parent 2b3389eedd
commit b25abcddbf
3 changed files with 94 additions and 12 deletions

View File

@ -7,7 +7,11 @@
@click.left="handleItemClick($event, item)"
@click.right="handleItemClick($event, item)"
@mouseover="handleMouseOver(index)"
:class="{ active: index === activeIndex, select: selectItemList.indexOf(item) !== -1 }"
:class="{
active: !isMultiple && index === activeIndex,
'multi-active': isMultiple && index === activeIndex,
select: selectItemList.indexOf(item) !== -1
}"
>
<div class="clip-info">
<div class="clip-time">
@ -70,6 +74,10 @@ const props = defineProps({
isMultiple: {
type: Boolean,
required: true
},
currentActiveTab: {
type: String,
required: true
}
})
const emit = defineEmits(['onDataChange', 'onDataRemove', 'onSelectItemAdd'])
@ -81,6 +89,7 @@ const isOverSizedContent = (item) => {
return JSON.parse(item.data).length >= 6
}
}
const isShiftDown = ref(false)
const selectItemList = ref([])
const emptySelectItemList = () => (selectItemList.value = [])
defineExpose({
@ -97,13 +106,59 @@ watch(
)
const handleItemClick = (ev, item) => {
if (props.isMultiple === true) {
const index = selectItemList.value.indexOf(item)
console.log(index)
if (index !== -1) {
selectItemList.value.splice(index, 1) //
const i = selectItemList.value.indexOf(item) //
const index = props.showList.indexOf(item) //
if (selectItemList.value.length !== 0 && isShiftDown.value) {
// Shift
// selectList
// index/
//
console.log(props.currentActiveTab)
const tmpArray = selectItemList.value
.filter((item) =>
props.currentActiveTab === 'all' ? true : item.type === props.currentActiveTab
)
.sort((a, b) => selectItemList.value.indexOf(a) - selectItemList.value.indexOf(b))
const h = props.showList.indexOf(tmpArray[0]) // index index
const l = props.showList.indexOf(tmpArray[tmpArray.length - 1]) // index
console.log(props.showList)
if (index < h) {
// : index0
// selectItemList.value = []
for (let i = index; i <= h; i++) {
selectItemList.value.push(props.showList[i])
}
//
selectItemList.value = selectItemList.value.filter(function (item, index) {
return selectItemList.value.indexOf(item) === index
})
} else if (index > l) {
//
// selectItemList.value = []
for (let i = h; i <= index; i++) {
selectItemList.value.push(props.showList[i])
}
//
selectItemList.value = selectItemList.value.filter(function (item, index) {
return selectItemList.value.indexOf(item) === index
})
} else if (index <= l && index >= h) {
//
if (i !== -1) {
selectItemList.value.splice(i, 1) //
} else {
selectItemList.value.push(item) //
}
}
} else {
selectItemList.value.push(item) //
// Shift
if (i !== -1) {
selectItemList.value.splice(i, 1) //
} else {
selectItemList.value.push(item) //
}
}
emit('onSelectItemAdd')
} else {
const { button } = ev
@ -165,6 +220,7 @@ onMounted(() => {
const isEnter = key === 'Enter'
const isCopy = (ctrlKey || metaKey) && (key === 'C' || key === 'c')
const isNumber = parseInt(key) <= 9 && parseInt(key) >= 0
const isShift = key === 'Shift'
if (isArrowUp) {
if (activeIndex.value > 0) {
activeIndex.value--
@ -196,6 +252,19 @@ onMounted(() => {
} else if ((ctrlKey || metaKey || altKey) && isNumber) {
window.copy(props.showList[parseInt(key) - 1])
window.paste()
} else if (isShift) {
if (props.isMultiple) {
isShiftDown.value = true
}
}
})
document.addEventListener('keyup', (e) => {
const { key } = e
const isShift = key === 'Shift'
if (isShift) {
if (props.isMultiple) {
isShiftDown.value = false
}
}
})
})

View File

@ -1,5 +1,6 @@
.clip-item-list {
background: @bg-color;
user-select: none;
.clip-item {
display: flex;
justify-content: space-between;
@ -16,6 +17,12 @@
background-color: @text-bg-color-lighter;
transition: all 0.15s;
}
&.multi-active {
border-style: hidden hidden hidden solid;
border-width: 1px 0px 0px 6px;
border-color: @bg-color @bg-color @bg-color @primary-color;
transition: all 0.15s;
}
&.select {
background-color: @text-bg-color-lighter;
}

View File

@ -44,7 +44,7 @@
:showList="showList"
:fullData="fullData"
:isMultiple="isMultiple"
@onSelectItemAdd="handleSelectItemAdd"
:currentActiveTab="outSideActiveTab"
@onDataChange="toggleFullData"
@onDataRemove="handleDataRemove"
>
@ -76,11 +76,6 @@ const handleSearchBtnClick = () => {
const ClipItemListRef = ref(null)
const selectCount = ref(0)
const handleSelectItemAdd = () => {
// item count
selectCount.value = ClipItemListRef.value.selectItemList.length
}
const handleMultiCopyBtnClick = (isPaste) => {
const itemList = ClipItemListRef.value.selectItemList
// /
@ -194,12 +189,19 @@ const handleDataRemove = () => {
updateShowList(ClipSwitchRef.value.activeTab)
}
const outSideActiveTab = ref('all')
onMounted(() => {
// Ref
const activeTab = computed(() => ClipSwitchRef.value.activeTab)
const toggleNav = ClipSwitchRef.value.toggleNav
const tabs = ClipSwitchRef.value.tabs
watch(activeTab, (val) => (outSideActiveTab.value = val))
//
selectCount.value = computed(() => ClipItemListRef.value?.selectItemList?.length)
//
list.value = window.db.dataBase.data
showList.value = list.value.slice(0, GAP) // 10
@ -250,6 +252,7 @@ onMounted(() => {
const isExit = key === 'Escape'
const isArrow = key === 'ArrowDown' || key === 'ArrowUp'
const isEnter = key === 'Enter'
const isShift = key === 'Shift'
if (isTab) {
const tabTypes = tabs.map((item) => item.type)
const index = tabTypes.indexOf(activeTab.value)
@ -274,6 +277,9 @@ onMounted(() => {
} else if (ctrlKey || metaKey || isArrow || isEnter) {
// Ctrl (utools)
e.preventDefault()
} else if (isShift) {
// Shift:
// e.preventDefault()
} else {
window.focus() //
}