Skip to content

Commit

Permalink
feat: 调整代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Chanzhaoyu committed Mar 6, 2023
1 parent e6db6a5 commit cd91cb1
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@
"dockerhub",
"esno",
"GPTAPI",
"highlightjs",
"hljs",
"iconify",
"katex",
"katexmath",
"linkify",
"logprobs",
"mdhljs",
"nodata",
"OPENAI",
"pinia",
"Popconfirm",
"rushstack",
"Sider",
"tailwindcss",
"traptitech",
"tsup",
"Typecheck",
"unplugin",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"dependencies": {
"@traptitech/markdown-it-katex": "^3.6.0",
"@vueuse/core": "^9.13.0",
"highlight.js": "^11.7.0",
"katex": "^0.16.4",
"markdown-it": "^13.0.1",
"markdown-it-highlightjs": "^4.0.1",
"naive-ui": "^2.34.3",
"pinia": "^2.0.32",
"vue": "^3.2.47",
Expand Down
59 changes: 29 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 0 additions & 23 deletions src/directives/highlight.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/directives/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createApp } from 'vue'
import App from './App.vue'
import { setupDirectives } from './directives'
import { setupI18n } from './locales'
import { setupAssets } from './plugins'
import { setupStore } from './store'
Expand All @@ -12,8 +11,6 @@ async function bootstrap() {

setupStore(app)

setupDirectives(app)

setupI18n(app)

await setupRouter(app)
Expand Down
19 changes: 15 additions & 4 deletions src/views/chat/components/Message/Text.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import { computed, ref } from 'vue'
import MarkdownIt from 'markdown-it'
import mdKatex from '@traptitech/markdown-it-katex'
import mdhljs from 'markdown-it-highlightjs'
import hljs from 'highlight.js'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { t } from '@/locales'
interface Props {
inversion?: boolean
Expand All @@ -20,11 +21,17 @@ const textRef = ref<HTMLElement>()
const mdi = new MarkdownIt({
linkify: true,
highlight(code, language) {
const validLang = !!(language && hljs.getLanguage(language))
if (validLang) {
const lang = language ?? ''
return highlightBlock(hljs.highlight(lang, code, true).value, lang)
}
return highlightBlock(hljs.highlightAuto(code).value, '')
},
})
mdi
.use(mdhljs, { auto: true, inline: true })
.use(mdKatex, { blockClass: 'katexmath-block rounded-md p-[10px]', errorColor: ' #cc0000' })
mdi.use(mdKatex, { blockClass: 'katexmath-block rounded-md p-[10px]', errorColor: ' #cc0000' })
const wrapClass = computed(() => {
return [
Expand All @@ -45,6 +52,10 @@ const text = computed(() => {
return value
})
function highlightBlock(str: string, lang?: string) {
return `<pre class="code-block-wrapper"><div class="code-block-header"><span class="code-block-header__lang">${lang}</span><span class="code-block-header__copy">${t('chat.copyCode')}</span></div><code class="hljs code-block-body ${lang}">${str}</code></pre>`
}
defineExpose({ textRef })
</script>

Expand Down
2 changes: 0 additions & 2 deletions src/views/chat/components/Message/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
line-height: 1.65;
}

.katexmath-block,
.highlight pre,
pre {
background-color: #fff;
Expand Down Expand Up @@ -61,7 +60,6 @@
html.dark {

.highlight pre,
.katexmath-block,
pre {
background-color: #282c34;
}
Expand Down
6 changes: 5 additions & 1 deletion src/views/chat/hooks/useCopyCode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { onMounted, onUpdated } from 'vue'
import { copyText } from '@/utils/format'

export function useCopyCode() {
function copyCodeBlock() {
Expand All @@ -8,7 +9,10 @@ export function useCopyCode() {
const codeBlock = wrapper.querySelector('.code-block-body')
if (copyBtn && codeBlock) {
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(codeBlock.textContent ?? '')
if (navigator.clipboard?.writeText)
navigator.clipboard.writeText(codeBlock.textContent ?? '')
else
copyText({ text: codeBlock.textContent ?? '', origin: true })
})
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/views/chat/layout/sider/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const dataSources = computed(() => chatStore.history)
async function handleSelect({ uuid }: Chat.History) {
if (isActive(uuid))
return
if(chatStore.active)
if (chatStore.active)
chatStore.updateHistory(chatStore.active, { isEdit: false })
await chatStore.setActive(uuid)
Expand Down

0 comments on commit cd91cb1

Please sign in to comment.