mirror of
https://github.com/multipleof4/sune.git
synced 2026-01-14 00:27:56 +00:00
Feat: Create popovers component module
This commit is contained in:
9
src/components/popovers.js
Normal file
9
src/components/popovers.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import{el,icons,positionPopover}from'../lib/dom.js';import{THREAD}from'../core/thread.js';import{SUNE}from'../core/sune.js';import{renderThreads,renderSidebar}from'./sidebars.js';import{clearChat,state}from'../core/state.js';import{dl,ts,imgToWebp}from'../lib/utils.js';import{reflectActiveSune}from'../main.js';
|
||||||
|
let menuThreadId=null,menuSuneId=null;
|
||||||
|
export const hideThreadPopover=()=>{el.threadPopover.classList.add('hidden');menuThreadId=null}
|
||||||
|
export const hideSunePopover=()=>{el.sunePopover.classList.add('hidden');menuSuneId=null}
|
||||||
|
function showThreadPopover(btn,id){menuThreadId=id;el.threadPopover.classList.remove('hidden');positionPopover(btn,el.threadPopover);icons()}
|
||||||
|
function showSunePopover(btn,id){menuSuneId=id;el.sunePopover.classList.remove('hidden');positionPopover(btn,el.sunePopover);icons()}
|
||||||
|
export function initPopovers(){$(el.threadPopover).on('click',async e=>{const act=e.target.closest('[data-action]')?.getAttribute('data-action');if(!act||!menuThreadId)return;const th=THREAD.get(menuThreadId);if(!th)return;if(act==='pin'){th.pinned=!th.pinned}else if(act==='rename'){const nv=prompt('Rename to:',th.title);if(nv!=null){th.title=(nv||'').replace(/\s+/g,' ').trim().slice(0,60)||'Untitled';th.updatedAt=Date.now()}}else if(act==='delete'){if(confirm('Delete this chat?')){THREAD.list=THREAD.list.filter(x=>x.id!==th.id);if(state.currentThreadId===th.id){state.currentThreadId=null;clearChat()}}}else if(act==='count_tokens'){const msgs=Array.isArray(th.messages)?th.messages:[];let totalChars=0;for(const m of msgs){if(!m||!m.role||m.role==='system')continue;totalChars+=String((m.content||'')||'').length}const tokens=Math.max(0,Math.ceil(totalChars/4));const k=tokens>=1000?Math.round(tokens/1000)+'k':String(tokens);alert(tokens+' tokens ('+k+')')}hideThreadPopover();await THREAD.save();renderThreads()})
|
||||||
|
$(el.sunePopover).on('click',async e=>{const act=e.target.closest('[data-action]')?.getAttribute('data-action');if(!act||!menuSuneId)return;const s=SUNE.get(menuSuneId);if(!s)return;const updateAndRender=async()=>{s.updatedAt=Date.now();SUNE.save();renderSidebar();await reflectActiveSune()};if(act==='pin'){s.pinned=!s.pinned;await updateAndRender()}else if(act==='rename'){const n=prompt('Rename sune to:',s.name);if(n!=null){s.name=n.trim();await updateAndRender()}}else if(act==='pfp'){const i=document.createElement('input');i.type='file';i.accept='image/*';i.onchange=async()=>{const f=i.files?.[0];if(!f)return;try{s.avatar=await imgToWebp(f);await updateAndRender()}catch{}};i.click()}else if(act==='export')dl(`sune-${(s.name||'sune').replace(/\W/g,'_')}-${ts()}.sune`,[s]);hideSunePopover()})
|
||||||
|
return{showSunePopover,showThreadPopover}}
|
||||||
Reference in New Issue
Block a user