mirror of
https://github.com/ZiuChen/ClipboardManager.git
synced 2025-06-08 14:24:03 +08:00
refactor: 将operate组件逻辑封装为hooks
This commit is contained in:
parent
d2be0dd842
commit
ef098863b0
@ -1,35 +1,25 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="clip-operate">
|
<div class="clip-operate">
|
||||||
<template v-for="{ id, title, icon } of operation">
|
<template v-for="o of operation">
|
||||||
<div
|
<div
|
||||||
v-if="
|
v-if="filterOperate(o, item, isFullData)"
|
||||||
(id !== 'collect' &&
|
:class="o.id"
|
||||||
id !== 'view' &&
|
:title="o.title"
|
||||||
id !== 'open-folder' &&
|
@click.stop="handleOperateClick(o, item)"
|
||||||
id !== 'word-break' &&
|
|
||||||
id !== 'save-file') ||
|
|
||||||
(id === 'collect' && item.type !== 'file') ||
|
|
||||||
(id === 'view' && !isFullData) ||
|
|
||||||
(id === 'open-folder' && item.type === 'file') ||
|
|
||||||
(id === 'save-file' && isFullData && item.type !== 'file') ||
|
|
||||||
(id === 'word-break' &&
|
|
||||||
isFullData &&
|
|
||||||
item.type === 'text' &&
|
|
||||||
item.data.length <= 500 &&
|
|
||||||
item.data.length >= 2)
|
|
||||||
"
|
|
||||||
:class="id"
|
|
||||||
:title="title"
|
|
||||||
@click.stop="handleOperateClick({ id, item })"
|
|
||||||
>
|
>
|
||||||
{{ icon }}
|
{{ o.icon }}
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ElMessage } from 'element-plus'
|
import { computed } from 'vue'
|
||||||
|
import defaultOperation from '../data/operation.json'
|
||||||
|
import setting from '../global/readSetting'
|
||||||
|
import useClipOperate from '../hooks/useClipOperate'
|
||||||
|
|
||||||
|
const emit = defineEmits(['onDataChange', 'onDataRemove', 'onOperateExecute'])
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
item: {
|
item: {
|
||||||
type: Object,
|
type: Object,
|
||||||
@ -40,60 +30,14 @@ const props = defineProps({
|
|||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const emit = defineEmits(['onDataChange', 'onDataRemove', 'onOperateExecute'])
|
|
||||||
const operation = [
|
const operation = computed(() =>
|
||||||
{ id: 'copy', title: '复制', icon: '📄' },
|
props.isFullData
|
||||||
{ id: 'view', title: '查看全部', icon: '💬' },
|
? [...defaultOperation, ...setting.operation.custom]
|
||||||
{ id: 'open-folder', title: '打开文件夹', icon: '📁' },
|
: [...defaultOperation, ...setting.operation.custom].splice(0, 5)
|
||||||
{ id: 'collect', title: '收藏', icon: '⭐' },
|
)
|
||||||
{ id: 'word-break', title: '分词', icon: '💣' },
|
|
||||||
{ id: 'save-file', title: '保存', icon: '💾' },
|
const { handleOperateClick, filterOperate } = useClipOperate({ emit })
|
||||||
{ id: 'remove', title: '删除', icon: '❌' }
|
|
||||||
]
|
|
||||||
const handleOperateClick = ({ id, item }) => {
|
|
||||||
const typeMap = {
|
|
||||||
text: 'text',
|
|
||||||
file: 'files',
|
|
||||||
image: 'img'
|
|
||||||
}
|
|
||||||
switch (id) {
|
|
||||||
case 'copy':
|
|
||||||
window.copy(item, false)
|
|
||||||
ElMessage({
|
|
||||||
message: '复制成功',
|
|
||||||
type: 'success'
|
|
||||||
})
|
|
||||||
break
|
|
||||||
case 'view':
|
|
||||||
emit('onDataChange', item)
|
|
||||||
break
|
|
||||||
case 'open-folder':
|
|
||||||
const { data } = item
|
|
||||||
const fl = JSON.parse(data)
|
|
||||||
utools.shellShowItemInFolder(fl[0].path) // 取第一个文件的路径打开
|
|
||||||
break
|
|
||||||
case 'collect':
|
|
||||||
utools.redirect('添加到「备忘快贴」', {
|
|
||||||
type: typeMap[item.type],
|
|
||||||
data: item.data
|
|
||||||
})
|
|
||||||
break
|
|
||||||
case 'word-break':
|
|
||||||
utools.redirect('超级分词', item.data)
|
|
||||||
break
|
|
||||||
case 'save-file':
|
|
||||||
utools.redirect('收集文件', {
|
|
||||||
type: typeMap[item.type],
|
|
||||||
data: item.data
|
|
||||||
})
|
|
||||||
break
|
|
||||||
case 'remove':
|
|
||||||
window.remove(item)
|
|
||||||
emit('onDataRemove')
|
|
||||||
break
|
|
||||||
}
|
|
||||||
emit('onOperateExecute')
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="less" scoped>
|
<style lang="less" scoped>
|
||||||
|
66
src/hooks/useClipOperate.js
Normal file
66
src/hooks/useClipOperate.js
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { ElMessage } from 'element-plus'
|
||||||
|
|
||||||
|
export default function useClipOperate({ emit }) {
|
||||||
|
return {
|
||||||
|
handleOperateClick: (operation, item) => {
|
||||||
|
const { id } = operation
|
||||||
|
const typeMap = {
|
||||||
|
text: 'text',
|
||||||
|
file: 'files',
|
||||||
|
image: 'img'
|
||||||
|
}
|
||||||
|
if (id === 'copy') {
|
||||||
|
window.copy(item, false)
|
||||||
|
ElMessage({
|
||||||
|
message: '复制成功',
|
||||||
|
type: 'success'
|
||||||
|
})
|
||||||
|
} else if (id === 'view') {
|
||||||
|
emit('onDataChange', item)
|
||||||
|
} else if (id === 'open-folder') {
|
||||||
|
const { data } = item
|
||||||
|
const fl = JSON.parse(data)
|
||||||
|
utools.shellShowItemInFolder(fl[0].path) // 取第一个文件的路径打开
|
||||||
|
} else if (id === 'collect') {
|
||||||
|
utools.redirect('添加到「备忘快贴」', {
|
||||||
|
type: typeMap[item.type],
|
||||||
|
data: item.data
|
||||||
|
})
|
||||||
|
} else if (id === 'word-break') {
|
||||||
|
utools.redirect('超级分词', item.data)
|
||||||
|
} else if (id === 'save-file') {
|
||||||
|
utools.redirect('收集文件', {
|
||||||
|
type: typeMap[item.type],
|
||||||
|
data: item.data
|
||||||
|
})
|
||||||
|
} else if (id === 'remove') {
|
||||||
|
window.remove(item)
|
||||||
|
emit('onDataRemove')
|
||||||
|
} else if (id.indexOf('custom') !== -1) {
|
||||||
|
console.log('custom')
|
||||||
|
}
|
||||||
|
emit('onOperateExecute')
|
||||||
|
},
|
||||||
|
|
||||||
|
filterOperate: (operation, item, isFullData) => {
|
||||||
|
const { id } = operation
|
||||||
|
if (id === 'copy') {
|
||||||
|
return true
|
||||||
|
} else if (id === 'view') {
|
||||||
|
return !isFullData
|
||||||
|
} else if (id === 'open-folder') {
|
||||||
|
return item.type === 'file'
|
||||||
|
} else if (id === 'collect') {
|
||||||
|
return item.type !== 'file'
|
||||||
|
} else if (id === 'word-break') {
|
||||||
|
return item.type === 'text' && item.data.length <= 500 && item.data.length >= 2
|
||||||
|
} else if (id === 'save-file') {
|
||||||
|
return item.type === 'file'
|
||||||
|
} else if (id === 'remove') {
|
||||||
|
return true
|
||||||
|
} else if (id.indexOf('custom') !== -1) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user