@@ -62,7 +62,7 @@ export class Tab extends StatefulClass {
6262 }
6363
6464 // only caller should be history.ts for this
65- navigate ( url : URL ) {
65+ _directnavigate ( url : URL ) {
6666 this . url = url ;
6767 if ( url . protocol == "puter:" ) {
6868 switch ( url . host ) {
@@ -80,6 +80,88 @@ export class Tab extends StatefulClass {
8080 this . frame . go ( url ) ;
8181 }
8282 }
83+
84+ pushNavigate ( url : URL ) {
85+ this . history . push ( url , undefined , true ) ;
86+ }
87+ replaceNavigate ( url : URL ) {
88+ this . history . replace ( url , undefined , true ) ;
89+ }
90+
91+ back ( ) {
92+ if ( this . canGoBack ) {
93+ this . history . go ( - 1 ) ;
94+ }
95+ }
96+ forward ( ) {
97+ if ( this . canGoForward ) {
98+ this . history . go ( 1 ) ;
99+ }
100+ }
101+ reload ( ) {
102+ if ( this . internalpage ) {
103+ this . _directnavigate ( this . url ) ;
104+ } else {
105+ this . frame . reload ( ) ;
106+ }
107+ }
108+ }
109+
110+ function pageContextItems ( client : ScramjetClient , tab : Tab ) {
111+ let frame = tab . frame ;
112+
113+ const selection = client . global . getSelection ( ) ;
114+ if ( selection && selection . toString ( ) . length > 0 ) {
115+ return [
116+ {
117+ label : "Search" ,
118+ action : ( ) => {
119+ const query = selection . toString ( ) ;
120+ if ( query ) {
121+ tab . pushNavigate (
122+ new URL (
123+ `https://www.google.com/search?q=${ encodeURIComponent ( query ) } `
124+ )
125+ ) ;
126+ }
127+ } ,
128+ } ,
129+ {
130+ label : "Copy" ,
131+ action : ( ) => {
132+ navigator . clipboard . writeText ( selection . toString ( ) ) ;
133+ } ,
134+ } ,
135+ ] ;
136+ }
137+
138+ return [
139+ {
140+ label : "Back" ,
141+ action : ( ) => {
142+ tab . back ( ) ;
143+ } ,
144+ } ,
145+ {
146+ label : "Forward" ,
147+ action : ( ) => {
148+ tab . forward ( ) ;
149+ } ,
150+ } ,
151+ {
152+ label : "Reload" ,
153+ action : ( ) => {
154+ tab . reload ( ) ;
155+ } ,
156+ } ,
157+ {
158+ label : "Bookmark" ,
159+ action : ( ) => {
160+ // TODO:
161+ console . log ( "Bookmarking" , tab . title , tab . url ) ;
162+ } ,
163+ } ,
164+ ] ;
83165}
84166
85167function injectContextMenu ( client : ScramjetClient , tab : Tab ) {
@@ -100,32 +182,8 @@ function injectContextMenu(client: ScramjetClient, tab: Tab) {
100182 let { x, y } = frame . frame . getBoundingClientRect ( ) ;
101183 xoff += x ;
102184 yoff += y ;
103- createMenu ( xoff + e . pageX , yoff + e . pageY , [
104- {
105- label : "Back" ,
106- action : ( ) => {
107- frame . back ( ) ;
108- } ,
109- } ,
110- {
111- label : "Forward" ,
112- action : ( ) => {
113- frame . forward ( ) ;
114- } ,
115- } ,
116- {
117- label : "Reload" ,
118- action : ( ) => {
119- frame . reload ( ) ;
120- } ,
121- } ,
122- {
123- label : "Bookmark" ,
124- action : ( ) => {
125- console . log ( "Bookmarking" , tab . title , tab . url ) ;
126- } ,
127- } ,
128- ] ) ;
185+
186+ createMenu ( xoff + e . pageX , yoff + e . pageY , pageContextItems ( client , tab ) ) ;
129187 e . preventDefault ( ) ;
130188 } ) ;
131189}
0 commit comments