-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<!-- | ||
- Copyright (c) 2023, Terwer . All rights reserved. | ||
- DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
- | ||
- This code is free software; you can redistribute it and/or modify it | ||
- under the terms of the GNU General Public License version 2 only, as | ||
- published by the Free Software Foundation. Terwer designates this | ||
- particular file as subject to the "Classpath" exception as provided | ||
- by Terwer in the LICENSE file that accompanied this code. | ||
- | ||
- This code is distributed in the hope that it will be useful, but WITHOUT | ||
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
- version 2 for more details (a copy is included in the LICENSE file that | ||
- accompanied this code). | ||
- | ||
- You should have received a copy of the GNU General Public License version | ||
- 2 along with this work; if not, write to the Free Software Foundation, | ||
- Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
- | ||
- Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
- or visit www.terwer.space if you need additional information or have any | ||
- questions. | ||
--> | ||
|
||
<script setup lang="ts"> | ||
import { useVueI18n } from "~/src/composables/useVueI18n.ts" | ||
import { reactive, watch } from "vue" | ||
import { createAppLogger } from "~/src/utils/appLogger.ts" | ||
import { ElMessage } from "element-plus" | ||
import { HtmlUtil } from "zhi-common" | ||
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts" | ||
const logger = createAppLogger("publish-description") | ||
const { t } = useVueI18n() | ||
const { kernelApi } = useSiyuanApi() | ||
const props = defineProps({ | ||
useAi: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
pageId: { | ||
type: String, | ||
default: "", | ||
}, | ||
desc: { | ||
type: String, | ||
default: "", | ||
}, | ||
content: { | ||
type: String, | ||
default: "", | ||
}, | ||
}) | ||
const MAX_PREVIEW_LENGTH = 255 | ||
const formData = reactive({ | ||
isDescLoading: false, | ||
useAi: props.useAi, | ||
pageId: props.pageId, | ||
desc: props.desc, | ||
html: props.content, | ||
}) | ||
// 对于未强制刷新组件的情况下需要watch或者computed | ||
watch( | ||
() => props.useAi, | ||
(newValue) => { | ||
formData.useAi = newValue | ||
} | ||
) | ||
const emit = defineEmits(["emitSyncDesc"]) | ||
const handleMakeDesc = async () => { | ||
logger.debug("准备生成摘要...") | ||
formData.isDescLoading = true | ||
try { | ||
if (formData.useAi) { | ||
ElMessage.warning("使用人工智能提取摘要。开发中...") | ||
formData.isDescLoading = false | ||
return | ||
} else { | ||
formData.desc = HtmlUtil.parseHtml(formData.html, MAX_PREVIEW_LENGTH, true) | ||
ElMessage.info(`未开启人工智能,直接截取文章前${MAX_PREVIEW_LENGTH}个字符作为摘要`) | ||
} | ||
// 保存属性到思源 | ||
const customAttr = { | ||
memo: formData.desc, | ||
} | ||
await kernelApi.setBlockAttrs(formData.pageId, customAttr) | ||
ElMessage.success(t("main.opt.success")) | ||
} catch (e) { | ||
logger.error(t("main.opt.failure") + "=>", e) | ||
ElMessage.error(t("main.opt.failure") + "=>", e) | ||
} | ||
formData.isDescLoading = false | ||
logger.debug("摘要生成完毕.") | ||
} | ||
const onDescChange = () => { | ||
emit("emitSyncDesc", formData.desc) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="form-desc"> | ||
<el-form-item :label="t('main.desc')"> | ||
<el-input v-model="formData.desc" :autosize="{ minRows: 3, maxRows: 16 }" type="textarea" @input="onDescChange" /> | ||
</el-form-item> | ||
<el-form-item> | ||
<el-button size="small" :loading="formData.isDescLoading" type="primary" @click="handleMakeDesc"> | ||
{{ formData.isDescLoading ? t("main.opt.loading") : t("main.auto.fetch.desc") }} | ||
</el-button> | ||
</el-form-item> | ||
</div> | ||
</template> | ||
|
||
<style scoped lang="stylus"></style> |