mirror of
https://github.com/Chanzhaoyu/chatgpt-web.git
synced 2025-10-18 17:52:25 +00:00
feat: 调整代码
This commit is contained in:
@@ -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
|
||||
@@ -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 [
|
||||
@@ -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>
|
||||
|
||||
|
@@ -19,7 +19,6 @@
|
||||
line-height: 1.65;
|
||||
}
|
||||
|
||||
.katexmath-block,
|
||||
.highlight pre,
|
||||
pre {
|
||||
background-color: #fff;
|
||||
@@ -61,7 +60,6 @@
|
||||
html.dark {
|
||||
|
||||
.highlight pre,
|
||||
.katexmath-block,
|
||||
pre {
|
||||
background-color: #282c34;
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { onMounted, onUpdated } from 'vue'
|
||||
import { copyText } from '@/utils/format'
|
||||
|
||||
export function useCopyCode() {
|
||||
function copyCodeBlock() {
|
||||
@@ -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 })
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user