Skip to content

Commit 2a88cb0

Browse files
shakyShaneShane Osbourne
andauthored
ntp: add url to open action (#1248)
Co-authored-by: Shane Osbourne <[email protected]>
1 parent 12b3ab3 commit 2a88cb0

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

special-pages/messages/new-tab/favorites_open.notify.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
"type": "object",
55
"required": [
66
"id",
7-
"target"
7+
"target",
8+
"url"
89
],
910
"properties": {
1011
"id": {
1112
"description": "Entity ID",
1213
"type": "string"
1314
},
15+
"url": {
16+
"description": "The url to open",
17+
"type": "string"
18+
},
1419
"target": {
1520
"type": "string",
1621
"enum": ["same-tab", "new-tab", "new-window"]

special-pages/pages/new-tab/app/favorites/components/Favorites.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const FavoritesMemo = memo(Favorites);
2525
* @param {Expansion} props.expansion
2626
* @param {() => void} props.toggle
2727
* @param {(id: string) => void} props.openContextMenu
28-
* @param {(id: string, target: OpenTarget) => void} props.openFavorite
28+
* @param {(id: string, url: string, target: OpenTarget) => void} props.openFavorite
2929
* @param {() => void} props.add
3030
*/
3131
export function Favorites({ gridRef, favorites, expansion, toggle, openContextMenu, openFavorite, add }) {
@@ -89,16 +89,16 @@ export function Favorites({ gridRef, favorites, expansion, toggle, openContextMe
8989
function onClick(event) {
9090
let target = /** @type {HTMLElement|null} */ (event.target);
9191
while (target && target !== event.currentTarget) {
92-
if (typeof target.dataset.id === 'string') {
92+
if (typeof target.dataset.id === 'string' && 'href' in target && typeof target.href === 'string') {
9393
event.preventDefault();
9494
event.stopImmediatePropagation();
9595
const isControlClick = platformName === 'macos' ? event.metaKey : event.ctrlKey;
9696
if (isControlClick) {
97-
return openFavorite(target.dataset.id, 'new-tab');
97+
return openFavorite(target.dataset.id, target.href, 'new-tab');
9898
} else if (event.shiftKey) {
99-
return openFavorite(target.dataset.id, 'new-window');
99+
return openFavorite(target.dataset.id, target.href, 'new-window');
100100
}
101-
return openFavorite(target.dataset.id, 'same-tab');
101+
return openFavorite(target.dataset.id, target.href, 'same-tab');
102102
} else {
103103
target = target.parentElement;
104104
}

special-pages/pages/new-tab/app/favorites/components/FavoritesProvider.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const FavoritesContext = createContext({
3232
openContextMenu: (id) => {
3333
throw new Error('must implement');
3434
},
35-
/** @type {(id: string, target: OpenTarget) => void} */
35+
/** @type {(id: string, url: string, target: OpenTarget) => void} */
3636
openFavorite: (id, target) => {
3737
throw new Error('must implement');
3838
},
@@ -86,11 +86,11 @@ export function FavoritesProvider({ children }) {
8686
[service],
8787
);
8888

89-
/** @type {(id: string, target: OpenTarget) => void} */
89+
/** @type {(id: string, url: string, target: OpenTarget) => void} */
9090
const openFavorite = useCallback(
91-
(id, target) => {
91+
(id, url, target) => {
9292
if (!service.current) return;
93-
service.current.openFavorite(id, target);
93+
service.current.openFavorite(id, url, target);
9494
},
9595
[service],
9696
);

special-pages/pages/new-tab/app/favorites/favorites.service.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ export class FavoritesService {
110110

111111
/**
112112
* @param {string} id - entity id
113+
* @param {string} url - target url
113114
* @param {FavoritesOpenAction['target']} target
114115
* @internal
115116
*/
116-
openFavorite(id, target) {
117+
openFavorite(id, url, target) {
117118
// let the native side know too
118-
this.ntp.messaging.notify('favorites_open', { id, target });
119+
this.ntp.messaging.notify('favorites_open', { id, url, target });
119120
}
120121

121122
/**

special-pages/pages/new-tab/app/favorites/integration-tests/favorites.page.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ export class FavoritesPage {
1919
async opensInNewTab() {
2020
await this.nthFavorite(0).click({ modifiers: ['Meta'] });
2121
const calls = await this.ntp.mocks.waitForCallCount({ method: 'favorites_open', count: 1 });
22-
expect(calls[0].payload.params).toStrictEqual({ id: 'id-many-1', target: 'new-tab' });
22+
expect(calls[0].payload.params).toStrictEqual({ id: 'id-many-1', url: 'https://example.com/?id=id-many-1', target: 'new-tab' });
2323
}
2424

2525
async opensInNewWindow() {
2626
await this.nthFavorite(0).click({ modifiers: ['Shift'] });
2727
const calls = await this.ntp.mocks.waitForCallCount({ method: 'favorites_open', count: 1 });
28-
expect(calls[0].payload.params).toStrictEqual({ id: 'id-many-1', target: 'new-window' });
28+
expect(calls[0].payload.params).toStrictEqual({ id: 'id-many-1', url: 'https://example.com/?id=id-many-1', target: 'new-window' });
2929
}
3030

3131
async opensInSameTab() {
3232
await this.nthFavorite(0).click();
3333
const calls = await this.ntp.mocks.waitForCallCount({ method: 'favorites_open', count: 1 });
34-
expect(calls[0].payload.params).toStrictEqual({ id: 'id-many-1', target: 'same-tab' });
34+
expect(calls[0].payload.params).toStrictEqual({ id: 'id-many-1', url: 'https://example.com/?id=id-many-1', target: 'same-tab' });
3535
}
3636

3737
async addsAnItem() {

special-pages/types/new-tab.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export interface FavoritesOpenAction {
133133
* Entity ID
134134
*/
135135
id: string;
136+
/**
137+
* The url to open
138+
*/
139+
url: string;
136140
target: "same-tab" | "new-tab" | "new-window";
137141
}
138142
/**

0 commit comments

Comments
 (0)