1. 优化 ChatFree 页面左侧会话列表展示逻辑

2. 渲染 Prompt 组件换行符
This commit is contained in:
RockYang
2023-04-21 14:15:23 +08:00
parent d8bc0fe125
commit 52398d9b8d
4 changed files with 51 additions and 21 deletions

View File

@@ -73,6 +73,7 @@ export function dateFormat(timestamp, format) {
return timeDate;
}
// 判断数组中是否包含某个元素
export function arrayContains(array, value, compare) {
if (typeof compare !== 'function') {
compare = function (v1, v2) {
@@ -85,4 +86,27 @@ export function arrayContains(array, value, compare) {
}
}
return false;
}
// 删除数组中指定的元素
export function removeArrayItem(array, value, compare) {
if (typeof compare !== 'function') {
compare = function (v1, v2) {
return v1 === v2;
}
}
for (let i = 0; i < array.length; i++) {
if (compare(array[i], value)) {
array.splice(i, 1);
break;
}
}
return array;
}
// 渲染输入的换行符
export function renderInputText(text) {
const replaceRegex = /(\n\r|\r\n|\r|\n)/g;
text = text || '';
return text.replace(replaceRegex, "<br/>");
}

View File

@@ -1,5 +1,5 @@
/* eslint-disable no-constant-condition */
import {dateFormat} from "@/utils/libs";
import {dateFormat, removeArrayItem} from "@/utils/libs";
import Storage from 'good-storage'
/**
@@ -69,32 +69,32 @@ export function getChatHistory(chatId) {
}
export function getChatList() {
return Storage.get(ChatListKey);
}
export function getChat(chatId) {
let chatList = Storage.get(ChatListKey);
if (!chatList) {
return null;
const list = Storage.get(ChatListKey);
if (list) {
if (typeof list.reverse !== 'function') {
Storage.remove(ChatListKey)
return null;
}
return list.reverse();
}
return chatList[chatId] ? chatList[chatId] : null;
}
export function setChat(chat) {
let chatList = Storage.get(ChatListKey);
if (!chatList) {
chatList = {};
chatList = [];
}
chatList[chat.id] = chat;
chatList.push(chat);
Storage.set(ChatListKey, chatList);
}
export function removeChat(chatId) {
const chatList = Storage.get(ChatListKey);
export function removeChat(chat) {
let chatList = Storage.get(ChatListKey);
if (chatList) {
delete chatList[chatId];
chatList = removeArrayItem(chatList, chat, function (v1, v2) {
return v1.id === v2.id
})
Storage.set(ChatListKey, chatList);
}
}