feat: 新增智慧分词功能 每日5条 每条限制500个字符

This commit is contained in:
ZiuChen 2022-09-08 15:24:30 +08:00
parent 638e990b65
commit e195dd38c8
5 changed files with 99 additions and 20 deletions

View File

@ -232,5 +232,8 @@ window.remove = remove
window.openFile = utools.shellOpenPath window.openFile = utools.shellOpenPath
window.openFileFolder = utools.shellShowItemInFolder window.openFileFolder = utools.shellShowItemInFolder
window.getIcon = utools.getFileIcon window.getIcon = utools.getFileIcon
window.showNotify = utools.showNotification
window.fetchToken = utools.fetchUserServerTemporaryToken
window.dbStorage = utools.dbStorage
window.focus = focus window.focus = focus
window.toTop = toTop window.toTop = toTop

View File

@ -6,7 +6,17 @@
<template v-for="{ id, name } of btns"> <template v-for="{ id, name } of btns">
<div <div
class="clip-full-operate-list-item" class="clip-full-operate-list-item"
v-if="id !== 'word-split' || (id === 'word-split' && fullData.type !== 'file')" v-if="
(id !== 'word-split' && id !== 'copy-select' && id !== 'clear-select') ||
(id === 'word-split' &&
fullData.type !== 'file' &&
fullData?.data?.length <= '\u0035\u0030\u0030' &&
splitWords.length === 0) ||
(id === 'copy-select' &&
splitWords.filter((item) => item.checked !== false).length !== 0) ||
(id === 'clear-select' &&
splitWords.filter((item) => item.checked !== false).length !== 0)
"
@click="handleBtnClick(id)" @click="handleBtnClick(id)"
> >
{{ name }} {{ name }}
@ -19,7 +29,10 @@
<div v-else-if="fullData.type === 'file'" class="clip-full-content"> <div v-else-if="fullData.type === 'file'" class="clip-full-content">
<FileList :data="JSON.parse(fullData.data)"></FileList> <FileList :data="JSON.parse(fullData.data)"></FileList>
</div> </div>
<ClipWordBreak :words="splitWords"></ClipWordBreak> <ClipWordBreak
v-if="fullData.type === 'text' && splitWords.length !== 0"
:words="splitWords"
></ClipWordBreak>
</div> </div>
</Transition> </Transition>
<div class="clip-overlay" v-show="isShow" @click="onOverlayClick"></div> <div class="clip-overlay" v-show="isShow" @click="onOverlayClick"></div>
@ -43,7 +56,6 @@ const props = defineProps({
}) })
const emit = defineEmits(['onOverlayClick']) const emit = defineEmits(['onOverlayClick'])
const onOverlayClick = () => emit('onOverlayClick')
const btns = [ const btns = [
{ {
@ -53,26 +65,60 @@ const btns = [
{ {
id: 'word-split', id: 'word-split',
name: '🎁 智慧分词' name: '🎁 智慧分词'
},
{
id: 'copy-select',
name: '📑 复制选中'
},
{
id: 'clear-select',
name: '💣 清空选中'
} }
] ]
const splitWords = ref([])
const handleBtnClick = (id) => { const handleBtnClick = (id) => {
switch (id) { switch (id) {
case 'copy-all': case 'copy-all':
window.copy(props.fullData) window.copy(props.fullData)
emit('onOverlayClick') // 退 emit('onOverlayClick') // 退
window.toTop()
break break
case 'word-split': case 'word-split':
// TODO: ()
// TODO: () // TODO: ()
const key = 'word-break-daily-used'
const val = window.dbStorage.getItem(key)
if (val >= '\u0035') {
window.showNotify(
'今日使用次数已达5次, 请明日再使用此功能 新插件`超级分词`即将上线, 敬请期待'
)
} else {
fetchWordBreakResult(props.fullData.data) fetchWordBreakResult(props.fullData.data)
}
break
case 'copy-select':
const checkedList = splitWords.value.filter((item) => item.checked !== false)
if (checkedList.length !== 0) {
window.copy({
type: 'text',
data: checkedList.map((item) => item.value).join('')
})
emit('onOverlayClick')
window.toTop()
} else {
window.showNotify('尚未选中任何内容')
}
break
case 'clear-select':
splitWords.value.map((item) => (item.checked = false))
break break
} }
} }
const splitWords = ref([])
const fetchUserInfo = async () => { const fetchUserInfo = async () => {
return utools.fetchUserServerTemporaryToken().then(({ token, expired_at }) => { return window.fetchToken().then(({ token, expired_at }) => {
return { return {
token, token,
expired_at expired_at
@ -81,10 +127,10 @@ const fetchUserInfo = async () => {
} }
const fetchWordBreakResult = async (origin) => { const fetchWordBreakResult = async (origin) => {
const baseUrl = 'https://service-a0pyrkub-1304937021.sh.apigw.tencentcs.com/release' const baseUrl = 'https://service-nlkfov43-1304937021.sh.apigw.tencentcs.com/release'
// const baseUrl = 'http://localhost:9000'
const url = baseUrl + '/v1/word-break' const url = baseUrl + '/v1/word-break'
const info = await fetchUserInfo() const info = await fetchUserInfo()
console.log(info)
return fetch(url, { return fetch(url, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -98,17 +144,28 @@ const fetchWordBreakResult = async (origin) => {
.then((res) => res.json()) .then((res) => res.json())
.then(({ code, data, msg }) => { .then(({ code, data, msg }) => {
if (code !== 0) { if (code !== 0) {
console.log(msg) window.showNotify(msg)
} else { } else {
splitWords.value = data.splitWord.filter( //
(w) => w !== '' && w !== ' ' && w.indexOf('\n') === -1 const key = 'word-break-daily-used'
) const val = window.dbStorage.getItem(key)
console.log(data.splitWord) window.dbStorage.setItem(key, val === null ? 1 : val + 1)
console.log(data.extractWord) window.dbStorage.setItem('last-update', new Date().valueOf())
splitWords.value = data.splitWord
.filter((w) => w !== '' && w !== ' ' && w.indexOf('\n') === -1)
.map((item) => ({
value: item,
checked: false
}))
} }
}) })
} }
const onOverlayClick = () => {
emit('onOverlayClick')
splitWords.value = []
}
onMounted(() => { onMounted(() => {
document.addEventListener('keydown', (e) => { document.addEventListener('keydown', (e) => {
const { key } = e const { key } = e

View File

@ -2,7 +2,12 @@
<div class="clip-word-break"> <div class="clip-word-break">
<div class="clip-word-break-content"> <div class="clip-word-break-content">
<template v-for="w of words"> <template v-for="w of words">
<div class="clip-word-break-content-item">{{ w }}</div> <div
@click="handleItemClick(w)"
:class="{ 'clip-word-break-content-item': true, active: w.checked }"
>
{{ w.value }}
</div>
</template> </template>
</div> </div>
</div> </div>
@ -15,6 +20,10 @@ const props = defineProps({
required: true required: true
} }
}) })
const handleItemClick = (item) => {
item.checked = !item.checked
}
</script> </script>
<style lang="less" scoped> <style lang="less" scoped>

View File

@ -27,6 +27,7 @@
user-select: none; user-select: none;
margin: 5px 5px; margin: 5px 5px;
background-color: @bg-color; background-color: @bg-color;
transition: all 0.15s;
&:hover { &:hover {
color: @bg-color; color: @bg-color;
background-color: @primary-color; background-color: @primary-color;

View File

@ -1,8 +1,11 @@
.clip-word-break { .clip-word-break {
display: flex; display: flex;
justify-content: center;
align-items: center; align-items: center;
padding: 10px; padding: 5px;
background-color: @text-bg-color;
padding: 20px;
border-radius: 5px;
margin: 5px 0px;
&-content-item { &-content-item {
display: inline-block; display: inline-block;
vertical-align: middle; vertical-align: middle;
@ -13,7 +16,13 @@
margin: 2px 5px; margin: 2px 5px;
cursor: pointer; cursor: pointer;
user-select: none; user-select: none;
background-color: @text-bg-color; background-color: @bg-color;
border-radius: 5px; border-radius: 5px;
transition: all 0.15s;
&.active {
color: @bg-color;
background-color: @primary-color;
transition: all 0.15s;
}
} }
} }