Skip to content

Commit a9669f4

Browse files
committed
Remove hard-coded built-in folder IDs
- Remove hard-coded built-in bookmark folder IDs, which aren't the same across browsers. - Remove dependency on `bookmarks.BookmarkTreeNode.type` field, which isn't available on all browsers.
1 parent ef0db2f commit a9669f4

19 files changed

+281
-298
lines changed

CHANGELOG.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,113 @@
11
# Treetop Changelog
2+
23
All notable changes to this project will be documented in this file.
34

45
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
56

67
## [Unreleased]
8+
79
### Changed
8-
- Update Vite to 4.1.
10+
11+
- Remove dependency on bookmarks.BookmarkTreeNode.type field, which isn't
12+
available on all browsers.
13+
- Remove dependency on hard-coded built-in bookmark folder IDs, which aren't the
14+
same across browsers.
915

1016
### Removed
17+
1118
- Remove options to set visibility of built-in bookmark folders.
1219

1320
## [1.5.0] - 2022-09-27
21+
1422
### Changed
23+
1524
- Update Svelte Material UI to 6.1.4.
1625
- Replace Rollup with Vite as the build tool.
1726

1827
### Fixed
28+
1929
- Fix selecting bookmark name or URL in properties dialog when focusing each input.
2030

2131
## [1.4.1] - 2022-06-20
32+
2233
### Fixed
34+
2335
- Fix color scheme when legacy 'system' option is stored.
2436

2537
## [1.4.0] - 2022-06-19
38+
2639
### Changed
40+
2741
- Update Svelte Material UI to 6.0.0-beta.16.
2842
- Update development and testing dependencies.
2943

3044
### Removed
45+
3146
- Remove option to follow the system color scheme.
3247

3348
### Fixed
49+
3450
- Fix loading stored options.
3551

3652
## [1.3.0] - 2021-10-02
53+
3754
### Added
55+
3856
- Add webextension-polyfill.
3957
- Pressing Escape clears the search input.
4058

4159
## [1.2.0] - 2021-09-18
60+
4261
### Added
62+
4363
- Add a search input to search for bookmarks by name or URL.
4464

4565
### Changed
66+
4667
- Update Svelte Material UI to 4.2.0.
4768

4869
## [1.1.3] - 2021-01-22
70+
4971
### Added
72+
5073
- List of attributions for open source software distributed in releases.
5174

5275
### Changed
76+
5377
- Use fonts from Fontsource packages.
5478

5579
## [1.1.2] - 2020-12-08
80+
5681
### Changed
82+
5783
- Fix visited bookmark styling.
5884

5985
## [1.1.1] - 2020-12-04
86+
6087
### Fixed
88+
6189
- Fix bad 1.1.0 release on addons.mozilla.org.
6290

6391
## [1.1.0] - 2020-12-04
92+
6493
### Changed
94+
6595
- Add option to change the color scheme.
6696

6797
## [1.0.1] - 2020-11-23
98+
6899
### Changed
100+
69101
- Improve timer that updates when bookmarks were last visited.
70102

71103
### Fixed
104+
72105
- Fix setting the document title.
73106

74107
## [1.0.0] - 2020-11-10
108+
75109
### Added
110+
76111
- Initial release.
77112

78113
[Unreleased]: https://github.com/msmolens/treetop/compare/v1.5.0...HEAD

src/treetop/BookmarksManager.ts

Lines changed: 47 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
import { get, writable } from 'svelte/store';
22
import browser, { type Bookmarks } from 'webextension-polyfill';
33

4-
import {
5-
BOOKMARK_TREE_NODE_TYPE_BOOKMARK,
6-
BOOKMARK_TREE_NODE_TYPE_FOLDER,
7-
BOOKMARK_TREE_NODE_TYPE_SEPARATOR,
8-
BOOKMARKS_MENU_GUID,
9-
BOOKMARKS_TOOLBAR_GUID,
10-
OTHER_BOOKMARKS_GUID,
11-
} from './constants';
4+
import { isBookmark, isFolder, isSeparator } from './bookmarktreenode-utils';
5+
import { MOBILE_BOOKMARKS_GUID } from './constants';
126
import * as Treetop from './types';
137

