feat: 匹配规则支持使用正则

This commit is contained in:
ZiuChen 2022-09-18 13:04:50 +08:00
parent f97ee19319
commit 813cc96ce0
2 changed files with 38 additions and 6 deletions

View File

@ -17,6 +17,20 @@
"icon": "🔍",
"match": ["text"],
"command": "redirect:百度一下"
},
{
"id": "custom.1663468466",
"title": "上传到图床",
"icon": "🚀",
"match": ["image", { "type": "file", "regex": "/.*(jpg|png|jpeg|gif)$/i" }],
"command": "redirect:上传到图床"
},
{
"id": "custom.1663476961",
"title": "翻译",
"icon": "🌎",
"match": ["text"],
"command": "redirect:翻译"
}
]
}

View File

@ -32,7 +32,7 @@ export default function useClipOperate({ emit }) {
} else if (id === 'save-file') {
utools.redirect('收集文件', {
type: typeMap[item.type],
data: item.data
data: item.type === 'file' ? JSON.parse(item.data).map((f) => f.path) : item.data
})
} else if (id === 'remove') {
window.remove(item)
@ -42,13 +42,12 @@ export default function useClipOperate({ emit }) {
if (a[0] === 'redirect') {
utools.redirect(a[1], {
type: typeMap[item.type],
data: item.data
data: item.type === 'file' ? JSON.parse(item.data).map((f) => f.path) : item.data
})
}
}
emit('onOperateExecute')
},
filterOperate: (operation, item, isFullData) => {
const { id } = operation
if (!isFullData) {
@ -69,14 +68,33 @@ export default function useClipOperate({ emit }) {
} 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'
return true
} else if (id === 'remove') {
return true
} else if (id.indexOf('custom') !== -1) {
// 如果匹配到了自定义的操作 则展示
for (const m of operation.match) {
if (item.type === m) {
return true
if (typeof m === 'string') {
if (item.type === m) {
return true
}
} else if (typeof m === 'object') {
// 根据正则匹配内容
const r = new RegExp(m.regex)
if (item.type === 'file') {
const fl = JSON.parse(item.data)
for (const f of fl) {
console.log(f.name, r.test(f.name))
// TODO: fix: 图片文件不能正确匹配
if (r.test(f.name)) {
return true
}
}
} else {
if (r.test(item.data)) {
return true
}
}
}
}
return false