Skip to content

Commit 64a2480

Browse files
committed
[frontend] selection context menu for tabs
1 parent e3c7f2b commit 64a2480

File tree

3 files changed

+93
-34
lines changed

3 files changed

+93
-34
lines changed

frontend/src/Tab.tsx

Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

85167
function 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
}

frontend/src/browser.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,21 @@ export class Browser extends StatefulClass {
8888
canGoBack={use(this.activetab.canGoBack)}
8989
canGoForwards={use(this.activetab.canGoForward)}
9090
goBack={() => {
91-
this.activetab.history.go(-1);
91+
this.activetab.back();
9292
}}
9393
goForwards={() => {
94-
this.activetab.history.go(1);
94+
this.activetab.forward();
9595
}}
9696
refresh={() => {
9797
this.activetab.frame.reload();
9898
}}
9999
navigate={(url: string) => {
100+
// TODO: dejank
100101
if (URL.canParse(url)) {
101-
this.activetab.history.push(new URL(url), undefined, true);
102+
this.activetab.pushNavigate(new URL(url));
102103
} else {
103104
const search = `https://google.com/search?q=${encodeURIComponent(url)}`;
104-
this.activetab.history.push(new URL(search), undefined, true);
105+
this.activetab.pushNavigate(new URL(search));
105106
}
106107
}}
107108
/>

frontend/src/history.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class History {
1616
this.states.push({ url, state });
1717
this.index++;
1818

19-
if (navigate) this.tab.navigate(url);
19+
if (navigate) this.tab._directnavigate(url);
2020

2121
this.tab.canGoBack = this.canGoBack();
2222
this.tab.canGoForward = this.canGoForward();
@@ -30,7 +30,7 @@ export class History {
3030
this.push(url, state);
3131
}
3232

33-
if (navigate) this.tab.navigate(url);
33+
if (navigate) this.tab._directnavigate(url);
3434

3535
this.tab.canGoBack = this.canGoBack();
3636
this.tab.canGoForward = this.canGoForward();
@@ -45,7 +45,7 @@ export class History {
4545
this.index = this.states.length - 1;
4646
}
4747

48-
if (navigate) this.tab.navigate(this.states[this.index].url);
48+
if (navigate) this.tab._directnavigate(this.states[this.index].url);
4949

5050
this.tab.canGoBack = this.canGoBack();
5151
this.tab.canGoForward = this.canGoForward();

0 commit comments

Comments
 (0)