Temporarily disable the typewriter effect and improve the highlight method

This commit is contained in:
Rafi
2023-03-09 23:03:20 +08:00
parent cd50086c1e
commit 4cfc9f4aea
2 changed files with 47 additions and 61 deletions

View File

@@ -3,32 +3,33 @@ import { marked } from "marked"
import hljs from "highlight.js" import hljs from "highlight.js"
import copy from 'copy-to-clipboard' import copy from 'copy-to-clipboard'
// Part of the code comes from this project https://github.com/arronhunt/highlightjs-copy, thanks to the author's contribution marked.setOptions({
highlight: function (code, lang) {
const language = hljs.getLanguage(lang) ? lang : 'plaintext'
return `<div class="hljs-code-header d-flex align-center justify-space-between bg-grey-darken-3 pa-1"><span class="pl-2 text-caption">${language}</span><button class="hljs-copy-button" data-copied="false">Copy</button></div><div class="hljs-code-body">${hljs.highlight(code, { language }).value}</div>`
},
langPrefix: 'hljs-code-container my-3 hljs language-', // highlight.js css class prefix
})
hljs.addPlugin({ const props = defineProps(['content'])
'after:highlightElement': ({ el, result, text }) => {
let header = el.parentElement.querySelector(".hljs-code-header"); const contentHtml = ref('')
if (header) {
header.remove(); const contentElm = ref(null)
watchEffect(() => {
contentHtml.value = props.content ? marked(props.content) : ''
})
const bindCopyCodeToButtons = () => {
if (!contentElm.value) {
return
} }
contentElm.value.querySelectorAll('.hljs-code-container').forEach((codeContainer) => {
header = Object.assign(document.createElement("div"), { const copyButton = codeContainer.querySelector('.hljs-copy-button');
className: "hljs-code-header d-flex align-center justify-space-between bg-grey-darken-3 pa-1", const codeBody = codeContainer.querySelector('.hljs-code-body');
innerHTML: `<div class="pl-2 text-caption">${result.language}</div>`
});
let copyButton = Object.assign(document.createElement("button"), {
innerHTML: "Copy",
className: "hljs-copy-button",
});
copyButton.dataset.copied = false;
header.append(copyButton);
//
el.parentElement.classList.add("d-flex","flex-column", "my-3");
el.parentElement.prepend(header);
copyButton.onclick = function () { copyButton.onclick = function () {
copy(text); copy(codeBody.textContent ?? '');
copyButton.innerHTML = "Copied!"; copyButton.innerHTML = "Copied!";
copyButton.dataset.copied = 'true'; copyButton.dataset.copied = 'true';
@@ -38,39 +39,15 @@ hljs.addPlugin({
copyButton.dataset.copied = 'false'; copyButton.dataset.copied = 'false';
}, 2000); }, 2000);
}; };
}
});
marked.setOptions({
// highlight: function (code, lang) {
// const language = hljs.getLanguage(lang) ? lang : 'plaintext'
// return hljs.highlight(code, { language }).value
// },
langPrefix: 'hljs language-', // highlight.js css class prefix
})
const props = defineProps(['content'])
const contentHtml = ref('')
const contentElm = ref(null)
const highlightCode = () => {
if (!contentElm.value) {
return
}
contentElm.value.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block)
}) })
} }
watchEffect(() => { onMounted(() => {
contentHtml.value = props.content ? marked(props.content) : '' bindCopyCodeToButtons()
if (props.content && props.content.endsWith('```')) {
nextTick(() => {
highlightCode()
}) })
}
onUpdated(() => {
bindCopyCodeToButtons()
}) })
</script> </script>
@@ -87,6 +64,12 @@ watchEffect(() => {
.chat-msg-content ol { .chat-msg-content ol {
list-style-position: inside; list-style-position: inside;
} }
.hljs-code-container {
border-radius: 3px;
}
.hljs-code-header {
margin: -1em -1em 10px -1em;
}
.hljs-copy-button{ .hljs-copy-button{
width:2rem;height:2rem;text-indent:-9999px;color:#fff; width:2rem;height:2rem;text-indent:-9999px;color:#fff;
border-radius:.25rem;border:1px solid #ffffff22; border-radius:.25rem;border:1px solid #ffffff22;

View File

@@ -24,16 +24,19 @@ const processMessageQueue = () => {
} }
isProcessingQueue = true isProcessingQueue = true
const nextMessage = messageQueue.shift() const nextMessage = messageQueue.shift()
let wordIndex = 0; currentConversation.value.messages[currentConversation.value.messages.length - 1].message += nextMessage
const intervalId = setInterval(() => {
currentConversation.value.messages[currentConversation.value.messages.length - 1].message += nextMessage[wordIndex]
wordIndex++
if (wordIndex === nextMessage.length) {
clearInterval(intervalId)
isProcessingQueue = false isProcessingQueue = false
processMessageQueue() processMessageQueue()
} // let wordIndex = 0;
}, 50) // const intervalId = setInterval(() => {
// currentConversation.value.messages[currentConversation.value.messages.length - 1].message += nextMessage[wordIndex]
// wordIndex++
// if (wordIndex === nextMessage.length) {
// clearInterval(intervalId)
// isProcessingQueue = false
// processMessageQueue()
// }
// }, 50)
} }
let ctrl let ctrl