SimpleChatTCRV:Cleanup:Delete keeps view around adjacent messages

Rename to get_chatmessage_index.

Return the index of the chatmessage deleted.

Add a scroll element into view helper.

Now the UniqId wrt the ChatMessage is stored as a attribute
wrt the corresponding chatmessage block. And same used wrt
scrolling after deleting messages.

When a chat message is deleted, previously the screen would have
moved to last message in the chat session, now it tries to remain
wrt adjacent messages on the screen.
This commit is contained in:
hanishkvc 2025-11-14 15:19:27 +05:30
parent c0dbc1d59c
commit e731d29dc9
1 changed files with 27 additions and 6 deletions

View File

@ -666,7 +666,7 @@ class SimpleChat {
* Get xchat index corresponding to given chat message uniqId. * Get xchat index corresponding to given chat message uniqId.
* @param {number} uniqId * @param {number} uniqId
*/ */
get_chatmessage(uniqId) { get_chatmessage_index(uniqId) {
return this.xchat.findIndex((msg)=>{ return this.xchat.findIndex((msg)=>{
if (msg.uniqId == uniqId) { if (msg.uniqId == uniqId) {
return true return true
@ -677,13 +677,15 @@ class SimpleChat {
/** /**
* Delete a chat message in place using chat message uniqId. * Delete a chat message in place using chat message uniqId.
* Returns index of the chatmessage deleted wrt xchat.
* @param {number} uniqId * @param {number} uniqId
*/ */
delete(uniqId) { delete(uniqId) {
let index = this.get_chatmessage(uniqId); let index = this.get_chatmessage_index(uniqId);
if (index >= 0) { if (index >= 0) {
this.xchat.splice(index, 1) this.xchat.splice(index, 1)
} }
return index
} }
/** /**
@ -1088,6 +1090,17 @@ class MultiChatUI {
} }
} }
/**
* Scroll the given element into view.
* @param {HTMLElement|null} el
*/
scroll_el_into_view(el) {
if (!el) {
return
}
/** @type{HTMLElement} */(el).scrollIntoView(false); // Stupid ts-check js-doc intersection ???
}
/** /**
* Reset/Setup Tool Call UI parts as needed * Reset/Setup Tool Call UI parts as needed
* @param {ChatMessageEx} ar * @param {ChatMessageEx} ar
@ -1223,6 +1236,7 @@ class MultiChatUI {
console.log(`DBUG:MCUI:ChatMessageMLeave:${msg.uniqId}`) console.log(`DBUG:MCUI:ChatMessageMLeave:${msg.uniqId}`)
}) })
elParent?.append(secMain) elParent?.append(secMain)
secMain.setAttribute("CMUniqId", String(msg.uniqId))
this.elLastChatMessage = secMain; this.elLastChatMessage = secMain;
// Create role para // Create role para
let entry = ui.el_create_append_p(`${msg.ns.role}`, secMain); let entry = ui.el_create_append_p(`${msg.ns.role}`, secMain);
@ -1321,7 +1335,7 @@ class MultiChatUI {
this.show_message(this.elDivChat, x, iFromLast, nextMsg) this.show_message(this.elDivChat, x, iFromLast, nextMsg)
} }
if (this.elLastChatMessage != null) { if (this.elLastChatMessage != null) {
/** @type{HTMLElement} */(this.elLastChatMessage).scrollIntoView(false); // Stupid ts-check js-doc intersection ??? this.scroll_el_into_view(this.elLastChatMessage)
} else { } else {
if (bClear) { if (bClear) {
this.elDivChat.innerHTML = usage_note(this.me.chatProps.iRecentUserMsgCnt-1); this.elDivChat.innerHTML = usage_note(this.me.chatProps.iRecentUserMsgCnt-1);
@ -1428,14 +1442,21 @@ class MultiChatUI {
this.elPopoverChatMsgDelBtn.addEventListener('click', (ev) => { this.elPopoverChatMsgDelBtn.addEventListener('click', (ev) => {
console.log(`DBUG:MCUI:ChatMsgPO:Del:${this.curChatId}:${this.uniqIdChatMsgPO}`) console.log(`DBUG:MCUI:ChatMsgPO:Del:${this.curChatId}:${this.uniqIdChatMsgPO}`)
this.simpleChats[this.curChatId].delete(this.uniqIdChatMsgPO) let index = this.simpleChats[this.curChatId].delete(this.uniqIdChatMsgPO)
this.chat_show(this.curChatId) if (index >= 0) {
this.chat_show(this.curChatId)
let msg = this.simpleChats[this.curChatId].xchat[index]
if (msg) {
let el = document.querySelector(`[CMUniqId="${msg.uniqId}"`)
this.scroll_el_into_view(el)
}
}
}) })
this.elPopoverChatMsgCopyBtn.addEventListener('click', (ev) => { this.elPopoverChatMsgCopyBtn.addEventListener('click', (ev) => {
console.log(`DBUG:MCUI:ChatMsgPO:Copy:${this.curChatId}:${this.uniqIdChatMsgPO}`) console.log(`DBUG:MCUI:ChatMsgPO:Copy:${this.curChatId}:${this.uniqIdChatMsgPO}`)
let chatSession = this.simpleChats[this.curChatId] let chatSession = this.simpleChats[this.curChatId]
let index = chatSession.get_chatmessage(this.uniqIdChatMsgPO) let index = chatSession.get_chatmessage_index(this.uniqIdChatMsgPO)
let chat = chatSession.xchat[index] let chat = chatSession.xchat[index]
if (!chat.ns.has_content()) { if (!chat.ns.has_content()) {
return return