14-
// Map bookmarks.BookmarkTreeNodeType to Treetop.NodeType
15-
const BookmarkTypeMap: Map<Bookmarks.BookmarkTreeNodeType, Treetop.NodeType> =
16-
new Map([
17-
[BOOKMARK_TREE_NODE_TYPE_BOOKMARK, Treetop.NodeType.Bookmark],
18-
[BOOKMARK_TREE_NODE_TYPE_FOLDER, Treetop.NodeType.Folder],
19-
[BOOKMARK_TREE_NODE_TYPE_SEPARATOR, Treetop.NodeType.Separator],
20-
]);
21-
228
/**
239
* Class to initialize and manage updating bookmark node stores.
2410
*/
2511
export class BookmarksManager {
26-
constructor(private readonly nodeStoreMap: Treetop.NodeStoreMap) {}
12+
constructor(
13+
private readonly nodeStoreMap: Treetop.NodeStoreMap,
14+
private readonly builtInFolderInfo: Treetop.BuiltInFolderInfo
15+
) {}
2716

2817
/**
2918
* Load all bookmarks and initialize node stores for folders.
19+
* Initialize built-in folder info.
3020
*/
3121
async loadBookmarks(): Promise<void> {
3222
const nodes = await browser.bookmarks.getTree();
33-
this.orderBookmarksRootChildren(nodes[0]);
3423

24+
// Store root node ID
25+
const rootNode = nodes[0];
26+
this.builtInFolderInfo.rootNodeId = rootNode.id;
27+
28+
// Firefox: Exclude Mobile Bookmarks folder
29+
rootNode.children = rootNode.children?.filter(
30+
({ id }) => id !== MOBILE_BOOKMARKS_GUID
31+
);
32+
33+
// Store built-in folder IDs (e.g. Other Bookmarks)
34+
this.builtInFolderInfo.builtInFolderIds = rootNode.children!.map(
35+
({ id }) => id
36+
);
37+
38+
// Initialize node stores for folders
3539
while (nodes.length > 0) {
3640
const node = nodes.pop()!;
3741

38-
if (node.type === BOOKMARK_TREE_NODE_TYPE_FOLDER) {
42+
if (isFolder(node)) {
3943
this.buildNodeStore(node);
4044
nodes.push(...node.children!);
4145
}
@@ -54,32 +58,27 @@ export class BookmarksManager {
5458
title: node.title,
5559
children: node.children!.map((child): Treetop.Node => {
5660
let newChild: Treetop.Node;
57-
const type = BookmarkTypeMap.get(child.type!)!;
58-
switch (type) {
59-
case Treetop.NodeType.Bookmark:
60-
newChild = {
61-
type,
62-
id: child.id,
63-
title: child.title,
64-
url: child.url!,
65-
};
66-
break;
67-
case Treetop.NodeType.Folder:
68-
newChild = {
69-
type,
70-
id: child.id,
71-
title: child.title,
72-
children: [],
73-
};
74-
break;
75-
case Treetop.NodeType.Separator:
76-
newChild = {
77-
type,
78-
id: child.id,
79-
};
80-
break;
81-
default:
82-
throw new TypeError();
61+
if (isBookmark(child)) {
62+
newChild = {
63+
id: child.id,
64+
title: child.title,
65+
type: Treetop.NodeType.Bookmark,
66+
url: child.url!,
67+
};
68+
} else if (isFolder(child)) {
69+
newChild = {
70+
id: child.id,
71+
title: child.title,
72+
type: Treetop.NodeType.Folder,
73+
children: [],
74+
};
75+
} else if (isSeparator(child)) {
76+
newChild = {
77+
id: child.id,
78+
type: Treetop.NodeType.Separator,
79+
};
80+
} else {
81+
throw new TypeError();
8382
}
8483

8584
return newChild;
@@ -93,10 +92,6 @@ export class BookmarksManager {
9392
* Create and record a node store for the specified bookmark node.
9493
*/
9594
private buildNodeStore(node: Bookmarks.BookmarkTreeNode): void {
96-
if (node.type !== BOOKMARK_TREE_NODE_TYPE_FOLDER) {
97-
throw new TypeError();
98-
}
99-
10095
const newNode = this.convertNode(node);
10196
const nodeStore = writable(newNode);
10297
this.nodeStoreMap.set(node.id, nodeStore);
@@ -108,7 +103,7 @@ export class BookmarksManager {
108103
private async updateNodeStore(nodeId: string): Promise<void> {
109104
const [node] = await browser.bookmarks.get(nodeId);
110105

111-
if (node.type !== BOOKMARK_TREE_NODE_TYPE_FOLDER) {
106+
if (!isFolder(node)) {
112107
throw new TypeError();
113108
}
114109

@@ -126,7 +121,7 @@ export class BookmarksManager {
126121
id: string,
127122
bookmark: Bookmarks.BookmarkTreeNode
128123
): Promise<void> {
129-
if (bookmark.type === BOOKMARK_TREE_NODE_TYPE_FOLDER) {
124+
if (isFolder(bookmark)) {
130125
// Add node store for the new folder
131126
const [node] = await browser.bookmarks.get(id);
132127
node.children = await browser.bookmarks.getChildren(id);
@@ -149,7 +144,7 @@ export class BookmarksManager {
149144
): Promise<string[]> {
150145
const removedNodeIds = [];
151146

152-
if (removeInfo.node.type === BOOKMARK_TREE_NODE_TYPE_FOLDER) {
147+
if (isFolder(removeInfo.node)) {
153148
const nodeStore = this.nodeStoreMap.get(id)!;
154149
const nodes: [Treetop.FolderNode] = [get(nodeStore)];
155150

@@ -219,35 +214,4 @@ export class BookmarksManager {
219214
await this.updateNodeStore(oldParentId);
220215
}
221216
}
222-
223-
/**
224-
* Order root bookmark node children like:
225-
* - Bookmarks Toolbar
226-
* - Bookmarks Menu
227-
* - Other Bookmarks
228-
*
229-
* Respect preference settings for whether to include each folder.
230-
*
231-
* Omit Mobile Bookmarks.
232-
*/
233-
private orderBookmarksRootChildren(node: Bookmarks.BookmarkTreeNode): void {
234-
const children: Bookmarks.BookmarkTreeNode[] = [];
235-
236-
const bookmarksToolbarNode = node.children!.find(
237-
({ id }) => id === BOOKMARKS_TOOLBAR_GUID
238-
)!;
239-
children.push(bookmarksToolbarNode);
240-
241-
const bookmarksMenuNode = node.children!.find(
242-
({ id }) => id === BOOKMARKS_MENU_GUID
243-
)!;
244-
children.push(bookmarksMenuNode);
245-
246-
const otherBookmarksNode = node.children!.find(
247-
({ id }) => id === OTHER_BOOKMARKS_GUID
248-
)!;
249-
children.push(otherBookmarksNode);
250-
251-
node.children = children;
252-
}
253217
}

src/treetop/FilterManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { get } from 'svelte/store';
22
import type { Bookmarks } from 'webextension-polyfill';
33

4-
import { BOOKMARK_TREE_NODE_TYPE_BOOKMARK } from './constants';
4+
import { isBookmark } from './bookmarktreenode-utils';
55
import * as Treetop from './types';
66

77
/**
@@ -92,7 +92,7 @@ export class FilterManager {
9292
return;
9393
}
9494

95-
if (bookmark.type !== BOOKMARK_TREE_NODE_TYPE_BOOKMARK) {
95+
if (!isBookmark(bookmark)) {
9696
return;
9797
}
9898

src/treetop/Folder.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import * as browser from 'webextension-polyfill';
55
66
import Bookmark from './Bookmark.svelte';
7-
import { BOOKMARKS_ROOT_GUID } from './constants';
87
import * as Treetop from './types';
98
109
export let nodeId: string;
1110
export let root = false;
1211
12+
const builtInFolderInfo: Treetop.BuiltInFolderInfo =
13+
getContext('builtInFolderInfo');
1314
const nodeStoreMap: Treetop.NodeStoreMap = getContext('nodeStoreMap');
1415
const filterActive = getContext<Writable<boolean>>('filterActive');
1516
const filterSet = getContext<Treetop.FilterSet>('filterSet');
@@ -51,7 +52,7 @@
5152
function getFallbackTitle(nodeId: string): string {
5253
let key;
5354
54-
if (nodeId === BOOKMARKS_ROOT_GUID) {
55+
if (nodeId === builtInFolderInfo.rootNodeId) {
5556
key = 'bookmarks';
5657
} else {
5758
key = 'folderNoTitle';

src/treetop/HistoryManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { get, writable } from 'svelte/store';
22
import browser, { type Bookmarks, type History } from 'webextension-polyfill';
33

4-
import { BOOKMARK_TREE_NODE_TYPE_BOOKMARK } from './constants';
4+
import { isBookmark } from './bookmarktreenode-utils';
55
import * as Treetop from './types';
66

77
/**
@@ -85,7 +85,7 @@ export class HistoryManager {
8585
_id: string,
8686
bookmark: Bookmarks.BookmarkTreeNode
8787
): Promise<void> {
88-
if (bookmark.type !== BOOKMARK_TREE_NODE_TYPE_BOOKMARK) {
88+
if (!isBookmark(bookmark)) {
8989
return;
9090
}
9191

0 commit comments

Comments
 (0)