@@ -4,14 +4,18 @@ import { createToolHostRuntime } from "../shared/toolHostRuntime.js";
44const refs = {
55 toolSelect : document . querySelector ( "[data-tool-host-select]" ) ,
66 mountButton : document . querySelector ( "[data-tool-host-mount]" ) ,
7+ prevButton : document . querySelector ( "[data-tool-host-prev]" ) ,
8+ nextButton : document . querySelector ( "[data-tool-host-next]" ) ,
79 unmountButton : document . querySelector ( "[data-tool-host-unmount]" ) ,
810 standaloneLink : document . querySelector ( "[data-tool-host-standalone]" ) ,
11+ switchMetaText : document . querySelector ( "[data-tool-host-switch-meta]" ) ,
912 statusText : document . querySelector ( "[data-tool-host-status]" ) ,
1013 currentLabel : document . querySelector ( "[data-tool-host-current-label]" ) ,
1114 mountContainer : document . querySelector ( "[data-tool-host-mount-container]" )
1215} ;
1316
1417const manifest = createToolHostManifest ( ) ;
18+ const toolIds = manifest . tools . map ( ( tool ) => tool . id ) ;
1519
1620function readSelectedToolId ( ) {
1721 return refs . toolSelect instanceof HTMLSelectElement ? refs . toolSelect . value : "" ;
@@ -29,6 +33,39 @@ function setCurrentLabel(text) {
2933 }
3034}
3135
36+ function writeSwitchMeta ( text ) {
37+ if ( refs . switchMetaText instanceof HTMLElement ) {
38+ refs . switchMetaText . textContent = text ;
39+ }
40+ }
41+
42+ function getSelectedToolIndex ( ) {
43+ const selectedToolId = readSelectedToolId ( ) ;
44+ return toolIds . findIndex ( ( toolId ) => toolId === selectedToolId ) ;
45+ }
46+
47+ function updateSwitchMeta ( ) {
48+ if ( toolIds . length === 0 ) {
49+ writeSwitchMeta ( "No active tools are available in host manifest." ) ;
50+ return ;
51+ }
52+ const selectedIndex = getSelectedToolIndex ( ) ;
53+ const oneBased = selectedIndex >= 0 ? selectedIndex + 1 : 1 ;
54+ writeSwitchMeta ( `Switch target ${ oneBased } /${ toolIds . length } .` ) ;
55+ }
56+
57+ function selectToolByOffset ( offset ) {
58+ if ( ! ( refs . toolSelect instanceof HTMLSelectElement ) || toolIds . length === 0 ) {
59+ return false ;
60+ }
61+
62+ const currentIndex = Math . max ( 0 , getSelectedToolIndex ( ) ) ;
63+ const nextIndex = ( currentIndex + offset + toolIds . length ) % toolIds . length ;
64+ refs . toolSelect . value = toolIds [ nextIndex ] ;
65+ updateSwitchMeta ( ) ;
66+ return true ;
67+ }
68+
3269function updateStandaloneHref ( toolId ) {
3370 if ( ! ( refs . standaloneLink instanceof HTMLAnchorElement ) ) {
3471 return ;
@@ -66,6 +103,7 @@ function populateToolSelect(initialToolId) {
66103 . map ( ( tool ) => `<option value="${ tool . id } ">${ tool . displayName } </option>` )
67104 . join ( "" ) ;
68105 refs . toolSelect . value = getToolHostEntryById ( manifest , initialToolId ) ? initialToolId : ( manifest . tools [ 0 ] ?. id || "" ) ;
106+ updateSwitchMeta ( ) ;
69107}
70108
71109const runtime = createToolHostRuntime ( {
@@ -88,6 +126,7 @@ function mountSelectedTool(source = "manual") {
88126 writeStatus ( "Select a tool to mount." ) ;
89127 return ;
90128 }
129+ updateSwitchMeta ( ) ;
91130 updateStandaloneHref ( toolId ) ;
92131 writeQueryToolId ( toolId , source === "init" ) ;
93132 runtime . mountTool ( toolId , {
@@ -103,6 +142,24 @@ function bindEvents() {
103142 } ) ;
104143 }
105144
145+ if ( refs . prevButton instanceof HTMLButtonElement ) {
146+ refs . prevButton . addEventListener ( "click" , ( ) => {
147+ if ( ! selectToolByOffset ( - 1 ) ) {
148+ return ;
149+ }
150+ mountSelectedTool ( "prev" ) ;
151+ } ) ;
152+ }
153+
154+ if ( refs . nextButton instanceof HTMLButtonElement ) {
155+ refs . nextButton . addEventListener ( "click" , ( ) => {
156+ if ( ! selectToolByOffset ( 1 ) ) {
157+ return ;
158+ }
159+ mountSelectedTool ( "next" ) ;
160+ } ) ;
161+ }
162+
106163 if ( refs . unmountButton instanceof HTMLButtonElement ) {
107164 refs . unmountButton . addEventListener ( "click" , ( ) => {
108165 runtime . unmountCurrentTool ( "manual" ) ;
@@ -111,6 +168,7 @@ function bindEvents() {
111168
112169 if ( refs . toolSelect instanceof HTMLSelectElement ) {
113170 refs . toolSelect . addEventListener ( "change" , ( ) => {
171+ updateSwitchMeta ( ) ;
114172 updateStandaloneHref ( readSelectedToolId ( ) ) ;
115173 mountSelectedTool ( "select" ) ;
116174 } ) ;
@@ -121,6 +179,7 @@ function bindEvents() {
121179 if ( refs . toolSelect instanceof HTMLSelectElement ) {
122180 refs . toolSelect . value = toolId ;
123181 }
182+ updateSwitchMeta ( ) ;
124183 updateStandaloneHref ( toolId ) ;
125184 mountSelectedTool ( "popstate" ) ;
126185 } ) ;
0 commit comments