Skip to content

Commit 9b29a25

Browse files
authored
Implement script loading and highlight.js functionality
Added script loading promise and highlight.js integration.
1 parent a0cd92f commit 9b29a25

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

src/main.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ class AIAssistant {
6868
this.$style
6969
);
7070

71+
this.scriptsLoaded = new Promise((resolve) => {
72+
let loaded = 0;
73+
const total = 2;
74+
const check = () => {
75+
loaded += 1;
76+
if (loaded >= total) {
77+
try {
78+
if (window.hljs && typeof window.hljs.highlightAll === "function") {
79+
window.hljs.highlightAll();
80+
}
81+
} catch (e) {}
82+
resolve();
83+
}
84+
};
85+
this.$higlightJsFile.onload = check;
86+
this.$higlightJsFile.onerror = check;
87+
this.$markdownItFile.onload = check;
88+
this.$markdownItFile.onerror = check;
89+
});
90+
91+
try {
92+
await this.scriptsLoaded;
93+
} catch (e) {}
94+
7195
this.apiKeyManager = new APIKeyManager("acode-ai-assistant-secret");
7296
this.sessions = [];
7397
this.currentSession = null;
@@ -339,7 +363,7 @@ class AIAssistant {
339363
switch (view) {
340364
case "chat":
341365
if (navBtns[0]) navBtns[0].classList.add("active");
342-
if (chatArea) chatArea.style.display = "block";
366+
if (chatArea) chatArea.style.display = "flex";
343367
break;
344368
case "history":
345369
if (navBtns[1]) navBtns[1].classList.add("active");
@@ -390,6 +414,9 @@ class AIAssistant {
390414

391415
this.updateMessage(aiMessageElement, response, app);
392416

417+
const chatAreaAfter = app.querySelector("#ai-chat-area");
418+
if (chatAreaAfter) chatAreaAfter.scrollTop = chatAreaAfter.scrollHeight;
419+
393420
if (!this.currentSession) {
394421
this.currentSession = {
395422
id: uuidv4(),
@@ -427,7 +454,7 @@ class AIAssistant {
427454

428455
const messageContent = createTag("div", {
429456
className: "ai-message-content",
430-
innerHTML: content,
457+
innerHTML: role === "assistant" ? content : (String(content).replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>")),
431458
});
432459

433460
messageDiv.appendChild(avatar);
@@ -676,4 +703,4 @@ if (window.acode) {
676703
acode.setPluginUnmount(plugin.id, () => {
677704
acodePlugin.destroy();
678705
});
679-
}
706+
}

0 commit comments

Comments
 (0)