【代码评审】AI:code review 思维导图的逻辑

pull/491/MERGE
YunaiV 2024-07-29 22:07:31 +08:00
parent a805ceb534
commit 108dcccafe
7 changed files with 816 additions and 61 deletions

View File

@ -29,6 +29,9 @@ const include = [
'@wangeditor/editor-for-vue',
'@microsoft/fetch-event-source',
'markdown-it',
'markmap-view',
'markmap-lib',
'markmap-toolbar',
'highlight.js',
'element-plus',
'element-plus/es',

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,6 @@ import { config } from '@/config/axios/config'
export interface AiMindMapGenerateReqVO {
prompt: string
}
//
export const AiMindMapApi = {
generateMindMap: ({

View File

@ -20,13 +20,14 @@
type="primary"
:loading="isGenerating"
@click="emits('submit', formData)"
>智能生成思维导图</el-button
>
智能生成思维导图
</el-button>
</div>
<div class="mt-[30px]">
<el-text tag="b">使用已有内容生成</el-text>
<el-input
v-model="existPrompt"
v-model="generatedContent"
maxlength="1024"
rows="5"
class="w-100% mt-15px"
@ -38,17 +39,18 @@
<el-button
class="!w-full mt-[15px]"
type="primary"
@click="emits('directGenerate', existPrompt)"
@click="emits('directGenerate', generatedContent)"
:disabled="isGenerating"
>直接生成</el-button
>
直接生成
</el-button>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { MindmapExitExample } from '@/views/ai/utils/constants'
import { MindMapContentExample } from '@/views/ai/utils/constants'
const emits = defineEmits(['submit', 'directGenerate'])
defineProps<{
@ -59,11 +61,12 @@ const formData = reactive({
prompt: ''
})
const existPrompt = ref(MindmapExitExample) //
const generatedContent = ref(MindMapContentExample) //
defineExpose({
setExistPrompt(e: string){ //
existPrompt.value = e
setGeneratedContent(newContent: string) {
//
generatedContent.value = newContent
}
})
</script>

View File

@ -1,7 +1,7 @@
<template>
<el-card class="my-card h-full flex-grow">
<template #header
><h3 class="m-0 px-7 shrink-0 flex items-center justify-between">
<template #header>
<h3 class="m-0 px-7 shrink-0 flex items-center justify-between">
<span>思维导图预览</span>
<!-- 展示在右上角 -->
<el-button type="primary" v-show="isEnd" @click="downloadImage" size="small">
@ -10,8 +10,8 @@
</template>
下载图片
</el-button>
</h3></template
>
</h3>
</template>
<div ref="contentRef" class="hide-scroll-bar h-full box-border">
<!--展示 markdown 的容器最终生成的是 html 字符串直接用 v-html 嵌入-->
@ -34,8 +34,11 @@ import { Toolbar } from 'markmap-toolbar'
import markdownit from 'markdown-it'
const md = markdownit()
const message = useMessage() //
// TODO @hheromindmap mindMap
const props = defineProps<{
mindmapResult: string //
mindmapResult: string // TODO @hhero generatedContent
isEnd: boolean //
isGenerating: boolean //
isStart: boolean // html
@ -50,8 +53,6 @@ const contentAreaHeight = ref(0) // 生成区域的高度出去header部分
let markMap: Markmap | null = null
const transformer = new Transformer()
const message = useMessage()
onMounted(() => {
contentAreaHeight.value = contentRef.value?.clientHeight || 0 //
/** 初始化思维导图 **/
@ -79,6 +80,7 @@ watch(props, ({ mindmapResult, isGenerating, isEnd, isStart }) => {
}
})
/** 更新思维导图的展示 */
const update = () => {
try {
const { root } = transformer.transform(processContent(props.mindmapResult))
@ -89,7 +91,8 @@ const update = () => {
}
}
const processContent = (text) => {
/** 处理内容 */
const processContent = (text: string) => {
const arr: string[] = []
const lines = text.split('\n')
for (let line of lines) {
@ -101,6 +104,9 @@ const processContent = (text) => {
}
return arr.join('\n')
}
/** 下载图片 */
// TODO @hhhero download src/utils/download.ts image
// download SVG to png file
const downloadImage = () => {
const svgElement = mindmapRef.value
@ -121,7 +127,7 @@ const downloadImage = () => {
image.onload = function () {
context?.drawImage(image, 0, 0)
const a = document.createElement('a')
a.download = 'ruoyi-mindmap.png'
a.download = 'mindmap.png'
a.href = canvas.toDataURL(`image/png`)
a.click()
}

View File

@ -1,7 +1,12 @@
<template>
<div class="absolute top-0 left-0 right-0 bottom-0 flex">
<!--表单区域-->
<Left ref="leftRef" @submit="submit" @direct-generate="directGenerate" :is-generating="isGenerating" />
<Left
ref="leftRef"
@submit="submit"
@direct-generate="directGenerate"
:is-generating="isGenerating"
/>
<!--右边生成思维导图区域-->
<Right
ref="rightRef"
@ -17,10 +22,10 @@
import Left from './components/Left.vue'
import Right from './components/Right.vue'
import { AiMindMapApi, AiMindMapGenerateReqVO } from '@/api/ai/mindmap'
import { MindmapExitExample } from '@/views/ai/utils/constants'
import { MindMapContentExample } from '@/views/ai/utils/constants'
defineOptions({
name: 'AIMindMap'
name: 'AiMindMap'
})
const ctrl = ref<AbortController>() //
const isGenerating = ref(false) //
@ -33,21 +38,21 @@ const mindmapResult = ref('') // 生成思维导图结果
const leftRef = ref<InstanceType<typeof Left>>() //
const rightRef = ref<InstanceType<typeof Right>>() //
onMounted(() => {
mindmapResult.value = MindmapExitExample
})
/** 使用已有内容直接生成 **/
const directGenerate = (existPrompt: string) => {
isEnd.value = false // falsetruewatch
mindmapResult.value = existPrompt
isEnd.value = true
}
/** 停止 stream 生成 */
const stopStream = () => {
isGenerating.value = false
isStart.value = false
ctrl.value?.abort()
}
/** 提交生成 */
const submit = (data: AiMindMapGenerateReqVO) => {
isGenerating.value = true
isStart.value = true
@ -69,7 +74,7 @@ const submit = (data: AiMindMapGenerateReqVO) => {
},
onClose() {
isEnd.value = true
leftRef.value?.setExistPrompt(mindmapResult.value)
leftRef.value?.setGeneratedContent(mindmapResult.value)
stopStream()
},
onError(err) {
@ -79,4 +84,9 @@ const submit = (data: AiMindMapGenerateReqVO) => {
ctrl: ctrl.value
})
}
/** 初始化 */
onMounted(() => {
mindmapResult.value = MindMapContentExample
})
</script>

View File

@ -67,10 +67,11 @@ export enum AiWriteTypeEnum {
// 表格展示对照map
export const AiWriteTypeTableRender = {
[AiWriteTypeEnum.WRITING]: '撰写',
[AiWriteTypeEnum.REPLY]: '回复',
[AiWriteTypeEnum.REPLY]: '回复'
}
// ========== 【图片 UI】相关的枚举 ==========
export const ImageHotWords = [
'中国旗袍',
'古装美女',
@ -414,8 +415,11 @@ export const WriteExample = {
data: '您的请假申请已收悉,经核实和考虑,暂时无法批准您的请假申请。\n\n如有特殊情况或紧急事务请及时与我联系。\n\n祝工作顺利。\n\n谢谢。'
}
}
// ========== 【思维导图 UI】相关的枚举 ==========
/** 思维导图已有内容生成示例 **/
export const MindmapExitExample = `# Java 技术栈
export const MindMapContentExample = `# Java 技术栈
##
### Java SE