Skip to content

Commit 86dd73b

Browse files
committed
[frontend] allow adding and removing bookmarks
1 parent 5f47343 commit 86dd73b

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

frontend/src/Browser.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type SerializedBrowser = {
4242
tabs: SerializedTab[];
4343
globalhistory: SerializedHistoryState[];
4444
activetab: number;
45+
bookmarks: BookmarkEntry[];
4546
};
4647

4748
export type GlobalHistoryEntry = {
@@ -51,20 +52,25 @@ export type GlobalHistoryEntry = {
5152
favicon?: string;
5253
};
5354

55+
export type BookmarkEntry = {
56+
url: string;
57+
title: string;
58+
favicon?: string;
59+
};
60+
5461
export class Browser extends StatefulClass {
5562
built: boolean = false;
5663

57-
tabs: Tab[];
58-
activetab: Tab;
64+
tabs: Tab[] = [];
65+
activetab: Tab = null!;
5966

60-
globalhistory: HistoryState[];
67+
globalhistory: HistoryState[] = [];
68+
bookmarks: BookmarkEntry[] = [];
6169

6270
unfocusframes: boolean = false;
6371

6472
constructor() {
6573
super(createState(Object.create(Browser.prototype)));
66-
this.tabs = [];
67-
this.globalhistory = [];
6874

6975
setInterval(saveBrowserState, 1000);
7076
}
@@ -74,6 +80,7 @@ export class Browser extends StatefulClass {
7480
tabs: this.tabs.map((t) => t.serialize()),
7581
activetab: this.activetab.id,
7682
globalhistory: this.globalhistory.map((s) => s.serialize()),
83+
bookmarks: this.bookmarks,
7784
};
7885
}
7986
deserialize(de: SerializedBrowser) {
@@ -88,6 +95,7 @@ export class Browser extends StatefulClass {
8895
tab.deserialize(detab);
8996
}
9097
this.activetab = this.tabs[0];
98+
this.bookmarks = de.bookmarks;
9199
// this.activetab = this.tabs.find((t) => t.id == de.activetab)!;
92100
console.log(this.activetab, this.activetab.url);
93101
}

frontend/src/components/BookmarksStrip.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ export const BookmarksStrip: Component = function () {
1010
<Icon icon={iconAdd}></Icon>
1111
<span>create bookmark</span>
1212
</button>
13-
{use(browser.globalhistory).mapEach((g) => (
14-
<button on:click={() => {}}>
15-
<img src={g.favicon}></img>
16-
<span>{g.title}</span>
13+
{use(browser.bookmarks).mapEach((b) => (
14+
<button
15+
on:auxclick={() => {
16+
browser.newTab(new URL(b.url));
17+
}}
18+
on:click={() => {
19+
browser.activetab.pushNavigate(new URL(b.url));
20+
}}
21+
>
22+
<img src={b.favicon}></img>
23+
<span>{b.title}</span>
1724
</button>
1825
))}
1926
</div>

frontend/src/components/IconButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const IconButton: Component<{
1616
on:click={(e) => this.click?.(e)}
1717
title={this.tooltip}
1818
>
19-
<Icon icon={this.icon} />
19+
<Icon icon={use(this.icon)} />
2020
</button>
2121
);
2222
};

frontend/src/components/UrlInput.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "dreamland/core";
77
import iconOptions from "@ktibow/iconset-ion/options-outline";
88
import iconStar from "@ktibow/iconset-ion/star-outline";
9+
import iconStarFilled from "@ktibow/iconset-ion/star";
910
import iconSearch from "@ktibow/iconset-ion/search";
1011
import iconForwards from "@ktibow/iconset-ion/arrow-forward";
1112
import { Icon } from "./Icon";
@@ -348,7 +349,38 @@ export const UrlInput: Component<
348349

349350
{use(this.active)
350351
.map((a) => !a)
351-
.andThen(<IconButton icon={iconStar}></IconButton>)}
352+
.andThen(
353+
<IconButton
354+
click={(e) => {
355+
e.stopPropagation();
356+
e.preventDefault();
357+
let bookmark = browser.bookmarks.find(
358+
(b) => b.url == this.tabUrl.href
359+
);
360+
if (bookmark) {
361+
browser.bookmarks = browser.bookmarks.filter(
362+
(b) => b.url !== this.tabUrl.href
363+
);
364+
} else {
365+
browser.bookmarks = [
366+
{
367+
url: browser.activetab.url.href,
368+
favicon: browser.activetab.icon,
369+
title:
370+
browser.activetab.title ||
371+
browser.activetab.url.hostname,
372+
},
373+
...browser.bookmarks,
374+
];
375+
}
376+
}}
377+
icon={use(browser.bookmarks, this.tabUrl).map(() =>
378+
browser.bookmarks.some((b) => b.url == this.tabUrl.href)
379+
? iconStarFilled
380+
: iconStar
381+
)}
382+
></IconButton>
383+
)}
352384
{use(this.active).andThen(
353385
<IconButton
354386
click={(e: MouseEvent) => {

0 commit comments

Comments
 (0)